{"id":15969,"date":"2016-12-08T13:33:00","date_gmt":"2016-12-08T21:33:00","guid":{"rendered":"https:\/\/devwww.3cloudsolutions.com\/post\/azure-iot-hub-and-streaming-analytics-services-2\/"},"modified":"2023-12-22T13:55:41","modified_gmt":"2023-12-22T21:55:41","slug":"azure-iot-hub-and-streaming-analytics-services","status":"publish","type":"post","link":"https:\/\/3cloudsolutions.com\/resources\/azure-iot-hub-and-streaming-analytics-services\/","title":{"rendered":"Azure IoT Hub and Streaming Analytics Services"},"content":{"rendered":"<p>The industry has reached an inflection point where cheap hardware, ubiquitous connections, and powerful cloud platforms allow us to consume data from devices all around us. The Internet of Things (IoT) represents a compelling opportunity for organizations to gain operational insight and perform predictive analytics in real-time and at a grain previously unimagined. These capabilities allow businesses to improve efficiency, reliability, and agility.<\/p>\n<p><!--more--><\/p>\n<p style=\"text-align: center;\"><img decoding=\"async\" style=\"width: 805px;\" title=\"AZURE IOT HUB AND STREAMING ANALYTICS SERVICES\" src=\"https:\/\/3cloudsolutions.com\/wp-content\/uploads\/2022\/11\/iStock-482707232edited.png\" alt=\"AZURE IOT HUB AND STREAMING ANALYTICS SERVICES\" width=\"805\" data-constrained=\"true\" \/><\/p>\n<p>From predicting and preventing product failures to rerouting flights to improve fuel efficiency, creative application of IoT capabilities represent a tremendous opportunity to gain a competitive advantage. Microsoft\u2019s Azure IoT Hub is a hyperscale cloud solution that is easy to provision, use, and manage while providing functionality across the spectrum of IoT solution needs, from security to device management.<\/p>\n<p>Microsoft offers several ways to get started with\u00a0its platform. <a href=\"https:\/\/www.microsoft.com\/en-us\/cloud-platform\/internet-of-things-azure-iot-suite\">Azure IoT Suite<\/a> offers two demo scenarios to demonstrate the power of the Azure IoT hub. The code for both the <a href=\"https:\/\/github.com\/Azure\/azure-iot-predictive-maintenance\">predictive maintenance<\/a> and the <a href=\"https:\/\/github.com\/Azure\/azure-iot-remote-monitoring\">remote monitoring<\/a> scenarios are available on <a href=\"https:\/\/github.com\/\">GitHub<\/a> for you to customize. Raspberry Pi boards are an inexpensive and useful tool to explore the features and functionality of Azure IoT Hub with a physical device. Microsoft, in collaboration with Adafruit, sells an <a href=\"https:\/\/www.adafruit.com\/products\/2733\">IoT Pack for Raspberry Pi 3<\/a> that is a wonderful way to jumpstart your development efforts or to get started with an IoT proof of concept. Microsoft also published a handy <a href=\"https:\/\/developer.microsoft.com\/en-us\/windows\/iot\/docs\/adafruitwelcome\">getting started guide<\/a> that shows how to set up a development environment and how to set up the Raspberry Pi board. The getting started guide is also useful if you already have a Raspberry Pi board and simply want to experiment with Windows IoT Core and Azure IoT Hub.<\/p>\n<p>Microsoft publishes Azure IoT Hub SDKs for C, .NET, Java, Node.js, and Python 2.7. The variety of popular languages gives you options to leverage your developer\u2019s existing skills.\u00a0 Windows IoT Core is targeted at .NET development, and Microsoft published a <a href=\"https:\/\/github.com\/ms-iot\/samples\">samples library<\/a> that has code examples for everything from controlling a single LED to building a voice-controlled coffee maker.<\/p>\n<p>One simple and fun starter project is to monitor the temperature and humidity in your environment using a DHT 11 or 12 sensor and to display a real-time graph of the data in PowerBI.\u00a0 You can find the wiring diagrams for both sensors <a href=\"https:\/\/learn.adafruit.com\/dht-humidity-sensing-on-raspberry-pi-with-gdocs-logging\/wiring\">here<\/a>.<\/p>\n<p><img decoding=\"async\" style=\"width: 660px; margin-right: auto; margin-left: auto; display: block;\" src=\"https:\/\/3cloudsolutions.com\/wp-content\/uploads\/2022\/11\/IoT.jpg\" alt=\"IoT.jpg\" width=\"660\" \/><\/p>\n<p>Once you have the Raspberry Pi board configured and the sensor wired correctly, you can begin programming the device to stream data to the Azure IoT hub.\u00a0 You can download and install the <a href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=MicrosoftIoT.WindowsIoTCoreProjectTemplates\">Windows IoT Core Project Templates<\/a> for visual studio and use the Background Application template as a simple starter project.\u00a0 In order to get started quickly, I recommend Daniel Porrey\u2019s <a href=\"https:\/\/github.com\/porrey\/dht\/tree\/master\/source\/Windows%2010%20IoT%20Core\/DHT%20Solution\">Universal Windows Library for the DHT11\/22 sensors<\/a>, which will allow you to focus on the Azure IoT Hub capabilities and device to cloud communication rather than low-level device programming. Add the library to your solution and add a reference to it in your background application project.\u00a0 <a href=\"https:\/\/www.nuget.org\/packages\/newtonsoft.json\/\">Newtonsoft\u2019s json library<\/a> is also an invaluable tool to easily serialize and de-serialize messages to and from the cloud.<\/p>\n<p>The <a href=\"https:\/\/github.com\/ms-iot\/samples\/tree\/develop\/IoTConnector\/IoTConnectorClient\/IoTConnectorClient\">IoT Connector Client<\/a> sample provides examples of how to connect to, send messages to, and receive messages from Azure IoT Hub.\u00a0 Note that the sample assumes you are using the Trusted Platform Module (TPM) to secure connection information.\u00a0 You can learn more about TPM and how to provision devices <a href=\"https:\/\/blogs.windows.com\/buildingapps\/2016\/07\/20\/building-secure-apps-for-windows-iot-core\/\">here<\/a>.\u00a0 The Raspberry Pi 3 does not contain a hardware TPM device, but rather uses a software emulated device. Your code should look something like this:<\/p>\n<p style=\"padding-left: 60px;\"><code><span style=\"color: #000000;\">public void Run(IBackgroundTaskInstance taskInstance)<\/span><\/code><br \/>\n<code><span style=\"color: #000000;\">{<\/span><\/code><br \/>\n<code><span style=\"color: #000000;\">\u00a0 \u00a0 deferral = taskInstance.GetDeferral();<br \/>\n<\/span><\/code><br \/>\n<code><span style=\"color: #000000;\">\u00a0 \u00a0 TpmDevice device = new TpmDevice(0);<\/span><\/code><br \/>\n<code><span style=\"color: #000000;\">\u00a0 \u00a0 string hubUri = device.GetHostName();<\/span><\/code><br \/>\n<code><span style=\"color: #000000;\">\u00a0 \u00a0 string deviceId = device.GetDeviceId();<\/span><\/code><br \/>\n<code><span style=\"color: #000000;\">\u00a0 \u00a0 string sasToken = device.GetSASToken();<\/span><\/code><\/p>\n<p><code><span style=\"color: #000000;\">\u00a0 \u00a0 _sendDeviceClient = DeviceClient.Create(hubUri, AuthenticationMethodFactory.CreateAuthenticationWithToken(deviceId, sasToken), TransportType.Amqp);<\/span><\/code><\/p>\n<p><code><span style=\"color: #000000;\">\u00a0 \u00a0 InitGPIO();<\/span><\/code><\/p>\n<p><code><span style=\"color: #000000;\">\u00a0 \u00a0 readingTimer = ThreadPoolTimer.CreatePeriodicTimer(ReadingTimerTick, TimeSpan.FromSeconds(1));<\/span><\/code><br \/>\n<code><span style=\"color: #000000;\">}<\/span><\/code><\/p>\n<p>Once you are connected, simply put the code to read the DHT sensor, serialize the data, and send the message into the Background Application\u2019s main timed loop.\u00a0 Your code may look something like this:<\/p>\n<p style=\"padding-left: 30px;\"><code>private async void ReadingTimerTick(ThreadPoolTimer timer)<\/code><br \/>\n<code>{<\/code><br \/>\n<code>\u00a0 \u00a0 DhtReading reading = new DhtReading();<\/code><\/p>\n<p><code>\u00a0 \u00a0 reading = await dht.GetReadingAsync().AsTask();<\/code><\/p>\n<p><code>\u00a0 \u00a0 if (reading.IsValid)<\/code><br \/>\n<code>\u00a0 \u00a0 { <\/code><br \/>\n<code>\u00a0 \u00a0 \u00a0 \u00a0 float currentTemperature = Convert.ToSingle(reading.Temperature);<\/code><br \/>\n<code>\u00a0 \u00a0 \u00a0 \u00a0 float currentHumidity = Convert.ToSingle(reading.Humidity);<\/code><\/p>\n<p><code>\u00a0 \u00a0 \u00a0 \u00a0 temperature = currentTemperature * 9 \/ 5 + 32;<\/code><br \/>\n<code>\u00a0 \u00a0 \u00a0 \u00a0 humidity = currentHumidity;<\/code><\/p>\n<p><code>\u00a0 \u00a0 \u00a0 \u00a0 var telemetryDataPoint = new<\/code><br \/>\n<code>\u00a0 \u00a0 \u00a0 \u00a0 {<\/code><br \/>\n<code>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 time = DateTime.Now.ToString(),<\/code><br \/>\n<code>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 deviceId = deviceId,<\/code><br \/>\n<code>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 location = \"Office\",<\/code><br \/>\n<code>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 currentHumidity = humidity,<\/code><br \/>\n<code>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 currentTemperature = temperature<\/code><br \/>\n<code>\u00a0 \u00a0 \u00a0 \u00a0 };<\/code><\/p>\n<p><code>\u00a0 \u00a0 \u00a0 \u00a0 var messageString = JsonConvert.SerializeObject(telemetryDataPoint);<\/code><br \/>\n<code>\u00a0 \u00a0 \u00a0 \u00a0 var message = new Message(Encoding.ASCII.GetBytes(messageString));<\/code><\/p>\n<p><code>\u00a0 \u00a0 \u00a0 \u00a0 await _sendDeviceClient.SendEventAsync(message);<\/code><br \/>\n<code>\u00a0 \u00a0 }<\/code><br \/>\n<code>}<\/code><\/p>\n<p>Build and deploy the project to your Raspberry Pi.\u00a0 You can use <a href=\"https:\/\/github.com\/Azure\/azure-iot-sdk-csharp\/tree\/master\/tools\/DeviceExplorer\" target=\"_blank\" rel=\"noopener\">Device Explorer<\/a> to confirm that your application is sending data to the Azure IoT hub.<\/p>\n<p><img decoding=\"async\" style=\"width: 523px; margin-right: auto; margin-left: auto; display: block;\" src=\"https:\/\/3cloudsolutions.com\/wp-content\/uploads\/2022\/11\/IoT2.png\" alt=\"IoT2.png\" width=\"523\" \/><\/p>\n<p>The final step is to use Azure Streaming Analytics Service to stream your data into PowerBI.\u00a0 Configuring the stream to PowerBI is straightforward.\u00a0 Simply choose Azure IoT Hub as a source and PowerBI as a destination.\u00a0 A very simple example query is shown below.<\/p>\n<p><img decoding=\"async\" style=\"width: 674px; margin-right: auto; margin-left: auto; display: block;\" src=\"https:\/\/3cloudsolutions.com\/wp-content\/uploads\/2022\/11\/IoT3.png\" alt=\"IoT3.png\" width=\"674\" \/><\/p>\n<p>Microsoft\u2019s how-to document on <a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/stream-analytics\/stream-analytics-power-bi-dashboard\">building a real-time analytics dashboard<\/a> contains detailed instructions on how to configure Streaming Analytics Service and PowerBI to visualize the temperature and humidity data in real time.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"margin-right: auto; margin-left: auto; display: block;\" src=\"https:\/\/3cloudsolutions.com\/wp-content\/uploads\/2022\/11\/IoT4.png\" alt=\"IoT4.png\" width=\"523\" height=\"362\" \/><\/p>\n<p>Azure IoT Hub is a unique offering that simplifies many of the difficult tasks involved with a large-scale IoT project\u2019s implementation.\u00a0 This article outlined a few possibilities to explore the features of the platform and to inexpensively begin experimenting with your own solution.<\/p>\n<p>To learn more about how BlueGranite could help you build and manage your Azure IoT Hub, <a href=\"https:\/\/www.blue-granite.com\/contact-us\">contact us<\/a> today!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Internet of Things represents an opportunity for organizations to gain operational insight and perform predictive analytics in real-time.<\/p>\n","protected":false},"author":21,"featured_media":14742,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"content-type":"","footnotes":""},"categories":[395,260],"tags":[319,317],"class_list":["post-15969","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-science-ai","category-data-ai","tag-machine-learning-ai","tag-manufacturing-industrial","topics-blog"],"acf":[],"_links":{"self":[{"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/posts\/15969","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/comments?post=15969"}],"version-history":[{"count":0,"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/posts\/15969\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/media\/14742"}],"wp:attachment":[{"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/media?parent=15969"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/categories?post=15969"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/tags?post=15969"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}