Modal Title

Tutorial: Connect and Configure Raspberry Pi as an Azure IoT Edge Device

Sep 21st, 2018 10:06am by
Featued image for: Tutorial: Connect and Configure Raspberry Pi as an Azure IoT Edge Device

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:

  1. Creating cloud resources in Azure IoT
  2. Configuring and connecting Raspberry Pi to Azure IoT
  3. Deploying modules to the edge devices

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!

Prerequisites

Step 1 – Configuring Azure IoT

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.


We will then create a resource group that acts as a logical boundary for all the resources that we create for this project.


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.


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.


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.

Step 2 – Configure Raspberry Pi

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.


Make sure Moby and the CLI are properly installed before going ahead with the next steps.


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.


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.


The output should be similar to the below screenshot:

You may want to explore the service further with the below command.


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.

Step 3: Deploying Modules

 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.


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.


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.

Group Created with Sketch.
TNS owner Insight Partners is an investor in: Docker.
THE NEW STACK UPDATE A newsletter digest of the week’s most important stories & analyses.