DEV Community

Mangirdas Judeikis
Mangirdas Judeikis

Posted on

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

Synpse is an end-to-end platform to manage your device fleet that can grow to hundreds of thousands of devices, perform OTA software updates, collect metrics, logs, deploy your containerized applications and facilitate tunnel-based SSH access to any of your device. You can find a Quick Start here.

Azure landing page

Intro into Azure IoT Hub

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:
az iot hub create --resource-group MyResourceGroup --name MyIotHub --location eastus --tags synpse=true
Enter fullscreen mode Exit fullscreen mode
  1. 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"
Enter fullscreen mode Exit fullscreen mode

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

  1. Create storage account:
az storage account create -n MyStorageAccountName -g MyResourceGroup -l eastus
Enter fullscreen mode Exit fullscreen mode
  1. Create container/bucket for results:
az storage container create --account-name MyStorageAccountName -n metrics
Enter fullscreen mode Exit fullscreen mode
  1. 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}
Enter fullscreen mode Exit fullscreen mode
  1. 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
Enter fullscreen mode Exit fullscreen mode

Deploy an application

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

  1. Create certificate secrets
synpse secret create azure-crt -f device1.crt
synpse secret create azure-key -f device1.key
Enter fullscreen mode Exit fullscreen mode

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

synpse deploy -f synpse-azure-example.yaml
Enter fullscreen mode Exit fullscreen mode

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: {}
Enter fullscreen mode Exit fullscreen mode

You should see messages coming into the Azure IoT Hub

message flow

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

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 land into a storage account without visibility how and the default data structure cannot easily adjust. Sometimes it takes a few minutes, sometimes multiple minutes. We assume this is due to the ETL (Extract, Transform, Load) process, which is not visible to users. But on the other hand, on GCP it was very visible and VERY expensive. Not sure if both here are possible :)

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

go run ./wrap_up.go

This is a 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 the public cloud. But where the cloud is 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 themselves - they are yours and yours only. And this is where Public cloud providers lack 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

Originally published at https://synpse.net/blog

Top comments (0)