Skip to main content
Goal: flip a Switch on the dashboard and toggle a pin on the device.

1. Add a Switch

In the designer, drop a Switch and configure the command it sends in the Triggers tab. A command arrives at the device as structured JSON, for example:
{
  "from": "controller-xyz",
  "payload": {
    "commands": [
      {
        "command": "GPIO_MANAGEMENT",
        "actions": [
          { "action": "ON", "params": { "gpio": 12, "pinmode": "OUTPUT", "status": "HIGH" } }
        ]
      }
    ]
  }
}

2. Handle the command in firmware

Register a handler and use the JSON helpers to pull out the command, action, and params:
void setup() {
  Serial.begin(115200);

  device.setUserCommandHandler([](JsonObject &msg) {
    JsonObject params = device.findParams(msg, "GPIO_MANAGEMENT", "ON");
    if (!params.isNull()) {
      int pin = params["gpio"];
      pinMode(pin, OUTPUT);
      digitalWrite(pin, params["status"] == "HIGH" ? HIGH : LOW);
    }
  });

  device.begin();
}

void loop() {
  device.loop();
}
findCommand, findAction, and findParams navigate the incoming payload so you don’t parse it by hand. See the API reference.

3. Test it

Flip the Switch in Preview. The device receives the command and toggles the pin.

How it works

This is the down half of the core loop: a control widget → setUserCommandHandler on the device. Combined with Lesson 2, you now have a full two-way device.

Next: store readings

Persist data to the product database.