Skip to content

Instantly share code, notes, and snippets.

@DevOpsKev
Forked from nihil0/rpi4-iot-edge.md
Created May 20, 2022 13:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DevOpsKev/f06b0fb28cc48e8d4b8205ec1a498b6b to your computer and use it in GitHub Desktop.
Save DevOpsKev/f06b0fb28cc48e8d4b8205ec1a498b6b to your computer and use it in GitHub Desktop.
What I did to get Azure IoT Edge installed on my Raspberry Pi 4

How to install Azure IoT Edge on a stock RaspberryPi 4

This weekend I decided to jump into the world of IoT by getting myself a thing, connecting it to the internet and hopefully make it do something useful with Microsoft's IoT Edge service. I decided to write everything down so I remember how to do this again when I inevitably brick my Rpi.

What I bought

  1. Raspberry Pi 4 Model B with 2GB of RAM
  2. Kingston 32GB MicroSD card, comes with handy microSD to SD adaptor
  3. Official Raspberry Pi USB-C Power Supply

Installing an OS

I chose to install Raspbian, a Debian-based OS optimized for the Raspberry Pi, I also opted for the lite version. Being too 133t to bother with a GUI, I decided to go for a headless install and SSH into my RPi from one of my laptops. Here is what I did to get there:

  1. Install balenaEtcher on your laptop
  2. Download the image zip archive
  3. Insert microSD card into laptop and follow the instructions in balenaEtcher to flash the Raspbian image on the card
  4. Insert microSD card into its slot on the Rpi4 and connect the power supply. The green LED should flash itermittently for several seconds. If it flashes once or twice, it means that something went wrong during the boot.
  5. Connect the Rpi4 to your network switch using an ethernet cable, ensure your laptop is on the name network as well. My router provides a nice view of all the devices in my network. You should see your device there if everything went okay.

  1. Now you should be able to do ssh pi@192.168.x.x and log in with the password: raspberry

Issue 1: Slow SSH response

I noticed that the latency was unacceptably bad when typing commands into the SSH terminal on my Pi. A few keystrokes in, the terminal would suddenly freeze for several seconds. I was using GitBash as my SSH client on Windows. Simply switching to my MacBook resolved this issue.

Issue 2: Slow network speeds

Despite being directly plugged into my 100Mbps router, I couldn't get a download speed of more than 12-13 kB/s when running apt-get update. After many hours of troubleshooting I suspect that either my device possibly has a faulty ethernet module. Luckily, the Raspberry Pi comes with a WiFi trasceiver, so I decided to use my WiFi instead. To connect to a wifi network through the command line, these are the steps (from here):

  1. sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
  2. Append the follwing lines to the file and save it:
network={
 ssid="<your-ssid>"
 psk="<your-password>"
}
  1. reboot the device with sudo reboot and immediately remove the ethernet cable. If everything went okay, you should see a new wifi device connected to your home network.

Now, I was able to see a speed of 1890 kB/s. Victory!

Installing Azure IoT Edge

For this, I mostly followed the instructions here.

In Azure

Install the Azure CLI and Azure CLI IoT extension on your laptop

  1. Create a resource group and an iot hub using the command az iot hub create --name <hub-name> -g <resource-group>
  2. Create a device identity az iot hub device-identity create -n <hub-name> -d <device-name> --edge-enabled. Note that the --edge-enabled switch is critical for your device to use IoT Edge.
  3. Note down the connection string returned by az iot hub device-identity show-connection-string -d <device> -n <hub-name>

On the RaspberryPi

  1. Install docker-ce: curl -sSL get.docker.com | sh && sudo usermod pi -aG docker && sudo reboot
  2. Install the correct version of libssl: sudo apt-get install libssl1.0.2
  3. Install IotEdge:
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/ && \
sudo apt-get update && sudo apt-get -y install iotedge
  1. In /etc/iotedge/config.yaml on the Raspberry Pi, paste the connection string where indicated:
# Manual provisioning configuration
provisioning:
  source: "manual"
  device_connection_string: "<ADD DEVICE CONNECTION STRING HERE>"
  1. Restart IoT Edge: sudo systemctl restart iotedge

  2. Now, check the status of the service: sudo systemctl status iotedge. You should see a bunch of log messages where the iotedge service is talking to the runtime to see what modules are running.

  3. docker ps should show that the IotEdge Agent is running:

CONTAINER ID        IMAGE                                      COMMAND                   CREATED             STATUS              PORTS               NAMES
3649138d999b        mcr.microsoft.com/azureiotedge-agent:1.0   "/bin/sh -c 'echo \"$…"   10 minutes ago      Up 10 minutes                           edgeAgent

Next steps

At this point you are able to run IoT Edge modules on the Pi. A good starting point is to deploy the simulated tempterature sensor. Instructions can be found here. If you're feeling more adventurous, you can learn how to deploy a Custom Vision model to differentiate between an apple and a banana!

APPLE-BANANA

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment