Notes From the Field: An Azure IoT journey


Although Mariner has been using Azure IoT for some time, this was my first assignment on an IoT project and although  an experienced architect gave me the overall design, it was also a learning assignment for the team were allowed to experiment and discover the details ourselves.

FIRST:

The project is delivering real-time and summary information on the performance of an industrial process. To do this we have to build:

  1. IoT Hub
  2. Device connectivity, send and receive messages
  3. Stream information to storage in Azure cloud and Power BI

It looked something like

iotjourney

The IoT Hub was pretty straightforward to create. We followed this link and we got out first IOT Hub created with no effort.

THEN:

More challenging was connecting to a device and sending messages, the below elements are need to complete the task

1.      Create a device Identity in Azure IoT Hub

2.      Create and Run Simulated Device  

A useful tool is hub explorer. IoT Hub explore can be used to create device identities. For our convenience, we used Device Explorer to create out device Identities. With Device Explorer we can:

a.      Create Devices in IoT Hub and respond with access keys

b.      Remove devices from IoT Hub

c.      Update access keys

d.      Send messages to devices via IoT Hub

e.      Monitor messages sent from devices via IoT Hub

We named out device as “TestDevice1” and it had the keys created in IoT Hub as seen in the device explorer below

Devicemgr

 

So now our IoT device identities were created and as per IoT Hub it had 1 device in its basket. But the hub didn’t receive any message from the device as there is no actual device running.

NEXT:

To create messages we created a Windows Console App running in local machine that would send and receive messages. We followed the below instructions from this link.

1)      In Visual Studio, add a new Visual C# Windows Classic Desktop project to the current solution, by using the Console Application project template. Make sure the .NET Framework version is 4.5.1 or later. Name the project  SimulatedApp

simapp

2)      In Solution Explorer, right-click the SimulatedApp project, and then click Manage NuGet Packages.

3)      In the NuGet Package Manager window, select Browse, search for Microsoft.Azure.Devices.Client, select Install to install the Microsoft.Azure.Devices.Client package, and accept the terms of use. This downloads, installs, and adds a reference to the Azure IoT Device SDK NuGet package and its dependencies.

4)      Add the following using statement at the top of the Program.cs file:

using Microsoft.Azure.Devices.Client;

using Newtonsoft.Json;

5)      Add the following fields to the Program class. Substitute the placeholder values with the IoT hub connection string from Device Explorer for the new device “TestDevice1” created in Hub

static string  DeviceConnString = “HostName=notgoingtotellyou-devices.net;

static string DeviceId=TestDevice1;

static string SharedAccessKey=nothingtoseehere;

static DeviceClient DeviceClnt = DeviceClient.CreateFromConnectionString(DeviceConnString);

6)      Add the following method to the Program class:

private static async Task SendDeviceToCloudMessagesAsync()

        {

            Random RandDepth = new Random(1);

            Random RandSpeed = new Random(2);

            int i = 0;

            string ReceivedMsgString;

            try

            {

                while (i <= 20)

                {

                        int currentdepth = RandDepth.Next(1, 10);

                        int currentspeed = RandSpeed.Next(1, 20);

                        string formatString = “yyyyMMddHHmmss”;

                        DateTime d = DateTime.Now;

                        string dateString = d.ToString(formatString);

                        DateTime timeNow = DateTime.ParseExact(dateString, formatString, null);

 

                        var telemetryDataPoint = new

                        {

                            recordtime = timeNow,

                            depth = currentdepth,

                           speed = currentspeed

                        };

                        var DeviceMsgString = JsonConvert.SerializeObject(telemetryDataPoint);

                        var DeviceMsg = new Microsoft.Azure.Devices.Client.Message(Encoding.ASCII.GetBytes(DeviceMsgString));

                        await DeviceClnt.SendEventAsync(DeviceMsg);

                        Console.WriteLine(“{0} > Sending message: {1}”, DateTime.Now, DeviceMsgString);

                    }

                    i = i + 1;

                    Task.Delay(2000).Wait();

            }

            catch (Exception err)

            {

                Console.WriteLine(err);

            }

            return;

        }

This method sends a new device-to-cloud message every 2 second for 20 times. The message contains a JSON-serialized object, with the device ID and a randomly generated number to simulate a depth and speed sensors.

6)      Finally, add the following lines to the Main method:

Console.WriteLine(“Simulated device\n”);

         SendDeviceToCloudMessagesAsync().Wait();

         Console.ReadLine();

      Environment.Exit(0);

FINALLY:

Thus we created out first Device client and sent simulated messages to the IoT Hub. Now with the device up and running, we wanted to visualize the data. There was an interesting article that helped us in that task.

In the Hub we had to create a new Stream Analytics Job that uses it as telemetry input and Power BI as telemetry stream analytics output. It looked like

streamanalytics

So now we have a hub and a client device that is transmitting messages to the hub.

More to come on my next Blog…..

Sankar Subramanian

Sankar Subramanian

Sankar is a Data Analytics Consultant with around nine years experience in the Business Intelligence and Data Analytics realms of IT.
Sankar Subramanian

Latest posts by Sankar Subramanian (see all)


One thought on “Notes From the Field: An Azure IoT journey

Please share your thoughts on this post:

Your email address will not be published. Required fields are marked *

About Mariner

Call Us Email Us

How Can We Help You?

Mariner © 2018. All Rights Reserved.