Files
spore/examples/relay/README.md
Patrick Balsiger bf17684dc6 feat: implement Spore framework class as main orchestration layer
- Add Spore class as unified interface for all core framework functionality
- Implement setup() and begin() methods for flexible initialization pattern
- Add service registration with addService() for both raw and smart pointers
- Provide accessor methods for core components (getTaskManager, getContext, etc.)
- Automatically include core services (Node, Network, Cluster, Task)
- Update all examples to use simplified Spore framework approach
- Fix circular dependency issues in include structure
- Remove setHostname and setApiPort configuration methods
- Add comprehensive documentation and usage examples

The Spore class encapsulates all core functionality from the base example
and provides a clean, unified API for the entire framework. Examples now
use just spore.setup() -> add services -> spore.begin() -> spore.loop().
2025-09-13 21:17:54 +02:00

121 lines
2.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Relay Service Example
A minimal example that demonstrates the Spore framework with a custom RelayService. The Spore framework automatically handles all core functionality (WiFi, clustering, API server, task management) while allowing easy registration of custom services.
- Default relay pin: `GPIO0` (ESP-01). Override with `-DRELAY_PIN=<pin>`.
- WiFi and API port are configured in `src/Config.cpp`.
## Spore Framework Usage
This example demonstrates the simplified Spore framework approach:
```cpp
#include <Arduino.h>
#include "Spore.h"
#include "RelayService.h"
Spore spore({
{"app", "relay"},
{"device", "actuator"},
{"pin", String(RELAY_PIN)}
});
RelayService* relayService = nullptr;
void setup() {
spore.setup();
relayService = new RelayService(spore.getTaskManager(), RELAY_PIN);
spore.addService(relayService);
spore.begin();
}
void loop() {
spore.loop();
}
```
The Spore framework automatically provides:
- WiFi connectivity management
- Cluster discovery and management
- REST API server with core endpoints
- Task scheduling and execution
- Node status monitoring
## Build & Upload
- ESP01S:
```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 -d state=on
```
- Turn relay OFF
```bash
curl -X POST http://192.168.1.50/api/relay -d state=off
```
- Toggle relay
```bash
curl -X POST http://192.168.1.50/api/relay -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
```