Brainy Pi

Available to select audience (currently beta)

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

  1. Login to Azure IOT Central
  2. Build an App
  3. Note the credentials for device provisioning
  4. Create a Device Template.
  5. Create a Dashboard

1.2 Steps

  1. Visit the link – https://apps.azureiotcentral.com/home & Login using your azure account.
  2. Go to Build App and create a custom app.

    Screenshot_20230125_134102

  3. Fill in the details of the app and click on create

    Screenshot_20230125_134135

    Screenshot_20230125_134143

  4. Your app should be ready.
  5. Create a Device Template.
    1. Device template is where you specify the device properties, telemetry (metrics) and commands (RPC).
    2. To create device template, Go to Device template –> Create Device Template –> Choose Azure IoT Edge , enter the name of the device template and create

      Screenshot_20230125_222935

      Screenshot_20230125_222940

      Screenshot_20230125_222946

      Screenshot_20230125_223025

    3. Here add in all the Device properties, Telemetry (metrics) or Commands.
      1. 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.
      2. 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.
      3. Command (Cloud to Device) it just runs the given function when triggered from the cloud

      Screenshot_20230125_233320

      Screenshot_20230125_223120

    4. Once done press save and then “Publish”

      Screenshot_20230125_233330

Step 2 : Device Side Setup

2.1 Compiling Azure SDK for edge

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

  1. Lets first run the sample program iothub_client/samples/pnp/pnp_temperature_controller
  2. We will use Automatic Device provisioning to provision the device while running the sample.
  3. To do Automatic Device provisioning we need 2 things from the Azure Server
    1. ID scope
    2. Primary Key
  4. Navigate to Permissions –> Device Connection Groups, Copy the ID scope . Then create a new group.

    Screenshot_20230125_223157

  5. Fill in the details, ensure that you choose the “IOT Edge devices” option and Choose the “SAS” option and create the group.

    Screenshot_20230125_223220

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

    Screenshot_20230125_223250

  7. 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.
  8. 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.
  9. Once run program will start running and will show RAW values on the Azure cloud.

    Screenshot_20230126_083932

    Screenshot_20230126_083950

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

  1. 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
  1. When the sample program located inside the repository is run. It uses DPS to provison the device on the azure platform.
  2. For example, in the file pnp_temperature_controller.c, the main function calls CreateDeviceClientAndAllocateComponents, which
    • Set 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
  1. The output should be similar to this Screenshot_2023-02-18_115857
  2. 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. Screenshot_2023-02-18_115930
  3. On expanding the telemetry tab, we can see the temperature readings being sent by the device. Screenshot_2023-02-18_120859
  4. In order to stop the application, just press ctrl+c.

Conclusion

In conclusion, the integration of Azure IoT with Brainy Pi provides an easy-to-use platform for building IoT solutions. By leveraging Azure IoT’s robust capabilities and Brainy Pi’s versatile hardware, organizations can quickly create an end-to-end IoT solution that can gather and process data from devices and send it to the cloud for analysis. With the help of Azure IoT, businesses can gain insights into their operations that were previously impossible, and make data-driven decisions that can drive growth and profitability. The possibilities of what can be achieved with Azure IoT and Brainy Pi are endless, and we are excited to see what the future holds for IoT.
0 Comments

Leave a reply

Your email address will not be published. Required fields are marked *

*