Docs

Button

The Button widget sends 1 or 0 to a virtual pin.

Use it for anything with two states: a relay, light, pump, fan, lock, or any other digital output.

Pump V1 = 0

Tap the button above. V1 switches between 0 and 1 — those are the values your board receives.

Example

This example controls an LED from a Button on V1.

button.ino
#include <PlynxSimpleEsp32.h>

#define LED_PIN 2

// Runs whenever Plynx sends a value to V1.
PLYNX_WRITE(V1)
{
  int value = param.asInt();
  digitalWrite(LED_PIN, value ? HIGH : LOW);
}

void setup()
{
  Serial.begin(115200);

  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW);

  // Auto setup: WiFi and the Plynx token are configured from the app.
  //
  // To provide them directly in the sketch instead, use:
  //   Plynx.begin(auth, ssid, pass);
  //
  // Or with a custom server:
  //   Plynx.begin(auth, ssid, pass, "your.server", 8080);
  Plynx.begin();
}

void loop()
{
  Plynx.run();
}

Plynx.begin() uses Auto setup. On a board without saved configuration, Plynx opens a Plynx-XXXX hotspot and the app sends the WiFi credentials and token.

PLYNX_WRITE(V1) receives the value sent to V1. For a Button this is normally 0 or 1, so param.asInt() is all you need.

Keep Plynx.run() in loop() so the board can receive updates.

Restore the state after a reboot

After a reboot, the ESP32 starts with the LED off. If V1 was already on, ask Plynx for its last value when the board reconnects:

Restore V1 after reconnect
PLYNX_CONNECTED()
{
  Plynx.syncVirtual(V1);
}

The stored value is sent back to the board and handled by the same PLYNX_WRITE(V1) callback.

Keep the app in sync

Sometimes the output changes somewhere else — for example from a physical button, a timer or a sensor.

When that happens, update the widget too:

Update the Button from the board
digitalWrite(LED_PIN, HIGH);
Plynx.virtualWrite(V1, 1);

Call virtualWrite() when the state changes, not continuously inside loop().

Switch and Push modes

The Button has two modes:

ModeBehaviourGood for
Switcheach tap toggles between 1 and 0lights, relays, pumps
Pushsends 1 while pressed and 0 when releaseddoorbells, horns, momentary controls

The Arduino code does not change. Only the way the app sends the values changes.

Troubleshooting

ProblemCheck
Button does nothingmake sure the widget and PLYNX_WRITE() use the same virtual pin
Board connects but stops respondingavoid long delay() calls or other code that blocks Plynx.run()
Board stays on the Plynx-XXXX hotspotAuto setup has not completed; run the setup again from the app

Next