Human face created like digital circuitry surrounded by digits

Setting up a Pi 4 as an Azure Edge Device

Please follow and share!
Twitter
Facebook
LinkedIn
RSS

Preamble: I started writing this blog post based on a Twitch stream that I had back in March 2020. A lot has happened since then as we all know. That being said, I am so happy to finally get back to finishing this one out!

I have been chomping at the bit to get myself a Pi 4 device. Of course I went with the 4 GB version. I landed on buying a Vilros starter kit on Amazon. I like that you can get (pretty much) everything you need in a single box.

Vilros Pi 4 Kit
Vilros Pi 4 Kit

The first thing I wanted to do is to make this thing into an Azure Edge device, so that I can easily deploy container-based applications remotely from IoT Hub and to act as a device gateway in my home (among other things).

Imaging the Operating System

I decided to use Raspbian Buster as the OS even though it is considered a tier 2 operating system. You can obtain the Raspbian Buster image from the Raspberry Pi website. I am running Windows, so I downloaded the Zip file for Raspbian Buster with desktop item. Please note, that for some reason, I needed to download this at least twice, because the file was corrupting somehow in transit [this was not an issue in October 2020]. Also, using 7-Zip is recommended as the unzip utility over the built-in Windows zip file extractor.

Raspbian Buster with desktop download.
Raspbian Buster with desktop download.

With the zip file extracted, I inserted the micro-SD card into my card reader and flashed the operating system image on it using the Balena Etcher tool.

Balena Etcher Tool
Balena Etcher Tool

While the OS is being imaged, cancel out of any Windows drive popups that surface.

Configuring Raspbian Buster

Insert the micro-sd card into the Raspberry Pi. Then connect a screen, mouse, keyboard, and network cable. Power on the device and wait for it to boot up.

On the welcome screen, be sure to note the IP address of the device – and continue setting up the Pi using your desired settings. It is equally important to remember the password you enter for the pi user.

Raspberry Pi Buster Welcome Screen
Raspberry Pi Buster Welcome Screen

Ensure that you allow for the Operating System to check for and install any necessary updates.

Next, we’ll enable SSH on the device. This is optional – but I find it much easier to work on my large monitors vs. the small screen that I have on my Pi. From the Pi menu, select Preferences, then Raspberry Pi Configuration.

Raspberry Pi Configuration Menu
Raspberry Pi Configuration Menu

Select the Interfaces tab, then ensure you enable SSH, and select OK. You may also enable all interfaces that you require for your project at this time. I’ve also enabled Camera, SPI, I2C, Serial Port, 1-Wire and Remote GPIO [mainly because I’m not sure all of what I want to do with it yet 😉 ]. Reboot the device so that the new settings take effect.

Raspberry Pi Configuration Screen
Raspberry Pi Configuration Screen

Install Azure IoT Edge

Next, you can either open the Terminal directly on your pi device, or SSH into it from the machine of your choice.

Raspberry Pi Terminal Icon
Raspberry Pi Terminal Icon

For SSH, enter the command ssh pi@ipaddressofpi and when prompted, type yes to continue connecting.

SSH Window

First thing we need to do is install an earlier version of OpenSSL that is compatible with Azure IoT Edge. Install this package by executing the following:

sudo apt-get install libssl1.0.2

We’ll need to register the Microsoft Key and software repository feed in order to obtain the software necessary to run Azure IoT Edge. Execute the following command to retrieve the list of products from the feed, and copy the downloaded list to the apt sources on the device. The last two commands in the series will install the Microsoft GPG public key as a trusted source.

curl https://packages.microsoft.com/config/debian/stretch/multiarch/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/

Now that we’ve added (and trusted) the Microsoft package feed. Let’s update our device’s package list.

sudo apt-get update

Next, we’ll install the Moby engine and its associated CLI so that we can deploy, control, and run our Docker containers. Moby and the CLI are available as a single package in Microsoft package feed. Install this package by executing the following two commands:

sudo apt-get install moby-engine

Now for the moment we’ve all been waiting for! Let’s install the most recent iotedge package.

sudo apt-get install iotedge

Woohoo! We’ve made it! We’ve installed Azure IoT Edge on the Pi. We now need to give it a home. Keep your terminal window open while we switch gears to provisioning the device to an IoT Hub !

SSH Window showing installation of the iotedge package
Installing the iotedge package

Provision the Edge Device with an IoT Hub

Log in to the Azure Portal and choose to + Create a resource. Search for IoT Hub and create the resource – choose either the Free tier or Standard tier.

Provisioning the IoT Hub

Once provisioned. Open the IoT Hub resource you just created, and select IoT Edge from the left menu (beneath Automatic Device Management).

On the IoT Edge screen, select + Add an IoT Edge device from the top toolbar. Give the device a name in the Device ID field. For authentication, we’ll just keep it simple and use a Symmetric key. Press the Save button to complete the device creation step.

Creating an IoT Edge Device
Creating an IoT Edge Device

The device is now listed on the IoT Edge screen. Select the device you just created from the table.

IoT Edge Device List
IoT Edge Device List

On the device screen, copy the Primary Connection String value, and paste it into a text editor. This is the connection string we will use to configure our IoT Edge module on the Pi.

IoT Edge Device Primary Connection String
IoT Edge Device Primary Connection String

While we’re still on the device screen, select the Set Modules item from the top toolbar. It makes sense to have some type of module installed that will send telemetry from our device up to the IoT Hub to verify connectivity.

In the IoT Edge Modules section of the Set Modules screen, expand the + Add toolbar item, and select the + Marketplace Module item.

Add Marketplace Module
Add Marketplace Module

In the search box for the marketplace, search for Simulated, and select to install the Simulated Temperature Sensor IoT Edge module.

Marketplace Search
Marketplace Search

Select the Review + create button to validate. Once validation has succeeded, select the Create button to persist the configuration.

Return to the terminal window for the final piece of the puzzle. We need to configure our Pi with the device connection string that we obtained from the IoT Hub.

Execute the following command to edit the configuration of our IoT Edge module:

sudo nano /etc/iotedge/config.yaml

Use your arrow keys to navigate down the document until you see the <ADD DEVICE CONNECTION STRING HERE> text. Replace that text with the connection string you copied for your device in the IoT Hub earlier. (CTRL+X to exit, choose y to save and enter to confirm the same file name).

Configuring the IoT Edge Module with the Device Connection String
Configuring the IoT Edge Module with the Device Connection String

Restart the IoT Edge module by executing the following command:

sudo systemctl restart iotedge

Give the module a couple of minutes to restart, and view the logs of the IoT Edge Daemon by executing the following (when finished use CTRL+C to exit the command):

systemctl status iotedge
Edge runtime is running
Edge runtime is running

Verify the success of the remote deployment of the SimulatedTemperatureSensor module onto the device by executing the following command:

sudo iotedge list
Simulated Temperature Module on the Device
Simulated Temperature Module on the Device

You can view the output of the telemetry being sent by the SimulatedTemperatureSensor module by executing the following command:

sudo iotedge logs -f SimulatedTemperatureSensor
Simulated Device Telemetry
Simulated Device Telemetry

Returning to the Azure Portal, refresh your Edge Device screen and you should see the IoT Edge runtime responding successfully. The SimulatedTemperatureSensor module should also have reported in from the device to the IoT Hub as well.

In the Azure Portal, you can view statistics of incoming messages into your IoT Hub resource on its Overview screen.

IoT Hub Usage
IoT Hub Usage

You can monitor incoming telemetry at the IoT Hub level by opening an instance of the Azure cloud shell, and run the following Azure CLI commands:

az extension add --name azure-iot

az iot hub monitor-events --device-id {YourDeviceId} --hub-name {YourIoTHubName}
Incoming Telemetry Cloud Shell
Incoming Telemetry Cloud Shell

Conclusion

Welcome to the beginning of your IoT Edge journey! I hope this blog post helps you get started !

Please follow and share!
Twitter
Facebook
LinkedIn
RSS

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.