Publishing Data to Azure IOT Hub

by dhruvarora561 in Circuits > Computers

223 Views, 0 Favorites, 0 Comments

Publishing Data to Azure IOT Hub

IOT_0.png

For the uninitiated, Azure iot hub provides a cloud-hosted solution back end to connect virtually any device. This allows users to connect their sensors to devices like brainypi or raspberry pis and have them send the data to cloud for processing.

The iot hub can be integrated to create end-to-end solutions with other azure services.

Most importantly, you can get started with a free azure account and a BrainyPi.

Supplies

  1. Azure account
  2. BrainyPi or Raspberry PI
  3. Keyboard
  4. Mouse
  5. Internet connection

Server Side Setup

  1. Start by logging in to the azure iot central, here
  2. Click on build a custom app.
  3. Give your application a name.
  4. Click on create and then hop on over to your BrainyPi.

Device Setup

  1. We'll start by compiling the Azure SDK.
  2. First we need to download the SDK from GitHub.
  3. Then, we'll install some dependencies.
https://github.com/Azure/azure-iot-sdk-c
sudo apt-get update
sudo apt-get install -y git cmake build-essential curl libcurl4-openssl-dev libssl-dev uuid-dev ca-certificates

Compiling the SDK

Now, that we have downloaded the code, we now need to compile it for our target device(BrainyPI).

  1. We can do that by, runnning the following one by one
cd azure-iot-sdk-c/
git submodule update --init
mkdir cmake
cd cmake
cmake -Duse_prov_client=ON -Dhsm_type_symm_key=ON -Drun_e2e_tests=OFF ..
cmake --build .


Getting Connection Information

  • This will be needed to run the sample applications later on
  • D scope: In your IoT Central application, navigate to Permissions > Device connection groups. Make a note of the ID scope value.


  • Group primary key: In your IoT Central application, navigate to Permissions > Device connection groups > SAS-IoT-Devices. Make a note of the shared access signature Primary key value.


  • Use the Azure Cloud Shell to generate a device key from the group primary key you retrieved:
  • First we need to install azure shell on our system
curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
  • Login to the azure shell by using the account credentials
az login
  • Now, we are going to make use of the D scope and Group primary key which we noted down earlier.
az extension add --name azure-iot
az iot central device compute-device-key --device-id <deviceid> --pk <primarykey>
  • Above will generate a device key, note this down.

Running the Code

1.jpg
  • We are finally at a stage where we can run the sample program which will send data to the cloud.
  • Navigate to the folder
cd ~/azure-iot-sdk-c/cmake
  • Set the environment variables
export IOTHUB_DEVICE_SECURITY_TYPE=DPS
export IOTHUB_DEVICE_DPS_ID_SCOPE=<DScope>
export IOTHUB_DEVICE_DPS_DEVICE_ID=<deviceid>
export IOTHUB_DEVICE_DPS_DEVICE_KEY=<devicekey>
export IOTHUB_DEVICE_DPS_ENDPOINT=global.azure-devices-provisioning.net
  • Replace the values in <> with values you got from the previous steps.
  • To run the sample, use the following commands
cd iothub_client/samples/pnp/pnp_temperature_controller/
./pnp_temperature_controller

  • The output would be similar to image attached above.
  • Open up the azurecentralhub and navigate to the app>devices>brainypi
  • If the status of the device is connected, then proceed to the raw data section and there you will be able to see the data sent by the device.
  • If you see an error on terminal or the data is not being sent, then check your internet connection or retrace your steps to find the error


Next Steps

  • What we achieved above, was running a sample a program which sent sample values to the cloud.
  • For customizing the program and sending real world values to the cloud. Hop on over to the next part of the Instructables.