How to use Azure IotHub as Custom Broker
Last updated
Last updated
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?
We set the stream ID as MQTT client ID (default behaviour). You can overwrite the MQTT client ID if needed.
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
Create an IotHub device, copy the stream ID from the Control Center
az iot hub device-identity create --hub-name <hubname> --device-id "<stream-id> --edge-enabled
Generate a SAS token for the IotHub device.
az iot hub generate-sas-token --hub-name <hubname> --duration 51840000 --device-id <stream-id>
Monitor incoming events
az iot hub monitor-events --hub-name <hubname> -d "stream-id"
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"}'
Make sure you receive messages at this point. Don't proceed unless this step works.
Enter URL, username, password and topic as custom broker in the Control Center.