Click here to Skip to main content
15,612,351 members
Articles / Internet of Things
Posted 31 Mar 2015


19 bookmarked

IoT Solution using Bluetooth Low Energy Sensors, Event Hub, Stream Analytics and MS Power BI

Rate me:
Please Sign up or sign in to vote.
4.89/5 (15 votes)
31 Mar 2015CPOL15 min read
The article showcases BLE sensors (IoT) solution developed using Azure Event Hub, Stream Analytics and Power BI.

This article is an entry in our Microsoft Azure IoT Contest. Articles in this section are not required to be full articles so care should be taken when voting.


The article showcases an IoT solution developed for a UK based leading Food Services company.

It currently handles 30,000 CUSTOMERS, managing 60,000 ORDERS/MONTH using 500+ CONNECTED DEVICES, processing 2.5-MILLION EVENTS/DAY and 1.5 MILLION MOBILE-TRANSACTIONS/MONTH.

We developed a Windows Phone based IoT solution which synched with Bluetooth Low Energy (BLE) Sensors to monitor freezer and chiller temperatures to give real-time data. This helped them monitor Trucks’ data for predictive analysis.

Another feather in the cap for this solution; Microsoft showcased this case-study at recently held Microsoft Convergence 2015 in Atlanta. It was a part of the Convergence since it showcased a powerful combination of business and technology solution.

Convergence 2015

You can access video of this IoT solution's demonstration at Microsoft Convergence 2015, Atlanta, using the below link


Link2: (30 mins onwards)

Table of Content



One of the key requirements of this application for delivery of goods was to store two critical information - the Chiller and the Freezer temperatures of the truck’s compartments that contained perishable food deliverables. Bluetooth-enabled ZenMeasure sensors were used to capture and monitor these temperatures.

If the temperature is not optimal at the time of delivery, then there is a risk of customer returning the goods. To avoid this situation, there was a need to monitor the real-time temperature periodically and check if it is within the optimal range and doesn’t go beyond the standard temperature limits or thresholds.   

We came up with the idea of storing & monitoring the temperature using Windows Phone (WP) application. For this we used IoT enabled devices (Bluetooth ZenMeasure sensors – as shown in Fig. 1) which captured Freezer and Chiller temperature. For real-time temperature monitoring, Windows Phone background tasks were enabled which sends real-time temperature data of Chiller and Freezer sensors to Azure Event Hubs.

Image 1
Figure 1: Bluetooth Low Energy (BLE) Sensors

The Azure Event Hub deployed here has the capability to collect millions of messages/second from remote IoT enabled devices and Stream Analytics. It then distributes this data to multiple consumers i.e. Azure SQL DB/ MS Power BI (for visual representation in Dashboard) etc.



Objective of this IoT Solution is to take the information from IoT enabled devices periodically and notify it to the application user, on his/her display screen, in the form of Power BI Dashboard as well as store this information in the Azure SQL DB.

To achieve this we used Azure Event Hub to collect data from thousands of sensors installed on trucks. This data was later pulled using Azure Stream Analytics which then used in real-time/on-the-fly analytics i.e., monitoring dashboard. For this we created the statistical output in Power BI in the form of graphical representation from Stream Analytics.

Azure SQL DB was used as a repository to store static data sourced from Stream Analytics for future analytics.

Here we have put forth the solution which demonstrates all these features. Below is the architecture diagram for this solution.


Image 2
Figure 2: Architecture of BLE Sensor Based Application

Technologies Used

In our application we have made use of following components:

  1. BLE sensors (IoT)

Sensor reading like Freezer and Chiller temperature was captured and sent to the Windows Phone Background Task. An important aspect of these Bluetooth Low Energy enabled devices is that they consume very less battery energy.

  1. Windows Phone Background Task

We used background task to collect information of sensors and then sent this information to the Event Hub using HttpClient object.

  1. Event Hub

Event hub has capability to capture millions of events per second. Hence using this, we were able to send hundreds of Events per seconds from sensors. This Event Hub stores the information which is processed by our ‘Stream Analytics Job’.

  1. Stream Analytics

We have used Azure Stream Analytics for performing job on the Event Hub. The major advantage of Azure Stream Analytics is the relatively simple SQL-like syntax it uses to describe data transformations. If you are already an Azure Developer then Stream Analytics will fit you like a glove.

  1. MS Power BI & Azure SQL DB

We have used Power BI for creating a visual representation of our data i.e. for displaying Freezer and Chiller temperature on the Dashboard. It served as a predictive analytics where user could analyze the temperature drop. We created a Power BI dashboard from Stream Analytics job and then stored the overall output in Azure SQL DB.


Our application make use of following components:

  1. BLE sensors connectivity (IoT)
  2. Windows Phone Background Task
  3. Event Hub
  4. Stream Analytics
  5. MS Power BI and Azure SQL DB

Given below is how data flows between all these components:

Image 3

Figure 3: IoT Communication With Azure

We have used BLE devices (temperature sensors) which capture the current chiller and freezer temperature. We used “bluetooth.genericAttributeProfile” capabilities to connect with these sensors. Once the sensor is connected then the current Freezer and Chiller temperature is displayed on UI using a registered Background Task.

As shown in the image below, we have connected to two BLE Sensors.

Image 4

Figure 4: Screenshot Showing Two Paired Sensors

Once Sensors are connected, we can now display the temperature on foreground task using MonitorTemperature() function. This method is called from the User-Control page (component User Interface) and its information is then bind to XAML code.

Temperature reading Screen is as shown in image below.

Image 5

Figure 5: Screenshot Showing Chiller and Freezer Temperature Readings

In above screen you can see the actual temperature reading. This data is continuously monitored and sent to the Event Hub. Event Hub captures these temperature readings at regular intervals from each sensor.

Stream Analytics takes this Event Hub data as an Input (which will be elaborated later in this article). Once Stream Analytics has this data, it then execute this query for fetching the desired output.  We also configured the Output to be processed by Power BI for Information visualization on Dashboard and on Azure SQL database to serve as history data for future analytics.

Using Power BI we have represented our Analytical data in visual form. We have created a Dashboard which contains Temperature indicator for both Chiller and Freezer, and a Graphical representation for the current delivery route.


Gathering information in real time without affecting the foreground app is what we were looking for. To tackle this we used Windows Phone Background Task.

What is Windows Phone Background task?

Background tasks are lightweight classes that the OS runs in the background. You can use background tasks to provide functionality when your app is suspended or not running. You can also use background tasks for real-time communication.

We used background task to collect information of sensors and sending information to the Event Hub using HttpClient object. However Event Hub API cannot be directly   integrated with background tasks, this is a limitation of Event Hub. Hence we usedHttpClient object and sent information directly to the Event Hub. Refer “Sending Event Using HttpClient” section of this Article.

Our background task routine runs continuously and gathers data about current temperature from respective temperature sensors and sends it to Event Hub when internet connection is available.   

Creating a Background Task for Windows Phone 8.1

Below are the steps to create a Background Task in Windows Phone 8.1 application.

Step 1: In the ‘Add New Project’ dialog, select the Windows Runtime Component template in the Visual C# > Windows Store section.

Image 6

Figure 6: Screenshot Showing Step to Create New Background Task

Step 2: Add background task - Temperature monitoring code “Run” method

Create a new class that implements the IBackgroundTask interface. The Run method is an entry point that will be called when the specified event is triggered. This method is required in every background task.

public sealed class Bg : IBackgroundTask
    public void Run(IBackgroundTaskInstance taskInstance)

Code Snippet 1: Syntax to declare Background Task Class

Step 3: Declare the Background Task

Below screen shows the settings to add the said background task to the appxmanifest declarations:

Image 7

Figure7: Screenshot Showing Settings to Add Background Task

Here, first choose “Properties” (supported type for triggering background task). Then select “Available Declaration” from dropdown.

Image 8

Figure 8: Selecting Declaration Type - Background Tasks

Select 'Background Tasks'; After adding Background Task Declaration add the entry point for it. Entry point is an achievable class id for this declaration.

Image 9

Figure 9: Adding Entry Point for Background Task

Note the Background Run method

public async void Run(IBackgroundTaskInstance taskInstance)
            await TemperatureSyncHelper.InitializeSyncContext();
            _deferral = taskInstance.GetDeferral();
            _taskInstance = taskInstance;
            _cancellationSource = new CancellationTokenSource();

            _settings = await Settings.LoadAsync();
            _settings.BackgroundTaskRunning = true;
            await _settings.SaveAsync();

            // since we are using DeviceUseTrigger, pretend we are going to use the accelerometer.

            // Get notified when the task is canceled.
            taskInstance.Canceled += new BackgroundTaskCanceledEventHandler(OnCanceled);

            _messages = new BackgroundTaskMessages();
            _messages.MessageReceived += OnHandleMessage;

            await MonitorTemperature();


            // give signal time to get to foreground app
            await Task.Delay(100);

            _taskInstance = null;

            if (_deferral != null)

            // make sure we stop waiting for settings changes.

            _settings.BackgroundTaskRunning = false;
            await _settings.SaveAsync();

Code Snippet 2: Run Method for Background Task

awaitMonitorTemperature() continuously monitors and checks temperature of sensor;

async Task MonitorTemperature()
            var token = _cancellationSource.Token;
            // start listening to all paired temperature sensors.
            foreach (BleZenMeasureTemperatureService sensor in await BleZenMeasureTemperatureService.FindAll())
                sensor.TemperatureMeasurementValueChanged += OnTemperatureMeasurementValueChanged;
                    if (await sensor.ConnectAsync())
                        sensor.ConnectionChanged += OnConnectionChanged;
                catch (Exception ex)
            while (!token.IsCancellationRequested)
                    await WriteSampleTemperatures();
                catch (Exception ex)

                    await Task.Delay(TimeSpan.FromSeconds(WriteSampleRate), token);
                    // cancelled.

Code Snippet 3: Code to Monitor Sensor Temperature Using WriteSampleTemperature() Method

WriteSampleTemperature(): In this method we are actually sending data to Event Hub. For all activity of Event hub we have created a Helper class. This helper class we have declared "CreateToken()", "PostEvent()" and we are calling these methods in WriteSampleTemperature().

What is Event Hub?

Microsoft features Event Hub which supports “Internet of Things” (IoT) based devices along with other Azure features. Event Hub can be configured to handle data generated from thousands of IP-enabled devices. Thus Event Hub has the capability to handle millions of events per second, collecting and processing all such events in near real time.

Event Hubs are ideal for enterprises that rely on massive, parallel and interdependent information routing for its operations.

Image 10

Figure 10: Event Hub
Source: MSDN website

Data continuously comes from Sensors to background task of Windows Phone application and then to the Event Hub. As Event hub has the capability to capture millions of events per second, using it we can process thousands of Events at any given instant. In our scenario, Event Hub stores the information which is processed by the ‘Stream Analytics Job’. Using Event hub we push on it details like Temperature, Time Stamp, MAC addresses of sensors and other monitoring specific data. This property is useful while creating a Query in Stream Analytics Job.

Creating a Custom Event Hub

We have created an Event Hub using following steps:

Adding Event Hub for collecting data from sensors

Image 11

Figure 11: Creating Event Hub
Note: For better results the Regions selected while creating ‘Event Hub’, ‘Stream Analytics’ and ‘Storage Account’ (if you are targeting Azure DB) should be same else a Warning message is displayed.

Go to the configuration tab and configure the “shared access policies”. In this you are required to specify Name of the policy and Permissions for them.

After setting this up, you can initiate the process of sending data to Event hub. We used this information for creating Token (for HttpClient object).

Sending real time temperature details from background task

We started capturing current sensor readings using a background task. Further we can send this temperature (event) data from the background task using HttpClient.

As mentioned before, while working with Background Task we found that we cannot interact with Event hub directly using Event Hub API while working on background task. Due to unavailability of Service bus Event hub API in background process we sent the data using HTTPClient for transmitting temperature data.

Here is the code snippet for sending Events:

In this we use following local variables:
  string uri = Service bus URL;
  string keyName = Access Policy name
  string key = Shared Access Key;

Using the above information we created token for Authorization. We created a new Helper Class which is dedicated to handle Event related activity as a generic class. In this class, we perform operations such as ‘Create Token’, ‘Create HttpClient’ object, ‘Serialize JSON object’ and ‘Post Event’ to Event Hub.

Below is the code/function for creating Token and Post Event using HttpClient object.

// This method creates a SAS token. This is an alternative to using GetSasToken1() method.
// SAS tokens are described in
public static string GetSasToken2(string uri, string keyName, string key)
    // Set token lifetime to 20 minutes.
    var expiry = GetExpiry(1200);
    string stringToSign = System.Net.WebUtility.UrlEncode(uri) + "\n" + expiry;
    string signature = HmacSha256(key, stringToSign);
    string token = String.Format(CultureInfo.InvariantCulture, "SharedAccessSignature sr={0}&sig={1}&se={2}&skn={3}",
        System.Net.WebUtility.UrlEncode(uri), System.Net.WebUtility.UrlEncode(signature), expiry, keyName);
    return token;

public static string HmacSha256(string key, string value)
    var keyStrm = CryptographicBuffer.ConvertStringToBinary(key, BinaryStringEncoding.Utf8);
    var valueStrm = CryptographicBuffer.ConvertStringToBinary(value, BinaryStringEncoding.Utf8);
    var objMacProv = MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha256);
    var hash = objMacProv.CreateHash(keyStrm);
    return CryptographicBuffer.EncodeToBase64String(hash.GetValueAndReset());

static uint GetExpiry(uint tokenLifetimeInSeconds)
    DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
    TimeSpan diff = DateTime.Now.ToUniversalTime() - origin;
    return Convert.ToUInt32(diff.TotalSeconds) + tokenLifetimeInSeconds;

Code Snippet 4: Creating Token with expiry

After getting the token, now we are ready to send the information. Now we can send the temperature Event information with the help of following code.

Create HttpClient object and post information:

//Post telemetry (Event) information to event hub 
public static async Task PostData(Temperature deviceTelemetry, string sas1)
                var sas = sas1;
                // Namespace info.
                var serviceNamespace = "SERVICE BUS NAMESPACE"; //Service bus namespace
                var hubName = "EVENT HUB NAME"; //Event hub name
                var url = string.Format("{0}/publishers/{1}/messages", hubName, deviceTelemetry.TruckId);

                // Create client.
                var httpClient = new HttpClient
                    BaseAddress = new Uri(string.Format("https://{0}", serviceNamespace))

                var payload = JsonConvert.SerializeObject(deviceTelemetry);
                httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", sas);
                var content = new StringContent(payload, Encoding.UTF8, "application/json");
                content.Headers.Add("ContentType", "application/atom+xml;type=entry;charset=utf-8");
                var result =  await httpClient.PostAsync(url, content);
            catch (Exception ex)


Code Snippet 5: Post events to Event Hub

After sending the data successfully you can view processes on Event Hub Dashboard in Azure portal.

Image 12

Figure 12: Event Hub Dashboard

Creating Stream Analytics Job for reading Event Hub data

What is Azure Stream Analytics

Stream Analytics is a component of the Microsoft Azure suite of Cloud and Big Data solutions. It offers the ability to find real time business insights from quantitatively large numbers of data streams and make those analysis available for various applications.

After creating all the processes for gathering and processing data, now we need to derive statistical information from processed data to an output. We can use Azure Stream Analytics as an Output option to store data. For this, first create a Stream Analytics Job as depicted in the diagram given below.


Image 13

Figure 13: Adding Stream Analytics

Input configuration

After creating a Stream Analytics Job, we have to create an input for this job. We can give already created Event hub instance as the input for this job.

For supplying input from Event Hub to Stream Analytics we have to configure following Event Hub settings:

Image 14

Figure 14: Creating Input Job from Event Hub
Note: Here we are creating Input job from Event Hubs

Field Values
INPUT ALIAS Any name for input job
SUBSCRIPTION  Use one of these options for subscribing
1.  Use Event Hub from Another Subscription
2.  Use Event Hub from Current Subscription
SERVICE BUS NAMESPACE Namespace of service bus in which this Event Hubs are present
EVENT HUB NAME Name of Event Hub
EVENT HUB POLICY NAME Policy name of Event Hub

Note: Here, the Event Hub Consumer Group is ‘Default’, so no need to specify anything there.

After creating the desired input mode we can make queries on input to get the desired output. We can use simple query as:

Image 15

Figure 15: Our Stream Analytics query.

The Query will be same as SQL query. You can use joints and condition here. Also you can use below query format to get query in decalred Output alias.

Image 16

Figure 16: Stream Analytics Query syntax.

Note: Here, [YourInputAlias] is “testinput” – it is the name of the input on which we are performing query.

And [YourOutputAlias] is the name given to your output job.

Tip: “Test” is a facility in which you can test your query on a JSON file. Create a JSON file which adds all the properties (as per data type) and run it. You will see the output of your query in the same window.

This query will run against the given input (Event hub’s data) and will give the output. We have to specify which output we desire to have for our purpose as shown below.

Image 17

Figure 17: Stream Analytics Output options.

As mentioned earlier, we created two outputs – SQL Azure and Power BI using below steps:

1. Output in SQL Azure Table 

Image 18

Figure 18: SQL database setting for Output.

Configure the output as per above details,

Fields Values 
Output Alias Any name for your output job
Subscription  Use one of these options for subscribing 
1.     Use SQL Database from Another Subscription
2.     Use SQL Database from Current Subscription
SQL DATABASE Name of the database under same subscription 
USERNAME Log in User name
PASSWORD Password 
Table The Table name in which you want to store job output.

After running analytics over the Event Hub we get the output in the SQL Azure table as below:

Image 19

Figure 19: Actual Azure SQL DB Output .

Monitor running Stream Analytics:

Monitor tab metrics are available for monitoring the usage and performance of Stream Analytics jobs. In the following image you can see “Input”, “Output” and “Out of order” Events. These are represented both in graphical and table format.

Image 20

Figure 20: Monitor tab metrics of Stream Analytics job.

2. Output in Power BI

What is MS Power BI?

In a nut-shell, Power BI is a strong real-time data visualizing tool which in true sense provide Business Intelligence inputs. Microsoft Azure’s Power BI as a service is the new experience for business entities which help visualize data in analytical form. There are basically three elements of Power BI which are put to use; Dashboard, Reports and Datasets. Dashboards display important business information using tile based interface in the form of charts, graphs, statistics etc., an important ingredient for strategic decision making. Reports and Datasets together can integrate with different types of data sources making it compatible across the platforms.

We have used Power BI for creating a visual representation of our data. We created a new Output  from Stream Analytics.

Below is the screen of dashboard for our application that we created.


Image 21


Figure 21: Freezer and Chiller temprature graph in Power BI


We provided a solution which could work in offline mode as well. Power BI helped in monitoring the business on all devices at any time by providing an insight to the real-time data. Event Hub has the ability to perform query on the incoming data to sort and take necessary information and give it to a proper output. In addition it has low latency and high reliability. Like most SaaS implementations, one of the prime advantages of Azure Stream Analytics is that you don’t need to invest in hardware nor manage the service yourself. It interfaces with your existing system, whether fully cloud or hybrid, and scales with your needs. Ensuring that you only pay for the capacity and service level that you actually use. For developers the major advantage of Azure Stream Analytics are the relatively simple SQL-like syntax. If you are already an Azure Developer then Stream Analytics will fit you like a glove.

In case you have any queries or you would like to know more about this solution then please reach to us at


Sample Projects

Key Contributors 

  • Onkarraj Ambatwar
  • Anjana Khaire
  • Rohit Lonkar
  • Rahul Khode









This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Architect Saviant
India India
I am a Technology Specialist and Microsoft Azure Consultant with 9 years of experience in Microsoft technologies development and Consulting. With experience from many international clients, I have developed skillsets across Business Analytics, IT System Transformation and Agile Methodology. I have also managed end-to-end project lifecycle development under extreme schedule constraints. Organized complex deployments of systems with various interfaces, external dependencies. Streamlined and documented milestone/life-cycle activity definition and standardization. Worked on user requirements, system architecture documents and user manuals. Also performed development activities to include: software and hardware preparation and configuration, GUI modification, testing, and troubleshooting.

I focus on structured and logical approach to development tasks and my deep analytical abilities help me in providing tailor-made solutions to clients' business problems. My daily routine includes activities like monitoring and tracking project status, schedule project activities, sprint planning, and to give recommendations for technical project direction. An expert in developing Windows Azure, multi-tenant and multilingual applications, I am fully adept in creating responsive interfaces for multiple hand held device platforms as well.

I am a proud holder of Master’s degree in Computer Science from a well-known University in Mumbai India. Besides having an appetite for learning, I utilize opportunities to mentor junior buddies on systems engineering and IT practices and business processes.

• Multi-tenant SaaS Application using Microsoft Azure PaaS
• Enterprise Mobility With Microsoft Azure Mobile service, SQLite, Xamarin
• Mobile solution with Windows 8.1, SQLite and Cloud
• SPA and Angular JS
• Technology Innovations with Microsoft Azure
• Agile Methodologies
• Software Configuration Management

Written By
Software Developer
India India
I am a Software Developer, with an inclination towards Arts - I sketch, play musical instruments. I believe in seeking divinity within one's self. Being a firm believer in humanity, I donate to causes - primarily supporting education of the underprivileged.

Reading books is one of my favorite pastimes, I am mostly into Fiction - I find books by Dan Brown very captivating. I watch documentaries, much more than movies.

Capability to 'End to end development' in web applications. I have worked with the latest technologies like 'AngularJs using.Net MVC', 'MVC Razor Engine' and few others. Domain Knowledge of Financial Services and Upstream. Aptitude for analyzing, unit testing, debugging code, identifying problems and creating solutions. Ability to initiate a client call, requirement gathering, manage workforce. Have a strong problem solving skills and RCA. Ability to work as a good team member or as an individual and confident in serving the best results.

Comments and Discussions

QuestionReally great project! Pin
Member 783885718-Dec-17 8:18
Member 783885718-Dec-17 8:18 
QuestionHow did your create the App? Pin
Member 1330384010-Jul-17 23:30
Member 1330384010-Jul-17 23:30 
QuestionZenMeasure Pin
Member 102070683-Jan-16 1:40
Member 102070683-Jan-16 1:40 
QuestionBest Mobile Article of March 2015 Pin
Sibeesh Passion8-Apr-15 18:53
professionalSibeesh Passion8-Apr-15 18:53 
QuestionFeaturing this Project on #idevthis Pin
Member 115791666-Apr-15 7:51
Member 115791666-Apr-15 7:51 
GeneralMy vote of 5 Pin
Member 115726681-Apr-15 8:29
Member 115726681-Apr-15 8:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.