Smart Enviro Backend is designed for bidirectional communication with ESP32 microcontrollers. The device authenticates using aDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/GaelCeballos/Smart_Enviro_Backend/llms.txt
Use this file to discover all available pages before exploring further.
device_token (not a user token), syncs its configuration on boot, and pushes sensor readings in exchange for actuator control instructions. No user credentials are ever stored on the hardware.
Step 1: Sync on Boot
On every power-up — and periodically thereafter (recommended: every 5 minutes) — the ESP32 callsGET /api/device/sync to receive its current command and full configuration. This endpoint is public (no Sanctum token required); the device_token alone authenticates the device.
Request — query parameter:
| Parameter | Required | Description |
|---|---|---|
device_token | ✅ Yes | The 64-character token provisioned into the device firmware |
command field maps directly to devices.current_state. The config object is a flat key→value map of every property stored for this device in device_properties.
Error response — invalid or inactive device (401):
When the response
command is SLEEP, the ESP32 should enter deep sleep mode immediately to conserve power. This response is returned whenever the device token is not found in the database or the device has is_active = false.Step 2: Push Sensor Readings
After a successful sync, the ESP32 sends sensor readings usingPOST /api/sensor-data. This endpoint is also public and authenticates via device_token. The backend verifies the token, resolves the sensor type by metric_key, stores the reading, marks the device online, and returns the latest device_control block — all in a single call.
Request body:
| Field | Type | Required | Description |
|---|---|---|---|
device_token | string | ✅ Yes | 64-character device authentication token |
sensor_type_id | string | ✅ Yes | The metric_key string for this reading, e.g. humedad_suelo |
value | numeric | ✅ Yes | The raw sensor reading |
- Looks up the device by
device_tokenand confirmsis_active = true - Resolves the
SensorTyperecord by matchingmetric_keyagainstsensor_type_id - Creates a new
SensorDatarecord withdevice_id,sensor_type_id, andreading_value - Updates
device.is_online = trueanddevice.last_seen_at = now() - Reads all
device_propertiesfor this device and builds thedevice_controlresponse block
Step 3: Act on Commands
Once the ESP32 has adevice_control block (from either sync or a sensor-data response), it should apply the following logic:
current_state | auto_water | Recommended ESP32 Behavior |
|---|---|---|
ON | any | Activate actuator immediately (e.g. turn pump on) |
OFF | any | Deactivate actuator immediately |
STANDBY | false | Idle — do nothing, wait for next sync |
STANDBY | true | Evaluate locally: activate actuator if current humidity reading is below humidity_threshold |
auto_water is true, the device operates autonomously: it compares the latest humidity sensor reading against the humidity_threshold integer (a percentage) and activates or deactivates the pump locally without waiting for a server-side command.
auto_water is returned as a boolean (true/false) in device_control, but it is stored as the string "true" or "false" in device_properties. The backend handles the cast before sending the response — the ESP32 always receives a JSON boolean.Recommended Polling Pattern
| Action | Endpoint | Recommended Frequency |
|---|---|---|
| Boot sync | GET /api/device/sync | On every power-up, then every 5 minutes |
| Sensor push | POST /api/sensor-data | Per measurement cycle (device-specific, e.g. every 30 s) |
The sensor data response always includes the latest
device_control block, so the ESP32 does not need an extra sync call between measurement cycles. The 5-minute periodic sync is a safety net to pick up property changes made while the device was between measurement cycles.Device Overview
Understand the full device lifecycle — from admin provisioning through user claiming and decommission.
Device Properties
Full reference for all standard property keys and how to update them from a mobile client.