Azure IoT Hub

Example for the Azure IoT hub application on Synps

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

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

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

Azure IoT Hub

Create Azure IoT hub:

az iot hub create --resource-group MyResourceGroup --name MyIotHub --location eastus --tags synpse=true

Create certificate based on Azure documentation.

Upload the certificates for Azure device by creating "device-identity"

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 message route to the storage account blob.

Create storage account:

az storage account create -n MyStorageAccountName -g MyResourceGroup -l eastus

Create container/bucket for results:

az storage container create --account-name MyStorageAccountName -n metrics

Create IoT hub endpoint for message routing:

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}

Use routing in question with our HUB (endpoint name is same as --endpoint-name)

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.

Create certificate secrets

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.

synpse deploy -f synpse-azure-example.yaml

where synpse-azure-example.yaml is

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

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

Last updated