How to use Azure IotHub as Custom Broker

The official documentation from Microsoft

Azure IotHub specialities

  • The IotHub device ID must correspond to the MQTT client ID

  • You can only connect with one client for a given IotHub device

  • The SAS token expires after a pre-defined time and needs to be refreshed. You need to update the token and update the MQTT password once in a while for every Stream in the Control Center.

What does this mean in terms of Swarm?

  • You can either:

    • create for every stream a corresponding IotHub device ID (recommended and used below) OR

    • create random IotHub device IDs and assign one to each stream by setting the MQTT client ID.

Steps

  1. Create an IotHub device, copy the stream ID from the Control Center

    1. az iot hub device-identity create --hub-name <hubname> --device-id "<stream-id> --edge-enabled

  2. Generate a SAS token for the IotHub device.

    az iot hub generate-sas-token --hub-name <hubname> --duration 51840000 --device-id <stream-id>

  3. Monitor incoming events

    az iot hub monitor-events --hub-name <hubname> -d "stream-id"

  4. Test with an MQTT client (e.g. mosquitto) to publish a message. We used this root.pem file. mosquitto_pub -p 8883 -i <stream-id> -u '<hubname>.azure-devices.net/<stream-id>/?api-version=2021-04-12' -P '<SAS token>' -t 'devices/<stream-id>/messages/events' --cafile root.pem -d -V mqttv311 -m '{"swarm":"test"}'

    1. Make sure you receive messages at this point. Don't proceed unless this step works.

  5. Enter URL, username, password and topic as custom broker in the Control Center.

Last updated