docs: rewrite README for current multi-meter behavior
This commit is contained in:
174
README.md
174
README.md
@@ -1,123 +1,129 @@
|
|||||||
# DD3-LoRa-Bridge-MultiSender
|
# DD3-LoRa-Bridge-MultiSender
|
||||||
|
|
||||||
Unified firmware for LilyGO T3 v1.6.1 (ESP32 + SX1276 + SSD1306) running as either:
|
Firmware for LilyGO T3 v1.6.1 (`ESP32 + SX1276 + SSD1306`) that runs as either:
|
||||||
- `Sender`: reads energy-only values from multiple IEC 62056-21 meters and sends binary batches over LoRa.
|
- `Sender` (PIN `GPIO14` HIGH): reads multiple IEC 62056-21 meters, batches data, sends over LoRa.
|
||||||
- `Receiver`: accepts batches, ACKs with optional time, publishes to MQTT/web.
|
- `Receiver` (PIN `GPIO14` LOW): receives/ACKs batches, publishes MQTT, serves web UI, logs to SD.
|
||||||
|
|
||||||
## Protocol (minimal)
|
## Current Architecture
|
||||||
|
|
||||||
Frame format:
|
- Single codebase, role selected at boot via `detect_role()` (`include/config.h`, `src/config.cpp`).
|
||||||
|
- LoRa link uses explicit CRC16 frame protection in firmware (`src/lora_transport.cpp`), in addition to LoRa PHY CRC.
|
||||||
|
- Sender batches up to `30` samples and retransmits on missing ACK (`BATCH_MAX_RETRIES=2`, policy `Keep`).
|
||||||
|
- Receiver handles AP fallback when STA config is missing/invalid and exposes a config/status web UI.
|
||||||
|
|
||||||
`[msg_kind:1][dev_id_short:2][payload...][crc16:2]`
|
## LoRa Frame Protocol (Current)
|
||||||
|
|
||||||
|
Frame format on-air:
|
||||||
|
|
||||||
|
`[msg_kind:1][device_short_id:2][payload...][crc16:2]`
|
||||||
|
|
||||||
`msg_kind`:
|
`msg_kind`:
|
||||||
- `0`: `BATCH_UP` (Sender -> Receiver)
|
- `0` = `BatchUp`
|
||||||
- `1`: `ACK_DOWN` (Receiver -> Sender)
|
- `1` = `AckDown`
|
||||||
|
|
||||||
Removed from protocol:
|
### `BatchUp`
|
||||||
- protocol version field
|
|
||||||
- payload type field
|
|
||||||
- MeterData JSON/compressed LoRa path
|
|
||||||
- standalone TimeSync packets
|
|
||||||
|
|
||||||
CRC16 validation is still required on every frame.
|
`BatchUp` is chunked in transport (`batch_id`, `chunk_index`, `chunk_count`, `total_len`) and then decoded via `payload_codec`.
|
||||||
|
|
||||||
## Payloads
|
Payload header contains:
|
||||||
|
- fixed magic/schema fields (`kMagic=0xDDB3`, `kSchema=2`)
|
||||||
|
- `schema_id`
|
||||||
|
- sender/batch/time/error metadata
|
||||||
|
|
||||||
### 1) `BATCH_UP`
|
Supported payload schemas in this branch:
|
||||||
- Uses existing binary batch/chunk transport.
|
- `schema_id=1` (`EnergyMulti`): integer kWh for up to 3 meters (`energy1_kwh`, `energy2_kwh`, `energy3_kwh`)
|
||||||
- `sample_count == 0` is valid and means `SYNC_REQUEST`.
|
- `schema_id=0` (legacy): older energy/power delta encoding path remains decode-compatible
|
||||||
- Payload starts with `schema_id`:
|
|
||||||
- `0`: legacy schema (kept for compatibility)
|
|
||||||
- `1`: `EnergyMulti` schema (ts + integer kWh for up to 3 meters)
|
|
||||||
|
|
||||||
### 2) `ACK_DOWN` (7 bytes)
|
`n == 0` is used as sync request (no meter samples).
|
||||||
- `flags` (`u8`): bit0 = `time_valid`
|
|
||||||
- `batch_id` (`u16`, big-endian)
|
|
||||||
- `epoch_utc` (`u32`, big-endian)
|
|
||||||
|
|
||||||
Receiver sets:
|
### `AckDown` (7 bytes)
|
||||||
- `time_valid=1` only when receiver time is authoritative and sane.
|
|
||||||
- Otherwise `time_valid=0` and `epoch_utc=0`.
|
|
||||||
|
|
||||||
## Time bootstrap safety
|
`[flags:1][batch_id_be:2][epoch_utc_be:4]`
|
||||||
|
|
||||||
Sender starts with:
|
- `flags bit0`: `time_valid`
|
||||||
|
- Receiver sends ACK repeatedly (`ACK_REPEAT_COUNT=3`, `ACK_REPEAT_DELAY_MS=200`).
|
||||||
|
- Sender accepts time only if `time_valid=1` and `epoch >= MIN_ACCEPTED_EPOCH_UTC` (`2026-02-01 00:00:00 UTC`).
|
||||||
|
|
||||||
|
## Time Bootstrap Guardrail
|
||||||
|
|
||||||
|
On sender boot:
|
||||||
- `g_time_acquired=false`
|
- `g_time_acquired=false`
|
||||||
- no real sampling/batching
|
- only sync requests every `SYNC_REQUEST_INTERVAL_MS` (15s)
|
||||||
- periodic `SYNC_REQUEST` every `SYNC_REQUEST_INTERVAL_MS` (default `15000ms`)
|
- no normal sampling/transmit until valid ACK time received
|
||||||
|
|
||||||
Sender only accepts time from `ACK_DOWN` if:
|
This prevents publishing/storing pre-threshold timestamps.
|
||||||
- `time_valid == 1`
|
|
||||||
- `epoch_utc >= 2026-02-01 00:00:00 UTC` (`MIN_ACCEPTED_EPOCH_UTC = 1769904000`)
|
|
||||||
|
|
||||||
Only then:
|
## Multi-Meter Sender Behavior
|
||||||
- system time is set
|
|
||||||
- `g_time_acquired=true`
|
|
||||||
- normal 1 Hz sampling + batch transmit starts
|
|
||||||
|
|
||||||
This guarantees no pre-`2026-02-01` epoch reaches MQTT or SD/DB paths.
|
Implemented in `src/meter_driver.cpp` + sender path in `src/main.cpp`:
|
||||||
|
|
||||||
## Multi-meter sender mode
|
- Meter protocol: IEC 62056-21 ASCII, Mode D style framing (`/ ... !`)
|
||||||
|
- UART settings: `9600 7E1`
|
||||||
- Meter protocol: IEC 62056-21 ASCII Mode D
|
- Parsed OBIS: `1-0:1.8.0`
|
||||||
- UART framing: `9600 7E1`
|
- Conversion: floor to integer kWh (`floorf`)
|
||||||
- Extracted OBIS: only total energy `1-0:1.8.0`
|
|
||||||
- Conversion: kWh is sent as integer using `floor` (`12345.67 -> 12345`)
|
|
||||||
|
|
||||||
### Meter count by build mode
|
|
||||||
|
|
||||||
|
Meter count is build-dependent (`include/config.h`):
|
||||||
- Debug builds (`SERIAL_DEBUG_MODE=1`): `METER_COUNT=2`
|
- Debug builds (`SERIAL_DEBUG_MODE=1`): `METER_COUNT=2`
|
||||||
- keeps USB serial logs on UART0
|
|
||||||
- uses UART1 + UART2 for meters
|
|
||||||
- Prod builds (`SERIAL_DEBUG_MODE=0`): `METER_COUNT=3`
|
- Prod builds (`SERIAL_DEBUG_MODE=0`): `METER_COUNT=3`
|
||||||
- no serial logging
|
|
||||||
- reclaims UART0 for meter 3 RX
|
|
||||||
|
|
||||||
### Meter RX pins (TTGO T3 v1.6.1 defaults)
|
Default RX pins:
|
||||||
|
- Meter1: `GPIO34` (`Serial2`)
|
||||||
|
- Meter2: `GPIO25` (`Serial1`)
|
||||||
|
- Meter3: `GPIO3` (`Serial`, prod only because debug serial is disabled)
|
||||||
|
|
||||||
- `PIN_METER1_RX = GPIO34` (UART2 RX)
|
## Receiver Behavior
|
||||||
- `PIN_METER2_RX = GPIO25` (UART1 RX)
|
|
||||||
- `PIN_METER3_RX = GPIO3` (UART0 RX, prod only)
|
|
||||||
|
|
||||||
All pins are configurable in `include/config.h`.
|
For valid `BatchUp` decode:
|
||||||
|
1. Reassemble chunks and decode payload.
|
||||||
|
2. Send `AckDown` immediately.
|
||||||
|
3. Drop duplicate batches per sender (`batch_id` tracking).
|
||||||
|
4. If `n==0`: treat as sync request only.
|
||||||
|
5. Else convert to `MeterData`, log to SD, update web UI, publish MQTT.
|
||||||
|
|
||||||
## Receiver MQTT output (EnergyMulti)
|
## MQTT Topics and Payloads
|
||||||
|
|
||||||
For `schema_id=1`, MQTT payload includes integer fields:
|
State topic:
|
||||||
- `energy1_kwh`
|
- `smartmeter/<device_id>/state`
|
||||||
- `energy2_kwh`
|
|
||||||
- `energy3_kwh` (when meter count is 3)
|
|
||||||
|
|
||||||
## Receiver behavior
|
Fault topic (retained):
|
||||||
|
- `smartmeter/<device_id>/faults`
|
||||||
|
|
||||||
On `BATCH_UP`:
|
For `EnergyMulti` samples, state JSON includes:
|
||||||
1. Decode batch/chunks.
|
- `id`, `ts`
|
||||||
2. Send `ACK_DOWN` immediately.
|
- `energy1_kwh`, `energy2_kwh`, optional `energy3_kwh`
|
||||||
3. If `sample_count == 0`: treat as `SYNC_REQUEST`, do not publish MQTT/update stats.
|
- `bat_v`, `bat_pct`
|
||||||
4. Else decode and publish samples as normal.
|
- optional link fields: `rssi`, `snr`
|
||||||
|
- fault/reject fields: `err_last`, `rx_reject`, `rx_reject_text` (+ non-zero counters)
|
||||||
|
|
||||||
## Sender/Receiver debug logs (`SERIAL_DEBUG_MODE`)
|
Home Assistant discovery publishing is enabled (`ENABLE_HA_DISCOVERY=true`) but still advertises legacy keys (`e_kwh`, `p_w`, `p1_w`, `p2_w`, `p3_w`) in `src/mqtt_client.cpp`.
|
||||||
|
|
||||||
Sender:
|
## Web UI, Wi-Fi, Storage
|
||||||
- `sync: request tx batch_id=%u`
|
|
||||||
- `ack: rx ok batch_id=%u time_valid=%u epoch=%lu set=%u`
|
|
||||||
- `ack: timeout batch_id=%u retry=%u`
|
|
||||||
|
|
||||||
Receiver:
|
- STA config is stored in Preferences (`wifi_manager`).
|
||||||
- `ack: tx batch_id=%u time_valid=%u epoch=%lu samples=%u`
|
- If STA/MQTT config is unavailable, receiver starts AP mode with SSID prefix `DD3-Bridge-`.
|
||||||
|
- Web auth defaults are `admin/admin` (`WEB_AUTH_DEFAULT_USER/PASS`).
|
||||||
|
- SD logging is enabled (`ENABLE_SD_LOGGING=true`).
|
||||||
|
|
||||||
## Removed hardware dependency
|
## Build Environments
|
||||||
|
|
||||||
DS3231 RTC support was removed:
|
From `platformio.ini`:
|
||||||
- no RTC files
|
|
||||||
- no RTC init/load/set logic
|
|
||||||
- no `ENABLE_DS3231` flow
|
|
||||||
|
|
||||||
## Build
|
- `lilygo-t3-v1-6-1`
|
||||||
|
- `lilygo-t3-v1-6-1-test`
|
||||||
|
- `lilygo-t3-v1-6-1-868`
|
||||||
|
- `lilygo-t3-v1-6-1-868-test`
|
||||||
|
- `lilygo-t3-v1-6-1-payload-test`
|
||||||
|
- `lilygo-t3-v1-6-1-868-payload-test`
|
||||||
|
- `lilygo-t3-v1-6-1-prod`
|
||||||
|
- `lilygo-t3-v1-6-1-868-prod`
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
pio run -e lilygo-t3-v1-6-1
|
~/.platformio/penv/bin/pio run -e lilygo-t3-v1-6-1
|
||||||
pio run -e lilygo-t3-v1-6-1-test
|
|
||||||
pio run -e lilygo-t3-v1-6-1-prod
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Test Mode
|
||||||
|
|
||||||
|
`ENABLE_TEST_MODE` replaces normal sender/receiver loops with dedicated test loops (`src/test_mode.cpp`).
|
||||||
|
It sends/receives plain JSON test frames and publishes to `smartmeter/<device_id>/test`.
|
||||||
|
|||||||
Reference in New Issue
Block a user