Skip to content
Stand with Ukraine flag

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.

  1. Install paho-mqtt:

    Terminal window
    pip install paho-mqtt
  2. Copy your device’s Access Token from Entities → Devices → [device] → Copy access token on the Edge instance.

import paho.mqtt.client as mqtt
import json
import time
EDGE_HOST = "192.168.1.100" # replace with your Edge host
ACCESS_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.

  1. Log in to the Edge instance and go to Entities → Devices.
  2. Open the device and click the Latest telemetry tab.
  3. Confirm that temperature and humidity values appear.

For the full list of MQTT topics and payload formats, see MQTT API.