Publish Sensor Topics
Send readings to named topics.
Publish Sensor Topics is a free Arduino & IoT Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Arduino & IoT Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Sending Data Is Publishing
Once connected, sharing a reading is one call. You publish a value to a topic and the broker delivers it to subscribers.
Choose a Clear Topic
Pick a descriptive topic path like home/garden/soil. Good names make a growing system easy to read and route.
home/garden/soil/moistureThe publish Call
The basic form takes a topic and a message. Both are simple strings, so MQTT does not care what is inside the payload.
client.publish("home/garden/temp", "23.5");Payloads Are Just Text
A payload is a plain byte string. You decide its meaning, whether it is a number, a word, or structured data.
Numbers Need Converting
Sensor readings are often floats. Turn them into a string first with dtostrf or String() before you publish.
char buf[8];
dtostrf(temp, 4, 1, buf);
client.publish("home/temp", buf);Send Structured Data as JSON
To bundle several values, format the payload as JSON. One message can carry temperature and humidity together.
client.publish("home/dht", "{\"t\":23.5,\"h\":55}");Do Not Flood the Broker
Publishing thousands of times a second wastes power and bandwidth. Add a sensible interval so messages stay useful.
Time It with millis
Use millis() instead of delay to publish every few seconds. Your device stays responsive while it waits.
if (millis() - last > 5000) {
last = millis();
publishReading();
}Retain the Latest Reading
Add a retain flag so the broker stores the most recent value. New dashboards then show data the instant they connect.
client.publish("home/temp", buf, true);One Topic Per Quantity
Give each measurement its own topic, like .../temp and .../humidity. Clean separation keeps subscribers simple.
Confirm It Worked
The publish call returns true on success. Log it, or watch with a desktop MQTT client, to be sure data left the board.
Quick Check
You want a new dashboard to immediately show the last temperature, even before the sensor publishes again. What helps?
Recap
You learned to publish sensor data: convert values to strings, pick clear topics, time sends with millis, and retain the latest. 🎉
Frequently asked questions
Is the “Publish Sensor Topics” lesson free?
Yes — the full text of “Publish Sensor Topics” is free to read here on the web, and the Arduino & IoT Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Arduino & IoT Academy course, upgrade to CoddyKit PRO.
What will I learn in “Publish Sensor Topics”?
Send readings to named topics. You practise Arduino & IoT Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Arduino & IoT Academy?
No prior experience is required. Arduino & IoT Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Publish Sensor Topics” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Arduino & IoT Academy lesson?
Yes. Every Arduino & IoT Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Why MQTT Fits IoT
- Connect to a Broker
- Publish Sensor Topics
- Subscribe & Act on Commands