Skip to main content

Azure IoT

warning

The document is a continuation of the previous document, if you have landed directly on this page then, Please read from page Get started.

What is Azure IoT?

Oops!, No Image to display.
  • Azure IoT is a message router hosted on Azure Cloud which allows the IoT devices to connect to Azure services.
  • IoT device can send and receive messages to/from the Azure IoT.

Setup:

Requirements for using Shunya Interfaces Azure API's are:

  1. RPI with Shunya OS Installed
  2. Azure account.

Requirements to connect to Azure IoT

Azure IoT Hub is the service which receives data from the IoT device.

To connect to Azure IoT hub you need

  1. Azure IoT hub name
  2. Azure IoT device ID
  3. SAS token for Azure IoT device

Creating a new IoT Hub in the Azure Portal

  • Login to the Azure portal: https://portal.azure.com/#home

  • Click on “Create a resource”:

    Oops!, No Image to display.
  • Put in and choose “IoT Hub”:

    Oops!, No Image to display.
  • Create the IoT Hub:

    Oops!, No Image to display.
  • Put in the following data: Subscription (1) Resource group (2) Region (3) IoT hub name (4) After that click on “Review + create” (5):

    Oops!, No Image to display.
  • Review and create:

    Oops!, No Image to display.

Creating a new IoT device in the Azure Portal

  1. Open the Azure IoT Portal > IoT Hub > IoT Devices tab and click the New button on top

    Oops!, No Image to display.
  2. Name the IoT Device and set the other options as shown in the image and Save

    Oops!, No Image to display.
  3. The Device will be added to the IoT devices list, click on the device name.

    Oops!, No Image to display.

Generating SAS token for Azure IoT device

To generate the SAS token you need to run these commands

# Install azure cli
sudo apt install azure-cli
# Login into azure cli
az login
# Run this command to generate the SAS token
az iot hub generate-sas-token -d <device-id> -n <iot-hub-name>

The SAS token will be printed on the command line, copy it and store it into the config file.

Configuration:

Write json configuration in /etc/shunya/config.json file.

For example:

{
"azure": {
"azureIotHubName": "test-rtu",
"azureIotPort": "8883",
"azureIotDeviceId": "rpi",
"azureIotSASToken": "SharedAccessSignature sr=test-rtu.azure-devices.net%2Fdevices%2Frpi&sig=xdjHBKXId59oBeEdUXj0UPmunu0yfdPcH1Atb23%2FU4Q%3D&se=1599563270",
"azureIotRootCA" : "../certs/IoTHubRootCA_Baltimore.pem"
}
}

Using the Azure Shunya Interfaces API

Primary uses of Azure Shunya Interfaces API's are

  1. Send data to Azure cloud

Send Data from Azure cloud

Step 1: Write Code to Send data to Azure IoT core

/* Create a new Azure instance */
azureObj azure = newAzure("azure"); /* Argument = JSON Title, Load settings from JSON file */
int32_t rc;
/* Connect to Azure IoT */
rc = azureConnectMqtt(&azure);

/* Check if Successfully connected to Azure IoT */
if (rc < 0) {
exit(-1); /* Exit with failure */
}

/* Dummy value for publish */
float temp = 28.7;

while (1) {
/* Publish the value to Azure IoT */
rc = azureSendTelemetry(&azure, "{ \"temperature\": %.2f }", temp);

/* Check if Successfully published to Azure IoT */
if (rc < 0) {
printf("Error code = %d\n", rc);
}
/* Delay and then send the next message */
delay(1000);
}
info

Checkout ready-to-use Azure example

Ready to use example provides sample code and sample cmake, which can be used to quickly get-started using the component.

Step 3: Compile and Run code

To compile and Run code

  1. Clone the program
    git clone https://gitlab.iotiot.in/repo-public/examples.git
    cd examples/demo-examples/send-data-to-azure
  2. Compile the program
    mkdir build && cd build
    cmake ../
    make
  3. Make sure that you have written the mqtt configuration in "/etc/shunya/config.json"
    {
    "azure": {
    "azureIotHubName": "test-rtu",
    "azureIotPort": "8883",
    "azureIotDeviceId": "rpi",
    "azureIotSASToken": "SharedAccessSignature sr=test-rtu.azure-devices.net%2Fdevices%2Frpi&sig=xdjHBKXId59oBeEdUXj0UPmunu0yfdPcH1Atb23%2FU4Q%3D&se=1599563270",
    "azureIotRootCA" : "../certs/IoTHubRootCA_Baltimore.pem"
    }
    }
  4. Run the code
    sudo ./azure

Facing errors with the component?