Skip to content

Sample outlining how Azure IoT Edge can be deployed, run and managed on a Raspberry Pi 4B with Sense HAT.

License

Notifications You must be signed in to change notification settings

fawohlsc/azure-iot-edge-raspberry-pi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Azure IoT Edge on Raspberry Pi

Overview

Sample outlining how Azure IoT Edge can be deployed, run and managed on a Raspberry Pi 4B with Sense HAT:

Overview

  • 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.

Prerequisities

Development Machine

  1. Open bash on your development machine e.g., by using Windows Subsystem for Linux.

  2. Verify whether Docker is installed, if not follow this guide for installation:

docker version # Version should be '20.10.12' or newer.
  1. Verify whether SSH is installed:
ssh -V
  1. Verify whether Azure CLI is installed, if not follow this guide for installation:
az version # Version should be '2.33.0' or newer.
  1. Verify whether Git is installed, if not follow this guide for installation:
git version # Version should be '2.25.1' or newer.
  1. Clone the GitHub repository by using the VSCode terminal:
git clone https://github.com/fawohlsc/azure-iot-edge-raspberry-pi.git
  1. Verify whether Visual Studio Code (VSCode) is installed, if not follow this guide for installation:
code -v
  1. Install the VSCode extension Azure IoT Tools:
code --install-extension "vsciot-vscode.azure-iot-tools" --force
  1. Open the repository in VSCode:
code ./azure-iot-edge-raspberry-pi

Raspberry Pi

  1. 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)'.
  1. Setup Sense HAT on your Raspberry Pi following this guide.

  2. Enable SSH on your Raspberry Pi:

sudo raspi-config # Navigate to 'Interfacing Options', 'SSH', and confirm with 'Yes'
  1. Determine the IP Address of the Raspberry Pi:
ifconfig
  1. 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}"
  1. Close the SSH connection to Raspberry Pi:
exit

Setup

Azure

  1. Open a terminal with bash in VSCode.

  2. Login to Azure:

az login
  1. Deploy to Azure:
chmod u+x ./azure/deploy.sh; ./azure/deploy.sh
  1. In VSCode, select your newly created IoT Hub:

Select IoT Hub

Raspberry Pi

  1. 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)
  1. 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)
  1. 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
  1. 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.

IoT Edge

  1. 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)
  1. Login to the Container Registry:
az acr login --name $acr_name
  1. In VSCode, right click on ./iot-edge/deployment.template.json to select 'Build and Push IoT Edge Solution'.

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
                }
            }
        }
    }
}
...
  1. Since a new tab 'Azure IoT Edge' in the terminal will be opened, switch to the previously used terminal tab 'bash'.

  2. 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).

  1. In VSCode, right click on ./iot-edge/deployment.template.json to select 'Create Deployment for Single Device' and your IoT Edge Device:

Create Deployment for Single Device

  1. After the deployment, login to the Raspberry Pi using SSH:
ssh "${raspberry_pi_user}@${raspberry_pi_ip_address}"
  1. List the IoT Edge Modules:
iotedge list
  1. View the logs of the IoT Edge Runtime:
iotedge logs edgeAgent

iotedge logs edgeHub
  1. The logs of our custom IoT Edge Module should contain telemetry readings:
iotedge logs sensehat

Sense HAT Logs

  1. Close the SSH connection to Raspberry Pi:
exit
  1. Monitor the telemetry arriving at the IoT Hub:
az iot hub monitor-events --hub-name $iot_hub_name
  1. Exit monitoring by pressing Ctrl + C.

Power BI

  1. Follow this guide to expose the real-time telemetry as Power BI Report:

Power BI Data Flow

  1. Create line diagrams to visualize temperature, humidity and pressure over time:

Power BI Report

  1. 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!

Clean-up

Raspberry Pi

  1. Login to the Raspberry Pi using SSH:
ssh "${raspberry_pi_user}@${raspberry_pi_ip_address}"
  1. Delete the IoT Edge Runtime:
sudo apt-get purge aziot-edge --yes
sudo apt-get purge aziot-identity-service --yes
  1. Remove all running container:
sudo docker rm -f $(sudo docker ps --all --quiet) >/dev/null 2>&1 || true
  1. Delete all unused container images:
sudo docker image prune --all --force
  1. Delete the Moby Engine:
sudo apt-get purge moby-engine --yes
  1. Close the SSH connection to Raspberry Pi:
exit

Azure

  1. Delete the resource group:
az group delete --name $resource_group_name --yes --no-wait

Power BI

  1. Delete the previously created Power BI Report.

Going further

  1. Debugging of IoT Edge Modules
  2. Deploy IoT Edge Modules at scale
  3. Remote monitoring for IoT Edge Devices
  4. Storing data on IoT Edge Devices using SQL Edge and Blob Storage
  5. Analyzing data on IoT Edge Devices
  6. Running machine learning on IoT Edge Devices
  7. Leveraging IoT Edge as a gateway to connect IoT devices with IoT Hub
  8. Reacting to IoT Hub events by using Event Grid to trigger actions

About

Sample outlining how Azure IoT Edge can be deployed, run and managed on a Raspberry Pi 4B with Sense HAT.

Resources

License

Stars

Watchers

Forks