Send Telemetry to Edge via MQTT
Send sensor readings from a Python script to ThingsBoard Edge over MQTT using the paho-mqtt client. Devices connect directly to the Edge instance — use the hostname or IP address of your Edge instance in place of the ThingsBoard Server URL.
Prerequisites
Section titled “Prerequisites”- Python 3.7+
- A device provisioned on your Edge instance with an Access Token — see Manage Devices on Edge
-
Install paho-mqtt:
Terminal window pip install paho-mqtt -
Copy your device’s Access Token from Entities → Devices → [device] → Copy access token on the Edge instance.
Send telemetry
Section titled “Send telemetry”import paho.mqtt.client as mqttimport jsonimport time
EDGE_HOST = "192.168.1.100" # replace with your Edge hostACCESS_TOKEN = "YOUR_ACCESS_TOKEN"
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION1)client.username_pw_set(ACCESS_TOKEN)client.connect(EDGE_HOST, 1883, 60)client.loop_start()
try: while True: payload = json.dumps({"temperature": 22.5, "humidity": 60}) client.publish("v1/devices/me/telemetry", payload, qos=1) print(f"Sent: {payload}") time.sleep(5)finally: client.loop_stop() client.disconnect()The script publishes a JSON telemetry payload every 5 seconds. Edge stores readings locally and forwards them to the ThingsBoard Server through the root rule chain when a cloud connection is available.
Verify in Edge
Section titled “Verify in Edge”- Log in to the Edge instance and go to Entities → Devices.
- Open the device and click the Latest telemetry tab.
- Confirm that
temperatureandhumidityvalues appear.
For the full list of MQTT topics and payload formats, see MQTT API.