What is Azure IoT Hub and how do I use it?

Published on October 26, 21

Intro into Azure IoT Hub

Azure IoT Core
Azure IoT Core

We often get questions how Synpse is compared or competes with Azure IoT Hub service. The short answer is that they operate in slightly different domains. Azure IoT hub focuses on application connectivity of the devices while Synpse targets deployment of the applications that may or may not be using services such as IoT Core.

The best results are achieved when solutions are used together. For example, when you build an application locally that utilizes AWS IoT Core message broker or device state services and then use Synpse to distribute your application to thousands of devices.

Example application

In this tutorial, we will deploy a simple open-source application that collects metrics and send them to AWS IoT Core for further processing. All code for this blog post can be found at:

https://github.com/synpse-hq/metrics-nats-example-app - Sample metrics application https://github.com/synpse-hq/azure-iot-hub-example - Azure IoT Hub example

Steps:

  1. Create Azure IoT hub
  2. Configure rules to forward results into Azure blob storage
  3. Create Azure “Thing”/IoT device for Synpse
  4. Demo Synpse application from 3 microservices - Metrics demo, NATs messaging, Azure IoT python forwarder containers

Technologies used

  1. Synpse - manage devices and deploy applications to them
  2. NATs - a lightweight message broker that can run on-prem
  3. Azure IoT Hub - message broker between all devices and Azure

Contrary to AWS, Azure was easy to configure using CLI. All steps here will be done using Azure CLI.

Azure IoT Hub

  1. Create Azure IoT hub:
1
az iot hub create --resource-group MyResourceGroup --name MyIotHub --location eastus --tags synpse=true
  1. Create certificate based on Azure documentation.

Upload the certificates for Azure device by creating “device-identity”

1
2
3
4
5
6
Thumbprint 1: SHA1 Fingerprint=56:0E:78:56:74:F1:1B:60:73:AA:7C:8E:12:73:C4:62:01:D5:A3:10 
Thumbprint 2: SHA1 Fingerprint=81:EB:0B:27:BB:3F:BB:D8:91:AF:38:28:BE:83:E5:46:C3:0F:4D:DE

# remove colons ':' from the fingerprints

az iot hub device-identity create -n MyIotHub -d synpse --am x509_thumbprint --ptp "560E785674F11B6073AA7C8E1273C46201D5A310" --stp "81EB0B27BB3FBBD891AF3828BE83E546C30F4DDE"

For this example, we gonna create a message route to the storage account blob.

  1. Create storage account:
1
az storage account create -n MyStorageAccountName -g MyResourceGroup -l eastus
  1. Create container/bucket for results:
1
az storage container create --account-name MyStorageAccountName -n metrics
  1. Create IoT hub endpoint for message routing:
1
2
3
4
5
6
7
storageConnectionString=$(az storage account show-connection-string --name MyStorageAccountName --query connectionString -o tsv)

az iot hub routing-endpoint create --resource-group MyResourceGroup --hub-name MyIotHub \
        --endpoint-name storage --endpoint-type azurestoragecontainer --endpoint-resource-group MyResourceGroup \
        --endpoint-subscription-id $(az account show | jq -r .id) --connection-string $storageConnectionString \
        --container-name metrics --batch-frequency 60 --chunk-size 10 \
        --ff {iothub}-{partition}-{YYYY}-{MM}-{DD}-{HH}-{mm}
  1. Use routing in question with our HUB (endpoint name is same as –endpoint-name)
1
az iot hub route create -g MyResourceGroup --hub-name MyIotHub --endpoint-name storage --source-type DeviceMessages --route-name Route --condition true --enabled true

Deploy an application

Deploy an application. Modify application YAML with your thing endpoint.

  1. Create certificate secrets
1
2
synpse secret create azure-crt -f device1.crt
synpse secret create azure-key -f device1.key

Deploy the application. You will need to modify other values inside YAML file.

1
synpse deploy -f synpse-azure-example.yaml

where synpse-azure-example.yaml is

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
name: Azure-IoT-Hub
description: Azure IoT Hub Synpse example
scheduling:
  type: Conditional
  selectors:
    # device selector
    azure: iot
spec:
  containers:
    - name: nats
      image: nats
      restartPolicy: {}
    - name: metrics
      image: quay.io/synpse/metrics-nats-example-app
      restartPolicy: {}
    - name: azure-iot
      image: quay.io/synpse/azure-iot-hub-example
      command: /server/azure.py
      env:
        - name: NATS_HOSTNAME
          value: nats
        - name: HOSTNAME
          # IoT hub DNS name
          value: "mj-hub.azure-devices.net"
        - name: DEVICE_ID
          # device id used when creating a device
          value: "synpse"
      secrets:
      - name: azure-crt
        filepath: /server/device1.crt
      - name: azure-key
        filepath: /server/device1.key
      restartPolicy: {}

You should see messages coming into the Azure IoT Hub

Message flow
Message flow

Once running, you should see the application running and data coming into Azure storage account blob.

Message flow
Message flow

Things to look for

Certificate configuration path is painful. But this is the common pattern between cloud offering. Azure offers shared private key authentication. This makes multiple device provisioning easier. If you are not interested in cloud portability - this might be easier way to deal with complexities.

Events lands into storage account without visibility how and default data structure is not possible to adjust easily. Sometimes it takes few minutes, sometimes multiple minutes. We assume this is due to ETL (Extract, Transform, Load) process, not visible to users.

Overall Azure experience was quite pleasant. API and CLI consistent, documentation is quite well written.

./wrap_up.sh

This is simple way to use Azure IoT Hub with Synpse. When it comes to consuming and managing a lot of data, constructing complex applications and integrating seamlessly into your current technological infrastructure - nothing can beat public cloud. But where cloud are lacking is - IoT device and application management.

Public cloud providers are built on assumption that they will manage infrastructure for you. When it comes to devices itself - they are yours and yours only. And this is where Public cloud providers lacks of influence and where Synpse comes into the picture.

If you have any questions or suggestions, feel free to start a new discussion in our forum or drop us a line on Discord