Register a callback
CPP
main.ino
// Register it in setup(): ruangiot.onMessage(receive);
void receive(String &variable, String &data) {
if (variable == "relay") {
const bool aktif = data == "ON";
digitalWrite(2, aktif ? HIGH : LOW);
ruangiot.sendStatus("relay", aktif ? "ON" : "OFF");
}
}
Validate commands
- Validate the variable name and payload type.
- Reject values outside the allowed range.
- Don't run dangerous commands without safeguards.
- Send the actual status after the action completes.
Keep the loop responsive
Blocking work that runs too long can drop the MQTT keep-alive and delay commands. Break long tasks into a state machine or timers.