开发者
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
AI 资讯
Why doesn't MQTT have its own Express? Introducing mqttkit
mqttkit: Elysia-style application framework for MQTT An ordered middleware pipeline, typed topic routes, MQTT 5 RPC, and auto-generated AsyncAPI docs — sitting on top of any MQTT broker. If you've ever built a serious MQTT backend in Node, you've probably written this code at least once: client . on ( ' message ' , ( topic , payload ) => { if ( topic . startsWith ( ' devices/ ' ) && topic . endsWith ( ' /events ' )) { const uid = topic . split ( ' / ' )[ 1 ] // ad-hoc auth check // ad-hoc JSON.parse + validation // ad-hoc error handling // ad-hoc metrics // ... } else if ( topic . startsWith ( ' server/ ' )) { // ... } }) That's the MQTT equivalent of writing an HTTP server with http.createServer((req, res) => { if (req.url === '/users') ... }) . We solved that pattern for HTTP a decade ago with Express, Koa, Fastify, and more recently Hono and Elysia. For MQTT, we haven't. That's the gap mqttkit is filling. The design choice: don't reimplement the protocol There are already excellent MQTT brokers in the Node ecosystem — most notably Aedes , which handles CONNECT, SUBSCRIBE, PUBLISH, QoS, retain, sessions, persistence, and MQTT-over-WebSocket. EMQX and Mosquitto cover production scale. None of these need replacing. What's missing is the application layer — the part where you ask: How do I declaratively say "this topic requires this auth check"? How do I validate payloads with the same schema I already use for HTTP? How do I do MQTT 5 request/response without writing correlation-id bookkeeping? How do I get AsyncAPI docs for free? How do I attach Prometheus / OpenTelemetry without lifting broker internals? mqttkit is purely that layer. It plugs into Aedes via @mqttkit/aedes , but the broker is just an adapter — you can write your own for EMQX, NanoMQ, or any other broker. What the code looks like import { aedes } from ' @mqttkit/aedes ' import { MqttApp , router } from ' @mqttkit/core ' import { z } from ' zod ' const app = new MqttApp < { principal ?: { uid : string
AI 资讯
MQTT, CoAP, or HTTP: Which IoT Protocol Fits Your Product?
There are more IoT protocols out there than most teams will ever need. That sounds overwhelming until you realize most connected products only use two or three; one for local communication, one for cloud connectivity, and sometimes one for device management. The problem is not the number of options. The problem is that the protocol you pick at the design stage gets baked into your firmware, your cloud pipeline, and your data model. Change it later and you are rewriting half of your stack. Here is a practical breakdown of the protocols that matter for most developers building connected products today. The Three Layers You Are Choosing Across IoT protocols sit in three distinct layers, and you typically pick one from each: Application Layer → MQTT, CoAP, HTTP, AMQP (how data reaches your cloud) Network Layer → LoRaWAN, NB-IoT, LTE-M, Wi-Fi, BLE (how data travels physically) Industrial Layer → Modbus, OPC UA, Profinet (machine-to-machine on the factory floor) A soil moisture sensor on a farm might use LoRaWAN at the network layer to push data 10 kilometers to a gateway and MQTT at the application layer to deliver that data to a cloud dashboard. Two protocols, two layers, one product. The Big Three for Cloud Connectivity MQTT - The Default for a Reason Publish-subscribe model. Lightweight. Three QoS levels for delivery guarantees. Run over TCP with TLS encryption. Roughly 70% of cloud-connected IoT deployments use MQTT today. A basic publish looks like this: import paho.mqtt.client as mqtt client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2) client.tls_set() client.connect("broker.example.com", 8883) client.publish("sensors/temperature", '{"value": 23.5, "unit": "C"}') Use when: You need real-time telemetry, bidirectional device control, or guaranteed message delivery across unreliable networks. HTTP - Not for Telemetry, But Still Essential Too heavy for continuous sensor data. But it is the standard for OTA firmware updates, cloud API integrations, and management das