Using Azure IoT Edge to exchange real time data from devices to IoT hub

Sanajit Ghosh
9 min readAug 20, 2018

--

In this post we are going to have an idea on how sensors and industrial devices send real time data to the IoT hubs via Microsoft IoT Edge platform.

For this we have created a .NET core web app that sends data, a Linux Virtual Machine which acts as a device/sensor and an active Microsoft Azure subscription for creating the IoT hub.

**Note: Before proceeding you need to have some basic understanding of docker containers and Linux commands.

1.Azure IoT Edge

Azure IoT Edge is an Internet of Things (IoT) service that builds on top of IoT Hub. This service is meant for customers who want to analyze data on devices, a.k.a. “at the edge”, instead of in the cloud. By moving parts of your workload to the edge, your devices can spend less time sending messages to the cloud and react more quickly to changes in status.

In this documentation we have turned our Linux virtual machine into an IoT Edge device. We have also created a custom module in .NET console app(.NET Core) and then we have deployed the module from the Azure portal to our device. The module that we deploy will send messages to the IoT Hub.

Architectural overview of IoT Edge

2. Creating Azure IoT Hub in portal

IoT hub can be created from Azure portal as well as through Azure CLI. In this project we have used azure portal and manually created it. If you are having an active azure subscription, then login to the portal and follow the steps below.

1. Sign in to the Azure portal.

2. Select Create a resource > Internet of Things > IoT Hub.

3. Enter the Subscription, Resource group, Region and Name

4. Once the above part is done you will be prompted to select the Pricing and scale tier

**Note Here is our IoT hub “IOTHubSg” and we have chosen the free tier that can receive 8000 daily messages. Based on the requirement and priority one can easily change their tier model.

3. Creating a device identity and registering with IoT hub

To make sure that devices gets an identity so that it can communicate with the IoT hub, we need to first connect it.

To do this we need to declare this to be an IoT Edge device from the very beginning

In the Azure portal, navigate to your IoT hub.

1. Select IoT Edge then select Add IoT Edge Device.

2. Give your simulated device a unique device ID. Here we have given “azureuser” as unique device name

3. Select Save to add your device.

4. Select your new device from the list of devices.

5. Copy the value for Connection string — primary key and save it. You’ll use this value to configure the IoT Edge runtime in the next content.

4.Creating the device with IoT Edge Runtime

Our next step is to create a Linux virtual machine and install the IoT Edge runtime on it. Since we are not having any physical sensors or hardware that sends data on real time basis, so our plan will be to prepare a device that can actually create such scenario.

For doing so we need to at first spin up a Ubuntu Linux Virtual machine from azure portal.

Here is link on how to create a Ubuntu Linux VM https://docs.microsoft.com/en-us/azure/virtual-machines/linux/quick-create-portal

You can see that we have a LinuxVM ready and we will now connect it with a SSH client and install our IoT edge runtime inside it.

4.1 Installing the IoT Edge Runtime

The IoT Edge runtime will be now deployed in IoT Edge device. IoT Edge device has three components. The IoT Edge security daemon starts each time an Edge device boots and bootstraps the device by starting the IoT Edge agent. The IoT Edge agent facilitates deployment and monitoring of modules on the IoT Edge device, including the IoT Edge hub. The IoT Edge hub manages communications between modules on the IoT Edge device, and between the device and IoT Hub.

Open the Linux terminal with root privileges and start using the following commands

4.2 Registering your device to use the software repository

> sudo –s /*root directory*/

> curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list ./microsoft-prod.list

> sudo cp ./microsoft-prod.list /etc/apt/sources.list.d/

> curl https://packages.microsoft.com/keys/microsoft.asc | gpg — dearmor > microsoft.gpg

> sudo cp ./microsoft.gpg /etc/apt/trusted.gpg.d/

4.3 Installing the Moby engine

>sudo apt-get update

>sudo apt-get install moby-engine

>sudo apt-get install moby-cli

Moby engine is an open source container runtime that powers Docker, as the engine for Azure IoT Edge. This design decision enables developers to package and deploy standard Docker containers as modules on Azure IoT Edge.

4.4 Installing the IoT Edge daemon

The IoT Edge daemon installs as a system service so that the IoT Edge runtime starts every time your device boots. The installation also includes a version of hsmlib that allows the security daemon to interact with the device’s hardware security.

Install it by the following command.

>sudo apt-get update

>sudo apt-get install iotedge

5. Creating the app (.NET core console app)

The device in IoT Edge requires a module, which is an image file of an application containerized in Docker and published in Docker hub. Microsoft has already provided a sample temperature Docker file in their documentation.

We will now create a module i.e a .NET core console app from scratch and then containerize it with Docker. For accessing the codes, you can follow up my GitHub handle https://github.com/sanajitghosh/consoleappcore.git

The code structure of the app is given in the below snapshot where we have also created an exe file from the .NET core app. You can follow the link on how to generate an exe file from a core app https://blog.jongallant.com/2017/09/dotnet-core-console-app-create-exe-instead-of-dll/

** Note: However, it is to keep in mind that exe .NET apps cannot be containerized directly in Docker especially if you are working in Linux environment. You can also create the device in windows platform using Docker for windows but is always advisable to use containers on Linux based distros.

Here is a basic code structure of the application which will simply return a string value as a message to the IoT hub using IoT Edge device.

Our next task is to containerize this application and push the image into Docker hub so that it can be accessible to the IoT hub as IoT edge module.

Module here is nothing but a containerized image of an application running with the help of IoT edge runtime. This runtime will in turn send data or message to the IoT hub.

6. Containerizing the application with Docker

In this part we will containerize the application with Docker. For more information about Docker and containerization please visit the link https://docs.docker.com/engine/examples/dotnetcore/#view-the-web-page-running-from-a-container

At first we need to create a Docker file in the Linux VM. Once the Docker file is ready you need to build and expose the ports of the container using the following commands.

$ docker build -t iot .

$ docker run -d -p 8080:80 — name iot iot

7. Running the application image in Docker container

Let’s see how the Docker image looks like and let’s run the application to see whether the output is in the form of the desired string value

To run the Docker image and to see whether the container running use the following commands

>docker run <image id>

>docker ps

You can clearly see the container is successfully running the application with the expected message “Welcome to IoT Hub!”

8. Pushing image to Docker hub

After we have successfully run the application in the container our next task will be to push the image in the Docker hub so that it can be accessible from anywhere. You must be noticing the repository sg7890/iotimage is the module for the IoT edge device. So basically we have successfully published our Docker image to Docker hub. We can easily build, run and ship the image from the Docker hub anywhere and at any time without much consideration on the platform and environment.

Below are the commands to push the Docker image to Docker hub

>docker tag <image id> yourhubusername/repositoryname

>docker push yourhubusername/repositoryname

9. Setting up the module in IoT Edge

In the previous part we have created an image module of the application and pushed it to Docker hub. Now we are going to add this module to IoT Edge.

At first we need to set up the module from Device Details. Here we have already created it and it is running. Follow these steps to set the module:

1. In the Azure portal, navigate to your IoT hub.

2. Go to IoT Edge and select your IoT Edge device.

3. Select Set Modules.

4. In the Deployment Modules section of the page, click Add then select IoT Edge Module.

5. In the Name field, enter iotimage.

6. In the Image URI field, enter sg7890/iotimage.

7. Leave the other settings unchanged, and select Save.

10. Connecting the device with Connection string

After completing the task in context 9, fill up the module details with name and image URL. Once the module is set and saved, navigate to the IoT Edge>Device Details and copy the Connection string (Primary Key)

10.1 IoT Edge Daemon Configuration File

Now we are going to paste the connecting string to the device.

Log in to the Linux vm as root and type the following command. Paste the connecting string in the IoT Edge Daemon Configuration by opening the editor.

>sudo nano /etc/iotedge/config.yaml

After the file has been saved we need to restart the IoT Edge Security daemon by

>sudo systemctl restart iotedge

**Note: One can also check the status of the Daemon from the command below. It is to be noted that there might be some errors due to the conflict of Docker and Moby engine which will prevent the Security daemon from starting. In that case you need to uninstall the Moby engine and then create a new repository and start the installation from very beginning.

>sudo systemctl status iotedge

11. View Generated data

In this Documentation, we have created a new IoT Edge device and installed the IoT Edge runtime on it. Then, we used the Azure portal to push an IoT Edge module to run on the device without having to make changes to the device itself.

Open the command prompt on the computer and run your device again. Confirm that the module deployed from the cloud is running on your IoT Edge device:

>sudo iotedge logs iotimage –f

Finally, we can see from the above command that our module i.e., iotimage (created from .Net application) have successfully send messages to the IoT hub via IoT Edge device.

This IoT Edge has many broader applications viewpoints like sending and receiving sensor data and then predicting the condition based on Data filter techniques, which in turn will alert the sensors present in the industries. For more such real time application refer to these links https://www.forbes.com/sites/janakirammsv/2018/07/01/5-reasons-why-azure-iot-edge-is-industrys-most-promising-edge-computing-platform/

https://venturebeat.com/2018/06/27/microsoft-launches-azure-iot-edge-out-of-preview/

--

--