# MicroPython on/off example

Control an output from Warblet or Alexa, with firmware that applies each on/off command and reports the resulting state. This guide includes a complete MicroPython example for the on-board LED and button on an Olimex ESP32-C3-DevKit-Lipo revision C.

The same on/off control works from the dashboard, routines, and Alexa. Follow the Alexa setup steps below when they are available on your account, or see [how to use on/off control without Alexa](/alexa-on-off#3-use-one-onoff-control). Installing firmware does not enable Alexa account access or allow Alexa control by itself.

## What you need

- An Olimex ESP32-C3-DevKit-Lipo revision C running MicroPython 1.28.0 for ESP32-C3, a USB data cable, and Wi-Fi.
- The device already added to your Warblet account, with its hardware ID and current device credentials.
- Python 3 and [mpremote](https://docs.micropython.org/en/latest/reference/mpremote.html) on your computer.
- The [complete on/off firmware example](https://warbletiot.com/examples/alexa-on-off.zip).

The example replaces the board’s current application. Back up its program files and private `chirp_cfg.json` before copying new files. It does not install MicroPython or continue the previous application’s sensor reporting.

## 1. Download and prepare the settings

Extract the example ZIP into a private folder on your computer. Keep all its files together.

If needed, install the computer tool and find the board’s serial port:

```sh
python -m pip install mpremote
mpremote connect list
```

In the commands below, replace `PORT` with that port, such as `COM5` or `/dev/ttyACM0`. Close other programs using the board’s serial connection.

Copy the board’s existing settings into the extracted folder:

```sh
mpremote connect PORT fs cp :chirp_cfg.json chirp_cfg.json
```

Use the device’s current hardware ID and credentials. For a device already using **Token** security, this is the configuration shape for HTTPS:

```json
{
  "hwid": "YOUR_EXISTING_HARDWARE_ID",
  "ssid": "YOUR_WIFI_NAME",
  "pass": "YOUR_WIFI_PASSWORD",
  "securityLevel": 1,
  "tls": true,
  "token": "YOUR_EXISTING_DEVICE_TOKEN",
  "capabilityId": "power"
}
```

Match `securityLevel` to the device’s Warblet settings. Keep the existing level and credentials when switching applications.

| Warblet setting | `securityLevel` | Credentials in the file |
| --- | --- | --- |
| Open | `0` | None. Anyone who knows the hardware ID can impersonate the device. |
| Token | `1` | Existing device token in `token`. |
| Signed | `2` | Existing token in `token` and base64 signing key in `key`. |

The firmware needs no Amazon credentials.

`tls` is a separate setting. Keep it `true` for HTTPS; it must be `true` when **Encrypted** is enabled in Warblet. Signing alone does not encrypt the connection.

Use HTTP (`tls: false`) only when **Encrypted** is off. HTTP exposes messages and tokens to anyone able to observe the connection.

Run the included helper on your computer:

```sh
python prepare_config.py chirp_cfg.json --output chirp_cfg.ready.json
```

It checks the settings and adds current network addresses for Warblet and the time server. Your original file stays unchanged. Keep both JSON files private.

## 2. Install the example

From the extracted folder, copy the seven runtime files and prepared settings to the board:

```sh
mpremote connect PORT fs cp main.py device_exchange.py elapsed.py protected_http.py coarse_time.py chirp_sign.py roots.pem :
mpremote connect PORT fs cp chirp_cfg.ready.json :chirp_cfg.json
mpremote connect PORT reset
```

The program starts with the LED off. Press and release **BUT1** to change it locally. GPIO8 drives the active-low LED; GPIO9 reads the button.

The example switches this output, not the board’s power. It confirms the GPIO level, not the LED’s brightness. It may report a paused connection until you allow control in Warblet.

## 3. Allow Alexa to control

On the device’s Warblet page:

1. Open **Alexa → Set up Alexa**.
2. Set **Name in Alexa**, such as **Desk light**.
3. Check **This device’s firmware supports Warblet on/off control** after installing the example.
4. Choose **Allow Alexa to control**.

This on/off control also appears on the dashboard and device page. You do not need another spec edit or firmware confirmation to use it there. Any custom command controls are under **More commands**.

Sending readings to Warblet is not enough for this confirmation. The installed firmware must receive the supported on/off commands, apply them, and confirm the result.

If Warblet is available to your account in the Alexa app, enable it and sign in with the same Warblet account. If you are already linked, keep that connection. Ask Alexa to discover devices, then find the name you chose.

Saving device permissions, linking your account, and discovering the output are separate steps. A saved setting does not confirm that a voice command has reached the board.

## 4. Check on and off

First, use the on/off control on the Warblet device page and check the LED. The displayed state comes from the firmware’s report of the output. Then test Alexa:

Say **“Alexa, turn Desk light on”** and check the LED. Then say **“Alexa, turn Desk light off”** and check it again.

Commands set an absolute state: **ON** keeps the output on, even if it was already on. They do not mean “toggle.” The firmware reads the output after applying a command and reports that result.

To stop Alexa control later, open **Alexa → Change**, clear **Allow Alexa to control**, and save. This leaves the local button available.

## If it does not work

| What you see | What to check |
| --- | --- |
| No **Set up Alexa** option | Alexa setup must be available on your account. Firmware cannot enable that access. |
| The settings helper cannot prepare the file | Check the JSON, hardware ID, Wi-Fi fields, explicit `securityLevel` and `tls`, and the credentials required by that security level. |
| **On/off control paused. Check device settings and network.** | Confirm control is allowed in Warblet, the device credentials match, and Wi-Fi is connected. HTTPS also needs the supplied `roots.pem` and a current clock; allow access to the time server. |
| A previously working connection stays paused | Run the settings helper again and upload the new prepared file to refresh network addresses. Keep certificate verification enabled. |
| Alexa cannot find the output | Check the linked Warblet account, saved control permission, and **Name in Alexa**. Ask Alexa to discover devices again. |
| **On/off control: command could not be confirmed** | Check the physical output before retrying. A failed confirmation does not prove that the output stayed unchanged. |

## Adapt it to your own output

Use the complete example as the starting point. In `main.py`, the supplied board mapping is:

```python
LED_PIN, BUTTON_PIN = 8, 9
```

The output is active-low: `0` means on and `1` means off. Another board needs the correct pins, active levels, and button wiring; changing the pin numbers alone may not be enough.

Keep the control helpers intact while adapting the output handling and readback. Apply **ON** or **OFF** explicitly, then report the state you actually observed. If your hardware has separate feedback, read that feedback rather than treating a GPIO write as proof of the load’s state.

Keep the example’s `power` control mapping. Verify local operation, remote on/off commands, repeated commands, and reconnection on the new hardware before relying on it.
