Monday, December 30, 2019

Step by Step Registering Raspberry Pi 4 as Azure IOT Edge Device


     I bought a Raspberry Pi 4 to use with Azure IOT. Here is the list of specifications of Raspberry Pi 4.   

  1. CPU – Broadcom BCM2711, Quad core Cortex-A72 (ARM v8) 64-bit SoC @ 1.5GHz
  2. RAM – 1GB, 2GB or 4GB LPDDR4-2400 SDRAM (depending on model)
  3. WiFI – 2.4 GHz and 5.0 GHz IEEE 802.11ac wireless, Bluetooth 5.0, BLE
  4. Ethernet – Gigabit
  5. USB – 2 USB 3.0 ports; 2 USB 2.0 ports
  6. GPIO header – Raspberry Pi standard 40 pin
  7. HDMI – 2 × micro-HDMI ports (up to 4kp60 supported)
  8. Display port – 2-lane MIPI DSI
  9. Camera port – 2-lane MIPI CSI
  10. Audio – 4-pole stereo audio and composite video port
  11. Storage – Micro-SD card slot for loading operating system and data storage
  12. Misc – H.265 (4kp60 decode), H264 (1080p60 decode, 1080p30 encode), OpenGL ES 3.0 graphics
  13. OS – Debian Linux 10 based
     You can buy all type of sensors and connect them to Raspberry Pi. Then you can use Python or .NET Core to write small applications to check your connected sensors and read data from the sensors. If you like to push this data to store or analyze in Azure, then you need to make Raspberry Pi ready by installing couple of applications.

      Installing an application in Windows, is not a big deal for me. I had to install and configure all the applications in Linux in this project. First thing we need to do is copying some files to register Microsoft GPG key and software repository feed. To do that, we will use the curl command. Curl is used for transferring data using various protocols including HTTP/S. We are going to use it to copy some files from Internet to local storage. It's a fancy copy tool.

      I have Linux Debian Operation System in my Raspberry Pi so I had to use Raspbian Stretch option from the following list. If you use Ubuntu, you need to use different path. Here are the available options. All we are doing here is copying file named prod.list from Internet to local storage.

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

Ubuntu 18.04
curl https://packages.microsoft.com/config/ubuntu/18.04/multiarch/prod.list > ./microsoft-prod.list

Raspbian Stretch
curl https://packages.microsoft.com/config/debian/stretch/multiarch/prod.list > ./microsoft-prod.list


     When it's completed, we need to move this file to a different folder. To do that we need to use Linux command cp which is used for copying files. You might need special security access to create folders or run some of the commands. Linux is not going to ask you (like Windows) if you want to run this as admin. You need to tell Linux that you want to run the command as admin. To do that, you need to use command named sudo before the any commands. Sudo stands for "superuser do". In Windows, you do that by right clicking on an application and click on Run As Admin. Following command copies the file we just downloaded to the folder /etc/apt/sources.list.d/

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

     Next, we need to install Microsoft GPG key. This is for encryption purposes. We are going to use curl to copy the key and install it to Raspberry Pi. Then we will use cp command to copy this key to a different folder.

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


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

    So far, we have downloaded some files from internet and copy/install them into special folders. Next, we need to install container runtime. Azure IOT uses containers to push applications into IOT devices. You can create your application and put it into a container then you can push this container to all your IOT devices. This way you can push the same container to multiple IOT devices very easily. If you will need to upgrade your application later, you can push upgrades to all your IOT devices easily.

    To do that, we need to use command apt-get apt stands for Application Package Tool. It handles the installation and removal software in Linux Operation systems. I am going to use sudo command front of apt-get too. Following commands will install Moby Engine container runtime engine and Moby command-line interface.

sudo apt-get update

sudo apt-get install moby-engine



sudo apt-get install moby-cli

We are ready to install Edge-IOT
sudo apt-get update
sudo apt-get install iotedge


     When I saw this first time, I felt I have been driving long time and just see my destination’s welcome sign. We are almost done with this ride, I promise! Next, we need to change connection string in config file. All you need to do is, edit the configuration file and add the connection string into configuration file. To do that, we need to use a text editor named nano. You want to run it with sudo since you might need super user rights to save your changes to configuration file.

sudo nano /etc/iotedge/config.yaml


     You need the connection string for this device. In this point, you should have an Azure IOT Hub and register an Edge IOT device to see the connection string information. Click on eye icon to see any values from the following page. I copied the Primary Connection String and paste it to config file.


    Config file should look like the following image. All you need to do is, paste the connection string and save the file by pressing Ctrl + O. After that, you can press Ctrl + X to exit from Nano.

# Manual provisioning configuration 
provisioning: 
 source: "manual" 
 device_connection_string: "Paste Connection String here
# DPS TPM provisioning configuration 
# provisioning: 
# source: "dps" 
# global_endpoint: "https://global.azure-devices-provisioning.net" 
# scope_id: "{scope_id}" 
# attestation: 
# method: "tpm" 
# registration_id: "{registration_id}"

    We are ready to start iotedge after changing the connection string. Use the following command to restart iotedge.
sudo systemctl restart iotedge

     You can use the following command to check if your IotEdge environment can connect to Azure IOT Hub and Container system.
sudo iotedge check

    If all is working, you should be able to see that your device connects to Azure IOT Hub.


No comments:

Post a Comment