Starting Out with Azure IoT Edge

In today’s article I’d like to discuss Azure IoT Edge and how you can get started with it. I will be explaining the benefits of Azure IoT Edge and providing a couple of demos to demonstrate how you can create a very simple IoT solution using an IoT Edge device.

Benefits of Azure IoT Edge

Azure IoT Edge is Microsoft’s fully managed service built on IoT Hub, which enables users to perform edge computing on one or more IoT Edge devices. Edge computing is the idea that the IoT devices (at the “edge” of the network) are the ones performing the heavy computing instead of server processes in the cloud.

Figure 1: Azure IoT Edge Diagram

This is beneficial as the individual devices can perform the calculation/processing/analysis and send the resulting insights to the cloud, instead of the devices sending all the raw data to the cloud for processing. Using edge computing results in reduced bandwidth costs, faster response times and reduced traffic.

Azure IoT Edge Components

There are three components that make up Azure IoT Edge:

Figure 2: Azure IoT Edge Components
  • IoT Edge Modules
  • IoT Edge Runtime
  • Cloud Interface

IoT Edge Modules are the Azure or third-party services that run in containers. You can also provide your own code. These modules are deployed to the IoT Edge devices and run locally on those devices. They provide the Edge “magic” as they can perform calculation/processing/analysis of your device data.

The IoT Edge Runtime is the code that runs on each IoT Edge device and manages the modules running on the device. The runtime also performs tasks such as maintaining security on the device, reporting module health to the cloud and managing communications between any downstream leaf devices and the IoT Edge device, between modules on the IoT Edge device and between the cloud and the IoT Edge device.

The cloud interface provides the ability to centrally manage the lifecycle for your IoT Edge devices. You can create and configure one or more workloads for your devices, as well as deploy them to all of your devices. You can also monitor your devices for any errors or devices that are not behaving properly.

Figure 3: IoT Edge Cloud Interface

Deploying an IoT Edge Module

The first demo I’d like to show you involves setting up a virtual IoT Edge device and deploying some sample code. We will also create an IoT Hub so the device can send its data to it.

Prerequisites

For both of these demos there are a couple of prerequisites that you will need. The first is an Azure subscription as we will be creating some cloud resources in these demos. You can sign up for a free account here.

The other prerequisite is installation of the Azure IoT Extension for Azure Cloud Shell. You can add this extension by launching a Cloud Shell session after logging into your Azure Portal. Once the shell window displays enter the following command to install the extension:

az extension add --name azure-iot

You only need to run this once.

Setup of IoT Edge Device

We will be using a virtual machine (VM) hosted in Azure as our IoT Edge device. I will run through the setup and configuration of this VM using Linux (there are similar steps if you wish to use a Windows VM).

There are two ways you can create a VM in Azure. The first way is to manually go through the steps in the Azure Portal to provision a VM. You should select the latest Ubuntu Server build for your image if you’re provisioning a Linux VM. This will give you a standard VM with no additional components installed. You would then be required to manually install the Azure IoT Edge components afterwards.

The second way to create this VM is to use the Azure Cloud Shell. I prefer this way as you can select a custom IoT Edge image that also automatically installs all the components needed to run IoT Edge on the VM (acting as a device). Accept the terms of use and create a VM by executing these commands:

az vm image terms accept --urn microsoft_iot_edge:iot_edge_vm_ubuntu:ubuntu_1604_edgeruntimeonly:latest
az vm create --resource-group IoTEdgeResources --name EdgeVM --image microsoft_iot_edge:iot_edge_vm_ubuntu:ubuntu_1604_edgeruntimeonly:latest --admin-username azureuser --generate-ssh-keys

Note that the commands expect you have a resource group created called IoTEdgeResources. The VM will be provisioned with a name of EdgeVM, and with an administrator username of azureuser and SSH public key authentication. You can see the command-line switch of –generate-ssh-keys which will generate the public and private keys and store them in the ~/.ssh folder in your Azure Cloud Shell instance.

Creating Azure IoT Hub and Device Registration

The next step is to provision an instance of Azure IoT Hub and register an IoT Edge device. Again, you can do this in two ways. The first is to manually run through the steps in the Azure Portal to provision IoT Hub and register your IoT Edge device.

The other way is to use the Azure Cloud Shell. Here are the commands to instantiate IoT Hub and register an IoT Edge device. Please note the resource group of IoTEdgeResources and make sure you enter your own name for the IoT Hub and Edge device. Take note of the –edge-enabled command-line switch, as this is what tells Azure to register a device as an IoT Edge device.

az iot hub create --resource-group IoTEdgeResources --name {hub_name} --sku F1 --partition-count 2
az iot hub device-identity create --hub-name {hub_name} --device-id {device_name} --edge-enabled

Once these commands finish, run the following command to display the device connection string and save it for a later step.

az iot hub device-identity show-connection-string --device-id {device_name} --hub-name {hub_name}
Figure 5: IoT Edge Device in Azure IoT Hub

Configure IoT Edge Device

Now that we have Azure IoT Hub and a registered IoT Edge device, we need to set the connection string with the value you saved previously from your registered IoT Edge device in IoT Hub. This will configure the VM to send its data to that registered device in Azure.

In your Azure Cloud Shell instance enter the following command, replacing for the IoT Edge device connection string:

az vm run-command invoke -g IoTEdgeResources -n EdgeVM --command-id RunShellScript --script "/etc/iotedge/configedge.sh '{device_connection_string}'"

Please note that running this command expects that the IoT Edge runtime is already installed on the VM. Since we used the Azure IoT Edge on Ubuntu image when provisioning the VM, this has already been installed. When complete, the output should look similar to this:

Figure 6: Set Connection String for IoT Edge Device

Once this command is complete you will need to connect to the VM using the public IP address that was given when the VM was created. This can be done right in the Azure Cloud Shell with this command:

ssh azureuser@{publicIpAddress}

The first time you do this you will be prompted to establish the authenticity of the host. Enter yes to continue connecting. This will also add an entry into a known_hosts file (so you are not prompted in the future).

Figure 7: SSH Host Authentication

Alternatively, you can use an SSH client (like PuTTY) to SSH into your VM. However, be aware that you need to copy the public and private SSH keys from your Azure Cloud Shell locally and create a .ppk file that PuTTY can use for authentication. Instructions on how to do this are located here.

Verify that the IoT Edge security daemon is running properly as a system service by executing the following command:

sudo systemctl status iotedge
Figure 8: IoT Edge Security Daemon

You can also view the running modules on the device. At this point you should only see the edgeAgent running.

sudo iotedge list
Figure 9: Running Modules on IoT Edge Device

Deploy IoT Edge Module

We can now deploy our module to the IoT Edge device. We are going to use a pre-built module from the IoT Edge section of the Azure Marketplace, which sends simulated temperature and pressure data to the cloud. You can also use your own custom code.

Note that we don’t have to access the device to deploy this module – we can perform the deployment right from the cloud. IoT Edge modules are executable packages implemented as containers. In order to deploy our module, navigate to your IoT Hub in the Azure Portal. Click on IoT Edge under Automatic Device Management from the left-hand pane.

Figure 10: Automatic Device Management

Click on your device in the list and select Set Modules from the upper bar. Click on the Add button to dropdown a menu selection. Click on +Marketplace Module to add a module from the marketplace.

Figure 11: Selecting a Marketplace Module

Enter simulated temperature sensor in the search box and select the results displayed. Click Routes to continue.

Figure 12: Simulated Temperature Sensor Module

You also need to configure the routes, which define how messages are passed between the module and IoT Hub. For our example, we want all the messages from the module to be sent to IoT Hub ($upstream), so add the following code for the Value for $upstream (if not auto-populated):

FROM /messages/* INTO $upstream

Click Review + Create and then Create to deploy the module.

After a few minutes if you refresh the page in the Azure Portal that lists the modules running on your device, you will see the SimulatedTemperatureSensor appear and show that its runtime status as running. Azure doesn’t push anything to your device when you add a module – the device checks Azure periodically for updates and when it sees that a module needs to be deployed it then pulls that module image from the cloud to start running it locally.

Figure 13: Simulated Temperature Sensor

Viewing the Data

You can view the simulated data from your IoT Edge device in two places. If you SSH into the VM and run the following command you can see the Simulated Temperature Sensor module executing and displaying the data it is sending to the cloud.

sudo iotedge logs SimulatedTemperatureSensor -f

You can also see the messages being received in your IoT Hub in the Azure Portal on the Overview page:

Figure 14: Azure IoT Hub Overview

Deploying to a Physical Device

The second demo I’d like to show you is how to deploy IoT Edge to a physical device. Using simulated devices is great for initial learning but I always like to actually deploy to a physical device since you will need to do this on an actual project! You will need a device that supports IoT Edge – I am using a Raspberry Pi 4 for this demo. You can use whatever device as long as the operating system can run containers. Please see this link for more details.

Most of the steps for the first demo still apply – the only difference is that instead of creating a virtual machine to simulate the IoT Edge device we will be using a real device (Raspberry Pi in our case). With that in mind, the first thing you should do is install the latest version of Raspian OS on your Pi.

Once you have the OS installed and you can SSH into the device, you will need to install the container runtime and the Azure IoT Edge components. The recommended container runtime is Moby. Run the following Bash commands on your device to update the package lists and install the Moby engine.

sudo apt-get update
sudo apt-get install moby-engine

You can optionally install the Moby command-line interface, which is helpful for development environments. When I ran this command the system indicated it was already installed when I installed the Moby engine.

sudo apt-get install moby-cli

After the container runtime is installed the next step is to install the Azure IoT Edge security daemon. This provides and maintains security standards on the device and bootstraps the device by starting the IoT Edge runtime when you boot up the device. Run the following Bash command to install the security daemon.

sudo apt-get update
sudo apt-get install iotedge

Once the security daemon is installed you then need to configure the IoT Edge device with the device connection string from your IoT Edge device in IoT Hub. This links your physical device to the device identity in the cloud. Open the configuration file at /etc/iotedge/config.yaml with this command:

sudo nano /etc/iotedge/config.yaml

Scroll down to the Manual Provisioning section and update the value of device_connection_string with your device connection string. Save the file when you are finished.

Figure 15: Configure Connection String for IoT Edge Device

You need to restart the security daemon to have the changes you made take effect, so run this command to restart IoT Edge on the device:

sudo systemctl restart iotedge

You can list the running modules on your device with this command. Please note that this point you should only see the edgeAgent module (since we haven’t deployed a module to the device yet).

sudo iotedge list

From this point you can follow the remaining steps from the first demo as they are identical. You would deploy your module from Azure IoT Hub in the portal, and once you do that you should be able to check the running modules on your physical device and see telemetry data (from the Simulated Temperature Sensor module) being sent to Azure IoT Hub.

Figure 16: IoT Edge Modules running on Physical Device

Congratulations! You have deployed an IoT Edge module to an actual physical device! You can now experiment further on creating other modules that can be deployed to your device.

I hope this article was helpful in explaining what Azure IoT Edge was and how you can deploy it to a simulated and real IoT Edge device. For more information on IoT Edge please visit https://docs.microsoft.com/en-us/azure/iot-edge/.

Thanks for reading!

Beer Kegerator with IoT Part 2 – How A Beer to the Face is a Good Thing

Normally, getting splashed in the face with beer is not a good thing. I am going to show you how using your face with beer can be a good thing. This blog is a continuation of my previous blog on using IoT with a Beer Kegerator (link).

Deploying the first version of the IoT app was well received, and there were a lot of great feedback suggestions on how to improve the experience. I wanted to showcase some of the newer Microsoft technologies, and ended up choosing facial recognition support for the app. This would have great benefits, as it would allow accurate tracking of who is pouring the beer and when they requested a pour. Coupled with the volume tracking of the selected beer, this would provide excellent data capabilities on who is drinking beer, when the beer is poured, along with the consumption rates of the different beers provided.

I also wanted to expand the app to include all four beer lines that we have available, so this required a bit of a redesign of the app. In order to do this, I needed to have the app enable/disable any of the four lines when someone requests to pour a specific beer.

Additional Hardware

First off, I purchased the additional three flow sensors so that I had the ability to track volume on all four beer lines. Modifying the code to account for this was simple, as it was just a matter of designating additional GPIO pins to receive the digital pulses.

More interesting is how I was going to handle the selection of beer when a pour is requested. I needed to verify that only the selected beer was able to be poured, while the other lines would not work. This meant providing the ability to disable each of the beer lines when necessary. I solved this problem with a food-grade quality solenoid valve, which allows opening/closing control of the valve via an electrical signal.

solenoid

This valve is normally closed, which means that when powered off the valve is closed, thereby stopping the flow of beer in the line it’s connected to. Providing power to the valve (12 volts) opens the valve as long as the power is applied to it.

The next question is how do I control the 12 volt signal to each of the solenoid valves connected to the four beer lines? This can be done with a simple relay switch, which allows a control signal to toggle the 12v on or off to the solenoid valve. I found a simple circuit board with four relays on it:

relayswitches

This will work perfectly for my situation, as I can use 5v control signals from the raspberry pi to turn on/off the different beer lines (activating a solenoid via the relay board) when a pour is requested.

Software Changes

With the hardware issues resolved, we now turn to the changes needed in the software. In order to support and control four beer lines, changes are needed in both the IoT app on the Raspberry Pi, as well as creating a web app to handle administration.

IoT Kegerator App

The main changes to the IoT app are centered around how to display the four different beer lines that a user can choose from, along with the process to select a beer and have the facial recognition routines recognize the person, so the selected beer line can be opened for the pour.

The previous version of this app only handled one line, so I needed to leverage the touchscreen I am using with the Raspberry Pi, in order that a person can touch an icon of the beer they want.

BeerKegerator2

This will start the facial recognition process so they can be recognized. Once the app recognizes them, it will open their selected choice for 30 seconds for them to pour their beer. After 30 seconds, the system will close the line and be ready for the next person.

BeerKegerator3

The following code snippet shows how the app processes a request from a person, when they touch one of the beer icons:

BeerLine1ButtonClick_CodeSnippet

The first thing this method does is display the associated web camera feed and then calls the facial recognition routine to take a snapshot of the webcam and process it for recognition. If it returns with a valid registered person, the routine will open the solenoid valve for that beer line ONLY, by setting a GPIO pin which will drive a specific relay, which in turn enables the 12v signal to the solenoid valve to open it. The method will then set the 30 seconds and start a timer to count down to 0, where it will then reset for the next time.

ActiveTimerTick_CodeSnippet

ActiveTimerTick2_CodeSnippet

This timer method runs every second, and records the digital pulses coming from the open beer line flow sensor, for the 30 seconds the beer line is open for. After it reaches the end of the 30 seconds, it will then take the total of the recorded digital pulses and send that info to Azure for saving into the database. The method will then reset some counter variables and turn itself off.

Facial Recognition

The final change needed for the IoT Kegerator App is adding support for facial recognition. Since I wanted to showcase Microsoft technology as part of this app, it made sense to leverage the Azure Cognitive Services suite of functionality that they provide (more info here). Specifically, I will be using the Face API that is part of the Computer Vision area.

The Face API is designed to detect and analyze faces/facial features in images. This will work for my implementation, as I can save the picture that is taken by the webcam when a person is requesting a beer. The trick then is to compare that image taken from the webcam against a “trained” model of images for a registered user. If recognized, the API reports that back to the app, which in turn will open the beer line for the person. Here is a code snippet showing how the Face API is called and leveraged to recognize a face when the app takes a picture.

FaceAPI_CodeSnippet

Please note that the CustomPerson class is based off the Face API Person class. The UploadandDetectFaces method calls the Face API to detect any faces in the webcam picture, and returns an array of detected faces. I then call the IdentifyAsync method against the array of detected faces to see if any correspond to any registered user face models that I trained previously. If the Face API determines there is a match I get additional details about the recognized face and return that to the calling method.

Here is a screenshot of the final redesigned app (with hardware):

BeerKegerator1

In conclusion, now that I’ve implemented this latest version of the IoT Kegerator app, I am able to track the consumption of each beer line, along with how much beer each registered user is consuming. I also have the ability to track time series data on when beer is being consumed, along with consumption rates of each beer on the four lines. Here is a quick example of a monthly consumption report:

ConsumptionReport

Here’s to getting a beer to the face!

Beginning IoT – Installing Windows 10 IoT Core on a Raspberry Pi

This is the first part in a 2-part series on how to install Microsoft Windows 10 IoT Core on an Internet-of-Things (IoT) device. This article will focus on the steps required to install Windows 10 IoT Core on a Raspberry Pi 3. Part 2 will focus on installation on an x86/x64 device.

 

What is Windows 10 IoT Core?

Windows 10 IoT Core is a version of Microsoft’s Windows 10 operating system that has been optimized for smaller devices that can run on either ARM or x86/x64 devices. These devices can run with or without a display device.

When we talk about the different IoT devices, the processor type needs some explanation. ARM devices are called Advanced RISC Machines, with RISC standing for Reduced Instruction Set Computer. What this means is that the processor has been slimmed down to only include a reduced set of commands it can process. While this means that the processor can’t do certain things, it requires a low amount of power to execute what it can do, so that translates to increased battery life. The Raspberry Pi is classified as an ARM device.

Devices with the x86/x64 architecture are classified as CISC processors, which stands for Complex Instruction Set Computer. These processors do not have their instruction sets slimmed down, so they can perform more complex operations, at the cost of increased power consumption (and therefore lower battery life). Intel’s Baytrail devices running the Intel Atom processor E3800 is an example of an x64 device.

https://www.intel.com/content/www/us/en/embedded/products/bay-trail/overview.html

 

Prerequisites

Before you can install on the Raspberry Pi, you need to make sure you have a PC that is running Windows 10 1507 (version 10.0.10240) or higher. You can find out what version you are running by clicking on the search box (next to the Start button) and typing ‘winver’. This will display a dialog as shown here:

1

 

You will also need to download and install the Windows 10 IoT Core Dashboard from here.

Of course you will also need a Raspberry Pi device to install onto. There are several different kits available on Amazon – I used the Canakit Starter Kit here.

Finally, you will need an SD card reader so that you can write the installation files to the SD card that will be placed in the Raspberry Pi.

 

Installation Steps

Once you have the prerequisites, you are now ready to begin the installation process.

First off, run the Windows 10 IoT Core Dashboard program, and click on Set up a new device from the menu on the left. This will display a screen that allows you to select the Device Type, OS Build and other information to configure as part of the installation.

2

Select Broadcomm [Raspberry Pi 2 & 3] as the Device Type, and Windows 10 IoT Core (17134) as the OS Build. You can also select Windows Insider Preview or Custom if you want to install a preview build of Windows 10 IoT Core, or a custom image file (flash.ffu file).

Next, insert your SD card into the Windows 10 PC you are using. Be aware that your SD card should be at least 8GB in size and I prefer formatting it prior to this step (this is optional – understand that the installation process will overwrite any pre-existing data on the SD card). The IoT Core Dashboard program should recognize the SD card and display it in the Drive selection.

You can then enter values for the Device Name and Password, and whether you want to use a Wi-Fi Network Connection when the Raspberry Pi starts up with our installation.

3

Check the box to accept the software license term and click on the Download and Install button. This will begin the installation process. During this process, Windows 10 IoT Core will be downloaded and the installation process will flash the files to the SD card.

5

If you see a prompt for UAC (User Access Control), click Yes to continue. The process may then open a command window to clean the SD card (if you had something on it previously) before it flashes the new installation onto it.

6

The installation process will then run the DISM program (Deployment Image Servicing and Management tool) to flash the installation files onto your SD card.

7

Once this is complete, the command window will close, and IoT Core Dashboard will state that your SD card is ready to be placed in the Raspberry Pi and started up.

8

Eject the SD card from your PC and place it in your Raspberry Pi. Connect an HDMI cable to a display source (monitor) and then plug in the power to start the device. If you didn’t choose to use a Wi-Fi network connection on startup you will need to plug in an Ethernet cable if you want Internet access.

IMG_0246

IMG_0243

You will first see the Windows logo with a spinner when the device is first powered on:

IMG_0236

IMG_0237

Let the device power itself on, it usually takes a minute or two (and might reboot itself). Once you get to the following screen, plugin a USB mouse and make your language selection.

IMG_0238

Clicking Next will display a screen asking if you want to configure Cortana – I selected Maybe Later.

IMG_0241

Windows 10 IoT Core will then run the default application, which looks like this:

IMG_0242

Congratulations! You have now completed the installation process and you have a standard Windows 10 IoT Core installation on your Raspberry Pi! You are now ready to begin deploying your applications to this device!

Happy coding!