Sample outlining how Azure IoT Edge can be deployed, run and managed on a Raspberry Pi 4B with Sense HAT:
- Raspberry Pi - Raspberry Pi configured as an IoT Edge Device, which is deployed at an edge site (e.g., home or factory building).
- Sense HAT - Sense HAT is an add-on board for Raspberry Pi which we will use to collect telemetry i.e., temperature, humidity and pressure. Alternatively, the Sense HAT emulator can be used.
- IoT Edge Module - Containerized custom code which reads telemetry from Sense HAT and sends it via the IoT Edge Runtime to IoT Hub.
- IoT Edge Runtime - The IoT Edge Runtime is a collection of components that turn a device into an IoT Edge Device. Collectively, the components enable IoT Edge Devices to receive code to run at the edge and communicate the results.
- Cloud - We will use Azure to manage our IoT Edge Devices and Modules. Also, we will feed a Power BI report via Stream Analytics Jobs to visualize the telemetry in real-time.
- Container Registry - IoT Edge Modules are stored in the Container Registry. The modules can be custom code or installed from the IoT Edge Module Marketplace.
- IoT Hub - IoT Hub enable highly secure and reliable communication between your IoT application and the devices it manages. Azure IoT Hub provides a cloud-hosted solution back end to connect virtually any device. Extend your solution from the cloud to the edge with per-device authentication, built-in device management, and scaled provisioning.
- Stream Analytics Jobs - The Stream Analytics Job runs real-time analytic computations on the telemetry from your devices.
- Power BI - Power BI will be used to visualize the telemetry coming from your device.
-
Open bash on your development machine e.g., by using Windows Subsystem for Linux.
-
Verify whether Docker is installed, if not follow this guide for installation:
docker version # Version should be '20.10.12' or newer.
- Verify whether SSH is installed:
ssh -V
- Verify whether Azure CLI is installed, if not follow this guide for installation:
az version # Version should be '2.33.0' or newer.
- Verify whether Git is installed, if not follow this guide for installation:
git version # Version should be '2.25.1' or newer.
- Clone the GitHub repository by using the VSCode terminal:
git clone https://github.com/fawohlsc/azure-iot-edge-raspberry-pi.git
- Verify whether Visual Studio Code (VSCode) is installed, if not follow this guide for installation:
code -v
- Install the VSCode extension Azure IoT Tools:
code --install-extension "vsciot-vscode.azure-iot-tools" --force
- Open the repository in VSCode:
code ./azure-iot-edge-raspberry-pi
- Setup your Raspberry Pi 4B following this guide. This sample targets Raspberry Pi OS Bullseye in 64-bit, which itself is based on Debian GNU/Linux 11 (bullseye) . Verify the version of Raspberry Pi OS:
cat /etc/os-release # Pretty name should be 'Debian GNU/Linux 11 (bullseye)'.
-
Setup Sense HAT on your Raspberry Pi following this guide.
-
Enable SSH on your Raspberry Pi:
sudo raspi-config # Navigate to 'Interfacing Options', 'SSH', and confirm with 'Yes'
- Determine the IP Address of the Raspberry Pi:
ifconfig
- Open bash on your development machine to login to the Raspberry Pi using SSH:
raspberry_pi_user="<REPLACE_RASPBERRY_USER_NAME>" # e.g., pi
raspberry_pi_ip_address="<REPLACE_RASPBERRY_PI_IP_ADDRESS>" # e.g., 192.168.0.123
ssh "${raspberry_pi_user}@${raspberry_pi_ip_address}"
- Close the SSH connection to Raspberry Pi:
exit
-
Open a terminal with bash in VSCode.
-
Login to Azure:
az login
- Deploy to Azure:
chmod u+x ./azure/deploy.sh; ./azure/deploy.sh
- In VSCode, select your newly created IoT Hub:
- Determine the IoT Hub name:
resource_group_name="azure-iot-edge-raspberry-pi"
iot_hub_name=$(az iot hub list --resource-group $resource_group_name --query "[0].name" --output tsv)
- Determine the IoT Edge Device connection string:
iot_edge_device_id="raspberry-pi"
iot_edge_device_connection_string=$(az iot hub device-identity connection-string show --hub-name $iot_hub_name --device-id $iot_edge_device_id --output tsv)
- Setup IoT Edge on the Raspberry Pi:
raspberry_pi_user="<REPLACE_RASPBERRY_USER_NAME>" # e.g., pi
raspberry_pi_ip_address="<REPLACE_RASPBERRY_PI_IP_ADDRESS>" # e.g., 192.168.0.123
ssh "${raspberry_pi_user}@${raspberry_pi_ip_address}" "bash -s '${iot_edge_device_connection_string}'" < ./raspberry-pi/setup.sh
- Wait a few minutes until the connection between the IoT Edge Device and IoT Hub is established (
ConnectionState: Connected
):
az iot hub device-identity list --hub-name $iot_hub_name --query "[].{DeviceId:deviceId,ConnectionState:connectionState}" --output table
See this guide for troubleshooting connectivity issues.
- In your already opened terminal, determine the Container Registry name:
acr_name=$(az acr list --resource-group $resource_group_name --query "[0].name" --output tsv)
- Login to the Container Registry:
az acr login --name $acr_name
- In VSCode, right click on ./iot-edge/deployment.template.json to select 'Build and Push IoT Edge Solution'.
Since the IoT Edge Module requires access Sense HAT, it runs as priviledged container (See: ./iot-edge/deployment.template.json):
...
"modules": {
"sensehat": {
"version": "1.0",
"type": "docker",
"status": "running",
"restartPolicy": "always",
"settings": {
"image": "${MODULES.sensehat}",
"createOptions": {
"HostConfig": {
"Privileged": true
}
}
}
}
}
...
-
Since a new tab 'Azure IoT Edge' in the terminal will be opened, switch to the previously used terminal tab 'bash'.
-
Wait for the previous operation to complete and verify the IoT Edge Module having been pushed to the Container Registry:
acr_repository_name="sensehat"
az acr repository show-tags --name $acr_name --repository $acr_repository_name -o tsv # Output should be '1.0.0-arm64v8'.
Do not forget to increment the version in the module.json when you update the IoT Edge Module. Otherwise IoT Edge Runtime will not update the IoT Edge Module on the IoT Edge Device during deployment (See: GitHub Issue).
- In VSCode, right click on ./iot-edge/deployment.template.json to select 'Create Deployment for Single Device' and your IoT Edge Device:
- After the deployment, login to the Raspberry Pi using SSH:
ssh "${raspberry_pi_user}@${raspberry_pi_ip_address}"
- List the IoT Edge Modules:
iotedge list
- View the logs of the IoT Edge Runtime:
iotedge logs edgeAgent
iotedge logs edgeHub
- The logs of our custom IoT Edge Module should contain telemetry readings:
iotedge logs sensehat
- Close the SSH connection to Raspberry Pi:
exit
- Monitor the telemetry arriving at the IoT Hub:
az iot hub monitor-events --hub-name $iot_hub_name
- Exit monitoring by pressing
Ctrl + C
.
- Follow this guide to expose the real-time telemetry as Power BI Report:
- Create line diagrams to visualize temperature, humidity and pressure over time:
- Congratulations! You have completed the setup. Your feedback and contributions are very much appreciated i.e., starring this repository, raising issues or opening pull requests. Many thanks upfront!
- Login to the Raspberry Pi using SSH:
ssh "${raspberry_pi_user}@${raspberry_pi_ip_address}"
- Delete the IoT Edge Runtime:
sudo apt-get purge aziot-edge --yes
sudo apt-get purge aziot-identity-service --yes
- Remove all running container:
sudo docker rm -f $(sudo docker ps --all --quiet) >/dev/null 2>&1 || true
- Delete all unused container images:
sudo docker image prune --all --force
- Delete the Moby Engine:
sudo apt-get purge moby-engine --yes
- Close the SSH connection to Raspberry Pi:
exit
- Delete the resource group:
az group delete --name $resource_group_name --yes --no-wait
- Delete the previously created Power BI Report.
- Debugging of IoT Edge Modules
- Deploy IoT Edge Modules at scale
- Remote monitoring for IoT Edge Devices
- Storing data on IoT Edge Devices using SQL Edge and Blob Storage
- Analyzing data on IoT Edge Devices
- Running machine learning on IoT Edge Devices
- Leveraging IoT Edge as a gateway to connect IoT devices with IoT Hub
- Reacting to IoT Hub events by using Event Grid to trigger actions