Skip to main content
Documentation v2.0
Subscribe to commands/docs/mqtt/subscribe-command

MQTT

Stable

Subscribe to commands

Receive dashboard commands on the device and send an acknowledgement.

On this page4 sections

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.

Security

Documentation feedback

Was this article helpful?

Documentation search

What do you want to learn?

Type at least two characters to search the documentation.
Code copied.