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.
- Raspberry Pi 4 Model B with 2GB of RAM
- Kingston 32GB MicroSD card, comes with handy microSD to SD adaptor
- Official Raspberry Pi USB-C Power Supply
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:
- Install balenaEtcher on your laptop
- Download the image zip archive
- Insert microSD card into laptop and follow the instructions in balenaEtcher to flash the Raspbian image on the card
- 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.
- 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.
- Now you should be able to do
ssh pi@192.168.x.x
and log in with the password:raspberry
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.
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):
sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
- Append the follwing lines to the file and save it:
network={
ssid="<your-ssid>"
psk="<your-password>"
}
- 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!
For this, I mostly followed the instructions here.
Install the Azure CLI and Azure CLI IoT extension on your laptop
- Create a resource group and an iot hub using the command
az iot hub create --name <hub-name> -g <resource-group>
- 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. - Note down the connection string returned by
az iot hub device-identity show-connection-string -d <device> -n <hub-name>
- Install docker-ce:
curl -sSL get.docker.com | sh && sudo usermod pi -aG docker && sudo reboot
- Install the correct version of libssl:
sudo apt-get install libssl1.0.2
- 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
- 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>"
-
Restart IoT Edge:
sudo systemctl restart iotedge
-
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. -
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
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!
Thank you for the tutorial dude