Azure IoT is a powerful platform that enables organizations to connect and manage a wide range of devices and sensors in real-time. In this article, we will explore the integration of Azure IoT with Brainy Pi, a popular single-board computer, to create an end-to-end IoT solution. We will cover the creation of an Azure IoT Hub, the provisioning of a Brainy Pi device to the IoT Hub, and the sending of data from the device to the cloud. By the end of this article, you will have a solid understanding of the key components required to build an Azure IoT solution with Brainy Pi. So, let’s dive in and explore the exciting world of Azure IoT!
Index
Step 1 : Server Side Setup
Create a Custom app
Step 2 : Device Side Setup
Compiling Azure SDK for edge
Provisioning device with keys from Cloud
Step 3 : Send data from Edge device to Cloud
Step 1 : Server Side Setup
1.1 Overview
Login to Azure IOT Central
Build an App
Note the credentials for device provisioning
Create a Device Template.
Create a Dashboard
1.2 Steps
Visit the link – https://apps.azureiotcentral.com/home & Login using your azure account.
Go to Build App and create a custom app.
Fill in the details of the app and click on create
Your app should be ready.
Create a Device Template.
Device template is where you specify the device properties, telemetry (metrics) and commands (RPC).
To create device template, Go to Device template –> Create Device Template –> Choose Azure IoT Edge , enter the name of the device template and create
Here add in all the Device properties, Telemetry (metrics) or Commands.
Telemetry (Device to Cloud) is a instantaneous data point of one or multiple measurements. i.e
It changes over time.
And previous values are lost and cannot be retaken.
for example: Value of current consumed by the laptop/mobile.
Property (Device to Cloud and Device to Cloud) is a data point, but with its own storage on the device. i.e
It may change over time, but not constantly.
And previous value is not lost but can be accessed. for example: MAC address of the laptop/mobile.
Command (Cloud to Device) it just runs the given function when triggered from the cloud
Once done press save and then “Publish”
Step 2 : Device Side Setup
2.1 Compiling Azure SDK for edge
Compile the azure sdk
git clone https://github.com/Azure/azure-iot-sdk-c.git sudo apt-get update sudo apt-get install -y git cmake build-essential curl libcurl4-openssl-dev libssl-dev uuid-dev ca-certificates cd azure-iot-sdk-c/ git submodule update --init mkdir cmake cd cmake cmake .. cmake --build . -j6
2.2 Provisioning device with keys from cloud
Lets first run the sample program
iothub_client/samples/pnp/pnp_temperature_controller
We will use Automatic Device provisioning to provision the device while running the sample.
To do Automatic Device provisioning we need 2 things from the Azure Server
ID scope
Primary Key
Navigate to Permissions –> Device Connection Groups, Copy the ID scope . Then create a new group.
Fill in the details, ensure that you choose the “IOT Edge devices” option and Choose the “SAS” option and create the group.
Once the group is created then note down the Primary Key, If primary key is not visible, then turn off the “Auto Generate keys” slider.
Now we need to convert the Primary key to Device Key, this is done using azure-cli command
# Install azure-cli curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash # Login to azure account az login # Generate device key az iot central device compute-device-key --primary-key <primary-key> --device-id <device-id>
For example:
az iot central device compute-device-key --primary-key AgMreQbsPIh/pOqOCGzT2XW0NeDMLzRwYX5aZMLeAQuV4lYCrti06QITjErsS5WC0H+vU6dUg== --device-id 16c14f2d26fe
In this case the device id is a MAC address of the device.
To run the sample program, run these steps
export IOTHUB_DEVICE_SECURITY_TYPE="DPS" export IOTHUB_DEVICE_DPS_ID_SCOPE="0ne0093714" export IOTHUB_DEVICE_DPS_DEVICE_ID=16c14f2d26fe export IOTHUB_DEVICE_DPS_DEVICE_KEY="OF77OWzH9ZCy8EDFndnU02WB7n7ABEdUdILJY=" ./iothub_client/samples/pnp/pnp_temperature_controller/pnp_temperature_controller
Recheck and ensure that the ID Scope, Device ID and Device Key are correct else the provisioning will not work.
Once run program will start running and will show RAW values on the Azure cloud.
Note: Since we have not changed the sample program the values will not be shown to the dashboard.
Step 3 : Send data from Edge device to Cloud
3.1 Downloading the code
Here, we start by preparing development environment
git clone https://gitlab.iotiot.in/shunya/products/shunya-interfaces/azure-iot-c-sdk.git cd azure-iot-sdk-c git submodule update --init mkdir cmake cd cmake
When the sample program located inside the repository is run. It uses DPS to provison the device on the azure platform.
For example, in the file
pnp_temperature_controller.c
, the main function callsCreateDeviceClientAndAllocateComponents
, whichSet the
dtmi:com:example:Thermostat;1
model ID. IoT Central uses the model ID to identify or generate the device template for this device.Use DPS to provision and register the device.
Create a device client handle, and connect to your IoT Central application.
Run the following to build SDK and Samples
cmake -Duse_prov_client=ON -Dhsm_type_symm_key=ON -Drun_e2e_tests=OFF ..
cmake --build .
3.2 Run the example
To run the sample
cd iothub_client/samples/pnp/pnp_temperature_controller/
./pnp_temperature_controller
The output should be similar to this
The device is sending telemetry data to azure iot hub and now we can go to the azure portal and see the data being received.
On expanding the telemetry tab, we can see the temperature readings being sent by the device.
In order to stop the application, just press
ctrl+c
.