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

:

Introduction

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

Link1: http://convergence.evo-td.com/library/CS15A022

Link2: http://convergence.evo-td.com/library/GEN150100 (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.


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.


Figure 2: Architecture of BLE Sensor Based Application

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:

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.

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.

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.

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.   

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.

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:

Figure7: Screenshot Showing Settings to Add Background Task

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

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.

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.
            Accelerometer.GetDefault();

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

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

            await MonitorTemperature();

            SetState(BackgroundTaskState.Completed);

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

            _taskInstance = null;

            if (_deferral != null)
            {
                _deferral.Complete();
            }

            // make sure we stop waiting for settings changes.
            _messages.StopWaitForMessages();

            _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;
                try
                {
                    if (await sensor.ConnectAsync())
                    {
                        sensor.ConnectionChanged += OnConnectionChanged;
                    }
                }
                catch (Exception ex)
                {
                    OnError(ex.Message);
                }
            }
            while (!token.IsCancellationRequested)
            {
                try
                {
                    await WriteSampleTemperatures();
                    SetState(BackgroundTaskState.DataAvailable);
                }
                catch (Exception ex)
                {
                    OnError(ex.ToString());
                }

                try
                {
                    await Task.Delay(TimeSpan.FromSeconds(WriteSampleRate), token);
                }
                catch
                {
                    // 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().

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.

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.

We have created an Event Hub using following steps:

Adding Event Hub for collecting data from sensors

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).

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.

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 http://msdn.microsoft.com/en-us/library/windowsazure/dn170477.aspx.
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);
    hash.Append(valueStrm);
    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)
        {
            try
            {
                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}.servicebus.windows.net/", 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)
            {

                throw;
            }
        }

Code Snippet 5: Post events to Event Hub

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

Figure 12: Event Hub Dashboard

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.

 

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:

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:

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.

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.

Figure 17: Stream Analytics Output options.

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

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 
SERVER NAME Server Name
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:

Figure 19: Actual Azure SQL DB Output .

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.

Figure 20: Monitor tab metrics of Stream Analytics job.

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.

 

 

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 onkarraj.ambatwar@saviantconsulting.com

Key Contributors 

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