83 lines
1.8 KiB
Markdown
83 lines
1.8 KiB
Markdown
# Relay Service Example
|
||
|
||
A minimal example that uses the framework's `NodeContext`, `NetworkManager`, `TaskManager`, and `ApiServer` to control a relay via REST and log status periodically as a task.
|
||
|
||
- Default relay pin: `GPIO0` (ESP-01). Override with `-DRELAY_PIN=<pin>`.
|
||
- WiFi and API port are configured in `src/Config.cpp`.
|
||
|
||
## Build & Upload
|
||
|
||
- ESP‑01S:
|
||
```bash
|
||
pio run -e esp01_1m_relay -t upload
|
||
```
|
||
- D1 Mini:
|
||
```bash
|
||
pio run -e d1_mini_relay -t upload
|
||
```
|
||
|
||
Monitor serial logs:
|
||
```bash
|
||
pio device monitor -b 115200
|
||
```
|
||
|
||
Assume the device IP is 192.168.1.50 below (replace with your device's IP shown in serial output).
|
||
|
||
## Relay API
|
||
|
||
- Get relay status
|
||
```bash
|
||
curl http://192.168.1.50/api/relay/status
|
||
```
|
||
|
||
- Turn relay ON
|
||
```bash
|
||
curl -X POST http://192.168.1.50/api/relay/set -d state=on
|
||
```
|
||
|
||
- Turn relay OFF
|
||
```bash
|
||
curl -X POST http://192.168.1.50/api/relay/set -d state=off
|
||
```
|
||
|
||
- Toggle relay
|
||
```bash
|
||
curl -X POST http://192.168.1.50/api/relay/set -d state=toggle
|
||
```
|
||
|
||
Notes:
|
||
- Requests use `application/x-www-form-urlencoded` by default when using `curl -d`.
|
||
|
||
## Task Management (optional)
|
||
The example registers a periodic task `relay_status_print` that logs the current relay state.
|
||
|
||
- Fetch all task statuses
|
||
```bash
|
||
curl http://192.168.1.50/api/tasks/status
|
||
```
|
||
|
||
- Query a specific task status
|
||
```bash
|
||
curl -X POST http://192.168.1.50/api/tasks/control \
|
||
-d task=relay_status_print -d action=status
|
||
```
|
||
|
||
- Enable / Disable the task
|
||
```bash
|
||
curl -X POST http://192.168.1.50/api/tasks/control \
|
||
-d task=relay_status_print -d action=enable
|
||
|
||
curl -X POST http://192.168.1.50/api/tasks/control \
|
||
-d task=relay_status_print -d action=disable
|
||
```
|
||
|
||
## General System Endpoints (optional)
|
||
- Node status
|
||
```bash
|
||
curl http://192.168.1.50/api/node/status
|
||
```
|
||
|
||
- Restart device
|
||
```bash
|
||
curl -X POST http://192.168.1.50/api/node/restart
|
||
``` |