Switch
A clean, persistent on/off toggle. Use it when the action is binary and stays in its new state — lights on/off, fan on/off, an automation routine enabled or disabled.
[image] Switch on canvas, OFF state, Filled style
What it does
Switch holds a boolean state. The user flips it from the dashboard; your sketch reacts on the OFF→ON edge, on the ON→OFF edge, or whenever the state changes. Three visual styles: Flat, Outlined, Filled. Three sizes: S, M, L.
When to use it
- Light switch — Wire
WHEN_TURNED_ONandWHEN_TURNED_OFFto a relay pin. - Automation enable —
WHEN_SWITCH_SET(isOn)flips a feature flag in your sketch.
Properties
The Inspector opens as a bottom sheet with a Widget ID field at the top, then these sections in order.
Widget ID
| Property | Type | What it controls |
|---|---|---|
| Widget ID | text | Identifier used by your sketch (ISwitch("…")). |
1. Essential
| Property | Type | Default | What it controls |
|---|---|---|---|
| Enabled | toggle | on | Allow user interaction. |
| Target Devices | multi-select dropdown | — | Pick which device(s) this widget communicates with. Server Mode only. |
2. Header
| Property | Type | Default | What it controls |
|---|---|---|---|
| Show header | toggle | off | Display a text label above the switch. |
| Header text | text | empty (placeholder e.g. Garage light) | Header text. Only when Show header is on. |
| Alignment | Left / Center / Right | Center | Header text position. Only when Show header is on. |
| Show ON / OFF labels | toggle | off | Show ON / OFF text alongside the switch. |
| When ON | text | empty (placeholder ON) | Label shown when ON. Only when Show ON / OFF labels is on. |
| When OFF | text | empty (placeholder OFF) | Label shown when OFF. Only when Show ON / OFF labels is on. |
3. Behavior
| Property | Type | Default | What it controls |
|---|---|---|---|
| Initial state | OFF / ON | OFF | Position when the dashboard loads. |
| Animate changes | toggle | on | Smooth transition between states. |
4. Appearance
| Property | Type | Default | What it controls |
|---|---|---|---|
| Style | Flat / Outlined / Filled | Filled | Visual variant. |
| Size | S / M / L | M | Switch size. |
5. Colors · ON state
| Property | Type | What it controls |
|---|---|---|
| Track | color | Background track when ON. |
| Thumb | color | Movable thumb when ON. |
| Border | color | Border when ON. |
| Label | color | ON / OFF text color when ON. |
6. Colors · OFF state
| Property | Type | What it controls |
|---|---|---|
| Track | color | Background track when OFF. |
| Thumb | color | Movable thumb when OFF. |
| Border | color | Border when OFF. |
| Label | color | ON / OFF text color when OFF. |
7. Colors · Disabled state
| Property | Type | What it controls |
|---|---|---|
| Track | color | Background track when disabled. |
| Thumb | color | Movable thumb when disabled. |
States and behavior
[image] Three states side by side: OFF, ON, Disabled
- OFF — OFF color set.
- ON — ON color set.
- Disabled — Disabled colors. Doesn’t react to taps.
The state is persistent across page navigation. In Server Mode, the state survives a server reboot (the server holds it).
In code
The switch mirrors what the user does on the dashboard. To react to it in your sketch:
ISwitch("kitchen_light") {
WHEN_SWITCH_SET(isOn) {
digitalWrite(RELAY_PIN, isOn ? HIGH : LOW);
}
};
That’s the minimum — one event block, the new state arrives in isOn. You can also use WHEN_TURNED_ON and WHEN_TURNED_OFF separately if you only react to one edge. See InstantIoT Library / Receive Command from App for details.
Working Arduino code example
A complete sketch you can flash. The Widget ID kitchen_light matches the one you set on your Switch in the app.
[gif] Switch on the dashboard turning a relay on the ESP32 on and off
Direct Mode
Your phone connects directly to the ESP32’s SoftAP — no router, no server. Best for prototyping and standalone setups.
#include <InstantIoTWiFiAP.hpp>
const char* AP_SSID = "InstantIoT-Greenhouse";
const char* AP_PASS = "12345678";
#define RELAY_PIN 5
InstantIoTWiFiAP instant(AP_SSID, AP_PASS);
ISwitch("kitchen_light") {
WHEN_SWITCH_SET(isOn) {
digitalWrite(RELAY_PIN, isOn ? HIGH : LOW);
}
};
void setup() {
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW);
instant.begin();
}
void loop() {
instant.loop();
}
Server Mode
The ESP32 connects to your self-hosted InstantIoT Server over WiFi. Multiple devices, persistent state, history.
#include <InstantIoTWiFiServer.hpp>
const char* WIFI_SSID = "YOUR_WIFI_SSID";
const char* WIFI_PASS = "YOUR_WIFI_PASSWORD";
const char* SERVER_IP = "192.168.1.42";
const uint16_t SERVER_PORT = 9001;
const char* DEVICE_TOKEN = "YOUR_DEVICE_TOKEN";
#define RELAY_PIN 5
InstantIoTWiFiServer instant(SERVER_IP, SERVER_PORT, DEVICE_TOKEN);
ISwitch("kitchen_light") {
WHEN_SWITCH_SET(isOn) {
digitalWrite(RELAY_PIN, isOn ? HIGH : LOW);
}
};
void setup() {
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW);
instant.begin(WIFI_SSID, WIFI_PASS);
}
void loop() {
instant.loop();
}
The widget code is identical — only the include and constructor change.
Notes
- The Colors · Disabled state section has only Track and Thumb — no Border or Label.
Next
→ Simple Button — When you want a momentary press instead of a held state.
→ Segmented Switch — When the choice is between 3+ options.