Subscribe & Act on Commands
Toggle a relay when a message arrives.
Subscribe & Act on Commands is a free Arduino & IoT Academy lesson on CoddyKit — lesson 4 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.
Listening for Messages
Publishing sends data out; subscribing lets your device receive it. This is how the cloud sends commands back to hardware.
Subscribe to a Topic
Tell the broker which topic you want with subscribe. From then on, matching messages are pushed straight to your device.
client.subscribe("home/livingroom/light");Messages Arrive in a Callback
Incoming messages trigger a callback function you write. The broker calls it every time a subscribed topic gets a message.
The Callback Signature
Your callback receives the topic, the payload bytes, and a length. You register it once during setup.
void onMessage(char* topic, byte* payload, unsigned int len) {
}Register the Callback
Hook your function up with setCallback before connecting. The library then routes every message to it.
client.setCallback(onMessage);Read the Payload Safely
The payload is raw bytes, not a finished string. Build a String by looping over its length so you never read past the end.
String msg;
for (int i = 0; i < len; i++)
msg += (char)payload[i];Decide What It Means
Compare the message to known commands. A simple if check on the text is enough to map ON and OFF to actions.
if (msg == "ON") digitalWrite(RELAY, HIGH);Act on the Command
Now do something real: flip a relay, move a servo, or change a light. This closes the loop from cloud to physical world.
Check the Topic Too
If you subscribe to several topics, inspect the topic argument first. That tells you which device the command targets.
Re-Subscribe After Reconnect
A dropped link forgets your subscriptions. After every reconnect, call subscribe again or you will hear silence.
Keep Callbacks Short
Do quick work inside the callback and return fast. Long jobs there can stall the MQTT loop and miss new messages.
Quick Check
After a WiFi drop your device reconnects but stops responding to commands. What did the reconnect break?
Recap
You learned to subscribe, handle messages in a callback, read the payload, act on commands, and re-subscribe after reconnects. 🎉
Frequently asked questions
Is the “Subscribe & Act on Commands” lesson free?
Yes — the full text of “Subscribe & Act on Commands” 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 “Subscribe & Act on Commands”?
Toggle a relay when a message arrives. 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 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Subscribe & Act on Commands” 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