![]() |
VOOZH | about |
We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.
Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.
Follow TNS on your favorite social media networks.
Become a TNS follower on LinkedIn.
Check out the latest featured and trending stories while you wait for your first TNS newsletter.
Azure IoT Edge is an edge computing platform from Microsoft. It is an extension of Azure IoT that can run on local devices in offline mode.
In the last part of this series, I introduced the technical architecture of Azure IoT Edge. In this tutorial, we will explore how to connect and configure Raspberry Pi as an edge device that talks to Azure IoT.
We will control an LED matrix that comes with a Raspberry Pi accessory known as Sense Hat. The idea is to change the color of the LED matrix remotely by pushing different versions of modules packaged as Docker containers.
There are three broad steps involved in this tutorial:
We will use the CLI for the most parts of the tutorial while switching to Azure Portal for configuring and deploying modules.
Let’s get started!
On your development machine (Mac or Windows), start by configuring the Azure IoT CLI. This tool makes it easy to work with Azure IoT Hub.
$ az extension add --name azure-cli-iot-ext
We will then create a resource group that acts as a logical boundary for all the resources that we create for this project.
$ az group create --name TNSIoT --location westus
Let’s create an IoT Hub which is the gateway to Azure IoT. This service handles device management, security, and communication among the connected devices.
$ az iot hub create --resource-group TNSIoT --name TNSIoTHub --sku S1
Once we have an IoT Hub in place, we can start connecting devices. But before that we need to create an identity for the device which will provide the credentials to authenticate with IoT Hub.
Since we are configuring an edge device, we will go ahead and add –edge-enabled flag. It’s important to understand that edge devices are not different from other devices except that they enjoy additional capabilities.
The below command creates an identity for Raspberry Pi with a device id Pi1. The output will contain a device-specific connection string that we need to save. Keep the connection string safe and secure. It is required in the next step.
$ az iot hub device-identity create --hub-name TNSIoTHub --device-id Pi1 --edge-enabled
We are done with the first part where a logical device identity is created in the cloud. We will now associate an actual Raspberry Pi device with this identity.
Assuming you have a Raspberry Pi running the latest Raspbian OS connected to the same network as your development machine, SSH into it.
Since Azure IoT Edge modules are run as containers, we need to have Docker installed the edge device. In this case, we are setting up Moby, an open source toolchain that powers Docker Engine.
$ curl -L https://aka.ms/moby-engine-armhf-latest -o moby_engine.deb && sudo dpkg -i ./moby_engine.deb $ curl -L https://aka.ms/moby-cli-armhf-latest -o moby_cli.deb && sudo dpkg -i ./moby_cli.deb $ sudo apt-get install -f
Make sure Moby and the CLI are properly installed before going ahead with the next steps.
$ sudo docker version
The second component that we need to install on Raspberry Pi is Azure IoT Edge runtime, which runs as a background daemon.
Run the below commands to install it.
$ curl -L https://aka.ms/libiothsm-std-linux-armhf-latest -o libiothsm-std.deb && sudo dpkg -i ./libiothsm-std.deb $ curl -L https://aka.ms/iotedged-linux-armhf-latest -o iotedge.deb && sudo dpkg -i ./iotedge.deb $sudo apt-get install -f
With the runtime in place, we are all set to connect the device to Azure IoT Hub. This is done by adding the device connection string that we acquired in step 1 to the configuration file.
Open /etc/iotedge/config.yaml in an editor of your choice and replace “<ADD DEVICE CONNECTION STRING HERE>” with the actual connection string.
Since the configuration is changed, let’s restart the runtime, and verify the status.
$ sudo systemctl restart iotedge $ sudo systemctl status iotedge
The output should be similar to the below screenshot:
You may want to explore the service further with the below command.
$ sudo journalctl -u iotedge --no-pager --no-full
We have successfully connected Raspberry Pi to Azure IoT and configured it as an edge device. We are now ready to deploy the module that can control the LED matrix.
Open your favorite browser and navigate to Azure Portal. Access the IoT Hub we created in the first step, and click on IoT Edge under Automation Device Management section.
Under IoT Edge Devices section, you should see PI1 device. Click on that to set the modules.
Choose IoT Edge Module from the list to deploy a custom module.
We will now use the portal to push a container image as a module to the edge device. You can use the prebuilt images that I have already pushed to Docker Hub.
The image, janakiramm/matrix:v1, is built to turn all the LEDs of Sense Hat to blue while janakiramm/matrix:v2 is configured to turn the matrix to green.
Let’s first push the first version to turn the matrix to blue.
Under the IoT Edge Custom Modules section, give the name of the module as Matrix and janakiramm/matrix:v1 for Image URI. Since we access the local I2C bus on Raspberry Pi, we need to run the container in privileged mode. Paste the following JSON string under Container Create Options.
{
"HostConfig": {
"Privileged": true
}
}
Click on the Save button and complete all the steps by accepting the defaults.
This will trigger custom module deployment on the edge device. After a while, the modules section should look like the below screenshot.
You can also verify that the module is deployed to the device by running iotedge CLI on Raspberry Pi.
$ sudo iotedge list
If everything went well, you should see the Sense Hat LED matrix turning blue.
Now, let’s go ahead and update the deployment to run the version 2 of the module. Click on Matrix module to access its properties. Change the Image URI to janakiramm/matrix:v2.
Click on the Save button and complete all the steps by accepting the defaults.
Within a few seconds, the LED matrix turns green indicating that the module has been updated to the latest version.
Running iotedge CLI on the device will confirm the same.
$ sudo iotedge list
Though changing the color of the LEDs can be achieved without actually pushing a new container, the idea of the tutorial is to highlight the concept of custom modules and managing the deployments.
In the last and final part of this series, I will walk you through the steps involved in configuring Kubernetes to orchestrate Azure IoT Edge modules. We will do a blue/green deployment of modules through kubectl, the CLI of Kubernetes.