How to connect a device over MQTT
Take a device that has its broker settings and credential, and get it publishing telemetry.
Prerequisites
- The broker host and port
- The device's id
- Its credential: a shared key (tier 0 and 1) or a client certificate and private key (tier 2)
Steps
1. Get the broker settings and credential
- Tier 0 — the operator loads them onto the device.
- Tier 1 and 2 — the device gets them from onboarding. See Onboarding flows for each tier.
2. Connect
Connect to the broker with the settings and credential from step 1:
import json
import paho.mqtt.client as mqtt
settings = json.load(open("settings.json"))
device_id = settings["device_id"]
client = mqtt.Client(client_id=device_id, clean_session=True)
client.username_pw_set(device_id, settings["primary_key"])
client.tls_set() # connecting by hostname below sends SNI and verifies it automatically
client.connect(settings["mqtt_host"], settings["mqtt_port"], keepalive=120)
client.loop_start()
Four things that matter:
- Use the device id as the client id. The broker uses it to detect duplicate connections.
- Set clean session to true. The platform does not rely on the broker to queue messages for the device.
- Do not subscribe to your own telemetry topic. The device may only publish to it.
- Connect by
mqtt_host, never by IP, and keep the keepalive at 120 s or less (60 s on cellular). Every tenant on an environment shares one IP and ports 8883/8884; the TLS SNI — sent automatically when you connect by hostname — is what picks this tenant's broker. A device that sends no SNI reaches only the environment's default MQTT tenant. See TLS, Hostname, and SNI for why the keepalive ceiling exists.
For a certificate device (tier 2), replace the username/password call with a client certificate:
client.tls_set(certfile="device.crt", keyfile="device.key",
ca_certs="platform-ca.pem")
3. Publish and subscribe
client.subscribe(f"device/{device_id}/twin/desired", qos=1)
client.subscribe(f"device/{device_id}/command/+/invoke", qos=1)
client.publish(f"device/{device_id}/telemetry",
json.dumps({"temperature": 22.5}), qos=1)
Verify it worked
The broker returns a CONNACK, and telemetry appears on the asset bound to this device.
If it doesn't work
Broker refuses the connection — for shared-key devices, the password is the device's primary or secondary key. On tier 1 that is the key from onboarding, not the enrollment secret the device used to fetch it.
Connected but no data appears — check the topic. Telemetry must go to
device/{deviceId}/telemetry. The device may use only its own five topics, see
Access Control.
Reconnection
Implement exponential backoff, doubling from 1 second to a 60-second ceiling. With clean session enabled, re-subscribe to the desired-state and command topics after every reconnect. When reconnecting keeps failing, see Reconnection for what each onboarding tier does next.
See also
- MQTT Protocol Reference — topics, payloads, QoS
- Device Twins — desired and reported properties
- Commands — invoking commands