MQTT to ThingsBoard Setting Up Device Telemetry from Scratch
ThingsBoard is one of the most capable open-source IoT platforms out there. But the first time you try to get a device publishing telemetry over MQTT, the documentation sends you in three different directions of device profiles, transport configurations, topic formats, and credential types. There are a lot of setups before you see a single data point on a dashboard. This post cuts through that. By the end, you will have a device sending live sensor data to ThingsBoard over MQTT and seeing it in the Latest Telemetry tab. No fluff, just working code. What You Need Before Starting A running ThingsBoard instance, Community Edition, is fine. You can use the live demo for a quick look, though a local Docker setup is more reliable for following along since the demo instance has usage limits. You also need mosquitto-clients installed for quick command-line testing and Python 3 with paho-mqtt for the scripting part. # Install mosquitto client tools sudo apt install mosquitto-clients # Install Python MQTT client pip install paho-mqtt Step 1: Create a Device and Grab the Access Token In the ThingsBoard UI, go to Entities → Devices and click the + button to add a new device. Name it something like sensor-01. Once created, click on the device and copy the access token from the credentials tab. This token is your MQTT username. No password needed. ThingsBoard uses it to identify which device is sending data. Step 2: Send Your First Telemetry via Command Line Before writing any code, test the connection with mosquitto_pub. This tells you immediately whether the setup works. mosquitto_pub -d -q 1 \ -h "YOUR_THINGSBOARD_HOST" \ -p 1883 \ -t "v1/devices/me/telemetry" \ -u "YOUR_ACCESS_TOKEN" \ -m '{"temperature": 25.4, "humidity": 62}' If you are running ThingsBoard 3.5 or later, you can use the shorter topic format: mosquitto_pub -d -q 1 \ -h "YOUR_THINGSBOARD_HOST" \ -p 1883 \ -t "v2/t" \ -u "YOUR_ACCESS_TOKEN" \ -m '{"temperature": 25.4, "humidity": 62}' Both do the same thing. v2