Compare commits
58 Commits
main
...
lora-refac
| Author | SHA1 | Date | |
|---|---|---|---|
| 195cce2bcf | |||
| 382e506f14 | |||
| 64afa8621e | |||
| 7540af7d71 | |||
| 9d5f5ed513 | |||
| 3a1de36b75 | |||
|
|
595a31f278 | ||
| 290ca55b8b | |||
| f177e5562d | |||
| cb6929bdc1 | |||
| c3e5ba3a53 | |||
| 373667ab8a | |||
| f0503af8c7 | |||
| f08d9a34d3 | |||
| 7e5e23e56c | |||
| 1024aa3dd0 | |||
| 0e7214d606 | |||
| 5a86d1bd30 | |||
| 0a99bf3268 | |||
| 4e06f7a96d | |||
| fde4719a50 | |||
| e0d35d49bc | |||
| e8fb8680cb | |||
| cbf0f7d9b9 | |||
| f7a2503d7a | |||
| 43893c24d1 | |||
| cd4c99f125 | |||
| b8a4c27daa | |||
| 2199627a35 | |||
| 90d830da6f | |||
| 237e392c02 | |||
| 8e6c64a18e | |||
| a4d9be1903 | |||
| 0e12b406de | |||
| b5477262ea | |||
| d32ae30014 | |||
| f3af5b3f1c | |||
| 5085b9ad3d | |||
| a03c2cdb07 | |||
| 13f2f02e42 | |||
| 16c1b90b1e | |||
| e5c4e04ff9 | |||
| e24798eb55 | |||
| d27b68c1cc | |||
| 01f4494f00 | |||
| 50436cd0bb | |||
| a0080b249d | |||
| 876c572bb3 | |||
| 13b4025443 | |||
| 7f31b9dd95 | |||
| 660d1cde94 | |||
| f9bcfbd5f2 | |||
| fbd18b2e78 | |||
| b4344db828 | |||
| 22ed41b55c | |||
| 430b0d7054 | |||
| 16c65744e3 | |||
| 8fba67fcf3 |
362
README.md
362
README.md
@@ -1,309 +1,135 @@
|
||||
# DD3 LoRa Bridge (Multi-Sender)
|
||||
# DD3-LoRa-Bridge-MultiSender
|
||||
|
||||
Unified firmware for LilyGO T3 v1.6.1 (ESP32 + SX1276 + SSD1306) that runs as **Sender** or **Receiver** based on a GPIO jumper. Senders read DD3 smart meter values, compress JSON, and transmit over LoRa. The receiver validates packets, publishes to MQTT, provides a web UI, and shows per-sender status on the OLED.
|
||||
Firmware for LilyGO T3 v1.6.1 (`ESP32 + SX1276 + SSD1306`) that runs as either:
|
||||
- `Sender` (PIN `GPIO14` HIGH): reads one IEC 62056-21 meter, batches samples, sends over LoRa.
|
||||
- `Receiver` (PIN `GPIO14` LOW): receives/ACKs batches, publishes MQTT, serves web UI, logs to SD.
|
||||
|
||||
## Hardware
|
||||
Board: **LilyGO T3 LoRa32 v1.6.1** (ESP32 + SX1276 + SSD1306 128x64 + LiPo)
|
||||
Variants:
|
||||
- SX1276 **433 MHz** module (default build)
|
||||
- SX1276 **868 MHz** module (use 868 build environments)
|
||||
## Current Architecture
|
||||
|
||||
### Pin Mapping
|
||||
- LoRa (SX1276)
|
||||
- SCK: GPIO5
|
||||
- MISO: GPIO19
|
||||
- MOSI: GPIO27
|
||||
- NSS/CS: GPIO18
|
||||
- RST: GPIO23
|
||||
- DIO0: GPIO26
|
||||
- OLED (SSD1306)
|
||||
- SDA: GPIO21
|
||||
- SCL: GPIO22
|
||||
- RST: **not used** (SSD1306 init uses `-1` reset pin)
|
||||
- I2C address: 0x3C
|
||||
- I2C RTC (DS3231)
|
||||
- SDA: GPIO21
|
||||
- SCL: GPIO22
|
||||
- I2C address: 0x68
|
||||
- Battery ADC: GPIO35 (via on-board divider)
|
||||
- **Role select**: GPIO13 (INPUT_PULLDOWN)
|
||||
- LOW = Sender
|
||||
- HIGH = Receiver
|
||||
- **OLED control**: GPIO14 (INPUT_PULLDOWN)
|
||||
- HIGH = force OLED on
|
||||
- LOW = allow auto-off after timeout
|
||||
- Smart meter UART RX: GPIO34 (input-only, always connected)
|
||||
- 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`).
|
||||
- Sender batches up to `30` samples and retries on missing ACK (`BATCH_MAX_RETRIES=2`, retry policy `Keep`).
|
||||
- Sender meter parsing is decoupled from LoRa ACK waits using a dedicated FreeRTOS reader task + queue (`src/main.cpp`).
|
||||
- Receiver uses STA mode when config is valid, otherwise AP fallback with web config.
|
||||
- No debug auto-reboot timer is active in normal firmware loops.
|
||||
|
||||
### Notes on GPIOs
|
||||
- GPIO34/35/36/39 are input-only and have **no internal pullups/pulldowns**.
|
||||
- Strap pins (GPIO0/2/4/5/12/15) can affect boot; avoid for role or control jumpers.
|
||||
## LoRa Frame Protocol (Current)
|
||||
|
||||
## Firmware Roles
|
||||
### Sender (battery-powered)
|
||||
- Reads smart meter via optical IR (UART 9600 7E1).
|
||||
- Extracts OBIS values:
|
||||
- Energy total: 1-0:1.8.0*255
|
||||
- Total power: 1-0:16.7.0*255
|
||||
- Phase power: 36.7 / 56.7 / 76.7
|
||||
- Phase voltage: 32.7 / 52.7 / 72.7
|
||||
- Reads battery voltage and estimates SoC.
|
||||
- Builds JSON payload, compresses, wraps in LoRa packet, transmits.
|
||||
- Light sleeps between meter reads; batches are sent every 30s.
|
||||
- Listens for LoRa time sync packets to set UTC clock.
|
||||
- Uses DS3231 RTC after boot if no time sync has arrived yet.
|
||||
- OLED shows status + meter data pages.
|
||||
On-air frame format:
|
||||
|
||||
**Sender flow (pseudo-code)**:
|
||||
```cpp
|
||||
void sender_loop() {
|
||||
meter_read_every_second(); // SML/OBIS -> MeterData samples
|
||||
read_battery(data); // VBAT + SoC
|
||||
`[msg_kind:1][device_short_id:2][payload...][crc16:2]`
|
||||
|
||||
if (time_to_send_batch()) {
|
||||
json = meterBatchToJson(samples);
|
||||
compressed = compressBuffer(json);
|
||||
lora_send(packet(MeterBatch, compressed));
|
||||
}
|
||||
`msg_kind`:
|
||||
- `0` = `BatchUp`
|
||||
- `1` = `AckDown`
|
||||
|
||||
display_set_last_meter(data);
|
||||
display_set_last_read(ok);
|
||||
display_set_last_tx(ok);
|
||||
display_tick();
|
||||
### `BatchUp`
|
||||
|
||||
lora_receive_time_sync(); // optional
|
||||
light_sleep_until_next_event();
|
||||
}
|
||||
```
|
||||
Transport is chunked (`batch_id`, `chunk_index`, `chunk_count`, `total_len`) and reassembled before payload decode.
|
||||
|
||||
**Key sender functions**:
|
||||
```cpp
|
||||
bool meter_read(MeterData &data); // parse SML frame, set OBIS fields
|
||||
void read_battery(MeterData &data); // ADC -> volts + percent
|
||||
bool meterDataToJson(const MeterData&, String&);
|
||||
bool compressBuffer(const uint8_t*, size_t, uint8_t*, size_t, size_t&);
|
||||
bool lora_send(const LoraPacket &pkt); // add header + CRC16 and transmit
|
||||
```
|
||||
Payload codec (`src/payload_codec.cpp`) currently uses:
|
||||
- `kMagic=0xDDB3`
|
||||
- `kSchema=2`
|
||||
- metadata: sender, batch, timestamp, interval, battery, fault counters
|
||||
- data arrays: `energy_wh[]`, `p1_w[]`, `p2_w[]`, `p3_w[]`
|
||||
|
||||
### Receiver (USB-powered)
|
||||
- WiFi STA connect using stored config; if not available/fails, starts AP.
|
||||
- NTP sync (UTC) and local display in Europe/Berlin.
|
||||
- Receives LoRa packets, verifies CRC16, decompresses, parses JSON.
|
||||
- Publishes meter JSON to MQTT.
|
||||
- Web UI:
|
||||
- AP mode: status + WiFi/MQTT config.
|
||||
- STA mode: status + per-sender pages.
|
||||
- OLED cycles through receiver status and per-sender pages.
|
||||
`n == 0` is valid and used for sync request packets.
|
||||
|
||||
**Receiver loop (pseudo-code)**:
|
||||
```cpp
|
||||
void receiver_loop() {
|
||||
if (lora_receive(pkt)) {
|
||||
if (pkt.type == MeterData) {
|
||||
json = decompressBuffer(pkt.payload);
|
||||
if (jsonToMeterData(json, data)) {
|
||||
update_sender_status(data);
|
||||
mqtt_publish_state(data);
|
||||
}
|
||||
} else if (pkt.type == MeterBatch) {
|
||||
json = reassemble_and_decompress_batch(pkt);
|
||||
for (sample in jsonToMeterBatch(json)) {
|
||||
update_sender_status(sample);
|
||||
mqtt_publish_state(sample);
|
||||
}
|
||||
}
|
||||
}
|
||||
### `AckDown` (7 bytes)
|
||||
|
||||
if (time_to_send_timesync()) {
|
||||
time_send_timesync(self_short_id); // 60s for first 10 min, then hourly if RTC is present
|
||||
}
|
||||
`[flags:1][batch_id_be:2][epoch_utc_be:4]`
|
||||
|
||||
mqtt_loop();
|
||||
web_server_loop();
|
||||
display_set_receiver_status(...);
|
||||
display_tick();
|
||||
}
|
||||
```
|
||||
- `flags bit0`: `time_valid`
|
||||
- Receiver repeats ACK (`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`).
|
||||
|
||||
**Key receiver functions**:
|
||||
```cpp
|
||||
bool lora_receive(LoraPacket &pkt, uint32_t timeout_ms);
|
||||
bool jsonToMeterData(const String &json, MeterData &data);
|
||||
bool jsonToMeterBatch(const String &json, MeterData *samples, size_t max, size_t &count);
|
||||
bool mqtt_publish_state(const MeterData &data);
|
||||
void web_server_loop(); // AP or STA UI
|
||||
void time_send_timesync(uint16_t self_id);
|
||||
```
|
||||
## Time Bootstrap Guardrail
|
||||
|
||||
## Test Mode (compile-time)
|
||||
Enabled by `-DENABLE_TEST_MODE` (see `platformio.ini` test environment).
|
||||
On sender boot:
|
||||
- `g_time_acquired=false`
|
||||
- no normal sampling/transmit yet
|
||||
- sync request every `SYNC_REQUEST_INTERVAL_MS` (15s)
|
||||
|
||||
- Sender: sends 4-digit test code every ~30s in JSON.
|
||||
- Receiver: shows last test code per sender and publishes to `/test` topic.
|
||||
- Normal behavior is excluded from test builds.
|
||||
Only after valid ACK time is received:
|
||||
- system time is set
|
||||
- normal 1 Hz sampling and periodic LoRa batch transmit start
|
||||
|
||||
**Test sender (pseudo-code)**:
|
||||
```cpp
|
||||
void test_sender_loop() {
|
||||
code = random_4_digits();
|
||||
json = {id, role:"sender", test_code: code, ts};
|
||||
lora_send(packet(TestCode, compress(json)));
|
||||
display_set_test_code(code);
|
||||
}
|
||||
```
|
||||
This blocks pre-threshold timestamps from MQTT/SD paths.
|
||||
|
||||
**Test receiver (pseudo-code)**:
|
||||
```cpp
|
||||
void test_receiver_loop() {
|
||||
if (pkt.type == TestCode) {
|
||||
json = decompress(pkt.payload);
|
||||
update_sender_test_code(json);
|
||||
mqtt_publish_test(id, json);
|
||||
}
|
||||
}
|
||||
```
|
||||
Timezone handling:
|
||||
- Local time rendering uses `TIMEZONE_TZ` from `include/config.h`.
|
||||
- Default value is `CET-1CEST,M3.5.0/2,M10.5.0/3` and can be changed at compile time.
|
||||
|
||||
## LoRa Protocol
|
||||
Packet layout:
|
||||
## Sender Meter Path
|
||||
|
||||
```
|
||||
[0] protocol_version (1)
|
||||
[1] role (0=sender, 1=receiver)
|
||||
[2..3] device_id_short (uint16)
|
||||
[4] payload_type (0=meter, 1=test, 2=time_sync, 3=meter_batch)
|
||||
[5..N-3] compressed payload
|
||||
[N-2..N-1] CRC16 (bytes 0..N-3)
|
||||
```
|
||||
Implemented in `src/meter_driver.cpp` + sender loop in `src/main.cpp`:
|
||||
|
||||
LoRa radio settings:
|
||||
- Frequency: **433 MHz** or **868 MHz** (set by build env via `LORA_FREQUENCY_HZ`)
|
||||
- SF12, BW 125 kHz, CR 4/5, CRC on, Sync Word 0x34
|
||||
- UART: `Serial2`, RX pin `GPIO34` (`PIN_METER_RX`), `9600 7E1`
|
||||
- ESP32 RX buffer is enlarged to `8192` bytes to survive long LoRa blocking sections.
|
||||
- Frame detection: starts at `'/'`, ends at `'!'`, timeout protection included (`METER_FRAME_TIMEOUT_MS=20000`).
|
||||
- Parsing runs in a dedicated sender task and is handed to the main sender loop via queue.
|
||||
- Parsed OBIS values:
|
||||
- `1-0:1.8.0` (total energy)
|
||||
- `1-0:16.7.0` (total power)
|
||||
- `1-0:36.7.0`, `56.7.0`, `76.7.0` (phase powers)
|
||||
- `1-0:1.8.0*Wh` is automatically scaled to kWh
|
||||
|
||||
## Data Format
|
||||
JSON payload (sender + MQTT):
|
||||
Sender samples every second and transmits batches every 30 seconds.
|
||||
|
||||
```json
|
||||
{
|
||||
"id": "F19C",
|
||||
"ts": 1737200000,
|
||||
"e_kwh": 1234.57,
|
||||
"p_w": 950.00,
|
||||
"p1_w": 500.00,
|
||||
"p2_w": 450.00,
|
||||
"p3_w": 0.00,
|
||||
"v1_v": 230.10,
|
||||
"v2_v": 229.80,
|
||||
"v3_v": 231.00,
|
||||
"bat_v": 3.92,
|
||||
"bat_pct": 78
|
||||
}
|
||||
```
|
||||
## Receiver Behavior
|
||||
|
||||
## Device IDs
|
||||
- Derived from WiFi STA MAC.
|
||||
- `short_id = (MAC[4] << 8) | MAC[5]`
|
||||
- `device_id = dd3-%04X`
|
||||
- JSON `id` uses only the last 4 hex digits (e.g., `F19C`) to save airtime.
|
||||
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 samples, log to SD, update web UI, publish MQTT.
|
||||
|
||||
Receiver expects known senders in `include/config.h` via:
|
||||
```cpp
|
||||
constexpr uint8_t NUM_SENDERS = 1;
|
||||
inline constexpr uint16_t EXPECTED_SENDER_IDS[NUM_SENDERS] = { 0xF19C };
|
||||
```
|
||||
## MQTT Topics and Payloads
|
||||
|
||||
## OLED Behavior
|
||||
- Sender: OLED stays **ON for 10 seconds** on each wake, then powers down for sleep.
|
||||
- Receiver: OLED follows the 10-minute auto-off behavior:
|
||||
- GPIO14 HIGH: OLED forced ON.
|
||||
- GPIO14 LOW: auto-off after 10 minutes.
|
||||
- Pages rotate every 4s.
|
||||
State topic:
|
||||
- `smartmeter/<device_id>/state`
|
||||
|
||||
## Power & Battery
|
||||
- Sender disables WiFi/BLE, reads VBAT via ADC, uses linear SoC map:
|
||||
- 3.0 V = 0%
|
||||
- 4.2 V = 100%
|
||||
- Uses deep sleep between cycles (`SENDER_WAKE_INTERVAL_SEC`).
|
||||
Fault topic (retained):
|
||||
- `smartmeter/<device_id>/faults`
|
||||
|
||||
## Web UI
|
||||
- AP SSID: `DD3-Bridge-<short_id>`
|
||||
- AP password: `changeme123`
|
||||
- Endpoints:
|
||||
- `/`: status overview
|
||||
- `/wifi`: WiFi/MQTT/NTP config (AP and STA)
|
||||
- `/sender/<device_id>`: per-sender details
|
||||
State JSON fields (`src/json_codec.cpp`):
|
||||
- `id`, `ts`, `e_kwh`
|
||||
- `p_w`, `p1_w`, `p2_w`, `p3_w`
|
||||
- `bat_v`, `bat_pct`
|
||||
- optional link fields: `rssi`, `snr`
|
||||
- fault/reject fields: `err_last`, `rx_reject`, `rx_reject_text` (+ non-zero counters)
|
||||
|
||||
## MQTT
|
||||
- Topic: `smartmeter/<deviceId>/state`
|
||||
- QoS 0
|
||||
- Test mode: `smartmeter/<deviceId>/test`
|
||||
- Client ID: `dd3-bridge-<device_id>` (stable, derived from MAC)
|
||||
Home Assistant discovery is enabled (`ENABLE_HA_DISCOVERY=true`) and publishes config topics under:
|
||||
- `homeassistant/sensor/<device_id>/<key>/config`
|
||||
|
||||
## NTP
|
||||
- NTP servers are configurable in the web UI (`/wifi`).
|
||||
- Defaults: `pool.ntp.org` and `time.nist.gov`.
|
||||
## RTC (DS3231)
|
||||
- Optional DS3231 on the I2C bus. Connect SDA to GPIO21 and SCL to GPIO22 (same bus as the OLED).
|
||||
- Enable/disable with `ENABLE_DS3231` in `include/config.h`.
|
||||
- Receiver time sync packets set the RTC.
|
||||
- On boot, if no LoRa time sync has arrived yet, the sender uses the RTC time as the initial `ts_utc`.
|
||||
- When no RTC is present or enabled, the receiver keeps sending time sync every 60 seconds.
|
||||
## Web UI, Wi-Fi, Storage
|
||||
|
||||
- Wi-Fi/MQTT/NTP/web-auth config persists in Preferences (`wifi_manager`).
|
||||
- AP fallback SSID prefix: `DD3-Bridge-`.
|
||||
- Default web credentials: `admin/admin`.
|
||||
- SD logging enabled (`ENABLE_SD_LOGGING=true`).
|
||||
|
||||
## Build Environments
|
||||
- `lilygo-t3-v1-6-1`: production build
|
||||
- `lilygo-t3-v1-6-1-test`: test build with `ENABLE_TEST_MODE`
|
||||
- `lilygo-t3-v1-6-1-868`: production build for 868 MHz modules
|
||||
- `lilygo-t3-v1-6-1-868-test`: test build for 868 MHz modules
|
||||
|
||||
## Limits & Known Constraints
|
||||
- **Compression**: uses lightweight RLE (good for JSON but not optimal).
|
||||
- **OBIS parsing**: supports IEC 62056-21 ASCII (Mode D) and SML; may need tuning for some meters.
|
||||
- **Payload size**: single JSON frames < 256 bytes (ArduinoJson static doc); batch frames are chunked and reassembled.
|
||||
- **Battery ADC**: uses simple linear calibration constant in `power_manager.cpp`.
|
||||
- **OLED**: no hardware reset line is used (matches working reference).
|
||||
From `platformio.ini`:
|
||||
|
||||
## Files & Modules
|
||||
- `include/config.h`, `src/config.cpp`: pins, radio settings, sender IDs
|
||||
- `include/data_model.h`, `src/data_model.cpp`: MeterData + ID init
|
||||
- `include/json_codec.h`, `src/json_codec.cpp`: JSON encode/decode
|
||||
- `include/compressor.h`, `src/compressor.cpp`: RLE compression
|
||||
- `include/lora_transport.h`, `src/lora_transport.cpp`: LoRa packet + CRC
|
||||
- `include/meter_driver.h`, `src/meter_driver.cpp`: IEC 62056-21 ASCII + SML parse
|
||||
- `include/power_manager.h`, `src/power_manager.cpp`: ADC + sleep
|
||||
- `include/time_manager.h`, `src/time_manager.cpp`: NTP + time sync
|
||||
- `include/wifi_manager.h`, `src/wifi_manager.cpp`: NVS config + WiFi
|
||||
- `include/mqtt_client.h`, `src/mqtt_client.cpp`: MQTT publish
|
||||
- `include/web_server.h`, `src/web_server.cpp`: AP/STA web pages
|
||||
- `include/display_ui.h`, `src/display_ui.cpp`: OLED pages + control
|
||||
- `include/test_mode.h`, `src/test_mode.cpp`: test sender/receiver
|
||||
- `src/main.cpp`: role detection and main loop
|
||||
- `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`
|
||||
|
||||
## Quick Start
|
||||
1. Set role jumper on GPIO13:
|
||||
- LOW: sender
|
||||
- HIGH: receiver
|
||||
2. OLED control on GPIO14:
|
||||
- HIGH: always on
|
||||
- LOW: auto-off after 10 minutes
|
||||
3. Build and upload:
|
||||
Example:
|
||||
|
||||
```bash
|
||||
pio run -e lilygo-t3-v1-6-1 -t upload --upload-port COMx
|
||||
~/.platformio/penv/bin/pio run -e lilygo-t3-v1-6-1
|
||||
```
|
||||
|
||||
Test mode:
|
||||
|
||||
```bash
|
||||
pio run -e lilygo-t3-v1-6-1-test -t upload --upload-port COMx
|
||||
```
|
||||
|
||||
868 MHz builds:
|
||||
|
||||
```bash
|
||||
pio run -e lilygo-t3-v1-6-1-868 -t upload --upload-port COMx
|
||||
```
|
||||
|
||||
868 MHz test mode:
|
||||
|
||||
```bash
|
||||
pio run -e lilygo-t3-v1-6-1-868-test -t upload --upload-port COMx
|
||||
```
|
||||
## Test Mode
|
||||
|
||||
`ENABLE_TEST_MODE` replaces normal sender/receiver loops with dedicated test loops (`src/test_mode.cpp`).
|
||||
It sends/receives JSON test frames and publishes to `smartmeter/<device_id>/test`.
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
bool compressBuffer(const uint8_t *in, size_t in_len, uint8_t *out, size_t out_max, size_t &out_len);
|
||||
bool decompressBuffer(const uint8_t *in, size_t in_len, uint8_t *out, size_t out_max, size_t &out_len);
|
||||
@@ -7,15 +7,11 @@ enum class DeviceRole : uint8_t {
|
||||
Receiver = 1
|
||||
};
|
||||
|
||||
enum class PayloadType : uint8_t {
|
||||
MeterData = 0,
|
||||
TestCode = 1,
|
||||
TimeSync = 2,
|
||||
MeterBatch = 3
|
||||
enum class BatchRetryPolicy : uint8_t {
|
||||
Keep = 0,
|
||||
Drop = 1
|
||||
};
|
||||
|
||||
constexpr uint8_t PROTOCOL_VERSION = 1;
|
||||
|
||||
// Pin definitions
|
||||
constexpr uint8_t PIN_LORA_SCK = 5;
|
||||
constexpr uint8_t PIN_LORA_MISO = 19;
|
||||
@@ -33,8 +29,8 @@ constexpr uint8_t OLED_HEIGHT = 64;
|
||||
|
||||
constexpr uint8_t PIN_BAT_ADC = 35;
|
||||
|
||||
constexpr uint8_t PIN_ROLE = 13;
|
||||
constexpr uint8_t PIN_OLED_CTRL = 14;
|
||||
constexpr uint8_t PIN_ROLE = 14;
|
||||
constexpr uint8_t PIN_OLED_CTRL = 13;
|
||||
|
||||
constexpr uint8_t PIN_METER_RX = 34;
|
||||
|
||||
@@ -47,23 +43,52 @@ constexpr uint8_t LORA_SPREADING_FACTOR = 12;
|
||||
constexpr long LORA_BANDWIDTH = 125E3;
|
||||
constexpr uint8_t LORA_CODING_RATE = 5;
|
||||
constexpr uint8_t LORA_SYNC_WORD = 0x34;
|
||||
constexpr uint8_t LORA_PREAMBLE_LEN = 8;
|
||||
|
||||
// Timing
|
||||
constexpr uint32_t SENDER_WAKE_INTERVAL_SEC = 30;
|
||||
constexpr uint32_t TIME_SYNC_INTERVAL_SEC = 60;
|
||||
constexpr uint32_t TIME_SYNC_SLOW_INTERVAL_SEC = 3600;
|
||||
constexpr uint32_t TIME_SYNC_FAST_WINDOW_MS = 10UL * 60UL * 1000UL;
|
||||
constexpr bool ENABLE_DS3231 = true;
|
||||
constexpr uint32_t SYNC_REQUEST_INTERVAL_MS = 15000;
|
||||
constexpr uint32_t OLED_PAGE_INTERVAL_MS = 4000;
|
||||
constexpr uint32_t OLED_AUTO_OFF_MS = 10UL * 60UL * 1000UL;
|
||||
constexpr uint32_t SENDER_OLED_READ_MS = 10000;
|
||||
constexpr uint32_t METER_SAMPLE_INTERVAL_MS = 1000;
|
||||
constexpr uint32_t METER_SEND_INTERVAL_MS = 30000;
|
||||
constexpr uint32_t BATTERY_SAMPLE_INTERVAL_MS = 60000;
|
||||
constexpr float BATTERY_CAL = 1.083f;
|
||||
constexpr uint32_t BATCH_ACK_TIMEOUT_MS = 3000;
|
||||
constexpr uint8_t ACK_REPEAT_COUNT = 3;
|
||||
constexpr uint32_t ACK_REPEAT_DELAY_MS = 200;
|
||||
constexpr uint8_t BATCH_MAX_RETRIES = 2;
|
||||
constexpr uint8_t METER_BATCH_MAX_SAMPLES = 30;
|
||||
constexpr uint8_t BATCH_QUEUE_DEPTH = 10;
|
||||
constexpr BatchRetryPolicy BATCH_RETRY_POLICY = BatchRetryPolicy::Keep;
|
||||
constexpr uint32_t WATCHDOG_TIMEOUT_SEC = 120;
|
||||
constexpr bool ENABLE_HA_DISCOVERY = true;
|
||||
#ifndef SERIAL_DEBUG_MODE_FLAG
|
||||
#define SERIAL_DEBUG_MODE_FLAG 0
|
||||
#endif
|
||||
constexpr bool SERIAL_DEBUG_MODE = SERIAL_DEBUG_MODE_FLAG != 0;
|
||||
constexpr bool SERIAL_DEBUG_DUMP_JSON = false;
|
||||
constexpr bool LORA_SEND_BYPASS = false;
|
||||
constexpr bool ENABLE_SD_LOGGING = true;
|
||||
constexpr uint8_t PIN_SD_CS = 13;
|
||||
constexpr uint8_t PIN_SD_MOSI = 15;
|
||||
constexpr uint8_t PIN_SD_MISO = 2;
|
||||
constexpr uint8_t PIN_SD_SCK = 14;
|
||||
constexpr uint16_t SD_HISTORY_MAX_DAYS = 30;
|
||||
constexpr uint16_t SD_HISTORY_MIN_RES_MIN = 1;
|
||||
constexpr uint16_t SD_HISTORY_MAX_BINS = 4000;
|
||||
constexpr uint16_t SD_HISTORY_TIME_BUDGET_MS = 10;
|
||||
constexpr const char *TIMEZONE_TZ = "CET-1CEST,M3.5.0/2,M10.5.0/3";
|
||||
constexpr const char *AP_SSID_PREFIX = "DD3-Bridge-";
|
||||
constexpr const char *AP_PASSWORD = "changeme123";
|
||||
constexpr bool WEB_AUTH_REQUIRE_STA = true;
|
||||
constexpr bool WEB_AUTH_REQUIRE_AP = false;
|
||||
constexpr const char *WEB_AUTH_DEFAULT_USER = "admin";
|
||||
constexpr const char *WEB_AUTH_DEFAULT_PASS = "admin";
|
||||
|
||||
constexpr uint8_t NUM_SENDERS = 1;
|
||||
constexpr uint32_t MIN_ACCEPTED_EPOCH_UTC = 1769904000UL; // 2026-02-01 00:00:00 UTC
|
||||
inline constexpr uint16_t EXPECTED_SENDER_IDS[NUM_SENDERS] = {
|
||||
0xF19C //433mhz sender
|
||||
//0x7EB4 //868mhz sender
|
||||
|
||||
@@ -9,6 +9,15 @@ enum class FaultType : uint8_t {
|
||||
LoraTx = 3
|
||||
};
|
||||
|
||||
enum class RxRejectReason : uint8_t {
|
||||
None = 0,
|
||||
CrcFail = 1,
|
||||
InvalidMsgKind = 2,
|
||||
LengthMismatch = 3,
|
||||
DeviceIdMismatch = 4,
|
||||
BatchIdMismatch = 5
|
||||
};
|
||||
|
||||
struct FaultCounters {
|
||||
uint32_t meter_read_fail;
|
||||
uint32_t decode_fail;
|
||||
@@ -22,7 +31,6 @@ struct MeterData {
|
||||
float energy_total_kwh;
|
||||
float phase_power_w[3];
|
||||
float total_power_w;
|
||||
float phase_voltage_v[3];
|
||||
float battery_voltage_v;
|
||||
uint8_t battery_percent;
|
||||
bool valid;
|
||||
@@ -33,6 +41,7 @@ struct MeterData {
|
||||
uint32_t err_decode;
|
||||
uint32_t err_lora_tx;
|
||||
FaultType last_error;
|
||||
uint8_t rx_reject_reason;
|
||||
};
|
||||
|
||||
struct SenderStatus {
|
||||
@@ -42,3 +51,4 @@ struct SenderStatus {
|
||||
};
|
||||
|
||||
void init_device_ids(uint16_t &short_id, char *device_id, size_t device_id_len);
|
||||
const char *rx_reject_reason_text(RxRejectReason reason);
|
||||
|
||||
@@ -11,6 +11,8 @@ void display_set_sender_statuses(const SenderStatus *statuses, uint8_t count);
|
||||
void display_set_last_meter(const MeterData &data);
|
||||
void display_set_last_read(bool ok, uint32_t ts_utc);
|
||||
void display_set_last_tx(bool ok, uint32_t ts_utc);
|
||||
void display_set_sender_queue(uint8_t depth, bool build_pending);
|
||||
void display_set_sender_batches(uint16_t last_acked_batch_id, uint16_t current_batch_id);
|
||||
void display_set_last_error(FaultType type, uint32_t ts_utc, uint32_t ts_ms);
|
||||
void display_set_receiver_status(bool ap_mode, const char *ssid, bool mqtt_ok);
|
||||
void display_power_down();
|
||||
|
||||
7
include/html_util.h
Normal file
7
include/html_util.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
String html_escape(const String &input);
|
||||
String url_encode_component(const String &input);
|
||||
bool sanitize_device_id(const String &input, String &out_device_id);
|
||||
@@ -4,6 +4,3 @@
|
||||
#include "data_model.h"
|
||||
|
||||
bool meterDataToJson(const MeterData &data, String &out_json);
|
||||
bool jsonToMeterData(const String &json, MeterData &data);
|
||||
bool meterBatchToJson(const MeterData *samples, size_t count, String &out_json, const FaultCounters *faults = nullptr, FaultType last_error = FaultType::None);
|
||||
bool jsonToMeterBatch(const String &json, MeterData *out_samples, size_t max_count, size_t &out_count);
|
||||
|
||||
@@ -2,14 +2,26 @@
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "config.h"
|
||||
#include "data_model.h"
|
||||
|
||||
constexpr size_t LORA_MAX_PAYLOAD = 200;
|
||||
constexpr size_t LORA_MAX_PAYLOAD = 230;
|
||||
constexpr size_t LORA_FRAME_HEADER_LEN = 3; // msg_kind + dev_id_short
|
||||
constexpr size_t LORA_FRAME_CRC_LEN = 2;
|
||||
constexpr size_t LORA_ACK_DOWN_PAYLOAD_LEN = 7; // flags(1) + batch_id(2) + epoch_utc(4)
|
||||
static_assert(LORA_ACK_DOWN_PAYLOAD_LEN == 7, "ACK_DOWN payload must remain 7 bytes");
|
||||
|
||||
constexpr size_t lora_frame_size(size_t payload_len) {
|
||||
return LORA_FRAME_HEADER_LEN + payload_len + LORA_FRAME_CRC_LEN;
|
||||
}
|
||||
|
||||
enum class LoraMsgKind : uint8_t {
|
||||
BatchUp = 0,
|
||||
AckDown = 1
|
||||
};
|
||||
|
||||
struct LoraPacket {
|
||||
uint8_t protocol_version;
|
||||
DeviceRole role;
|
||||
LoraMsgKind msg_kind;
|
||||
uint16_t device_id_short;
|
||||
PayloadType payload_type;
|
||||
uint8_t payload[LORA_MAX_PAYLOAD];
|
||||
size_t payload_len;
|
||||
int16_t rssi_dbm;
|
||||
@@ -19,4 +31,10 @@ struct LoraPacket {
|
||||
void lora_init();
|
||||
bool lora_send(const LoraPacket &pkt);
|
||||
bool lora_receive(LoraPacket &pkt, uint32_t timeout_ms);
|
||||
RxRejectReason lora_get_last_rx_reject_reason();
|
||||
bool lora_get_last_rx_signal(int16_t &rssi_dbm, float &snr_db);
|
||||
void lora_idle();
|
||||
void lora_sleep();
|
||||
void lora_receive_continuous();
|
||||
bool lora_receive_window(LoraPacket &pkt, uint32_t timeout_ms);
|
||||
uint32_t lora_airtime_ms(size_t packet_len);
|
||||
|
||||
@@ -5,3 +5,5 @@
|
||||
|
||||
void meter_init();
|
||||
bool meter_read(MeterData &data);
|
||||
bool meter_poll_frame(const char *&frame, size_t &len);
|
||||
bool meter_parse_frame(const char *frame, size_t len, MeterData &data);
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
void power_sender_init();
|
||||
void power_receiver_init();
|
||||
void power_configure_unused_pins_sender();
|
||||
void read_battery(MeterData &data);
|
||||
uint8_t battery_percent_from_voltage(float voltage_v);
|
||||
void light_sleep_ms(uint32_t ms);
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
bool rtc_ds3231_init();
|
||||
bool rtc_ds3231_is_present();
|
||||
bool rtc_ds3231_read_epoch(uint32_t &epoch_utc);
|
||||
bool rtc_ds3231_set_epoch(uint32_t epoch_utc);
|
||||
@@ -1,17 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
#include "lora_transport.h"
|
||||
|
||||
void time_receiver_init(const char *ntp_server_1, const char *ntp_server_2);
|
||||
uint32_t time_get_utc();
|
||||
bool time_is_synced();
|
||||
void time_set_utc(uint32_t epoch);
|
||||
bool time_send_timesync(uint16_t device_id_short);
|
||||
bool time_handle_timesync_payload(const uint8_t *payload, size_t len);
|
||||
void time_get_local_hhmm(char *out, size_t out_len);
|
||||
void time_rtc_init();
|
||||
bool time_try_load_from_rtc();
|
||||
bool time_rtc_present();
|
||||
uint32_t time_get_last_sync_utc();
|
||||
uint32_t time_get_last_sync_age_sec();
|
||||
|
||||
@@ -7,4 +7,6 @@
|
||||
void web_server_begin_ap(const SenderStatus *statuses, uint8_t count);
|
||||
void web_server_begin_sta(const SenderStatus *statuses, uint8_t count);
|
||||
void web_server_set_config(const WifiMqttConfig &config);
|
||||
void web_server_set_sender_faults(const FaultCounters *faults, const FaultType *last_errors);
|
||||
void web_server_set_last_batch(uint8_t sender_index, const MeterData *samples, size_t count);
|
||||
void web_server_loop();
|
||||
|
||||
@@ -12,6 +12,8 @@ struct WifiMqttConfig {
|
||||
String mqtt_pass;
|
||||
String ntp_server_1;
|
||||
String ntp_server_2;
|
||||
String web_user;
|
||||
String web_pass;
|
||||
bool valid;
|
||||
};
|
||||
|
||||
|
||||
@@ -9,18 +9,7 @@
|
||||
; https://docs.platformio.org/page/projectconf.html
|
||||
|
||||
[env:lilygo-t3-v1-6-1]
|
||||
platform = espressif32
|
||||
board = ttgo-lora32-v1
|
||||
framework = arduino
|
||||
lib_deps =
|
||||
sandeepmistry/LoRa@^0.8.0
|
||||
bblanchon/ArduinoJson@^6.21.5
|
||||
adafruit/Adafruit SSD1306@^2.5.9
|
||||
adafruit/Adafruit GFX Library@^1.11.9
|
||||
knolleary/PubSubClient@^2.8
|
||||
|
||||
[env:lilygo-t3-v1-6-1-test]
|
||||
platform = espressif32
|
||||
platform = https://github.com/pioarduino/platform-espressif32/releases/download/51.03.07/platform-espressif32.zip
|
||||
board = ttgo-lora32-v1
|
||||
framework = arduino
|
||||
lib_deps =
|
||||
@@ -30,10 +19,24 @@ lib_deps =
|
||||
adafruit/Adafruit GFX Library@^1.11.9
|
||||
knolleary/PubSubClient@^2.8
|
||||
build_flags =
|
||||
-DSERIAL_DEBUG_MODE_FLAG=1
|
||||
|
||||
[env:lilygo-t3-v1-6-1-test]
|
||||
platform = https://github.com/pioarduino/platform-espressif32/releases/download/51.03.07/platform-espressif32.zip
|
||||
board = ttgo-lora32-v1
|
||||
framework = arduino
|
||||
lib_deps =
|
||||
sandeepmistry/LoRa@^0.8.0
|
||||
bblanchon/ArduinoJson@^6.21.5
|
||||
adafruit/Adafruit SSD1306@^2.5.9
|
||||
adafruit/Adafruit GFX Library@^1.11.9
|
||||
knolleary/PubSubClient@^2.8
|
||||
build_flags =
|
||||
-DSERIAL_DEBUG_MODE_FLAG=1
|
||||
-DENABLE_TEST_MODE
|
||||
|
||||
[env:lilygo-t3-v1-6-1-868]
|
||||
platform = espressif32
|
||||
platform = https://github.com/pioarduino/platform-espressif32/releases/download/51.03.07/platform-espressif32.zip
|
||||
board = ttgo-lora32-v1
|
||||
framework = arduino
|
||||
lib_deps =
|
||||
@@ -43,10 +46,11 @@ lib_deps =
|
||||
adafruit/Adafruit GFX Library@^1.11.9
|
||||
knolleary/PubSubClient@^2.8
|
||||
build_flags =
|
||||
-DSERIAL_DEBUG_MODE_FLAG=1
|
||||
-DLORA_FREQUENCY_HZ=868E6
|
||||
|
||||
[env:lilygo-t3-v1-6-1-868-test]
|
||||
platform = espressif32
|
||||
platform = https://github.com/pioarduino/platform-espressif32/releases/download/51.03.07/platform-espressif32.zip
|
||||
board = ttgo-lora32-v1
|
||||
framework = arduino
|
||||
lib_deps =
|
||||
@@ -56,5 +60,62 @@ lib_deps =
|
||||
adafruit/Adafruit GFX Library@^1.11.9
|
||||
knolleary/PubSubClient@^2.8
|
||||
build_flags =
|
||||
-DSERIAL_DEBUG_MODE_FLAG=1
|
||||
-DENABLE_TEST_MODE
|
||||
-DLORA_FREQUENCY_HZ=868E6
|
||||
|
||||
[env:lilygo-t3-v1-6-1-payload-test]
|
||||
platform = https://github.com/pioarduino/platform-espressif32/releases/download/51.03.07/platform-espressif32.zip
|
||||
board = ttgo-lora32-v1
|
||||
framework = arduino
|
||||
lib_deps =
|
||||
sandeepmistry/LoRa@^0.8.0
|
||||
bblanchon/ArduinoJson@^6.21.5
|
||||
adafruit/Adafruit SSD1306@^2.5.9
|
||||
adafruit/Adafruit GFX Library@^1.11.9
|
||||
knolleary/PubSubClient@^2.8
|
||||
build_flags =
|
||||
-DSERIAL_DEBUG_MODE_FLAG=1
|
||||
-DPAYLOAD_CODEC_TEST
|
||||
|
||||
[env:lilygo-t3-v1-6-1-868-payload-test]
|
||||
platform = https://github.com/pioarduino/platform-espressif32/releases/download/51.03.07/platform-espressif32.zip
|
||||
board = ttgo-lora32-v1
|
||||
framework = arduino
|
||||
lib_deps =
|
||||
sandeepmistry/LoRa@^0.8.0
|
||||
bblanchon/ArduinoJson@^6.21.5
|
||||
adafruit/Adafruit SSD1306@^2.5.9
|
||||
adafruit/Adafruit GFX Library@^1.11.9
|
||||
knolleary/PubSubClient@^2.8
|
||||
build_flags =
|
||||
-DSERIAL_DEBUG_MODE_FLAG=1
|
||||
-DPAYLOAD_CODEC_TEST
|
||||
-DLORA_FREQUENCY_HZ=868E6
|
||||
|
||||
[env:lilygo-t3-v1-6-1-prod]
|
||||
platform = https://github.com/pioarduino/platform-espressif32/releases/download/51.03.07/platform-espressif32.zip
|
||||
board = ttgo-lora32-v1
|
||||
framework = arduino
|
||||
lib_deps =
|
||||
sandeepmistry/LoRa@^0.8.0
|
||||
bblanchon/ArduinoJson@^6.21.5
|
||||
adafruit/Adafruit SSD1306@^2.5.9
|
||||
adafruit/Adafruit GFX Library@^1.11.9
|
||||
knolleary/PubSubClient@^2.8
|
||||
build_flags =
|
||||
-DSERIAL_DEBUG_MODE_FLAG=0
|
||||
|
||||
[env:lilygo-t3-v1-6-1-868-prod]
|
||||
platform = https://github.com/pioarduino/platform-espressif32/releases/download/51.03.07/platform-espressif32.zip
|
||||
board = ttgo-lora32-v1
|
||||
framework = arduino
|
||||
lib_deps =
|
||||
sandeepmistry/LoRa@^0.8.0
|
||||
bblanchon/ArduinoJson@^6.21.5
|
||||
adafruit/Adafruit SSD1306@^2.5.9
|
||||
adafruit/Adafruit GFX Library@^1.11.9
|
||||
knolleary/PubSubClient@^2.8
|
||||
build_flags =
|
||||
-DSERIAL_DEBUG_MODE_FLAG=0
|
||||
-DLORA_FREQUENCY_HZ=868E6
|
||||
|
||||
@@ -1,71 +0,0 @@
|
||||
#include "compressor.h"
|
||||
|
||||
static constexpr uint8_t RLE_MARKER = 0xFF;
|
||||
|
||||
bool compressBuffer(const uint8_t *in, size_t in_len, uint8_t *out, size_t out_max, size_t &out_len) {
|
||||
out_len = 0;
|
||||
if (!in || !out) {
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t i = 0;
|
||||
while (i < in_len) {
|
||||
uint8_t value = in[i];
|
||||
size_t run = 1;
|
||||
while (i + run < in_len && in[i + run] == value && run < 255) {
|
||||
run++;
|
||||
}
|
||||
|
||||
if (value == RLE_MARKER || run >= 4) {
|
||||
if (out_len + 3 > out_max) {
|
||||
return false;
|
||||
}
|
||||
out[out_len++] = RLE_MARKER;
|
||||
out[out_len++] = static_cast<uint8_t>(run);
|
||||
out[out_len++] = value;
|
||||
} else {
|
||||
if (out_len + run > out_max) {
|
||||
return false;
|
||||
}
|
||||
for (size_t j = 0; j < run; ++j) {
|
||||
out[out_len++] = value;
|
||||
}
|
||||
}
|
||||
|
||||
i += run;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool decompressBuffer(const uint8_t *in, size_t in_len, uint8_t *out, size_t out_max, size_t &out_len) {
|
||||
out_len = 0;
|
||||
if (!in || !out) {
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t i = 0;
|
||||
while (i < in_len) {
|
||||
uint8_t value = in[i++];
|
||||
if (value == RLE_MARKER) {
|
||||
if (i + 1 >= in_len) {
|
||||
return false;
|
||||
}
|
||||
uint8_t run = in[i++];
|
||||
uint8_t data = in[i++];
|
||||
if (out_len + run > out_max) {
|
||||
return false;
|
||||
}
|
||||
for (uint8_t j = 0; j < run; ++j) {
|
||||
out[out_len++] = data;
|
||||
}
|
||||
} else {
|
||||
if (out_len + 1 > out_max) {
|
||||
return false;
|
||||
}
|
||||
out[out_len++] = value;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -2,5 +2,5 @@
|
||||
|
||||
DeviceRole detect_role() {
|
||||
pinMode(PIN_ROLE, INPUT_PULLDOWN);
|
||||
return digitalRead(PIN_ROLE) == HIGH ? DeviceRole::Receiver : DeviceRole::Sender;
|
||||
return digitalRead(PIN_ROLE) == HIGH ? DeviceRole::Sender : DeviceRole::Receiver;
|
||||
}
|
||||
|
||||
@@ -8,3 +8,20 @@ void init_device_ids(uint16_t &short_id, char *device_id, size_t device_id_len)
|
||||
short_id = (static_cast<uint16_t>(mac[4]) << 8) | mac[5];
|
||||
snprintf(device_id, device_id_len, "dd3-%04X", short_id);
|
||||
}
|
||||
|
||||
const char *rx_reject_reason_text(RxRejectReason reason) {
|
||||
switch (reason) {
|
||||
case RxRejectReason::CrcFail:
|
||||
return "crc_fail";
|
||||
case RxRejectReason::InvalidMsgKind:
|
||||
return "invalid_msg_kind";
|
||||
case RxRejectReason::LengthMismatch:
|
||||
return "length_mismatch";
|
||||
case RxRejectReason::DeviceIdMismatch:
|
||||
return "device_id_mismatch";
|
||||
case RxRejectReason::BatchIdMismatch:
|
||||
return "batch_id_mismatch";
|
||||
default:
|
||||
return "none";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
#include <Wire.h>
|
||||
#include <Adafruit_GFX.h>
|
||||
#include <Adafruit_SSD1306.h>
|
||||
#include <limits.h>
|
||||
#include <math.h>
|
||||
#include <time.h>
|
||||
|
||||
static Adafruit_SSD1306 display(OLED_WIDTH, OLED_HEIGHT, &Wire, -1);
|
||||
@@ -19,6 +21,10 @@ static uint32_t g_last_read_ts = 0;
|
||||
static uint32_t g_last_tx_ts = 0;
|
||||
static uint32_t g_last_read_ms = 0;
|
||||
static uint32_t g_last_tx_ms = 0;
|
||||
static uint8_t g_sender_queue_depth = 0;
|
||||
static bool g_sender_build_pending = false;
|
||||
static uint16_t g_sender_last_acked_batch_id = 0;
|
||||
static uint16_t g_sender_current_batch_id = 0;
|
||||
static FaultType g_last_error = FaultType::None;
|
||||
static uint32_t g_last_error_ts = 0;
|
||||
static uint32_t g_last_error_ms = 0;
|
||||
@@ -32,10 +38,9 @@ static bool g_mqtt_ok = false;
|
||||
|
||||
static bool g_oled_on = true;
|
||||
static bool g_prev_ctrl_high = false;
|
||||
static uint32_t g_oled_off_start = 0;
|
||||
static uint32_t g_last_page_ms = 0;
|
||||
static uint8_t g_page = 0;
|
||||
static uint32_t g_boot_ms = 0;
|
||||
static uint32_t g_last_activity_ms = 0;
|
||||
static bool g_display_ready = false;
|
||||
static uint32_t g_last_init_attempt_ms = 0;
|
||||
static bool g_last_oled_on = true;
|
||||
@@ -65,7 +70,9 @@ void display_power_down() {
|
||||
}
|
||||
|
||||
void display_init() {
|
||||
pinMode(PIN_OLED_CTRL, INPUT_PULLDOWN);
|
||||
if (g_role == DeviceRole::Sender) {
|
||||
pinMode(PIN_OLED_CTRL, INPUT_PULLDOWN);
|
||||
}
|
||||
Wire.begin(PIN_OLED_SDA, PIN_OLED_SCL);
|
||||
Wire.setClock(100000);
|
||||
g_display_ready = display.begin(SSD1306_SWITCHCAPVCC, OLED_I2C_ADDR);
|
||||
@@ -77,7 +84,7 @@ void display_init() {
|
||||
display.display();
|
||||
}
|
||||
g_last_init_attempt_ms = millis();
|
||||
g_boot_ms = millis();
|
||||
g_last_activity_ms = millis();
|
||||
}
|
||||
|
||||
void display_set_role(DeviceRole role) {
|
||||
@@ -111,6 +118,16 @@ void display_set_last_tx(bool ok, uint32_t ts_utc) {
|
||||
g_last_tx_ms = millis();
|
||||
}
|
||||
|
||||
void display_set_sender_queue(uint8_t depth, bool build_pending) {
|
||||
g_sender_queue_depth = depth;
|
||||
g_sender_build_pending = build_pending;
|
||||
}
|
||||
|
||||
void display_set_sender_batches(uint16_t last_acked_batch_id, uint16_t current_batch_id) {
|
||||
g_sender_last_acked_batch_id = last_acked_batch_id;
|
||||
g_sender_current_batch_id = current_batch_id;
|
||||
}
|
||||
|
||||
void display_set_last_error(FaultType type, uint32_t ts_utc, uint32_t ts_ms) {
|
||||
g_last_error = type;
|
||||
g_last_error_ts = ts_utc;
|
||||
@@ -146,6 +163,20 @@ static uint32_t age_seconds(uint32_t ts_utc, uint32_t ts_ms) {
|
||||
return (millis() - ts_ms) / 1000;
|
||||
}
|
||||
|
||||
static int32_t round_power_w(float value) {
|
||||
if (isnan(value)) {
|
||||
return 0;
|
||||
}
|
||||
long rounded = lroundf(value);
|
||||
if (rounded > INT32_MAX) {
|
||||
return INT32_MAX;
|
||||
}
|
||||
if (rounded < INT32_MIN) {
|
||||
return INT32_MIN;
|
||||
}
|
||||
return static_cast<int32_t>(rounded);
|
||||
}
|
||||
|
||||
static bool render_last_error_line(uint8_t y) {
|
||||
if (g_last_error == FaultType::None) {
|
||||
return false;
|
||||
@@ -195,7 +226,13 @@ static void render_sender_status() {
|
||||
display.printf("Read %s %lus ago", g_last_read_ok ? "OK" : "ERR", static_cast<unsigned long>(age_seconds(g_last_read_ts, g_last_read_ms)));
|
||||
|
||||
display.setCursor(0, 36);
|
||||
display.printf("TX %s %lus ago", g_last_tx_ok ? "OK" : "ERR", static_cast<unsigned long>(age_seconds(g_last_tx_ts, g_last_tx_ms)));
|
||||
display.printf("TX %s %lus Q%u%s A%u C%u",
|
||||
g_last_tx_ok ? "OK" : "ERR",
|
||||
static_cast<unsigned long>(age_seconds(g_last_tx_ts, g_last_tx_ms)),
|
||||
g_sender_queue_depth,
|
||||
g_sender_build_pending ? "+" : "",
|
||||
g_sender_last_acked_batch_id,
|
||||
g_sender_current_batch_id);
|
||||
|
||||
#ifdef ENABLE_TEST_MODE
|
||||
if (strlen(g_test_code) > 0) {
|
||||
@@ -215,15 +252,15 @@ static void render_sender_status() {
|
||||
static void render_sender_measurement() {
|
||||
display.clearDisplay();
|
||||
display.setCursor(0, 0);
|
||||
display.printf("E %.1f kWh", g_last_meter.energy_total_kwh);
|
||||
display.printf("E %.2f kWh", g_last_meter.energy_total_kwh);
|
||||
display.setCursor(0, 12);
|
||||
display.printf("P %.0fW", g_last_meter.total_power_w);
|
||||
display.printf("P %dW", static_cast<int>(round_power_w(g_last_meter.total_power_w)));
|
||||
display.setCursor(0, 24);
|
||||
display.printf("L1 %.0fV %.0fW", g_last_meter.phase_voltage_v[0], g_last_meter.phase_power_w[0]);
|
||||
display.printf("L1 %dW", static_cast<int>(round_power_w(g_last_meter.phase_power_w[0])));
|
||||
display.setCursor(0, 36);
|
||||
display.printf("L2 %.0fV %.0fW", g_last_meter.phase_voltage_v[1], g_last_meter.phase_power_w[1]);
|
||||
display.printf("L2 %dW", static_cast<int>(round_power_w(g_last_meter.phase_power_w[1])));
|
||||
display.setCursor(0, 48);
|
||||
display.printf("L3 %.0fV %.0fW", g_last_meter.phase_voltage_v[2], g_last_meter.phase_power_w[2]);
|
||||
display.printf("L3 %dW", static_cast<int>(round_power_w(g_last_meter.phase_power_w[2])));
|
||||
display.display();
|
||||
}
|
||||
|
||||
@@ -248,16 +285,10 @@ static void render_receiver_status() {
|
||||
display.printf("Time: %s", time_buf);
|
||||
|
||||
uint32_t latest = 0;
|
||||
bool link_valid = false;
|
||||
int16_t link_rssi = 0;
|
||||
float link_snr = 0.0f;
|
||||
if (g_statuses) {
|
||||
for (uint8_t i = 0; i < g_status_count; ++i) {
|
||||
if (g_statuses[i].has_data && g_statuses[i].last_update_ts_utc > latest) {
|
||||
latest = g_statuses[i].last_update_ts_utc;
|
||||
link_valid = g_statuses[i].last_data.link_valid;
|
||||
link_rssi = g_statuses[i].last_data.link_rssi_dbm;
|
||||
link_snr = g_statuses[i].last_data.link_snr_db;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -271,9 +302,6 @@ static void render_receiver_status() {
|
||||
localtime_r(&t, &timeinfo);
|
||||
display.printf("Upd %02d:%02d", timeinfo.tm_hour, timeinfo.tm_min);
|
||||
}
|
||||
if (link_valid) {
|
||||
display.printf(" R:%d S:%.1f", link_rssi, link_snr);
|
||||
}
|
||||
|
||||
render_last_error_line(56);
|
||||
display.display();
|
||||
@@ -292,7 +320,15 @@ static void render_receiver_sender(uint8_t index) {
|
||||
display.setCursor(0, 0);
|
||||
uint8_t bat = status.has_data ? status.last_data.battery_percent : 0;
|
||||
if (status.has_data) {
|
||||
display.printf("%s B%u", status.last_data.device_id, bat);
|
||||
const char *device_id = status.last_data.device_id;
|
||||
if (strlen(device_id) >= 4 && strncmp(device_id, "dd3-", 4) == 0) {
|
||||
device_id += 4;
|
||||
}
|
||||
if (status.last_data.link_valid) {
|
||||
display.printf("%s R:%d S:%.1f", device_id, status.last_data.link_rssi_dbm, status.last_data.link_snr_db);
|
||||
} else {
|
||||
display.printf("%s B%u", device_id, bat);
|
||||
}
|
||||
} else {
|
||||
display.printf("%s B--", status.last_data.device_id);
|
||||
}
|
||||
@@ -314,19 +350,28 @@ static void render_receiver_sender(uint8_t index) {
|
||||
#endif
|
||||
|
||||
display.setCursor(0, 12);
|
||||
display.printf("E %.1f kWh", status.last_data.energy_total_kwh);
|
||||
display.setCursor(0, 24);
|
||||
display.printf("P %.0fW", status.last_data.total_power_w);
|
||||
display.setCursor(0, 36);
|
||||
display.printf("L1 %.0fV %.0fW", status.last_data.phase_voltage_v[0], status.last_data.phase_power_w[0]);
|
||||
display.setCursor(0, 48);
|
||||
display.printf("L2 %.0fV %.0fW", status.last_data.phase_voltage_v[1], status.last_data.phase_power_w[1]);
|
||||
display.setCursor(0, 56);
|
||||
if (status.last_data.link_valid) {
|
||||
display.printf("R:%d S:%.1f", status.last_data.link_rssi_dbm, status.last_data.link_snr_db);
|
||||
} else {
|
||||
display.printf("L3 %.0fV %.0fW", status.last_data.phase_voltage_v[2], status.last_data.phase_power_w[2]);
|
||||
display.printf("E %.2f kWh", status.last_data.energy_total_kwh);
|
||||
display.setCursor(0, 22);
|
||||
display.printf("L1 %dW", static_cast<int>(round_power_w(status.last_data.phase_power_w[0])));
|
||||
display.setCursor(0, 32);
|
||||
display.printf("L2 %dW", static_cast<int>(round_power_w(status.last_data.phase_power_w[1])));
|
||||
display.setCursor(0, 42);
|
||||
display.printf("L3 %dW", static_cast<int>(round_power_w(status.last_data.phase_power_w[2])));
|
||||
display.setCursor(0, 52);
|
||||
display.print("P");
|
||||
char p_buf[16];
|
||||
snprintf(p_buf, sizeof(p_buf), "%dW", static_cast<int>(round_power_w(status.last_data.total_power_w)));
|
||||
int16_t x1 = 0;
|
||||
int16_t y1 = 0;
|
||||
uint16_t w = 0;
|
||||
uint16_t h = 0;
|
||||
display.getTextBounds(p_buf, 0, 0, &x1, &y1, &w, &h);
|
||||
int16_t x = static_cast<int16_t>(display.width() - w);
|
||||
if (x < 0) {
|
||||
x = 0;
|
||||
}
|
||||
display.setCursor(x, 52);
|
||||
display.print(p_buf);
|
||||
display.display();
|
||||
}
|
||||
|
||||
@@ -345,26 +390,21 @@ void display_tick() {
|
||||
}
|
||||
return;
|
||||
}
|
||||
bool ctrl_high = digitalRead(PIN_OLED_CTRL) == HIGH;
|
||||
bool ctrl_high = false;
|
||||
if (g_role == DeviceRole::Sender) {
|
||||
ctrl_high = digitalRead(PIN_OLED_CTRL) == HIGH;
|
||||
}
|
||||
|
||||
bool in_boot_window = (millis() - g_boot_ms) < OLED_AUTO_OFF_MS;
|
||||
if (in_boot_window) {
|
||||
uint32_t now_ms = millis();
|
||||
bool ctrl_falling_edge = g_prev_ctrl_high && !ctrl_high;
|
||||
if (g_role == DeviceRole::Receiver) {
|
||||
g_oled_on = true;
|
||||
g_last_activity_ms = now_ms;
|
||||
} else {
|
||||
if (ctrl_high) {
|
||||
g_oled_on = true;
|
||||
g_oled_off_start = 0;
|
||||
} else if (g_prev_ctrl_high && !ctrl_high) {
|
||||
g_oled_off_start = millis();
|
||||
} else if (!g_prev_ctrl_high && !ctrl_high && g_oled_off_start == 0) {
|
||||
g_oled_off_start = millis();
|
||||
if (ctrl_high || ctrl_falling_edge) {
|
||||
g_last_activity_ms = now_ms;
|
||||
}
|
||||
|
||||
if (!ctrl_high && g_oled_off_start > 0 && millis() - g_oled_off_start > OLED_AUTO_OFF_MS) {
|
||||
g_oled_on = false;
|
||||
}
|
||||
|
||||
// fall through to power gating below
|
||||
g_oled_on = (now_ms - g_last_activity_ms) < OLED_AUTO_OFF_MS;
|
||||
}
|
||||
|
||||
if (g_oled_on) {
|
||||
|
||||
98
src/html_util.cpp
Normal file
98
src/html_util.cpp
Normal file
@@ -0,0 +1,98 @@
|
||||
#include "html_util.h"
|
||||
|
||||
String html_escape(const String &input) {
|
||||
String out;
|
||||
out.reserve(input.length() + 8);
|
||||
for (size_t i = 0; i < input.length(); ++i) {
|
||||
char c = input[i];
|
||||
switch (c) {
|
||||
case '&':
|
||||
out += "&";
|
||||
break;
|
||||
case '<':
|
||||
out += "<";
|
||||
break;
|
||||
case '>':
|
||||
out += ">";
|
||||
break;
|
||||
case '"':
|
||||
out += """;
|
||||
break;
|
||||
case '\'':
|
||||
out += "'";
|
||||
break;
|
||||
default:
|
||||
out += c;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
String url_encode_component(const String &input) {
|
||||
String out;
|
||||
out.reserve(input.length() * 3);
|
||||
const char *hex = "0123456789ABCDEF";
|
||||
for (size_t i = 0; i < input.length(); ++i) {
|
||||
unsigned char c = static_cast<unsigned char>(input[i]);
|
||||
bool safe = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
|
||||
(c >= '0' && c <= '9') || c == '-' || c == '_' || c == '.' || c == '~';
|
||||
if (safe) {
|
||||
out += static_cast<char>(c);
|
||||
} else {
|
||||
out += '%';
|
||||
out += hex[(c >> 4) & 0x0F];
|
||||
out += hex[c & 0x0F];
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
static bool is_hex_char(char c) {
|
||||
return (c >= '0' && c <= '9') ||
|
||||
(c >= 'a' && c <= 'f') ||
|
||||
(c >= 'A' && c <= 'F');
|
||||
}
|
||||
|
||||
static String to_upper_hex4(const String &input) {
|
||||
String out = input;
|
||||
out.toUpperCase();
|
||||
return out;
|
||||
}
|
||||
|
||||
bool sanitize_device_id(const String &input, String &out_device_id) {
|
||||
String trimmed = input;
|
||||
trimmed.trim();
|
||||
if (trimmed.length() == 0) {
|
||||
return false;
|
||||
}
|
||||
if (trimmed.indexOf('/') >= 0 || trimmed.indexOf('\\') >= 0 || trimmed.indexOf("..") >= 0) {
|
||||
return false;
|
||||
}
|
||||
if (trimmed.indexOf('%') >= 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (trimmed.length() == 4) {
|
||||
for (size_t i = 0; i < 4; ++i) {
|
||||
if (!is_hex_char(trimmed[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
out_device_id = String("dd3-") + to_upper_hex4(trimmed);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (trimmed.length() == 8 && trimmed.startsWith("dd3-")) {
|
||||
String hex = trimmed.substring(4);
|
||||
for (size_t i = 0; i < 4; ++i) {
|
||||
if (!is_hex_char(hex[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
out_device_id = String("dd3-") + to_upper_hex4(hex);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
#include "json_codec.h"
|
||||
#include <ArduinoJson.h>
|
||||
#include <limits.h>
|
||||
#include <math.h>
|
||||
#include "power_manager.h"
|
||||
|
||||
static constexpr size_t STATE_JSON_DOC_CAPACITY = 512;
|
||||
|
||||
static float round2(float value) {
|
||||
if (isnan(value)) {
|
||||
@@ -10,6 +12,20 @@ static float round2(float value) {
|
||||
return roundf(value * 100.0f) / 100.0f;
|
||||
}
|
||||
|
||||
static int32_t round_to_i32(float value) {
|
||||
if (isnan(value)) {
|
||||
return 0;
|
||||
}
|
||||
long rounded = lroundf(value);
|
||||
if (rounded > INT32_MAX) {
|
||||
return INT32_MAX;
|
||||
}
|
||||
if (rounded < INT32_MIN) {
|
||||
return INT32_MIN;
|
||||
}
|
||||
return static_cast<int32_t>(rounded);
|
||||
}
|
||||
|
||||
static const char *short_id_from_device_id(const char *device_id) {
|
||||
if (!device_id) {
|
||||
return "";
|
||||
@@ -32,27 +48,28 @@ static void format_float_2(char *buf, size_t buf_len, float value) {
|
||||
snprintf(buf, buf_len, "%.2f", round2(value));
|
||||
}
|
||||
|
||||
static void set_int_or_null(JsonDocument &doc, const char *key, float value) {
|
||||
if (!key || key[0] == '\0') {
|
||||
return;
|
||||
}
|
||||
if (isnan(value)) {
|
||||
doc[key] = nullptr;
|
||||
return;
|
||||
}
|
||||
doc[key] = round_to_i32(value);
|
||||
}
|
||||
|
||||
bool meterDataToJson(const MeterData &data, String &out_json) {
|
||||
StaticJsonDocument<256> doc;
|
||||
StaticJsonDocument<STATE_JSON_DOC_CAPACITY> doc;
|
||||
doc["id"] = short_id_from_device_id(data.device_id);
|
||||
doc["ts"] = data.ts_utc;
|
||||
char buf[16];
|
||||
format_float_2(buf, sizeof(buf), data.energy_total_kwh);
|
||||
doc["e_kwh"] = serialized(buf);
|
||||
format_float_2(buf, sizeof(buf), data.total_power_w);
|
||||
doc["p_w"] = serialized(buf);
|
||||
format_float_2(buf, sizeof(buf), data.phase_power_w[0]);
|
||||
doc["p1_w"] = serialized(buf);
|
||||
format_float_2(buf, sizeof(buf), data.phase_power_w[1]);
|
||||
doc["p2_w"] = serialized(buf);
|
||||
format_float_2(buf, sizeof(buf), data.phase_power_w[2]);
|
||||
doc["p3_w"] = serialized(buf);
|
||||
format_float_2(buf, sizeof(buf), data.phase_voltage_v[0]);
|
||||
doc["v1_v"] = serialized(buf);
|
||||
format_float_2(buf, sizeof(buf), data.phase_voltage_v[1]);
|
||||
doc["v2_v"] = serialized(buf);
|
||||
format_float_2(buf, sizeof(buf), data.phase_voltage_v[2]);
|
||||
doc["v3_v"] = serialized(buf);
|
||||
set_int_or_null(doc, "p_w", data.total_power_w);
|
||||
set_int_or_null(doc, "p1_w", data.phase_power_w[0]);
|
||||
set_int_or_null(doc, "p2_w", data.phase_power_w[1]);
|
||||
set_int_or_null(doc, "p3_w", data.phase_power_w[2]);
|
||||
format_float_2(buf, sizeof(buf), data.battery_voltage_v);
|
||||
doc["bat_v"] = serialized(buf);
|
||||
doc["bat_pct"] = data.battery_percent;
|
||||
@@ -69,177 +86,11 @@ bool meterDataToJson(const MeterData &data, String &out_json) {
|
||||
if (data.err_lora_tx > 0) {
|
||||
doc["err_tx"] = data.err_lora_tx;
|
||||
}
|
||||
if (data.last_error != FaultType::None) {
|
||||
doc["err_last"] = static_cast<uint8_t>(data.last_error);
|
||||
}
|
||||
|
||||
out_json = "";
|
||||
size_t len = serializeJson(doc, out_json);
|
||||
return len > 0 && len < 256;
|
||||
}
|
||||
|
||||
static float read_float_or_legacy(JsonDocument &doc, const char *key, const char *legacy_key) {
|
||||
if (doc[key].isNull()) {
|
||||
return doc[legacy_key] | NAN;
|
||||
}
|
||||
return doc[key] | NAN;
|
||||
}
|
||||
|
||||
bool jsonToMeterData(const String &json, MeterData &data) {
|
||||
StaticJsonDocument<256> doc;
|
||||
DeserializationError err = deserializeJson(doc, json);
|
||||
if (err) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const char *id = doc["id"] | "";
|
||||
if (strlen(id) == 4) {
|
||||
snprintf(data.device_id, sizeof(data.device_id), "dd3-%s", id);
|
||||
} else {
|
||||
strncpy(data.device_id, id, sizeof(data.device_id));
|
||||
}
|
||||
data.device_id[sizeof(data.device_id) - 1] = '\0';
|
||||
|
||||
data.ts_utc = doc["ts"] | 0;
|
||||
data.energy_total_kwh = read_float_or_legacy(doc, "e_kwh", "energy_kwh");
|
||||
data.total_power_w = read_float_or_legacy(doc, "p_w", "p_total_w");
|
||||
data.phase_power_w[0] = doc["p1_w"] | NAN;
|
||||
data.phase_power_w[1] = doc["p2_w"] | NAN;
|
||||
data.phase_power_w[2] = doc["p3_w"] | NAN;
|
||||
data.phase_voltage_v[0] = doc["v1_v"] | NAN;
|
||||
data.phase_voltage_v[1] = doc["v2_v"] | NAN;
|
||||
data.phase_voltage_v[2] = doc["v3_v"] | NAN;
|
||||
data.battery_voltage_v = doc["bat_v"] | NAN;
|
||||
if (doc["bat_pct"].isNull() && !isnan(data.battery_voltage_v)) {
|
||||
data.battery_percent = battery_percent_from_voltage(data.battery_voltage_v);
|
||||
} else {
|
||||
data.battery_percent = doc["bat_pct"] | 0;
|
||||
}
|
||||
data.valid = true;
|
||||
data.link_valid = false;
|
||||
data.link_rssi_dbm = 0;
|
||||
data.link_snr_db = NAN;
|
||||
data.err_meter_read = doc["err_m"] | 0;
|
||||
data.err_decode = doc["err_d"] | 0;
|
||||
data.err_lora_tx = doc["err_tx"] | 0;
|
||||
data.last_error = static_cast<FaultType>(doc["err_last"] | 0);
|
||||
|
||||
if (strlen(data.device_id) >= 8) {
|
||||
const char *suffix = data.device_id + strlen(data.device_id) - 4;
|
||||
data.short_id = static_cast<uint16_t>(strtoul(suffix, nullptr, 16));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool meterBatchToJson(const MeterData *samples, size_t count, String &out_json, const FaultCounters *faults, FaultType last_error) {
|
||||
if (!samples || count == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
DynamicJsonDocument doc(8192);
|
||||
doc["id"] = short_id_from_device_id(samples[count - 1].device_id);
|
||||
doc["bat_v"] = round2(samples[count - 1].battery_voltage_v);
|
||||
doc["bat_pct"] = samples[count - 1].battery_percent;
|
||||
if (faults) {
|
||||
if (faults->meter_read_fail > 0) {
|
||||
doc["err_m"] = faults->meter_read_fail;
|
||||
}
|
||||
if (faults->lora_tx_fail > 0) {
|
||||
doc["err_tx"] = faults->lora_tx_fail;
|
||||
}
|
||||
}
|
||||
if (last_error != FaultType::None) {
|
||||
doc["err_last"] = static_cast<uint8_t>(last_error);
|
||||
}
|
||||
JsonArray arr = doc.createNestedArray("s");
|
||||
for (size_t i = 0; i < count; ++i) {
|
||||
JsonArray row = arr.createNestedArray();
|
||||
row.add(samples[i].ts_utc);
|
||||
row.add(round2(samples[i].energy_total_kwh));
|
||||
row.add(round2(samples[i].total_power_w));
|
||||
row.add(round2(samples[i].phase_power_w[0]));
|
||||
row.add(round2(samples[i].phase_power_w[1]));
|
||||
row.add(round2(samples[i].phase_power_w[2]));
|
||||
row.add(round2(samples[i].phase_voltage_v[0]));
|
||||
row.add(round2(samples[i].phase_voltage_v[1]));
|
||||
row.add(round2(samples[i].phase_voltage_v[2]));
|
||||
row.add(samples[i].valid ? 1 : 0);
|
||||
}
|
||||
doc["err_last"] = static_cast<uint8_t>(data.last_error);
|
||||
doc["rx_reject"] = data.rx_reject_reason;
|
||||
doc["rx_reject_text"] = rx_reject_reason_text(static_cast<RxRejectReason>(data.rx_reject_reason));
|
||||
|
||||
out_json = "";
|
||||
size_t len = serializeJson(doc, out_json);
|
||||
return len > 0;
|
||||
}
|
||||
|
||||
bool jsonToMeterBatch(const String &json, MeterData *out_samples, size_t max_count, size_t &out_count) {
|
||||
out_count = 0;
|
||||
if (!out_samples || max_count == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
DynamicJsonDocument doc(8192);
|
||||
DeserializationError err = deserializeJson(doc, json);
|
||||
if (err) {
|
||||
return false;
|
||||
}
|
||||
|
||||
JsonArray arr = doc["s"].as<JsonArray>();
|
||||
if (arr.isNull()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const char *id = doc["id"] | "";
|
||||
float bat_v = doc["bat_v"] | NAN;
|
||||
uint8_t bat_pct = doc["bat_pct"] | 0;
|
||||
uint32_t err_m = doc["err_m"] | 0;
|
||||
uint32_t err_tx = doc["err_tx"] | 0;
|
||||
FaultType last_error = static_cast<FaultType>(doc["err_last"] | 0);
|
||||
|
||||
size_t idx = 0;
|
||||
for (JsonArray row : arr) {
|
||||
if (idx >= max_count) {
|
||||
break;
|
||||
}
|
||||
MeterData &data = out_samples[idx];
|
||||
data = {};
|
||||
if (strlen(id) == 4) {
|
||||
snprintf(data.device_id, sizeof(data.device_id), "dd3-%s", id);
|
||||
} else {
|
||||
strncpy(data.device_id, id, sizeof(data.device_id));
|
||||
}
|
||||
data.device_id[sizeof(data.device_id) - 1] = '\0';
|
||||
data.ts_utc = row[0] | 0;
|
||||
data.energy_total_kwh = row[1] | NAN;
|
||||
data.total_power_w = row[2] | NAN;
|
||||
data.phase_power_w[0] = row[3] | NAN;
|
||||
data.phase_power_w[1] = row[4] | NAN;
|
||||
data.phase_power_w[2] = row[5] | NAN;
|
||||
data.phase_voltage_v[0] = row[6] | NAN;
|
||||
data.phase_voltage_v[1] = row[7] | NAN;
|
||||
data.phase_voltage_v[2] = row[8] | NAN;
|
||||
data.valid = (row[9] | 1) != 0;
|
||||
data.battery_voltage_v = bat_v;
|
||||
if (doc["bat_pct"].isNull() && !isnan(bat_v)) {
|
||||
data.battery_percent = battery_percent_from_voltage(bat_v);
|
||||
} else {
|
||||
data.battery_percent = bat_pct;
|
||||
}
|
||||
data.link_valid = false;
|
||||
data.link_rssi_dbm = 0;
|
||||
data.link_snr_db = NAN;
|
||||
data.err_meter_read = err_m;
|
||||
data.err_decode = 0;
|
||||
data.err_lora_tx = err_tx;
|
||||
data.last_error = last_error;
|
||||
|
||||
if (strlen(data.device_id) >= 8) {
|
||||
const char *suffix = data.device_id + strlen(data.device_id) - 4;
|
||||
data.short_id = static_cast<uint16_t>(strtoul(suffix, nullptr, 16));
|
||||
}
|
||||
idx++;
|
||||
}
|
||||
|
||||
out_count = idx;
|
||||
return idx > 0;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,39 @@
|
||||
#include "lora_transport.h"
|
||||
#include <LoRa.h>
|
||||
#include <SPI.h>
|
||||
#include <math.h>
|
||||
|
||||
static RxRejectReason g_last_rx_reject_reason = RxRejectReason::None;
|
||||
static uint32_t g_last_rx_reject_log_ms = 0;
|
||||
static bool g_last_rx_signal_valid = false;
|
||||
static int16_t g_last_rx_rssi_dbm = 0;
|
||||
static float g_last_rx_snr_db = 0.0f;
|
||||
|
||||
static void note_reject(RxRejectReason reason) {
|
||||
g_last_rx_reject_reason = reason;
|
||||
if (SERIAL_DEBUG_MODE) {
|
||||
uint32_t now_ms = millis();
|
||||
if (now_ms - g_last_rx_reject_log_ms >= 1000) {
|
||||
g_last_rx_reject_log_ms = now_ms;
|
||||
Serial.printf("lora_rx: reject reason=%s\n", rx_reject_reason_text(reason));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
RxRejectReason lora_get_last_rx_reject_reason() {
|
||||
RxRejectReason reason = g_last_rx_reject_reason;
|
||||
g_last_rx_reject_reason = RxRejectReason::None;
|
||||
return reason;
|
||||
}
|
||||
|
||||
bool lora_get_last_rx_signal(int16_t &rssi_dbm, float &snr_db) {
|
||||
if (!g_last_rx_signal_valid) {
|
||||
return false;
|
||||
}
|
||||
rssi_dbm = g_last_rx_rssi_dbm;
|
||||
snr_db = g_last_rx_snr_db;
|
||||
return true;
|
||||
}
|
||||
|
||||
static uint16_t crc16_ccitt(const uint8_t *data, size_t len) {
|
||||
uint16_t crc = 0xFFFF;
|
||||
@@ -29,13 +62,26 @@ void lora_init() {
|
||||
}
|
||||
|
||||
bool lora_send(const LoraPacket &pkt) {
|
||||
uint8_t buffer[5 + LORA_MAX_PAYLOAD + 2];
|
||||
if (LORA_SEND_BYPASS) {
|
||||
return true;
|
||||
}
|
||||
uint32_t t0 = 0;
|
||||
uint32_t t1 = 0;
|
||||
uint32_t t2 = 0;
|
||||
uint32_t t3 = 0;
|
||||
uint32_t t4 = 0;
|
||||
if (SERIAL_DEBUG_MODE) {
|
||||
t0 = millis();
|
||||
}
|
||||
LoRa.idle();
|
||||
if (SERIAL_DEBUG_MODE) {
|
||||
t1 = millis();
|
||||
}
|
||||
uint8_t buffer[1 + 2 + LORA_MAX_PAYLOAD + 2];
|
||||
size_t idx = 0;
|
||||
buffer[idx++] = pkt.protocol_version;
|
||||
buffer[idx++] = static_cast<uint8_t>(pkt.role);
|
||||
buffer[idx++] = static_cast<uint8_t>(pkt.msg_kind);
|
||||
buffer[idx++] = static_cast<uint8_t>(pkt.device_id_short >> 8);
|
||||
buffer[idx++] = static_cast<uint8_t>(pkt.device_id_short & 0xFF);
|
||||
buffer[idx++] = static_cast<uint8_t>(pkt.payload_type);
|
||||
|
||||
if (pkt.payload_len > LORA_MAX_PAYLOAD) {
|
||||
return false;
|
||||
@@ -49,8 +95,24 @@ bool lora_send(const LoraPacket &pkt) {
|
||||
buffer[idx++] = static_cast<uint8_t>(crc & 0xFF);
|
||||
|
||||
LoRa.beginPacket();
|
||||
if (SERIAL_DEBUG_MODE) {
|
||||
t2 = millis();
|
||||
}
|
||||
LoRa.write(buffer, idx);
|
||||
int result = LoRa.endPacket();
|
||||
if (SERIAL_DEBUG_MODE) {
|
||||
t3 = millis();
|
||||
}
|
||||
int result = LoRa.endPacket(false);
|
||||
if (SERIAL_DEBUG_MODE) {
|
||||
t4 = millis();
|
||||
Serial.printf("lora_tx: idle=%lums begin=%lums write=%lums end=%lums total=%lums len=%u\n",
|
||||
static_cast<unsigned long>(t1 - t0),
|
||||
static_cast<unsigned long>(t2 - t1),
|
||||
static_cast<unsigned long>(t3 - t2),
|
||||
static_cast<unsigned long>(t4 - t3),
|
||||
static_cast<unsigned long>(t4 - t0),
|
||||
static_cast<unsigned>(idx));
|
||||
}
|
||||
return result == 1;
|
||||
}
|
||||
|
||||
@@ -59,40 +121,61 @@ bool lora_receive(LoraPacket &pkt, uint32_t timeout_ms) {
|
||||
while (true) {
|
||||
int packet_size = LoRa.parsePacket();
|
||||
if (packet_size > 0) {
|
||||
if (packet_size < 7) {
|
||||
g_last_rx_rssi_dbm = static_cast<int16_t>(LoRa.packetRssi());
|
||||
g_last_rx_snr_db = LoRa.packetSnr();
|
||||
g_last_rx_signal_valid = true;
|
||||
|
||||
if (packet_size < 5) {
|
||||
while (LoRa.available()) {
|
||||
LoRa.read();
|
||||
}
|
||||
note_reject(RxRejectReason::LengthMismatch);
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t buffer[5 + LORA_MAX_PAYLOAD + 2];
|
||||
uint8_t buffer[1 + 2 + LORA_MAX_PAYLOAD + 2];
|
||||
size_t len = 0;
|
||||
while (LoRa.available() && len < sizeof(buffer)) {
|
||||
buffer[len++] = LoRa.read();
|
||||
}
|
||||
if (LoRa.available()) {
|
||||
while (LoRa.available()) {
|
||||
LoRa.read();
|
||||
}
|
||||
if (SERIAL_DEBUG_MODE) {
|
||||
Serial.println("rx_reject: oversize packet drained");
|
||||
}
|
||||
note_reject(RxRejectReason::LengthMismatch);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (len < 7) {
|
||||
if (len < 5) {
|
||||
note_reject(RxRejectReason::LengthMismatch);
|
||||
return false;
|
||||
}
|
||||
|
||||
uint16_t crc_calc = crc16_ccitt(buffer, len - 2);
|
||||
uint16_t crc_rx = static_cast<uint16_t>(buffer[len - 2] << 8) | buffer[len - 1];
|
||||
if (crc_calc != crc_rx) {
|
||||
note_reject(RxRejectReason::CrcFail);
|
||||
return false;
|
||||
}
|
||||
uint8_t msg_kind = buffer[0];
|
||||
if (msg_kind > static_cast<uint8_t>(LoraMsgKind::AckDown)) {
|
||||
note_reject(RxRejectReason::InvalidMsgKind);
|
||||
return false;
|
||||
}
|
||||
|
||||
pkt.protocol_version = buffer[0];
|
||||
pkt.role = static_cast<DeviceRole>(buffer[1]);
|
||||
pkt.device_id_short = static_cast<uint16_t>(buffer[2] << 8) | buffer[3];
|
||||
pkt.payload_type = static_cast<PayloadType>(buffer[4]);
|
||||
pkt.payload_len = len - 7;
|
||||
pkt.msg_kind = static_cast<LoraMsgKind>(msg_kind);
|
||||
pkt.device_id_short = static_cast<uint16_t>(buffer[1] << 8) | buffer[2];
|
||||
pkt.payload_len = len - 5;
|
||||
if (pkt.payload_len > LORA_MAX_PAYLOAD) {
|
||||
note_reject(RxRejectReason::LengthMismatch);
|
||||
return false;
|
||||
}
|
||||
memcpy(pkt.payload, &buffer[5], pkt.payload_len);
|
||||
pkt.rssi_dbm = static_cast<int16_t>(LoRa.packetRssi());
|
||||
pkt.snr_db = LoRa.packetSnr();
|
||||
memcpy(pkt.payload, &buffer[3], pkt.payload_len);
|
||||
pkt.rssi_dbm = g_last_rx_rssi_dbm;
|
||||
pkt.snr_db = g_last_rx_snr_db;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -103,6 +186,52 @@ bool lora_receive(LoraPacket &pkt, uint32_t timeout_ms) {
|
||||
}
|
||||
}
|
||||
|
||||
void lora_idle() {
|
||||
LoRa.idle();
|
||||
}
|
||||
|
||||
void lora_sleep() {
|
||||
LoRa.sleep();
|
||||
}
|
||||
|
||||
void lora_receive_continuous() {
|
||||
LoRa.receive();
|
||||
}
|
||||
|
||||
bool lora_receive_window(LoraPacket &pkt, uint32_t timeout_ms) {
|
||||
if (timeout_ms == 0) {
|
||||
return false;
|
||||
}
|
||||
g_last_rx_signal_valid = false;
|
||||
g_last_rx_rssi_dbm = 0;
|
||||
g_last_rx_snr_db = 0.0f;
|
||||
LoRa.receive();
|
||||
bool got = lora_receive(pkt, timeout_ms);
|
||||
LoRa.sleep();
|
||||
return got;
|
||||
}
|
||||
|
||||
uint32_t lora_airtime_ms(size_t packet_len) {
|
||||
if (packet_len == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const double bw = static_cast<double>(LORA_BANDWIDTH);
|
||||
const double sf = static_cast<double>(LORA_SPREADING_FACTOR);
|
||||
const double cr = static_cast<double>(LORA_CODING_RATE - 4); // coding rate denominator: 4/(4+cr)
|
||||
const double tsym = (1 << LORA_SPREADING_FACTOR) / bw;
|
||||
const double t_preamble = (static_cast<double>(LORA_PREAMBLE_LEN) + 4.25) * tsym;
|
||||
|
||||
const bool low_data_rate_opt = (LORA_SPREADING_FACTOR >= 11) && (LORA_BANDWIDTH <= 125000);
|
||||
const double de = low_data_rate_opt ? 1.0 : 0.0;
|
||||
const double ih = 0.0;
|
||||
const double crc = 1.0;
|
||||
|
||||
const double payload_symb_nb = 8.0 + max(
|
||||
ceil((8.0 * packet_len - 4.0 * sf + 28.0 + 16.0 * crc - 20.0 * ih) / (4.0 * (sf - 2.0 * de))) * (cr + 4.0),
|
||||
0.0);
|
||||
const double t_payload = payload_symb_nb * tsym;
|
||||
const double t_packet = t_preamble + t_payload;
|
||||
|
||||
return static_cast<uint32_t>(ceil(t_packet * 1000.0));
|
||||
}
|
||||
|
||||
1193
src/main.cpp
1193
src/main.cpp
File diff suppressed because it is too large
Load Diff
@@ -4,168 +4,35 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static constexpr uint32_t METER_READ_TIMEOUT_MS = 2000;
|
||||
static constexpr size_t SML_BUFFER_SIZE = 2048;
|
||||
// LoRa TX/RX windows can block the main loop for several seconds at SF12.
|
||||
// Keep partial frame state long enough so valid telegrams are not dropped.
|
||||
static constexpr uint32_t METER_FRAME_TIMEOUT_MS = 20000;
|
||||
static constexpr size_t METER_FRAME_MAX = 512;
|
||||
|
||||
static const uint8_t OBIS_ENERGY_TOTAL[6] = {0x01, 0x00, 0x01, 0x08, 0x00, 0xFF};
|
||||
static const uint8_t OBIS_TOTAL_POWER[6] = {0x01, 0x00, 0x10, 0x07, 0x00, 0xFF};
|
||||
static const uint8_t OBIS_P1[6] = {0x01, 0x00, 0x24, 0x07, 0x00, 0xFF};
|
||||
static const uint8_t OBIS_P2[6] = {0x01, 0x00, 0x38, 0x07, 0x00, 0xFF};
|
||||
static const uint8_t OBIS_P3[6] = {0x01, 0x00, 0x4C, 0x07, 0x00, 0xFF};
|
||||
static const uint8_t OBIS_V1[6] = {0x01, 0x00, 0x20, 0x07, 0x00, 0xFF};
|
||||
static const uint8_t OBIS_V2[6] = {0x01, 0x00, 0x34, 0x07, 0x00, 0xFF};
|
||||
static const uint8_t OBIS_V3[6] = {0x01, 0x00, 0x48, 0x07, 0x00, 0xFF};
|
||||
enum class MeterRxState : uint8_t {
|
||||
WaitStart = 0,
|
||||
InFrame = 1
|
||||
};
|
||||
|
||||
static bool find_obis_value(const uint8_t *buf, size_t len, const uint8_t *obis, float &out_value) {
|
||||
for (size_t i = 0; i + 6 < len; ++i) {
|
||||
if (memcmp(&buf[i], obis, 6) == 0) {
|
||||
int8_t scaler = 0;
|
||||
bool scaler_found = false;
|
||||
bool value_found = false;
|
||||
int64_t value = 0;
|
||||
size_t cursor = i + 6;
|
||||
size_t limit = (i + 6 + 120 < len) ? i + 6 + 120 : len;
|
||||
|
||||
while (cursor < limit) {
|
||||
uint8_t tl = buf[cursor++];
|
||||
if (tl == 0x00) {
|
||||
continue;
|
||||
}
|
||||
uint8_t type = (tl >> 4) & 0x0F;
|
||||
uint8_t tlen = tl & 0x0F;
|
||||
if (tlen == 0 || cursor + tlen > len) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (type == 0x05 || type == 0x06) {
|
||||
int64_t val = 0;
|
||||
for (uint8_t b = 0; b < tlen; ++b) {
|
||||
val = (val << 8) | buf[cursor + b];
|
||||
}
|
||||
if (type == 0x05) {
|
||||
int64_t sign_bit = 1LL << ((tlen * 8) - 1);
|
||||
if (val & sign_bit) {
|
||||
int64_t mask = (1LL << (tlen * 8)) - 1;
|
||||
val = -((~val + 1) & mask);
|
||||
}
|
||||
}
|
||||
|
||||
if (!scaler_found && tlen <= 2 && val >= -6 && val <= 6) {
|
||||
scaler = static_cast<int8_t>(val);
|
||||
scaler_found = true;
|
||||
} else if (!value_found) {
|
||||
value = val;
|
||||
value_found = true;
|
||||
}
|
||||
}
|
||||
cursor += tlen;
|
||||
if (value_found && scaler_found) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (value_found) {
|
||||
out_value = static_cast<float>(value) * powf(10.0f, scaler);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
static MeterRxState g_rx_state = MeterRxState::WaitStart;
|
||||
static char g_frame_buf[METER_FRAME_MAX + 1];
|
||||
static size_t g_frame_len = 0;
|
||||
static uint32_t g_last_rx_ms = 0;
|
||||
static uint32_t g_bytes_rx = 0;
|
||||
static uint32_t g_frames_ok = 0;
|
||||
static uint32_t g_frames_parse_fail = 0;
|
||||
static uint32_t g_rx_overflow = 0;
|
||||
static uint32_t g_rx_timeout = 0;
|
||||
static uint32_t g_last_log_ms = 0;
|
||||
|
||||
void meter_init() {
|
||||
#ifdef ARDUINO_ARCH_ESP32
|
||||
// Buffer enough serial data to survive long LoRa blocking sections.
|
||||
Serial2.setRxBufferSize(8192);
|
||||
#endif
|
||||
Serial2.begin(9600, SERIAL_7E1, PIN_METER_RX, -1);
|
||||
}
|
||||
|
||||
static bool meter_read_sml(MeterData &data) {
|
||||
uint8_t buffer[SML_BUFFER_SIZE];
|
||||
size_t len = 0;
|
||||
bool started = false;
|
||||
uint32_t start = millis();
|
||||
const uint8_t start_seq[] = {0x1B, 0x1B, 0x1B, 0x1B, 0x01, 0x01, 0x01, 0x01};
|
||||
const uint8_t end_seq[] = {0x1B, 0x1B, 0x1B, 0x1B, 0x1A};
|
||||
|
||||
while (millis() - start < METER_READ_TIMEOUT_MS) {
|
||||
while (Serial2.available()) {
|
||||
uint8_t b = Serial2.read();
|
||||
if (!started) {
|
||||
buffer[len++] = b;
|
||||
if (len >= sizeof(start_seq)) {
|
||||
if (memcmp(&buffer[len - sizeof(start_seq)], start_seq, sizeof(start_seq)) == 0) {
|
||||
started = true;
|
||||
len = 0;
|
||||
}
|
||||
}
|
||||
if (len >= sizeof(buffer)) {
|
||||
len = 0;
|
||||
}
|
||||
} else {
|
||||
if (len < sizeof(buffer)) {
|
||||
buffer[len++] = b;
|
||||
if (len >= sizeof(end_seq)) {
|
||||
if (memcmp(&buffer[len - sizeof(end_seq)], end_seq, sizeof(end_seq)) == 0) {
|
||||
start = millis();
|
||||
goto parse_frame;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
delay(5);
|
||||
}
|
||||
|
||||
parse_frame:
|
||||
if (!started || len == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
data.energy_total_kwh = NAN;
|
||||
data.total_power_w = NAN;
|
||||
data.phase_power_w[0] = NAN;
|
||||
data.phase_power_w[1] = NAN;
|
||||
data.phase_power_w[2] = NAN;
|
||||
data.phase_voltage_v[0] = NAN;
|
||||
data.phase_voltage_v[1] = NAN;
|
||||
data.phase_voltage_v[2] = NAN;
|
||||
|
||||
bool ok = true;
|
||||
float value = 0.0f;
|
||||
|
||||
if (find_obis_value(buffer, len, OBIS_ENERGY_TOTAL, value)) {
|
||||
data.energy_total_kwh = value;
|
||||
} else {
|
||||
ok = false;
|
||||
}
|
||||
|
||||
if (find_obis_value(buffer, len, OBIS_TOTAL_POWER, value)) {
|
||||
data.total_power_w = value;
|
||||
} else {
|
||||
ok = false;
|
||||
}
|
||||
|
||||
if (find_obis_value(buffer, len, OBIS_P1, value)) {
|
||||
data.phase_power_w[0] = value;
|
||||
}
|
||||
if (find_obis_value(buffer, len, OBIS_P2, value)) {
|
||||
data.phase_power_w[1] = value;
|
||||
}
|
||||
if (find_obis_value(buffer, len, OBIS_P3, value)) {
|
||||
data.phase_power_w[2] = value;
|
||||
}
|
||||
if (find_obis_value(buffer, len, OBIS_V1, value)) {
|
||||
data.phase_voltage_v[0] = value;
|
||||
}
|
||||
if (find_obis_value(buffer, len, OBIS_V2, value)) {
|
||||
data.phase_voltage_v[1] = value;
|
||||
}
|
||||
if (find_obis_value(buffer, len, OBIS_V3, value)) {
|
||||
data.phase_voltage_v[2] = value;
|
||||
}
|
||||
|
||||
data.valid = ok;
|
||||
return ok;
|
||||
}
|
||||
|
||||
static bool parse_obis_ascii_value(const char *line, const char *obis, float &out_value) {
|
||||
const char *p = strstr(line, obis);
|
||||
if (!p) {
|
||||
@@ -233,98 +100,156 @@ static bool parse_obis_ascii_unit_scale(const char *line, const char *obis, floa
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool meter_read_ascii(MeterData &data) {
|
||||
const uint32_t start_ms = millis();
|
||||
bool in_telegram = false;
|
||||
bool got_any = false;
|
||||
static void meter_debug_log() {
|
||||
if (!SERIAL_DEBUG_MODE) {
|
||||
return;
|
||||
}
|
||||
uint32_t now_ms = millis();
|
||||
if (now_ms - g_last_log_ms < 60000) {
|
||||
return;
|
||||
}
|
||||
g_last_log_ms = now_ms;
|
||||
Serial.printf("meter: ok=%lu parse_fail=%lu overflow=%lu timeout=%lu bytes=%lu\n",
|
||||
static_cast<unsigned long>(g_frames_ok),
|
||||
static_cast<unsigned long>(g_frames_parse_fail),
|
||||
static_cast<unsigned long>(g_rx_overflow),
|
||||
static_cast<unsigned long>(g_rx_timeout),
|
||||
static_cast<unsigned long>(g_bytes_rx));
|
||||
}
|
||||
|
||||
bool meter_poll_frame(const char *&frame, size_t &len) {
|
||||
frame = nullptr;
|
||||
len = 0;
|
||||
uint32_t now_ms = millis();
|
||||
|
||||
if (g_rx_state == MeterRxState::InFrame && (now_ms - g_last_rx_ms > METER_FRAME_TIMEOUT_MS)) {
|
||||
g_rx_timeout++;
|
||||
g_rx_state = MeterRxState::WaitStart;
|
||||
g_frame_len = 0;
|
||||
}
|
||||
|
||||
while (Serial2.available()) {
|
||||
char c = static_cast<char>(Serial2.read());
|
||||
g_bytes_rx++;
|
||||
g_last_rx_ms = now_ms;
|
||||
|
||||
if (g_rx_state == MeterRxState::WaitStart) {
|
||||
if (c == '/') {
|
||||
g_rx_state = MeterRxState::InFrame;
|
||||
g_frame_len = 0;
|
||||
g_frame_buf[g_frame_len++] = c;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (g_frame_len + 1 >= sizeof(g_frame_buf)) {
|
||||
g_rx_overflow++;
|
||||
g_rx_state = MeterRxState::WaitStart;
|
||||
g_frame_len = 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
g_frame_buf[g_frame_len++] = c;
|
||||
if (c == '!') {
|
||||
g_frame_buf[g_frame_len] = '\0';
|
||||
frame = g_frame_buf;
|
||||
len = g_frame_len;
|
||||
g_rx_state = MeterRxState::WaitStart;
|
||||
g_frame_len = 0;
|
||||
meter_debug_log();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
meter_debug_log();
|
||||
return false;
|
||||
}
|
||||
|
||||
bool meter_parse_frame(const char *frame, size_t len, MeterData &data) {
|
||||
if (!frame || len == 0) {
|
||||
return false;
|
||||
}
|
||||
bool got_any = false;
|
||||
bool energy_ok = false;
|
||||
bool total_p_ok = false;
|
||||
bool p1_ok = false;
|
||||
bool p2_ok = false;
|
||||
bool p3_ok = false;
|
||||
bool v1_ok = false;
|
||||
bool v2_ok = false;
|
||||
bool v3_ok = false;
|
||||
|
||||
char line[128];
|
||||
size_t line_len = 0;
|
||||
|
||||
while (millis() - start_ms < METER_READ_TIMEOUT_MS) {
|
||||
while (Serial2.available()) {
|
||||
char c = static_cast<char>(Serial2.read());
|
||||
if (!in_telegram) {
|
||||
if (c == '/') {
|
||||
in_telegram = true;
|
||||
line_len = 0;
|
||||
line[line_len++] = c;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (c == '\r') {
|
||||
continue;
|
||||
}
|
||||
if (c == '\n') {
|
||||
line[line_len] = '\0';
|
||||
if (line[0] == '!') {
|
||||
return got_any;
|
||||
}
|
||||
|
||||
float value = NAN;
|
||||
if (parse_obis_ascii_value(line, "1-0:1.8.0", value)) {
|
||||
parse_obis_ascii_unit_scale(line, "1-0:1.8.0", value);
|
||||
data.energy_total_kwh = value;
|
||||
energy_ok = true;
|
||||
got_any = true;
|
||||
}
|
||||
if (parse_obis_ascii_value(line, "1-0:16.7.0", value)) {
|
||||
data.total_power_w = value;
|
||||
total_p_ok = true;
|
||||
got_any = true;
|
||||
}
|
||||
if (parse_obis_ascii_value(line, "1-0:36.7.0", value)) {
|
||||
data.phase_power_w[0] = value;
|
||||
p1_ok = true;
|
||||
got_any = true;
|
||||
}
|
||||
if (parse_obis_ascii_value(line, "1-0:56.7.0", value)) {
|
||||
data.phase_power_w[1] = value;
|
||||
p2_ok = true;
|
||||
got_any = true;
|
||||
}
|
||||
if (parse_obis_ascii_value(line, "1-0:76.7.0", value)) {
|
||||
data.phase_power_w[2] = value;
|
||||
p3_ok = true;
|
||||
got_any = true;
|
||||
}
|
||||
if (parse_obis_ascii_value(line, "1-0:32.7.0", value)) {
|
||||
data.phase_voltage_v[0] = value;
|
||||
v1_ok = true;
|
||||
got_any = true;
|
||||
}
|
||||
if (parse_obis_ascii_value(line, "1-0:52.7.0", value)) {
|
||||
data.phase_voltage_v[1] = value;
|
||||
v2_ok = true;
|
||||
got_any = true;
|
||||
}
|
||||
if (parse_obis_ascii_value(line, "1-0:72.7.0", value)) {
|
||||
data.phase_voltage_v[2] = value;
|
||||
v3_ok = true;
|
||||
got_any = true;
|
||||
}
|
||||
|
||||
line_len = 0;
|
||||
continue;
|
||||
}
|
||||
for (size_t i = 0; i < len; ++i) {
|
||||
char c = frame[i];
|
||||
if (c == '\r') {
|
||||
continue;
|
||||
}
|
||||
if (c == '!') {
|
||||
if (line_len + 1 < sizeof(line)) {
|
||||
line[line_len++] = c;
|
||||
}
|
||||
line[line_len] = '\0';
|
||||
data.valid = energy_ok || total_p_ok || p1_ok || p2_ok || p3_ok;
|
||||
if (data.valid) {
|
||||
g_frames_ok++;
|
||||
} else {
|
||||
g_frames_parse_fail++;
|
||||
}
|
||||
return data.valid;
|
||||
}
|
||||
if (c == '\n') {
|
||||
line[line_len] = '\0';
|
||||
if (line[0] == '!') {
|
||||
data.valid = energy_ok || total_p_ok || p1_ok || p2_ok || p3_ok;
|
||||
if (data.valid) {
|
||||
g_frames_ok++;
|
||||
} else {
|
||||
g_frames_parse_fail++;
|
||||
}
|
||||
return data.valid;
|
||||
}
|
||||
|
||||
float value = NAN;
|
||||
if (parse_obis_ascii_value(line, "1-0:1.8.0", value)) {
|
||||
parse_obis_ascii_unit_scale(line, "1-0:1.8.0", value);
|
||||
data.energy_total_kwh = value;
|
||||
energy_ok = true;
|
||||
got_any = true;
|
||||
}
|
||||
if (parse_obis_ascii_value(line, "1-0:16.7.0", value)) {
|
||||
data.total_power_w = value;
|
||||
total_p_ok = true;
|
||||
got_any = true;
|
||||
}
|
||||
if (parse_obis_ascii_value(line, "1-0:36.7.0", value)) {
|
||||
data.phase_power_w[0] = value;
|
||||
p1_ok = true;
|
||||
got_any = true;
|
||||
}
|
||||
if (parse_obis_ascii_value(line, "1-0:56.7.0", value)) {
|
||||
data.phase_power_w[1] = value;
|
||||
p2_ok = true;
|
||||
got_any = true;
|
||||
}
|
||||
if (parse_obis_ascii_value(line, "1-0:76.7.0", value)) {
|
||||
data.phase_power_w[2] = value;
|
||||
p3_ok = true;
|
||||
got_any = true;
|
||||
}
|
||||
|
||||
line_len = 0;
|
||||
continue;
|
||||
}
|
||||
if (line_len + 1 < sizeof(line)) {
|
||||
line[line_len++] = c;
|
||||
}
|
||||
delay(5);
|
||||
}
|
||||
|
||||
data.valid = energy_ok || total_p_ok || p1_ok || p2_ok || p3_ok || v1_ok || v2_ok || v3_ok;
|
||||
data.valid = got_any;
|
||||
if (data.valid) {
|
||||
g_frames_ok++;
|
||||
} else {
|
||||
g_frames_parse_fail++;
|
||||
}
|
||||
return data.valid;
|
||||
}
|
||||
|
||||
@@ -334,13 +259,12 @@ bool meter_read(MeterData &data) {
|
||||
data.phase_power_w[0] = NAN;
|
||||
data.phase_power_w[1] = NAN;
|
||||
data.phase_power_w[2] = NAN;
|
||||
data.phase_voltage_v[0] = NAN;
|
||||
data.phase_voltage_v[1] = NAN;
|
||||
data.phase_voltage_v[2] = NAN;
|
||||
data.valid = false;
|
||||
|
||||
if (meter_read_ascii(data)) {
|
||||
return true;
|
||||
const char *frame = nullptr;
|
||||
size_t len = 0;
|
||||
if (!meter_poll_frame(frame, len)) {
|
||||
return false;
|
||||
}
|
||||
return meter_read_sml(data);
|
||||
return meter_parse_frame(frame, len, data);
|
||||
}
|
||||
|
||||
@@ -10,6 +10,19 @@ static PubSubClient mqtt_client(wifi_client);
|
||||
static WifiMqttConfig g_cfg;
|
||||
static String g_client_id;
|
||||
|
||||
static const char *fault_text(FaultType fault) {
|
||||
switch (fault) {
|
||||
case FaultType::MeterRead:
|
||||
return "meter";
|
||||
case FaultType::Decode:
|
||||
return "decode";
|
||||
case FaultType::LoraTx:
|
||||
return "loratx";
|
||||
default:
|
||||
return "none";
|
||||
}
|
||||
}
|
||||
|
||||
void mqtt_init(const WifiMqttConfig &config, const char *device_id) {
|
||||
g_cfg = config;
|
||||
mqtt_client.setServer(config.mqtt_host.c_str(), config.mqtt_port);
|
||||
@@ -66,10 +79,9 @@ bool mqtt_publish_faults(const char *device_id, const FaultCounters &counters, F
|
||||
doc["err_m"] = counters.meter_read_fail;
|
||||
doc["err_d"] = counters.decode_fail;
|
||||
doc["err_tx"] = counters.lora_tx_fail;
|
||||
if (last_error != FaultType::None) {
|
||||
doc["err_last"] = static_cast<uint8_t>(last_error);
|
||||
doc["err_last_age"] = last_error_age_sec;
|
||||
}
|
||||
doc["err_last"] = static_cast<uint8_t>(last_error);
|
||||
doc["err_last_text"] = fault_text(last_error);
|
||||
doc["err_last_age"] = last_error != FaultType::None ? last_error_age_sec : 0;
|
||||
|
||||
String payload;
|
||||
size_t len = serializeJson(doc, payload);
|
||||
@@ -126,9 +138,6 @@ bool mqtt_publish_discovery(const char *device_id) {
|
||||
bool ok = true;
|
||||
ok = ok && publish_discovery_sensor(device_id, "energy", "Energy", "kWh", "energy", state_topic.c_str(), "{{ value_json.e_kwh }}");
|
||||
ok = ok && publish_discovery_sensor(device_id, "power", "Power", "W", "power", state_topic.c_str(), "{{ value_json.p_w }}");
|
||||
ok = ok && publish_discovery_sensor(device_id, "v1", "Voltage L1", "V", "voltage", state_topic.c_str(), "{{ value_json.v1_v }}");
|
||||
ok = ok && publish_discovery_sensor(device_id, "v2", "Voltage L2", "V", "voltage", state_topic.c_str(), "{{ value_json.v2_v }}");
|
||||
ok = ok && publish_discovery_sensor(device_id, "v3", "Voltage L3", "V", "voltage", state_topic.c_str(), "{{ value_json.v3_v }}");
|
||||
ok = ok && publish_discovery_sensor(device_id, "p1", "Power L1", "W", "power", state_topic.c_str(), "{{ value_json.p1_w }}");
|
||||
ok = ok && publish_discovery_sensor(device_id, "p2", "Power L2", "W", "power", state_topic.c_str(), "{{ value_json.p2_w }}");
|
||||
ok = ok && publish_discovery_sensor(device_id, "p3", "Power L3", "W", "power", state_topic.c_str(), "{{ value_json.p3_w }}");
|
||||
@@ -141,6 +150,8 @@ bool mqtt_publish_discovery(const char *device_id) {
|
||||
ok = ok && publish_discovery_sensor(device_id, "err_m", "Meter Read Errors", "count", "", faults_topic.c_str(), "{{ value_json.err_m }}");
|
||||
ok = ok && publish_discovery_sensor(device_id, "err_d", "Decode Errors", "count", "", faults_topic.c_str(), "{{ value_json.err_d }}");
|
||||
ok = ok && publish_discovery_sensor(device_id, "err_tx", "LoRa TX Errors", "count", "", faults_topic.c_str(), "{{ value_json.err_tx }}");
|
||||
ok = ok && publish_discovery_sensor(device_id, "err_last", "Last Error Code", "", "", faults_topic.c_str(), "{{ value_json.err_last }}");
|
||||
ok = ok && publish_discovery_sensor(device_id, "err_last_text", "Last Error", "", "", faults_topic.c_str(), "{{ value_json.err_last_text }}");
|
||||
ok = ok && publish_discovery_sensor(device_id, "err_last_age", "Last Error Age", "s", "", faults_topic.c_str(), "{{ value_json.err_last_age }}");
|
||||
return ok;
|
||||
}
|
||||
|
||||
356
src/payload_codec.cpp
Normal file
356
src/payload_codec.cpp
Normal file
@@ -0,0 +1,356 @@
|
||||
#include "payload_codec.h"
|
||||
#include <limits.h>
|
||||
|
||||
static constexpr uint16_t kMagic = 0xDDB3;
|
||||
static constexpr uint8_t kSchema = 2;
|
||||
static constexpr uint8_t kFlags = 0x01;
|
||||
static constexpr size_t kMaxSamples = 30;
|
||||
|
||||
static void write_u16_le(uint8_t *dst, uint16_t value) {
|
||||
dst[0] = static_cast<uint8_t>(value & 0xFF);
|
||||
dst[1] = static_cast<uint8_t>((value >> 8) & 0xFF);
|
||||
}
|
||||
|
||||
static void write_u32_le(uint8_t *dst, uint32_t value) {
|
||||
dst[0] = static_cast<uint8_t>(value & 0xFF);
|
||||
dst[1] = static_cast<uint8_t>((value >> 8) & 0xFF);
|
||||
dst[2] = static_cast<uint8_t>((value >> 16) & 0xFF);
|
||||
dst[3] = static_cast<uint8_t>((value >> 24) & 0xFF);
|
||||
}
|
||||
|
||||
static uint16_t read_u16_le(const uint8_t *src) {
|
||||
return static_cast<uint16_t>(src[0]) | (static_cast<uint16_t>(src[1]) << 8);
|
||||
}
|
||||
|
||||
static uint32_t read_u32_le(const uint8_t *src) {
|
||||
return static_cast<uint32_t>(src[0]) |
|
||||
(static_cast<uint32_t>(src[1]) << 8) |
|
||||
(static_cast<uint32_t>(src[2]) << 16) |
|
||||
(static_cast<uint32_t>(src[3]) << 24);
|
||||
}
|
||||
|
||||
size_t uleb128_encode(uint32_t v, uint8_t *out, size_t cap) {
|
||||
size_t i = 0;
|
||||
do {
|
||||
if (i >= cap) {
|
||||
return 0;
|
||||
}
|
||||
uint8_t byte = static_cast<uint8_t>(v & 0x7F);
|
||||
v >>= 7;
|
||||
if (v != 0) {
|
||||
byte |= 0x80;
|
||||
}
|
||||
out[i++] = byte;
|
||||
} while (v != 0);
|
||||
return i;
|
||||
}
|
||||
|
||||
bool uleb128_decode(const uint8_t *in, size_t len, size_t *pos, uint32_t *v) {
|
||||
if (!in || !pos || !v) {
|
||||
return false;
|
||||
}
|
||||
uint32_t result = 0;
|
||||
uint8_t shift = 0;
|
||||
size_t p = *pos;
|
||||
for (uint8_t i = 0; i < 5; ++i) {
|
||||
if (p >= len) {
|
||||
return false;
|
||||
}
|
||||
uint8_t byte = in[p++];
|
||||
if (i == 4 && (byte & 0xF0) != 0) {
|
||||
return false;
|
||||
}
|
||||
result |= static_cast<uint32_t>(byte & 0x7F) << shift;
|
||||
if ((byte & 0x80) == 0) {
|
||||
*pos = p;
|
||||
*v = result;
|
||||
return true;
|
||||
}
|
||||
shift = static_cast<uint8_t>(shift + 7);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
uint32_t zigzag32(int32_t x) {
|
||||
return (static_cast<uint32_t>(x) << 1) ^ static_cast<uint32_t>(x >> 31);
|
||||
}
|
||||
|
||||
int32_t unzigzag32(uint32_t u) {
|
||||
return static_cast<int32_t>((u >> 1) ^ (static_cast<uint32_t>(-static_cast<int32_t>(u & 1))));
|
||||
}
|
||||
|
||||
size_t svarint_encode(int32_t x, uint8_t *out, size_t cap) {
|
||||
uint32_t zz = zigzag32(x);
|
||||
return uleb128_encode(zz, out, cap);
|
||||
}
|
||||
|
||||
bool svarint_decode(const uint8_t *in, size_t len, size_t *pos, int32_t *x) {
|
||||
uint32_t u = 0;
|
||||
if (!uleb128_decode(in, len, pos, &u)) {
|
||||
return false;
|
||||
}
|
||||
*x = unzigzag32(u);
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool ensure_capacity(size_t needed, size_t cap, size_t pos) {
|
||||
return pos + needed <= cap;
|
||||
}
|
||||
|
||||
bool encode_batch(const BatchInput &in, uint8_t *out, size_t out_cap, size_t *out_len) {
|
||||
if (!out || !out_len) {
|
||||
return false;
|
||||
}
|
||||
if (in.n > kMaxSamples) {
|
||||
return false;
|
||||
}
|
||||
if (in.dt_s == 0) {
|
||||
return false;
|
||||
}
|
||||
size_t pos = 0;
|
||||
if (!ensure_capacity(21, out_cap, pos)) {
|
||||
return false;
|
||||
}
|
||||
write_u16_le(&out[pos], kMagic);
|
||||
pos += 2;
|
||||
out[pos++] = kSchema;
|
||||
out[pos++] = kFlags;
|
||||
write_u16_le(&out[pos], in.sender_id);
|
||||
pos += 2;
|
||||
write_u16_le(&out[pos], in.batch_id);
|
||||
pos += 2;
|
||||
write_u32_le(&out[pos], in.t_last);
|
||||
pos += 4;
|
||||
out[pos++] = in.dt_s;
|
||||
out[pos++] = in.n;
|
||||
write_u16_le(&out[pos], in.battery_mV);
|
||||
pos += 2;
|
||||
out[pos++] = in.err_m;
|
||||
out[pos++] = in.err_d;
|
||||
out[pos++] = in.err_tx;
|
||||
out[pos++] = in.err_last;
|
||||
out[pos++] = in.err_rx_reject;
|
||||
|
||||
if (in.n == 0) {
|
||||
*out_len = pos;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!ensure_capacity(4, out_cap, pos)) {
|
||||
return false;
|
||||
}
|
||||
write_u32_le(&out[pos], in.energy_wh[0]);
|
||||
pos += 4;
|
||||
for (uint8_t i = 1; i < in.n; ++i) {
|
||||
if (in.energy_wh[i] < in.energy_wh[i - 1]) {
|
||||
return false;
|
||||
}
|
||||
uint32_t delta = in.energy_wh[i] - in.energy_wh[i - 1];
|
||||
size_t wrote = uleb128_encode(delta, &out[pos], out_cap - pos);
|
||||
if (wrote == 0) {
|
||||
return false;
|
||||
}
|
||||
pos += wrote;
|
||||
}
|
||||
|
||||
auto encode_phase = [&](const int16_t *phase) -> bool {
|
||||
if (!ensure_capacity(2, out_cap, pos)) {
|
||||
return false;
|
||||
}
|
||||
write_u16_le(&out[pos], static_cast<uint16_t>(phase[0]));
|
||||
pos += 2;
|
||||
for (uint8_t i = 1; i < in.n; ++i) {
|
||||
int32_t delta = static_cast<int32_t>(phase[i]) - static_cast<int32_t>(phase[i - 1]);
|
||||
size_t wrote = svarint_encode(delta, &out[pos], out_cap - pos);
|
||||
if (wrote == 0) {
|
||||
return false;
|
||||
}
|
||||
pos += wrote;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
if (!encode_phase(in.p1_w)) {
|
||||
return false;
|
||||
}
|
||||
if (!encode_phase(in.p2_w)) {
|
||||
return false;
|
||||
}
|
||||
if (!encode_phase(in.p3_w)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
*out_len = pos;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool decode_batch(const uint8_t *buf, size_t len, BatchInput *out) {
|
||||
if (!buf || !out) {
|
||||
return false;
|
||||
}
|
||||
size_t pos = 0;
|
||||
if (len < 21) {
|
||||
return false;
|
||||
}
|
||||
uint16_t magic = read_u16_le(&buf[pos]);
|
||||
pos += 2;
|
||||
uint8_t schema = buf[pos++];
|
||||
uint8_t flags = buf[pos++];
|
||||
if (magic != kMagic || schema != kSchema || (flags & 0x01) == 0) {
|
||||
return false;
|
||||
}
|
||||
out->sender_id = read_u16_le(&buf[pos]);
|
||||
pos += 2;
|
||||
out->batch_id = read_u16_le(&buf[pos]);
|
||||
pos += 2;
|
||||
out->t_last = read_u32_le(&buf[pos]);
|
||||
pos += 4;
|
||||
out->dt_s = buf[pos++];
|
||||
out->n = buf[pos++];
|
||||
out->battery_mV = read_u16_le(&buf[pos]);
|
||||
pos += 2;
|
||||
out->err_m = buf[pos++];
|
||||
out->err_d = buf[pos++];
|
||||
out->err_tx = buf[pos++];
|
||||
out->err_last = buf[pos++];
|
||||
out->err_rx_reject = buf[pos++];
|
||||
|
||||
if (out->n > kMaxSamples || out->dt_s == 0) {
|
||||
return false;
|
||||
}
|
||||
if (out->n == 0) {
|
||||
for (uint8_t i = 0; i < kMaxSamples; ++i) {
|
||||
out->energy_wh[i] = 0;
|
||||
out->p1_w[i] = 0;
|
||||
out->p2_w[i] = 0;
|
||||
out->p3_w[i] = 0;
|
||||
}
|
||||
return pos == len;
|
||||
}
|
||||
if (pos + 4 > len) {
|
||||
return false;
|
||||
}
|
||||
out->energy_wh[0] = read_u32_le(&buf[pos]);
|
||||
pos += 4;
|
||||
for (uint8_t i = 1; i < out->n; ++i) {
|
||||
uint32_t delta = 0;
|
||||
if (!uleb128_decode(buf, len, &pos, &delta)) {
|
||||
return false;
|
||||
}
|
||||
uint64_t sum = static_cast<uint64_t>(out->energy_wh[i - 1]) + static_cast<uint64_t>(delta);
|
||||
if (sum > UINT32_MAX) {
|
||||
return false;
|
||||
}
|
||||
out->energy_wh[i] = static_cast<uint32_t>(sum);
|
||||
}
|
||||
|
||||
auto decode_phase = [&](int16_t *phase) -> bool {
|
||||
if (pos + 2 > len) {
|
||||
return false;
|
||||
}
|
||||
phase[0] = static_cast<int16_t>(read_u16_le(&buf[pos]));
|
||||
pos += 2;
|
||||
int32_t prev = static_cast<int32_t>(phase[0]);
|
||||
for (uint8_t i = 1; i < out->n; ++i) {
|
||||
int32_t delta = 0;
|
||||
if (!svarint_decode(buf, len, &pos, &delta)) {
|
||||
return false;
|
||||
}
|
||||
int32_t value = prev + delta;
|
||||
if (value < INT16_MIN || value > INT16_MAX) {
|
||||
return false;
|
||||
}
|
||||
phase[i] = static_cast<int16_t>(value);
|
||||
prev = value;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
if (!decode_phase(out->p1_w)) {
|
||||
return false;
|
||||
}
|
||||
if (!decode_phase(out->p2_w)) {
|
||||
return false;
|
||||
}
|
||||
if (!decode_phase(out->p3_w)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (uint8_t i = out->n; i < kMaxSamples; ++i) {
|
||||
out->energy_wh[i] = 0;
|
||||
out->p1_w[i] = 0;
|
||||
out->p2_w[i] = 0;
|
||||
out->p3_w[i] = 0;
|
||||
}
|
||||
|
||||
return pos == len;
|
||||
}
|
||||
|
||||
#ifdef PAYLOAD_CODEC_TEST
|
||||
bool payload_codec_self_test() {
|
||||
BatchInput in = {};
|
||||
in.sender_id = 1;
|
||||
in.batch_id = 42;
|
||||
in.t_last = 1700000000;
|
||||
in.dt_s = 1;
|
||||
in.n = 5;
|
||||
in.battery_mV = 3750;
|
||||
in.err_m = 2;
|
||||
in.err_d = 1;
|
||||
in.err_tx = 3;
|
||||
in.err_last = 2;
|
||||
in.err_rx_reject = 1;
|
||||
in.energy_wh[0] = 100000;
|
||||
in.energy_wh[1] = 100001;
|
||||
in.energy_wh[2] = 100050;
|
||||
in.energy_wh[3] = 100050;
|
||||
in.energy_wh[4] = 100200;
|
||||
in.p1_w[0] = -120;
|
||||
in.p1_w[1] = -90;
|
||||
in.p1_w[2] = 1910;
|
||||
in.p1_w[3] = -90;
|
||||
in.p1_w[4] = 500;
|
||||
in.p2_w[0] = 50;
|
||||
in.p2_w[1] = -1950;
|
||||
in.p2_w[2] = 60;
|
||||
in.p2_w[3] = 2060;
|
||||
in.p2_w[4] = -10;
|
||||
in.p3_w[0] = 0;
|
||||
in.p3_w[1] = 10;
|
||||
in.p3_w[2] = -1990;
|
||||
in.p3_w[3] = 10;
|
||||
in.p3_w[4] = 20;
|
||||
|
||||
uint8_t buf[256];
|
||||
size_t len = 0;
|
||||
if (!encode_batch(in, buf, sizeof(buf), &len)) {
|
||||
Serial.println("payload_codec_self_test: encode failed");
|
||||
return false;
|
||||
}
|
||||
|
||||
BatchInput out = {};
|
||||
if (!decode_batch(buf, len, &out)) {
|
||||
Serial.println("payload_codec_self_test: decode failed");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (out.sender_id != in.sender_id || out.batch_id != in.batch_id || out.t_last != in.t_last ||
|
||||
out.dt_s != in.dt_s || out.n != in.n || out.battery_mV != in.battery_mV ||
|
||||
out.err_m != in.err_m || out.err_d != in.err_d || out.err_tx != in.err_tx || out.err_last != in.err_last ||
|
||||
out.err_rx_reject != in.err_rx_reject) {
|
||||
Serial.println("payload_codec_self_test: header mismatch");
|
||||
return false;
|
||||
}
|
||||
|
||||
for (uint8_t i = 0; i < in.n; ++i) {
|
||||
if (out.energy_wh[i] != in.energy_wh[i] || out.p1_w[i] != in.p1_w[i] || out.p2_w[i] != in.p2_w[i] ||
|
||||
out.p3_w[i] != in.p3_w[i]) {
|
||||
Serial.println("payload_codec_self_test: sample mismatch");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Serial.printf("payload_codec_self_test: ok len=%u\n", static_cast<unsigned>(len));
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
37
src/payload_codec.h
Normal file
37
src/payload_codec.h
Normal file
@@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
struct BatchInput {
|
||||
uint16_t sender_id;
|
||||
uint16_t batch_id;
|
||||
uint32_t t_last;
|
||||
uint8_t dt_s;
|
||||
uint8_t n;
|
||||
uint16_t battery_mV;
|
||||
uint8_t err_m;
|
||||
uint8_t err_d;
|
||||
uint8_t err_tx;
|
||||
uint8_t err_last;
|
||||
uint8_t err_rx_reject;
|
||||
uint32_t energy_wh[30];
|
||||
int16_t p1_w[30];
|
||||
int16_t p2_w[30];
|
||||
int16_t p3_w[30];
|
||||
};
|
||||
|
||||
bool encode_batch(const BatchInput &in, uint8_t *out, size_t out_cap, size_t *out_len);
|
||||
bool decode_batch(const uint8_t *buf, size_t len, BatchInput *out);
|
||||
|
||||
size_t uleb128_encode(uint32_t v, uint8_t *out, size_t cap);
|
||||
bool uleb128_decode(const uint8_t *in, size_t len, size_t *pos, uint32_t *v);
|
||||
|
||||
uint32_t zigzag32(int32_t x);
|
||||
int32_t unzigzag32(uint32_t u);
|
||||
|
||||
size_t svarint_encode(int32_t x, uint8_t *out, size_t cap);
|
||||
bool svarint_decode(const uint8_t *in, size_t len, size_t *pos, int32_t *x);
|
||||
|
||||
#ifdef PAYLOAD_CODEC_TEST
|
||||
bool payload_codec_self_test();
|
||||
#endif
|
||||
@@ -6,11 +6,13 @@
|
||||
#include <esp_sleep.h>
|
||||
|
||||
static constexpr float BATTERY_DIVIDER = 2.0f;
|
||||
static constexpr float BATTERY_CAL = 1.0f;
|
||||
static constexpr float ADC_REF_V = 3.3f;
|
||||
|
||||
void power_sender_init() {
|
||||
setCpuFrequencyMhz(80);
|
||||
WiFi.mode(WIFI_OFF);
|
||||
esp_wifi_stop();
|
||||
esp_wifi_deinit();
|
||||
btStop();
|
||||
analogReadResolution(12);
|
||||
pinMode(PIN_BAT_ADC, INPUT);
|
||||
@@ -22,29 +24,89 @@ void power_receiver_init() {
|
||||
pinMode(PIN_BAT_ADC, INPUT);
|
||||
}
|
||||
|
||||
void read_battery(MeterData &data) {
|
||||
const int samples = 8;
|
||||
uint32_t sum = 0;
|
||||
for (int i = 0; i < samples; ++i) {
|
||||
sum += analogRead(PIN_BAT_ADC);
|
||||
delay(5);
|
||||
void power_configure_unused_pins_sender() {
|
||||
// Board-specific: only touch pins that are known unused and safe on TTGO LoRa32 v1.6.1
|
||||
const uint8_t pins[] = {32, 33};
|
||||
for (uint8_t pin : pins) {
|
||||
pinMode(pin, INPUT_PULLDOWN);
|
||||
}
|
||||
float avg = static_cast<float>(sum) / samples;
|
||||
}
|
||||
|
||||
void read_battery(MeterData &data) {
|
||||
uint32_t sum = 0;
|
||||
uint16_t samples[5] = {};
|
||||
for (uint8_t i = 0; i < 5; ++i) {
|
||||
samples[i] = analogRead(PIN_BAT_ADC);
|
||||
sum += samples[i];
|
||||
}
|
||||
float avg = static_cast<float>(sum) / 5.0f;
|
||||
float v = (avg / 4095.0f) * ADC_REF_V * BATTERY_DIVIDER * BATTERY_CAL;
|
||||
if (SERIAL_DEBUG_MODE) {
|
||||
Serial.printf("bat_adc: %u %u %u %u %u avg=%.1f v=%.3f\n",
|
||||
samples[0], samples[1], samples[2], samples[3], samples[4],
|
||||
static_cast<double>(avg), static_cast<double>(v));
|
||||
}
|
||||
|
||||
data.battery_voltage_v = v;
|
||||
data.battery_percent = battery_percent_from_voltage(v);
|
||||
}
|
||||
|
||||
uint8_t battery_percent_from_voltage(float voltage_v) {
|
||||
float pct = (voltage_v - 3.0f) / (4.2f - 3.0f) * 100.0f;
|
||||
if (pct < 0.0f) {
|
||||
pct = 0.0f;
|
||||
if (isnan(voltage_v)) {
|
||||
return 0;
|
||||
}
|
||||
if (pct > 100.0f) {
|
||||
pct = 100.0f;
|
||||
struct LutPoint {
|
||||
float v;
|
||||
uint8_t pct;
|
||||
};
|
||||
static const LutPoint kCurve[] = {
|
||||
{4.20f, 100},
|
||||
{4.15f, 95},
|
||||
{4.11f, 90},
|
||||
{4.08f, 85},
|
||||
{4.02f, 80},
|
||||
{3.98f, 75},
|
||||
{3.95f, 70},
|
||||
{3.91f, 60},
|
||||
{3.87f, 50},
|
||||
{3.85f, 45},
|
||||
{3.84f, 40},
|
||||
{3.82f, 35},
|
||||
{3.80f, 30},
|
||||
{3.77f, 25},
|
||||
{3.75f, 20},
|
||||
{3.73f, 15},
|
||||
{3.70f, 10},
|
||||
{3.65f, 5},
|
||||
{3.60f, 2},
|
||||
{2.90f, 0},
|
||||
};
|
||||
if (voltage_v >= kCurve[0].v) {
|
||||
return kCurve[0].pct;
|
||||
}
|
||||
return static_cast<uint8_t>(pct + 0.5f);
|
||||
if (voltage_v <= kCurve[sizeof(kCurve) / sizeof(kCurve[0]) - 1].v) {
|
||||
return 0;
|
||||
}
|
||||
for (size_t i = 0; i + 1 < sizeof(kCurve) / sizeof(kCurve[0]); ++i) {
|
||||
const LutPoint &hi = kCurve[i];
|
||||
const LutPoint &lo = kCurve[i + 1];
|
||||
if (voltage_v <= hi.v && voltage_v >= lo.v) {
|
||||
float span = hi.v - lo.v;
|
||||
if (span <= 0.0f) {
|
||||
return lo.pct;
|
||||
}
|
||||
float t = (voltage_v - lo.v) / span;
|
||||
float pct = lo.pct + t * (hi.pct - lo.pct);
|
||||
if (pct < 0.0f) {
|
||||
pct = 0.0f;
|
||||
}
|
||||
if (pct > 100.0f) {
|
||||
pct = 100.0f;
|
||||
}
|
||||
return static_cast<uint8_t>(pct + 0.5f);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void light_sleep_ms(uint32_t ms) {
|
||||
|
||||
@@ -1,122 +0,0 @@
|
||||
#include "rtc_ds3231.h"
|
||||
#include "config.h"
|
||||
#include <Wire.h>
|
||||
#include <time.h>
|
||||
|
||||
static constexpr uint8_t DS3231_ADDR = 0x68;
|
||||
|
||||
static uint8_t bcd_to_dec(uint8_t val) {
|
||||
return static_cast<uint8_t>((val >> 4) * 10 + (val & 0x0F));
|
||||
}
|
||||
|
||||
static uint8_t dec_to_bcd(uint8_t val) {
|
||||
return static_cast<uint8_t>(((val / 10) << 4) | (val % 10));
|
||||
}
|
||||
|
||||
static time_t timegm_fallback(struct tm *tm_utc) {
|
||||
if (!tm_utc) {
|
||||
return static_cast<time_t>(-1);
|
||||
}
|
||||
char *old_tz = getenv("TZ");
|
||||
setenv("TZ", "UTC0", 1);
|
||||
tzset();
|
||||
time_t t = mktime(tm_utc);
|
||||
if (old_tz) {
|
||||
setenv("TZ", old_tz, 1);
|
||||
} else {
|
||||
unsetenv("TZ");
|
||||
}
|
||||
tzset();
|
||||
return t;
|
||||
}
|
||||
|
||||
static bool read_registers(uint8_t start_reg, uint8_t *out, size_t len) {
|
||||
if (!out || len == 0) {
|
||||
return false;
|
||||
}
|
||||
Wire.beginTransmission(DS3231_ADDR);
|
||||
Wire.write(start_reg);
|
||||
if (Wire.endTransmission(false) != 0) {
|
||||
return false;
|
||||
}
|
||||
size_t read = Wire.requestFrom(DS3231_ADDR, static_cast<uint8_t>(len));
|
||||
if (read != len) {
|
||||
return false;
|
||||
}
|
||||
for (size_t i = 0; i < len; ++i) {
|
||||
out[i] = Wire.read();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool write_registers(uint8_t start_reg, const uint8_t *data, size_t len) {
|
||||
if (!data || len == 0) {
|
||||
return false;
|
||||
}
|
||||
Wire.beginTransmission(DS3231_ADDR);
|
||||
Wire.write(start_reg);
|
||||
for (size_t i = 0; i < len; ++i) {
|
||||
Wire.write(data[i]);
|
||||
}
|
||||
return Wire.endTransmission() == 0;
|
||||
}
|
||||
|
||||
bool rtc_ds3231_init() {
|
||||
Wire.begin(PIN_OLED_SDA, PIN_OLED_SCL);
|
||||
Wire.setClock(100000);
|
||||
return rtc_ds3231_is_present();
|
||||
}
|
||||
|
||||
bool rtc_ds3231_is_present() {
|
||||
Wire.beginTransmission(DS3231_ADDR);
|
||||
return Wire.endTransmission() == 0;
|
||||
}
|
||||
|
||||
bool rtc_ds3231_read_epoch(uint32_t &epoch_utc) {
|
||||
uint8_t regs[7] = {};
|
||||
if (!read_registers(0x00, regs, sizeof(regs))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t sec = bcd_to_dec(regs[0] & 0x7F);
|
||||
uint8_t min = bcd_to_dec(regs[1] & 0x7F);
|
||||
uint8_t hour = bcd_to_dec(regs[2] & 0x3F);
|
||||
uint8_t day = bcd_to_dec(regs[4] & 0x3F);
|
||||
uint8_t month = bcd_to_dec(regs[5] & 0x1F);
|
||||
uint16_t year = 2000 + bcd_to_dec(regs[6]);
|
||||
|
||||
struct tm tm_utc = {};
|
||||
tm_utc.tm_sec = sec;
|
||||
tm_utc.tm_min = min;
|
||||
tm_utc.tm_hour = hour;
|
||||
tm_utc.tm_mday = day;
|
||||
tm_utc.tm_mon = month - 1;
|
||||
tm_utc.tm_year = year - 1900;
|
||||
tm_utc.tm_isdst = 0;
|
||||
|
||||
time_t t = timegm_fallback(&tm_utc);
|
||||
if (t <= 0) {
|
||||
return false;
|
||||
}
|
||||
epoch_utc = static_cast<uint32_t>(t);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool rtc_ds3231_set_epoch(uint32_t epoch_utc) {
|
||||
time_t t = static_cast<time_t>(epoch_utc);
|
||||
struct tm tm_utc = {};
|
||||
if (!gmtime_r(&t, &tm_utc)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
uint8_t regs[7] = {};
|
||||
regs[0] = dec_to_bcd(static_cast<uint8_t>(tm_utc.tm_sec));
|
||||
regs[1] = dec_to_bcd(static_cast<uint8_t>(tm_utc.tm_min));
|
||||
regs[2] = dec_to_bcd(static_cast<uint8_t>(tm_utc.tm_hour));
|
||||
regs[3] = dec_to_bcd(static_cast<uint8_t>(tm_utc.tm_wday + 1));
|
||||
regs[4] = dec_to_bcd(static_cast<uint8_t>(tm_utc.tm_mday));
|
||||
regs[5] = dec_to_bcd(static_cast<uint8_t>(tm_utc.tm_mon + 1));
|
||||
regs[6] = dec_to_bcd(static_cast<uint8_t>((tm_utc.tm_year + 1900) - 2000));
|
||||
|
||||
return write_registers(0x00, regs, sizeof(regs));
|
||||
}
|
||||
128
src/sd_logger.cpp
Normal file
128
src/sd_logger.cpp
Normal file
@@ -0,0 +1,128 @@
|
||||
#include "sd_logger.h"
|
||||
#include "config.h"
|
||||
#include <SD.h>
|
||||
#include <SPI.h>
|
||||
#include <time.h>
|
||||
|
||||
static bool g_sd_ready = false;
|
||||
static SPIClass *g_sd_spi = nullptr;
|
||||
|
||||
static const char *fault_text(FaultType fault) {
|
||||
switch (fault) {
|
||||
case FaultType::MeterRead:
|
||||
return "meter";
|
||||
case FaultType::Decode:
|
||||
return "decode";
|
||||
case FaultType::LoraTx:
|
||||
return "loratx";
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
static bool ensure_dir(const String &path) {
|
||||
if (SD.exists(path)) {
|
||||
return true;
|
||||
}
|
||||
return SD.mkdir(path);
|
||||
}
|
||||
|
||||
static String format_date_utc(uint32_t ts_utc) {
|
||||
time_t t = static_cast<time_t>(ts_utc);
|
||||
struct tm tm_utc;
|
||||
gmtime_r(&t, &tm_utc);
|
||||
char buf[16];
|
||||
snprintf(buf, sizeof(buf), "%04d-%02d-%02d",
|
||||
tm_utc.tm_year + 1900,
|
||||
tm_utc.tm_mon + 1,
|
||||
tm_utc.tm_mday);
|
||||
return String(buf);
|
||||
}
|
||||
|
||||
void sd_logger_init() {
|
||||
if (!ENABLE_SD_LOGGING) {
|
||||
g_sd_ready = false;
|
||||
return;
|
||||
}
|
||||
if (!g_sd_spi) {
|
||||
g_sd_spi = new SPIClass(HSPI);
|
||||
}
|
||||
g_sd_spi->begin(PIN_SD_SCK, PIN_SD_MISO, PIN_SD_MOSI, PIN_SD_CS);
|
||||
g_sd_ready = SD.begin(PIN_SD_CS, *g_sd_spi);
|
||||
if (SERIAL_DEBUG_MODE) {
|
||||
if (g_sd_ready) {
|
||||
uint8_t type = SD.cardType();
|
||||
uint64_t size = SD.cardSize();
|
||||
Serial.printf("sd: ok type=%u size=%llu\n", static_cast<unsigned>(type), static_cast<unsigned long long>(size));
|
||||
} else {
|
||||
Serial.println("sd: init failed");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool sd_logger_is_ready() {
|
||||
return g_sd_ready;
|
||||
}
|
||||
|
||||
void sd_logger_log_sample(const MeterData &data, bool include_error_text) {
|
||||
if (!g_sd_ready || data.ts_utc == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
String root_dir = "/dd3";
|
||||
if (!ensure_dir(root_dir)) {
|
||||
return;
|
||||
}
|
||||
|
||||
String sender_dir = root_dir + "/" + String(data.device_id);
|
||||
if (!ensure_dir(sender_dir)) {
|
||||
return;
|
||||
}
|
||||
|
||||
String filename = sender_dir + "/" + format_date_utc(data.ts_utc) + ".csv";
|
||||
bool new_file = !SD.exists(filename);
|
||||
File f = SD.open(filename, FILE_APPEND);
|
||||
if (!f) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (new_file) {
|
||||
f.println("ts_utc,p_w,p1_w,p2_w,p3_w,e_kwh,bat_v,bat_pct,rssi,snr,err_m,err_d,err_tx,err_last");
|
||||
}
|
||||
|
||||
f.print(data.ts_utc);
|
||||
f.print(',');
|
||||
f.print(data.total_power_w, 1);
|
||||
f.print(',');
|
||||
f.print(data.phase_power_w[0], 1);
|
||||
f.print(',');
|
||||
f.print(data.phase_power_w[1], 1);
|
||||
f.print(',');
|
||||
f.print(data.phase_power_w[2], 1);
|
||||
f.print(',');
|
||||
f.print(data.energy_total_kwh, 3);
|
||||
f.print(',');
|
||||
f.print(data.battery_voltage_v, 2);
|
||||
f.print(',');
|
||||
f.print(data.battery_percent);
|
||||
f.print(',');
|
||||
f.print(data.link_rssi_dbm);
|
||||
f.print(',');
|
||||
if (isnan(data.link_snr_db)) {
|
||||
f.print("");
|
||||
} else {
|
||||
f.print(data.link_snr_db, 1);
|
||||
}
|
||||
f.print(',');
|
||||
f.print(data.err_meter_read);
|
||||
f.print(',');
|
||||
f.print(data.err_decode);
|
||||
f.print(',');
|
||||
f.print(data.err_lora_tx);
|
||||
f.print(',');
|
||||
if (include_error_text && data.last_error != FaultType::None) {
|
||||
f.print(fault_text(data.last_error));
|
||||
}
|
||||
f.println();
|
||||
f.close();
|
||||
}
|
||||
7
src/sd_logger.h
Normal file
7
src/sd_logger.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "data_model.h"
|
||||
|
||||
void sd_logger_init();
|
||||
bool sd_logger_is_ready();
|
||||
void sd_logger_log_sample(const MeterData &data, bool include_error_text);
|
||||
@@ -2,9 +2,7 @@
|
||||
|
||||
#ifdef ENABLE_TEST_MODE
|
||||
#include "config.h"
|
||||
#include "compressor.h"
|
||||
#include "lora_transport.h"
|
||||
#include "json_codec.h"
|
||||
#include "time_manager.h"
|
||||
#include "display_ui.h"
|
||||
#include "mqtt_client.h"
|
||||
@@ -14,16 +12,9 @@
|
||||
|
||||
static uint32_t g_last_test_ms = 0;
|
||||
static uint16_t g_test_code_counter = 1000;
|
||||
static uint32_t g_last_timesync_ms = 0;
|
||||
static constexpr uint32_t TEST_SEND_INTERVAL_MS = 30000;
|
||||
static constexpr uint32_t TEST_TIMESYNC_OFFSET_MS = 15000;
|
||||
|
||||
void test_sender_loop(uint16_t short_id, const char *device_id) {
|
||||
LoraPacket rx = {};
|
||||
if (lora_receive(rx, 0) && rx.payload_type == PayloadType::TimeSync) {
|
||||
time_handle_timesync_payload(rx.payload, rx.payload_len);
|
||||
}
|
||||
|
||||
if (millis() - g_last_test_ms < TEST_SEND_INTERVAL_MS) {
|
||||
return;
|
||||
}
|
||||
@@ -60,48 +51,34 @@ void test_sender_loop(uint16_t short_id, const char *device_id) {
|
||||
data.ts_utc = ts;
|
||||
display_set_last_meter(data);
|
||||
|
||||
uint8_t compressed[LORA_MAX_PAYLOAD];
|
||||
size_t compressed_len = 0;
|
||||
if (!compressBuffer(reinterpret_cast<const uint8_t *>(json.c_str()), json.length(), compressed, sizeof(compressed), compressed_len)) {
|
||||
if (json.length() > LORA_MAX_PAYLOAD) {
|
||||
return;
|
||||
}
|
||||
|
||||
LoraPacket pkt = {};
|
||||
pkt.protocol_version = PROTOCOL_VERSION;
|
||||
pkt.role = DeviceRole::Sender;
|
||||
pkt.msg_kind = LoraMsgKind::BatchUp;
|
||||
pkt.device_id_short = short_id;
|
||||
pkt.payload_type = PayloadType::TestCode;
|
||||
pkt.payload_len = compressed_len;
|
||||
memcpy(pkt.payload, compressed, compressed_len);
|
||||
pkt.payload_len = json.length();
|
||||
memcpy(pkt.payload, json.c_str(), pkt.payload_len);
|
||||
lora_send(pkt);
|
||||
}
|
||||
|
||||
void test_receiver_loop(SenderStatus *statuses, uint8_t count, uint16_t self_short_id) {
|
||||
if (g_last_timesync_ms == 0) {
|
||||
g_last_timesync_ms = millis() - (TIME_SYNC_INTERVAL_SEC * 1000UL - TEST_TIMESYNC_OFFSET_MS);
|
||||
}
|
||||
if (millis() - g_last_timesync_ms > TIME_SYNC_INTERVAL_SEC * 1000UL) {
|
||||
g_last_timesync_ms = millis();
|
||||
time_send_timesync(self_short_id);
|
||||
}
|
||||
|
||||
(void)self_short_id;
|
||||
LoraPacket pkt = {};
|
||||
if (!lora_receive(pkt, 0)) {
|
||||
return;
|
||||
}
|
||||
if (pkt.payload_type != PayloadType::TestCode) {
|
||||
if (pkt.msg_kind != LoraMsgKind::BatchUp) {
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t decompressed[160];
|
||||
size_t decompressed_len = 0;
|
||||
if (!decompressBuffer(pkt.payload, pkt.payload_len, decompressed, sizeof(decompressed) - 1, decompressed_len)) {
|
||||
if (pkt.payload_len >= sizeof(decompressed)) {
|
||||
return;
|
||||
}
|
||||
if (decompressed_len >= sizeof(decompressed)) {
|
||||
return;
|
||||
}
|
||||
decompressed[decompressed_len] = '\0';
|
||||
memcpy(decompressed, pkt.payload, pkt.payload_len);
|
||||
decompressed[pkt.payload_len] = '\0';
|
||||
|
||||
StaticJsonDocument<128> doc;
|
||||
if (deserializeJson(doc, reinterpret_cast<const char *>(decompressed)) != DeserializationError::Ok) {
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
#include "time_manager.h"
|
||||
#include "compressor.h"
|
||||
#include "config.h"
|
||||
#include "rtc_ds3231.h"
|
||||
#include <time.h>
|
||||
|
||||
static bool g_time_synced = false;
|
||||
static bool g_tz_set = false;
|
||||
static bool g_rtc_present = false;
|
||||
static uint32_t g_last_sync_utc = 0;
|
||||
|
||||
static void note_last_sync(uint32_t epoch) {
|
||||
@@ -16,15 +13,20 @@ static void note_last_sync(uint32_t epoch) {
|
||||
g_last_sync_utc = epoch;
|
||||
}
|
||||
|
||||
static void ensure_timezone_set() {
|
||||
if (g_tz_set) {
|
||||
return;
|
||||
}
|
||||
setenv("TZ", TIMEZONE_TZ, 1);
|
||||
tzset();
|
||||
g_tz_set = true;
|
||||
}
|
||||
|
||||
void time_receiver_init(const char *ntp_server_1, const char *ntp_server_2) {
|
||||
const char *server1 = (ntp_server_1 && ntp_server_1[0] != '\0') ? ntp_server_1 : "pool.ntp.org";
|
||||
const char *server2 = (ntp_server_2 && ntp_server_2[0] != '\0') ? ntp_server_2 : "time.nist.gov";
|
||||
configTime(0, 0, server1, server2);
|
||||
if (!g_tz_set) {
|
||||
setenv("TZ", "CET-1CEST,M3.5.0/2,M10.5.0/3", 1);
|
||||
tzset();
|
||||
g_tz_set = true;
|
||||
}
|
||||
ensure_timezone_set();
|
||||
}
|
||||
|
||||
uint32_t time_get_utc() {
|
||||
@@ -44,70 +46,13 @@ bool time_is_synced() {
|
||||
}
|
||||
|
||||
void time_set_utc(uint32_t epoch) {
|
||||
if (!g_tz_set) {
|
||||
setenv("TZ", "CET-1CEST,M3.5.0/2,M10.5.0/3", 1);
|
||||
tzset();
|
||||
g_tz_set = true;
|
||||
}
|
||||
ensure_timezone_set();
|
||||
struct timeval tv;
|
||||
tv.tv_sec = epoch;
|
||||
tv.tv_usec = 0;
|
||||
settimeofday(&tv, nullptr);
|
||||
g_time_synced = true;
|
||||
note_last_sync(epoch);
|
||||
|
||||
if (g_rtc_present) {
|
||||
rtc_ds3231_set_epoch(epoch);
|
||||
}
|
||||
}
|
||||
|
||||
bool time_send_timesync(uint16_t device_id_short) {
|
||||
uint32_t epoch = time_get_utc();
|
||||
if (epoch == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
char payload_str[32];
|
||||
snprintf(payload_str, sizeof(payload_str), "T:%lu", static_cast<unsigned long>(epoch));
|
||||
|
||||
uint8_t compressed[LORA_MAX_PAYLOAD];
|
||||
size_t compressed_len = 0;
|
||||
if (!compressBuffer(reinterpret_cast<const uint8_t *>(payload_str), strlen(payload_str), compressed, sizeof(compressed), compressed_len)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
LoraPacket pkt = {};
|
||||
pkt.protocol_version = PROTOCOL_VERSION;
|
||||
pkt.role = DeviceRole::Receiver;
|
||||
pkt.device_id_short = device_id_short;
|
||||
pkt.payload_type = PayloadType::TimeSync;
|
||||
pkt.payload_len = compressed_len;
|
||||
memcpy(pkt.payload, compressed, compressed_len);
|
||||
return lora_send(pkt);
|
||||
}
|
||||
|
||||
bool time_handle_timesync_payload(const uint8_t *payload, size_t len) {
|
||||
uint8_t decompressed[64];
|
||||
size_t decompressed_len = 0;
|
||||
if (!decompressBuffer(payload, len, decompressed, sizeof(decompressed), decompressed_len)) {
|
||||
return false;
|
||||
}
|
||||
if (decompressed_len >= sizeof(decompressed)) {
|
||||
return false;
|
||||
}
|
||||
decompressed[decompressed_len] = '\0';
|
||||
|
||||
if (decompressed_len < 3 || decompressed[0] != 'T' || decompressed[1] != ':') {
|
||||
return false;
|
||||
}
|
||||
|
||||
uint32_t epoch = static_cast<uint32_t>(strtoul(reinterpret_cast<const char *>(decompressed + 2), nullptr, 10));
|
||||
if (epoch == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
time_set_utc(epoch);
|
||||
return true;
|
||||
}
|
||||
|
||||
void time_get_local_hhmm(char *out, size_t out_len) {
|
||||
@@ -121,33 +66,6 @@ void time_get_local_hhmm(char *out, size_t out_len) {
|
||||
snprintf(out, out_len, "%02d:%02d", timeinfo.tm_hour, timeinfo.tm_min);
|
||||
}
|
||||
|
||||
void time_rtc_init() {
|
||||
if (!ENABLE_DS3231) {
|
||||
g_rtc_present = false;
|
||||
return;
|
||||
}
|
||||
g_rtc_present = rtc_ds3231_init();
|
||||
}
|
||||
|
||||
bool time_try_load_from_rtc() {
|
||||
if (!g_rtc_present) {
|
||||
return false;
|
||||
}
|
||||
if (time_is_synced()) {
|
||||
return true;
|
||||
}
|
||||
uint32_t epoch = 0;
|
||||
if (!rtc_ds3231_read_epoch(epoch) || epoch == 0) {
|
||||
return false;
|
||||
}
|
||||
time_set_utc(epoch);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool time_rtc_present() {
|
||||
return g_rtc_present;
|
||||
}
|
||||
|
||||
uint32_t time_get_last_sync_utc() {
|
||||
return g_last_sync_utc;
|
||||
}
|
||||
|
||||
@@ -1,17 +1,137 @@
|
||||
#include "web_server.h"
|
||||
#include <WebServer.h>
|
||||
#include "wifi_manager.h"
|
||||
#include "config.h"
|
||||
#include "sd_logger.h"
|
||||
#include "time_manager.h"
|
||||
#include "html_util.h"
|
||||
#include <SD.h>
|
||||
#include <WiFi.h>
|
||||
#include <time.h>
|
||||
#include <new>
|
||||
#include <limits.h>
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
static WebServer server(80);
|
||||
static const SenderStatus *g_statuses = nullptr;
|
||||
static uint8_t g_status_count = 0;
|
||||
static WifiMqttConfig g_config;
|
||||
static bool g_is_ap = false;
|
||||
static String g_web_user;
|
||||
static String g_web_pass;
|
||||
static const FaultCounters *g_sender_faults = nullptr;
|
||||
static const FaultType *g_sender_last_errors = nullptr;
|
||||
static MeterData g_last_batch[NUM_SENDERS][METER_BATCH_MAX_SAMPLES];
|
||||
static uint8_t g_last_batch_count[NUM_SENDERS] = {};
|
||||
|
||||
struct HistoryBin {
|
||||
uint32_t ts;
|
||||
float value;
|
||||
uint32_t count;
|
||||
};
|
||||
|
||||
enum class HistoryMode : uint8_t {
|
||||
Avg = 0,
|
||||
Max = 1
|
||||
};
|
||||
|
||||
struct HistoryJob {
|
||||
bool active;
|
||||
bool done;
|
||||
bool error;
|
||||
String error_msg;
|
||||
String device_id;
|
||||
HistoryMode mode;
|
||||
uint32_t start_ts;
|
||||
uint32_t end_ts;
|
||||
uint32_t res_sec;
|
||||
uint32_t bins_count;
|
||||
uint32_t bins_filled;
|
||||
uint16_t day_index;
|
||||
File file;
|
||||
HistoryBin *bins;
|
||||
};
|
||||
|
||||
static HistoryJob g_history = {};
|
||||
static constexpr size_t SD_LIST_MAX_FILES = 200;
|
||||
static constexpr size_t SD_DOWNLOAD_MAX_PATH = 160;
|
||||
|
||||
static String format_utc_timestamp(uint32_t ts_utc) {
|
||||
if (ts_utc == 0) {
|
||||
return "n/a";
|
||||
}
|
||||
time_t t = static_cast<time_t>(ts_utc);
|
||||
struct tm tm_utc;
|
||||
gmtime_r(&t, &tm_utc);
|
||||
char buf[32];
|
||||
snprintf(buf, sizeof(buf), "%04d-%02d-%02d %02d:%02d:%02d UTC",
|
||||
tm_utc.tm_year + 1900,
|
||||
tm_utc.tm_mon + 1,
|
||||
tm_utc.tm_mday,
|
||||
tm_utc.tm_hour,
|
||||
tm_utc.tm_min,
|
||||
tm_utc.tm_sec);
|
||||
return String(buf);
|
||||
}
|
||||
|
||||
static uint32_t timestamp_age_seconds(uint32_t ts_utc) {
|
||||
uint32_t now_utc = time_get_utc();
|
||||
if (ts_utc == 0 || now_utc < ts_utc) {
|
||||
return 0;
|
||||
}
|
||||
return now_utc - ts_utc;
|
||||
}
|
||||
|
||||
static int32_t round_power_w(float value) {
|
||||
if (isnan(value)) {
|
||||
return 0;
|
||||
}
|
||||
long rounded = lroundf(value);
|
||||
if (rounded > INT32_MAX) {
|
||||
return INT32_MAX;
|
||||
}
|
||||
if (rounded < INT32_MIN) {
|
||||
return INT32_MIN;
|
||||
}
|
||||
return static_cast<int32_t>(rounded);
|
||||
}
|
||||
|
||||
static bool auth_required() {
|
||||
return g_is_ap ? WEB_AUTH_REQUIRE_AP : WEB_AUTH_REQUIRE_STA;
|
||||
}
|
||||
|
||||
static const char *fault_text(FaultType fault) {
|
||||
switch (fault) {
|
||||
case FaultType::MeterRead:
|
||||
return "meter";
|
||||
case FaultType::Decode:
|
||||
return "decode";
|
||||
case FaultType::LoraTx:
|
||||
return "loratx";
|
||||
default:
|
||||
return "none";
|
||||
}
|
||||
}
|
||||
|
||||
static bool ensure_auth() {
|
||||
if (!auth_required()) {
|
||||
return true;
|
||||
}
|
||||
const char *user = g_web_user.c_str();
|
||||
const char *pass = g_web_pass.c_str();
|
||||
if (server.authenticate(user, pass)) {
|
||||
return true;
|
||||
}
|
||||
server.requestAuthentication(BASIC_AUTH, "DD3", "Authentication required");
|
||||
return false;
|
||||
}
|
||||
|
||||
static String html_header(const String &title) {
|
||||
String safe_title = html_escape(title);
|
||||
String h = "<!DOCTYPE html><html><head><meta charset='utf-8'><meta name='viewport' content='width=device-width,initial-scale=1'>";
|
||||
h += "<title>" + title + "</title></head><body>";
|
||||
h += "<h2>" + title + "</h2>";
|
||||
h += "<title>" + safe_title + "</title></head><body>";
|
||||
h += "<h2>" + safe_title + "</h2>";
|
||||
return h;
|
||||
}
|
||||
|
||||
@@ -19,22 +139,324 @@ static String html_footer() {
|
||||
return "</body></html>";
|
||||
}
|
||||
|
||||
static String format_faults(uint8_t idx) {
|
||||
if (!g_sender_faults || !g_sender_last_errors || idx >= g_status_count) {
|
||||
return "";
|
||||
}
|
||||
String s;
|
||||
s += " faults m:";
|
||||
s += String(g_sender_faults[idx].meter_read_fail);
|
||||
s += " d:";
|
||||
s += String(g_sender_faults[idx].decode_fail);
|
||||
s += " tx:";
|
||||
s += String(g_sender_faults[idx].lora_tx_fail);
|
||||
s += " last:";
|
||||
s += String(static_cast<uint8_t>(g_sender_last_errors[idx]));
|
||||
s += " (" + String(fault_text(g_sender_last_errors[idx])) + ")";
|
||||
return s;
|
||||
}
|
||||
|
||||
static bool sanitize_sd_download_path(String &path, String &error) {
|
||||
path.trim();
|
||||
if (path.length() == 0) {
|
||||
error = "empty";
|
||||
return false;
|
||||
}
|
||||
if (path.startsWith("dd3/")) {
|
||||
path = "/" + path;
|
||||
}
|
||||
if (path.length() > SD_DOWNLOAD_MAX_PATH) {
|
||||
error = "too_long";
|
||||
return false;
|
||||
}
|
||||
if (!path.startsWith("/dd3/")) {
|
||||
error = "prefix";
|
||||
return false;
|
||||
}
|
||||
if (path.indexOf("..") >= 0) {
|
||||
error = "dotdot";
|
||||
return false;
|
||||
}
|
||||
if (path.indexOf('\\') >= 0) {
|
||||
error = "backslash";
|
||||
return false;
|
||||
}
|
||||
if (path.indexOf("//") >= 0) {
|
||||
error = "repeated_slash";
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool checkbox_checked(const char *name) {
|
||||
if (!server.hasArg(name)) {
|
||||
return false;
|
||||
}
|
||||
String val = server.arg(name);
|
||||
return val == "on" || val == "true" || val == "1";
|
||||
}
|
||||
|
||||
static bool sanitize_history_device_id(const String &input, String &out_device_id) {
|
||||
if (sanitize_device_id(input, out_device_id)) {
|
||||
return true;
|
||||
}
|
||||
if (g_statuses) {
|
||||
for (uint8_t i = 0; i < g_status_count; ++i) {
|
||||
String known = g_statuses[i].last_data.device_id;
|
||||
if (input.equalsIgnoreCase(known) && sanitize_device_id(known, out_device_id)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static String sanitize_download_filename(const String &input, bool &clean) {
|
||||
String out;
|
||||
out.reserve(input.length());
|
||||
clean = true;
|
||||
for (size_t i = 0; i < input.length(); ++i) {
|
||||
unsigned char c = static_cast<unsigned char>(input[i]);
|
||||
if (c < 32 || c == 127 || c == '"' || c == '\\' || c == '/') {
|
||||
out += '_';
|
||||
clean = false;
|
||||
continue;
|
||||
}
|
||||
out += static_cast<char>(c);
|
||||
}
|
||||
out.trim();
|
||||
if (out.length() == 0) {
|
||||
out = "download.bin";
|
||||
clean = false;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
static void history_reset() {
|
||||
if (g_history.file) {
|
||||
g_history.file.close();
|
||||
}
|
||||
if (g_history.bins) {
|
||||
delete[] g_history.bins;
|
||||
}
|
||||
g_history = {};
|
||||
}
|
||||
|
||||
static String history_date_from_epoch(uint32_t ts_utc) {
|
||||
time_t t = static_cast<time_t>(ts_utc);
|
||||
struct tm tm_utc;
|
||||
gmtime_r(&t, &tm_utc);
|
||||
char buf[16];
|
||||
snprintf(buf, sizeof(buf), "%04d-%02d-%02d", tm_utc.tm_year + 1900, tm_utc.tm_mon + 1, tm_utc.tm_mday);
|
||||
return String(buf);
|
||||
}
|
||||
|
||||
static bool history_open_next_file() {
|
||||
if (!g_history.active || g_history.done || g_history.error) {
|
||||
return false;
|
||||
}
|
||||
if (g_history.file) {
|
||||
g_history.file.close();
|
||||
}
|
||||
uint32_t day_ts = g_history.start_ts + static_cast<uint32_t>(g_history.day_index) * 86400UL;
|
||||
if (day_ts > g_history.end_ts) {
|
||||
g_history.done = true;
|
||||
return false;
|
||||
}
|
||||
String path = String("/dd3/") + g_history.device_id + "/" + history_date_from_epoch(day_ts) + ".csv";
|
||||
g_history.file = SD.open(path.c_str(), FILE_READ);
|
||||
g_history.day_index++;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool history_parse_line(const char *line, uint32_t &ts_out, float &p_out) {
|
||||
if (!line || line[0] < '0' || line[0] > '9') {
|
||||
return false;
|
||||
}
|
||||
const char *comma = strchr(line, ',');
|
||||
if (!comma) {
|
||||
return false;
|
||||
}
|
||||
char ts_buf[16];
|
||||
size_t ts_len = static_cast<size_t>(comma - line);
|
||||
if (ts_len >= sizeof(ts_buf)) {
|
||||
return false;
|
||||
}
|
||||
memcpy(ts_buf, line, ts_len);
|
||||
ts_buf[ts_len] = '\0';
|
||||
char *end = nullptr;
|
||||
uint32_t ts = static_cast<uint32_t>(strtoul(ts_buf, &end, 10));
|
||||
if (end == ts_buf) {
|
||||
return false;
|
||||
}
|
||||
const char *p_start = comma + 1;
|
||||
const char *p_end = strchr(p_start, ',');
|
||||
char p_buf[16];
|
||||
size_t p_len = p_end ? static_cast<size_t>(p_end - p_start) : strlen(p_start);
|
||||
if (p_len >= sizeof(p_buf)) {
|
||||
return false;
|
||||
}
|
||||
memcpy(p_buf, p_start, p_len);
|
||||
p_buf[p_len] = '\0';
|
||||
char *endp = nullptr;
|
||||
float p = strtof(p_buf, &endp);
|
||||
if (endp == p_buf) {
|
||||
return false;
|
||||
}
|
||||
ts_out = ts;
|
||||
p_out = p;
|
||||
return true;
|
||||
}
|
||||
|
||||
static void history_tick() {
|
||||
if (!g_history.active || g_history.done || g_history.error) {
|
||||
return;
|
||||
}
|
||||
if (!sd_logger_is_ready()) {
|
||||
g_history.error = true;
|
||||
g_history.error_msg = "sd_not_ready";
|
||||
return;
|
||||
}
|
||||
|
||||
uint32_t start_ms = millis();
|
||||
while (millis() - start_ms < SD_HISTORY_TIME_BUDGET_MS) {
|
||||
if (!g_history.file) {
|
||||
if (!history_open_next_file()) {
|
||||
if (g_history.done) {
|
||||
g_history.active = false;
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (!g_history.file.available()) {
|
||||
g_history.file.close();
|
||||
continue;
|
||||
}
|
||||
|
||||
char line[160];
|
||||
size_t n = g_history.file.readBytesUntil('\n', line, sizeof(line) - 1);
|
||||
line[n] = '\0';
|
||||
if (n == 0) {
|
||||
continue;
|
||||
}
|
||||
uint32_t ts = 0;
|
||||
float p = 0.0f;
|
||||
if (!history_parse_line(line, ts, p)) {
|
||||
continue;
|
||||
}
|
||||
if (ts < g_history.start_ts || ts > g_history.end_ts) {
|
||||
continue;
|
||||
}
|
||||
uint32_t idx = (ts - g_history.start_ts) / g_history.res_sec;
|
||||
if (idx >= g_history.bins_count) {
|
||||
continue;
|
||||
}
|
||||
HistoryBin &bin = g_history.bins[idx];
|
||||
if (bin.count == 0) {
|
||||
bin.ts = g_history.start_ts + idx * g_history.res_sec;
|
||||
bin.value = p;
|
||||
bin.count = 1;
|
||||
g_history.bins_filled++;
|
||||
} else if (g_history.mode == HistoryMode::Avg) {
|
||||
bin.value += p;
|
||||
bin.count++;
|
||||
} else {
|
||||
if (p > bin.value) {
|
||||
bin.value = p;
|
||||
}
|
||||
bin.count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static String render_sender_block(const SenderStatus &status) {
|
||||
String s;
|
||||
s += "<div style='margin-bottom:10px;padding:6px;border:1px solid #ccc'>";
|
||||
s += "<strong>" + String(status.last_data.device_id) + "</strong><br>";
|
||||
uint8_t idx = 0;
|
||||
if (g_statuses) {
|
||||
for (uint8_t i = 0; i < g_status_count; ++i) {
|
||||
if (&g_statuses[i] == &status) {
|
||||
idx = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
String device_id = status.last_data.device_id;
|
||||
String device_id_safe = html_escape(device_id);
|
||||
String device_id_url = url_encode_component(device_id);
|
||||
s += "<strong><a href='/sender/" + device_id_url + "' target='_blank' rel='noopener noreferrer'>" + device_id_safe + "</a></strong>";
|
||||
if (status.has_data && status.last_data.link_valid) {
|
||||
s += " RSSI:" + String(status.last_data.link_rssi_dbm) + " SNR:" + String(status.last_data.link_snr_db, 1);
|
||||
}
|
||||
if (status.has_data) {
|
||||
s += " err_tx:" + String(status.last_data.err_lora_tx);
|
||||
s += " err_last:" + String(static_cast<uint8_t>(status.last_data.last_error));
|
||||
s += " (" + String(fault_text(status.last_data.last_error)) + ")";
|
||||
s += " rx_reject:" + String(status.last_data.rx_reject_reason);
|
||||
s += " (" + String(rx_reject_reason_text(static_cast<RxRejectReason>(status.last_data.rx_reject_reason))) + ")";
|
||||
}
|
||||
s += format_faults(idx);
|
||||
s += "<br>";
|
||||
if (!status.has_data) {
|
||||
s += "No data";
|
||||
} else {
|
||||
s += "Energy: " + String(status.last_data.energy_total_kwh, 3) + " kWh<br>";
|
||||
s += "Power: " + String(status.last_data.total_power_w, 1) + " W<br>";
|
||||
s += "Battery: " + String(status.last_data.battery_voltage_v, 2) + " V (" + String(status.last_data.battery_percent) + ")";
|
||||
s += "Last update: " + format_utc_timestamp(status.last_update_ts_utc);
|
||||
if (time_is_synced()) {
|
||||
s += " (" + String(timestamp_age_seconds(status.last_update_ts_utc)) + "s ago)";
|
||||
}
|
||||
s += "<br>";
|
||||
s += "Energy: " + String(status.last_data.energy_total_kwh, 2) + " kWh<br>";
|
||||
s += "Power: " + String(round_power_w(status.last_data.total_power_w)) + " W<br>";
|
||||
s += "P1/P2/P3: " + String(round_power_w(status.last_data.phase_power_w[0])) + " / " +
|
||||
String(round_power_w(status.last_data.phase_power_w[1])) + " / " +
|
||||
String(round_power_w(status.last_data.phase_power_w[2])) + " W<br>";
|
||||
s += "Battery: " + String(status.last_data.battery_percent) + "% (" + String(status.last_data.battery_voltage_v, 2) + " V)";
|
||||
}
|
||||
s += "</div>";
|
||||
return s;
|
||||
}
|
||||
|
||||
static void append_sd_listing(String &html, const String &dir_path, uint8_t depth, size_t &count) {
|
||||
if (count >= SD_LIST_MAX_FILES || depth > 4) {
|
||||
return;
|
||||
}
|
||||
File dir = SD.open(dir_path.c_str());
|
||||
if (!dir || !dir.isDirectory()) {
|
||||
return;
|
||||
}
|
||||
File entry = dir.openNextFile();
|
||||
while (entry && count < SD_LIST_MAX_FILES) {
|
||||
String name = entry.name();
|
||||
String full_path = name;
|
||||
if (!full_path.startsWith(dir_path)) {
|
||||
if (!dir_path.endsWith("/")) {
|
||||
full_path = dir_path + "/" + name;
|
||||
} else {
|
||||
full_path = dir_path + name;
|
||||
}
|
||||
}
|
||||
if (entry.isDirectory()) {
|
||||
html += "<li><strong>" + html_escape(full_path) + "/</strong></li>";
|
||||
append_sd_listing(html, full_path, depth + 1, count);
|
||||
} else {
|
||||
String href = full_path;
|
||||
if (!href.startsWith("/")) {
|
||||
href = "/" + href;
|
||||
}
|
||||
String href_enc = url_encode_component(href);
|
||||
html += "<li><a href='/sd/download?path=" + href_enc + "' target='_blank' rel='noopener noreferrer'>" + html_escape(full_path) + "</a>";
|
||||
html += " (" + String(entry.size()) + " bytes)</li>";
|
||||
count++;
|
||||
}
|
||||
entry = dir.openNextFile();
|
||||
}
|
||||
dir.close();
|
||||
}
|
||||
|
||||
static void handle_root() {
|
||||
if (!ensure_auth()) {
|
||||
return;
|
||||
}
|
||||
String html = html_header("DD3 Bridge Status");
|
||||
html += g_is_ap ? "<p>Mode: AP</p>" : "<p>Mode: STA</p>";
|
||||
|
||||
@@ -44,22 +466,45 @@ static void handle_root() {
|
||||
}
|
||||
}
|
||||
|
||||
if (sd_logger_is_ready()) {
|
||||
html += "<h3>SD Files</h3><ul>";
|
||||
size_t count = 0;
|
||||
append_sd_listing(html, "/dd3", 0, count);
|
||||
if (count >= SD_LIST_MAX_FILES) {
|
||||
html += "<li>Listing truncated...</li>";
|
||||
}
|
||||
html += "</ul>";
|
||||
} else {
|
||||
html += "<p>SD: not ready</p>";
|
||||
}
|
||||
|
||||
html += "<p><a href='/wifi'>Configure WiFi/MQTT/NTP</a></p>";
|
||||
html += "<p><a href='/manual'>Manual</a></p>";
|
||||
html += html_footer();
|
||||
server.send(200, "text/html", html);
|
||||
}
|
||||
|
||||
static void handle_wifi_get() {
|
||||
if (!ensure_auth()) {
|
||||
return;
|
||||
}
|
||||
String html = html_header("WiFi/MQTT Config");
|
||||
html += "<form method='POST' action='/wifi'>";
|
||||
html += "SSID: <input name='ssid' value='" + g_config.ssid + "'><br>";
|
||||
html += "Password: <input name='pass' type='password' value='" + g_config.password + "'><br>";
|
||||
html += "MQTT Host: <input name='mqhost' value='" + g_config.mqtt_host + "'><br>";
|
||||
html += "SSID: <input name='ssid' value='" + html_escape(g_config.ssid) + "'><br>";
|
||||
html += "Password: <input name='pass' type='password'> ";
|
||||
html += "<label><input type='checkbox' name='clear_wifi_pass'> Clear password</label><br>";
|
||||
html += "MQTT Host: <input name='mqhost' value='" + html_escape(g_config.mqtt_host) + "'><br>";
|
||||
html += "MQTT Port: <input name='mqport' value='" + String(g_config.mqtt_port) + "'><br>";
|
||||
html += "MQTT User: <input name='mquser' value='" + g_config.mqtt_user + "'><br>";
|
||||
html += "MQTT Pass: <input name='mqpass' type='password' value='" + g_config.mqtt_pass + "'><br>";
|
||||
html += "NTP Server 1: <input name='ntp1' value='" + g_config.ntp_server_1 + "'><br>";
|
||||
html += "NTP Server 2: <input name='ntp2' value='" + g_config.ntp_server_2 + "'><br>";
|
||||
html += "MQTT User: <input name='mquser' value='" + html_escape(g_config.mqtt_user) + "'><br>";
|
||||
html += "MQTT Pass: <input name='mqpass' type='password'> ";
|
||||
html += "<label><input type='checkbox' name='clear_mqtt_pass'> Clear password</label><br>";
|
||||
html += "NTP Server 1: <input name='ntp1' value='" + html_escape(g_config.ntp_server_1) + "'><br>";
|
||||
html += "NTP Server 2: <input name='ntp2' value='" + html_escape(g_config.ntp_server_2) + "'><br>";
|
||||
html += "<hr>";
|
||||
html += "Web UI User: <input name='webuser' value='" + html_escape(g_config.web_user) + "'><br>";
|
||||
html += "Web UI Pass: <input name='webpass' type='password'> ";
|
||||
html += "<label><input type='checkbox' name='clear_web_pass'> Clear password</label><br>";
|
||||
html += "<div style='font-size:12px;color:#666;'>Leaving password blank keeps the existing one.</div>";
|
||||
html += "<button type='submit'>Save</button>";
|
||||
html += "</form>";
|
||||
html += html_footer();
|
||||
@@ -67,15 +512,38 @@ static void handle_wifi_get() {
|
||||
}
|
||||
|
||||
static void handle_wifi_post() {
|
||||
WifiMqttConfig cfg;
|
||||
cfg.ntp_server_1 = "pool.ntp.org";
|
||||
cfg.ntp_server_2 = "time.nist.gov";
|
||||
if (!ensure_auth()) {
|
||||
return;
|
||||
}
|
||||
WifiMqttConfig cfg = g_config;
|
||||
cfg.ntp_server_1 = g_config.ntp_server_1.length() > 0 ? g_config.ntp_server_1 : "pool.ntp.org";
|
||||
cfg.ntp_server_2 = g_config.ntp_server_2.length() > 0 ? g_config.ntp_server_2 : "time.nist.gov";
|
||||
cfg.ssid = server.arg("ssid");
|
||||
cfg.password = server.arg("pass");
|
||||
String wifi_pass = server.arg("pass");
|
||||
if (checkbox_checked("clear_wifi_pass")) {
|
||||
cfg.password = "";
|
||||
} else if (wifi_pass.length() > 0) {
|
||||
cfg.password = wifi_pass;
|
||||
}
|
||||
cfg.mqtt_host = server.arg("mqhost");
|
||||
cfg.mqtt_port = static_cast<uint16_t>(server.arg("mqport").toInt());
|
||||
cfg.mqtt_user = server.arg("mquser");
|
||||
cfg.mqtt_pass = server.arg("mqpass");
|
||||
String mqtt_pass = server.arg("mqpass");
|
||||
if (checkbox_checked("clear_mqtt_pass")) {
|
||||
cfg.mqtt_pass = "";
|
||||
} else if (mqtt_pass.length() > 0) {
|
||||
cfg.mqtt_pass = mqtt_pass;
|
||||
}
|
||||
String web_user = server.arg("webuser");
|
||||
if (web_user.length() > 0) {
|
||||
cfg.web_user = web_user;
|
||||
}
|
||||
String web_pass = server.arg("webpass");
|
||||
if (checkbox_checked("clear_web_pass")) {
|
||||
cfg.web_pass = "";
|
||||
} else if (web_pass.length() > 0) {
|
||||
cfg.web_pass = web_pass;
|
||||
}
|
||||
if (server.arg("ntp1").length() > 0) {
|
||||
cfg.ntp_server_1 = server.arg("ntp1");
|
||||
}
|
||||
@@ -83,6 +551,9 @@ static void handle_wifi_post() {
|
||||
cfg.ntp_server_2 = server.arg("ntp2");
|
||||
}
|
||||
cfg.valid = true;
|
||||
g_config = cfg;
|
||||
g_web_user = cfg.web_user;
|
||||
g_web_pass = cfg.web_pass;
|
||||
wifi_save_config(cfg);
|
||||
server.send(200, "text/html", "<html><body>Saved. Rebooting...</body></html>");
|
||||
delay(1000);
|
||||
@@ -90,16 +561,105 @@ static void handle_wifi_post() {
|
||||
}
|
||||
|
||||
static void handle_sender() {
|
||||
if (!ensure_auth()) {
|
||||
return;
|
||||
}
|
||||
if (!g_statuses) {
|
||||
server.send(404, "text/plain", "No senders");
|
||||
return;
|
||||
}
|
||||
String uri = server.uri();
|
||||
String device_id = uri.substring(String("/sender/").length());
|
||||
String device_id_url = url_encode_component(device_id);
|
||||
for (uint8_t i = 0; i < g_status_count; ++i) {
|
||||
if (device_id.equalsIgnoreCase(g_statuses[i].last_data.device_id)) {
|
||||
String html = html_header("Sender " + device_id);
|
||||
html += render_sender_block(g_statuses[i]);
|
||||
html += "<h3>History (Power)</h3>";
|
||||
html += "<div>";
|
||||
html += "Days: <input id='hist_days' type='number' min='1' max='" + String(SD_HISTORY_MAX_DAYS) + "' value='7' style='width:60px'> ";
|
||||
html += "Res(min): <input id='hist_res' type='number' min='" + String(SD_HISTORY_MIN_RES_MIN) + "' value='5' style='width:60px'> ";
|
||||
html += "<select id='hist_mode'><option value='avg'>avg</option><option value='max'>max</option></select> ";
|
||||
html += "<button onclick='drawHistory()'>Draw</button>";
|
||||
html += "<div id='hist_status' style='font-size:12px;margin-top:4px;color:#666;'></div>";
|
||||
html += "<canvas id='hist_canvas' width='320' height='140' style='width:100%;max-width:520px;border:1px solid #ccc;margin-top:6px;'></canvas>";
|
||||
html += "</div>";
|
||||
html += "<script>";
|
||||
html += "const deviceId='" + device_id_url + "';";
|
||||
html += "let histTimer=null;";
|
||||
html += "function histStatus(msg){document.getElementById('hist_status').textContent=msg;}";
|
||||
html += "function drawHistory(){";
|
||||
html += "const days=document.getElementById('hist_days').value;";
|
||||
html += "const res=document.getElementById('hist_res').value;";
|
||||
html += "const mode=document.getElementById('hist_mode').value;";
|
||||
html += "histStatus('Starting...');";
|
||||
html += "fetch(`/history/start?device_id=${deviceId}&days=${days}&res=${res}&mode=${mode}`)";
|
||||
html += ".then(r=>r.json()).then(j=>{";
|
||||
html += "if(!j.ok){histStatus('Error: '+(j.error||'failed'));return;}";
|
||||
html += "if(histTimer){clearInterval(histTimer);}";
|
||||
html += "histTimer=setInterval(()=>fetchHistory(),1000);";
|
||||
html += "fetchHistory();";
|
||||
html += "});";
|
||||
html += "}";
|
||||
html += "function fetchHistory(){";
|
||||
html += "fetch(`/history/data?device_id=${deviceId}`).then(r=>r.json()).then(j=>{";
|
||||
html += "if(!j.ready){histStatus(j.error?('Error: '+j.error):('Processing... '+(j.progress||0)+'%'));return;}";
|
||||
html += "if(histTimer){clearInterval(histTimer);histTimer=null;}";
|
||||
html += "renderChart(j.series);";
|
||||
html += "histStatus('Done');";
|
||||
html += "});";
|
||||
html += "}";
|
||||
html += "function renderChart(series){";
|
||||
html += "const canvas=document.getElementById('hist_canvas');";
|
||||
html += "const w=canvas.clientWidth;const h=canvas.clientHeight;";
|
||||
html += "canvas.width=w;canvas.height=h;";
|
||||
html += "const ctx=canvas.getContext('2d');";
|
||||
html += "ctx.clearRect(0,0,w,h);";
|
||||
html += "if(!series||series.length===0){ctx.fillText('No data',10,20);return;}";
|
||||
html += "let min=Infinity,max=-Infinity;";
|
||||
html += "for(const p of series){if(p[1]===null)continue; if(p[1]<min)min=p[1]; if(p[1]>max)max=p[1];}";
|
||||
html += "if(!isFinite(min)||!isFinite(max)){ctx.fillText('No data',10,20);return;}";
|
||||
html += "if(min===max){min=0;}";
|
||||
html += "ctx.strokeStyle='#333';ctx.lineWidth=1;ctx.beginPath();";
|
||||
html += "let first=true;";
|
||||
html += "for(let i=0;i<series.length;i++){";
|
||||
html += "const v=series[i][1];";
|
||||
html += "if(v===null)continue;";
|
||||
html += "const x=(i/(series.length-1))* (w-2) + 1;";
|
||||
html += "const y=h-2-((v-min)/(max-min))*(h-4);";
|
||||
html += "if(first){ctx.moveTo(x,y);first=false;} else {ctx.lineTo(x,y);} }";
|
||||
html += "ctx.stroke();";
|
||||
html += "ctx.fillStyle='#666';ctx.fillText(min.toFixed(0)+'W',4,h-4);";
|
||||
html += "ctx.fillText(max.toFixed(0)+'W',4,12);";
|
||||
html += "}";
|
||||
html += "</script>";
|
||||
if (g_last_batch_count[i] > 0) {
|
||||
html += "<h3>Last batch (" + String(g_last_batch_count[i]) + " samples)</h3>";
|
||||
html += "<table border='1' cellspacing='0' cellpadding='3'>";
|
||||
html += "<tr><th>#</th><th>ts</th><th>e_kwh</th><th>p_w</th><th>p1_w</th><th>p2_w</th><th>p3_w</th>";
|
||||
html += "<th>bat_v</th><th>bat_pct</th><th>rssi</th><th>snr</th><th>err_tx</th><th>err_last</th><th>rx_reject</th></tr>";
|
||||
for (uint8_t r = 0; r < g_last_batch_count[i]; ++r) {
|
||||
const MeterData &d = g_last_batch[i][r];
|
||||
html += "<tr>";
|
||||
html += "<td>" + String(r) + "</td>";
|
||||
html += "<td>" + String(d.ts_utc) + "</td>";
|
||||
html += "<td>" + String(d.energy_total_kwh, 2) + "</td>";
|
||||
html += "<td>" + String(round_power_w(d.total_power_w)) + "</td>";
|
||||
html += "<td>" + String(round_power_w(d.phase_power_w[0])) + "</td>";
|
||||
html += "<td>" + String(round_power_w(d.phase_power_w[1])) + "</td>";
|
||||
html += "<td>" + String(round_power_w(d.phase_power_w[2])) + "</td>";
|
||||
html += "<td>" + String(d.battery_voltage_v, 2) + "</td>";
|
||||
html += "<td>" + String(d.battery_percent) + "</td>";
|
||||
html += "<td>" + String(d.link_rssi_dbm) + "</td>";
|
||||
html += "<td>" + String(d.link_snr_db, 1) + "</td>";
|
||||
html += "<td>" + String(d.err_lora_tx) + "</td>";
|
||||
html += "<td>" + String(static_cast<uint8_t>(d.last_error)) + " (" + String(fault_text(d.last_error)) + ")</td>";
|
||||
html += "<td>" + String(d.rx_reject_reason) + " (" +
|
||||
String(rx_reject_reason_text(static_cast<RxRejectReason>(d.rx_reject_reason))) + ")</td>";
|
||||
html += "</tr>";
|
||||
}
|
||||
html += "</table>";
|
||||
}
|
||||
html += html_footer();
|
||||
server.send(200, "text/html", html);
|
||||
return;
|
||||
@@ -108,8 +668,217 @@ static void handle_sender() {
|
||||
server.send(404, "text/plain", "Not found");
|
||||
}
|
||||
|
||||
static void handle_manual() {
|
||||
if (!ensure_auth()) {
|
||||
return;
|
||||
}
|
||||
String html = html_header("DD3 Manual");
|
||||
html += "<ul>";
|
||||
html += "<li>Energy: total kWh since meter start.</li>";
|
||||
html += "<li>Power: total active power in W.</li>";
|
||||
html += "<li>P1/P2/P3: phase power in W.</li>";
|
||||
html += "<li>Battery: percent with voltage in V.</li>";
|
||||
html += "<li>RSSI/SNR: LoRa link quality from last packet.</li>";
|
||||
html += "<li>err_tx: sender-side LoRa TX error counter.</li>";
|
||||
html += "<li>err_last: last error code (0=None, 1=MeterRead, 2=Decode, 3=LoraTx).</li>";
|
||||
html += "<li>rx_reject: last RX reject reason (0=None, 1=crc_fail, 2=invalid_msg_kind, 3=length_mismatch, 4=device_id_mismatch, 5=batch_id_mismatch).</li>";
|
||||
html += "<li>faults m/d/tx: receiver-side counters (meter read fails, decode fails, LoRa TX fails).</li>";
|
||||
html += "<li>faults last: last receiver-side error code (same mapping as err_last).</li>";
|
||||
html += "</ul>";
|
||||
html += html_footer();
|
||||
server.send(200, "text/html", html);
|
||||
}
|
||||
|
||||
static void handle_history_start() {
|
||||
if (!ensure_auth()) {
|
||||
return;
|
||||
}
|
||||
if (!sd_logger_is_ready()) {
|
||||
server.send(200, "application/json", "{\"ok\":false,\"error\":\"sd_not_ready\"}");
|
||||
return;
|
||||
}
|
||||
if (!time_is_synced()) {
|
||||
server.send(200, "application/json", "{\"ok\":false,\"error\":\"time_not_synced\"}");
|
||||
return;
|
||||
}
|
||||
String device_id_arg = server.arg("device_id");
|
||||
String device_id;
|
||||
if (!sanitize_history_device_id(device_id_arg, device_id)) {
|
||||
server.send(200, "application/json", "{\"ok\":false,\"error\":\"bad_device_id\"}");
|
||||
return;
|
||||
}
|
||||
uint16_t days = static_cast<uint16_t>(server.arg("days").toInt());
|
||||
uint16_t res_min = static_cast<uint16_t>(server.arg("res").toInt());
|
||||
String mode_str = server.arg("mode");
|
||||
if (device_id.length() == 0 || days == 0 || res_min == 0) {
|
||||
server.send(200, "application/json", "{\"ok\":false,\"error\":\"bad_params\"}");
|
||||
return;
|
||||
}
|
||||
if (days > SD_HISTORY_MAX_DAYS) {
|
||||
days = SD_HISTORY_MAX_DAYS;
|
||||
}
|
||||
if (res_min < SD_HISTORY_MIN_RES_MIN) {
|
||||
res_min = SD_HISTORY_MIN_RES_MIN;
|
||||
}
|
||||
uint32_t bins = (static_cast<uint32_t>(days) * 24UL * 60UL) / res_min;
|
||||
if (bins == 0 || bins > SD_HISTORY_MAX_BINS) {
|
||||
String resp = String("{\"ok\":false,\"error\":\"too_many_bins\",\"max_bins\":") + SD_HISTORY_MAX_BINS + "}";
|
||||
server.send(200, "application/json", resp);
|
||||
return;
|
||||
}
|
||||
|
||||
history_reset();
|
||||
g_history.active = true;
|
||||
g_history.done = false;
|
||||
g_history.error = false;
|
||||
g_history.device_id = device_id;
|
||||
g_history.mode = (mode_str == "max") ? HistoryMode::Max : HistoryMode::Avg;
|
||||
g_history.res_sec = static_cast<uint32_t>(res_min) * 60UL;
|
||||
g_history.bins_count = bins;
|
||||
g_history.day_index = 0;
|
||||
g_history.bins = new (std::nothrow) HistoryBin[bins];
|
||||
if (!g_history.bins) {
|
||||
g_history.error = true;
|
||||
g_history.error_msg = "oom";
|
||||
server.send(200, "application/json", "{\"ok\":false,\"error\":\"oom\"}");
|
||||
return;
|
||||
}
|
||||
for (uint32_t i = 0; i < bins; ++i) {
|
||||
g_history.bins[i] = {};
|
||||
}
|
||||
g_history.end_ts = time_get_utc();
|
||||
uint32_t span = static_cast<uint32_t>(days) * 86400UL;
|
||||
g_history.start_ts = g_history.end_ts > span ? (g_history.end_ts - span) : 0;
|
||||
if (g_history.res_sec > 0) {
|
||||
g_history.start_ts = (g_history.start_ts / g_history.res_sec) * g_history.res_sec;
|
||||
}
|
||||
|
||||
String resp = String("{\"ok\":true,\"bins\":") + bins + "}";
|
||||
server.send(200, "application/json", resp);
|
||||
}
|
||||
|
||||
static void handle_history_data() {
|
||||
if (!ensure_auth()) {
|
||||
return;
|
||||
}
|
||||
String device_id_arg = server.arg("device_id");
|
||||
String device_id;
|
||||
if (!sanitize_history_device_id(device_id_arg, device_id)) {
|
||||
server.send(200, "application/json", "{\"ready\":false,\"error\":\"bad_device_id\"}");
|
||||
return;
|
||||
}
|
||||
if (!g_history.bins || device_id.length() == 0 || device_id != g_history.device_id) {
|
||||
server.send(200, "application/json", "{\"ready\":false,\"error\":\"no_job\"}");
|
||||
return;
|
||||
}
|
||||
if (g_history.error) {
|
||||
String resp = String("{\"ready\":false,\"error\":\"") + g_history.error_msg + "\"}";
|
||||
server.send(200, "application/json", resp);
|
||||
return;
|
||||
}
|
||||
if (g_history.active && !g_history.done) {
|
||||
uint32_t progress = g_history.bins_count == 0 ? 0 : (g_history.bins_filled * 100UL / g_history.bins_count);
|
||||
String resp = String("{\"ready\":false,\"progress\":") + progress + "}";
|
||||
server.send(200, "application/json", resp);
|
||||
return;
|
||||
}
|
||||
|
||||
server.setContentLength(CONTENT_LENGTH_UNKNOWN);
|
||||
server.send(200, "application/json", "");
|
||||
server.sendContent("{\"ready\":true,\"series\":[");
|
||||
bool first = true;
|
||||
for (uint32_t i = 0; i < g_history.bins_count; ++i) {
|
||||
const HistoryBin &bin = g_history.bins[i];
|
||||
if (!first) {
|
||||
server.sendContent(",");
|
||||
}
|
||||
first = false;
|
||||
float value = NAN;
|
||||
if (bin.count > 0) {
|
||||
value = (g_history.mode == HistoryMode::Avg) ? (bin.value / static_cast<float>(bin.count)) : bin.value;
|
||||
}
|
||||
if (bin.count == 0) {
|
||||
server.sendContent(String("[") + bin.ts + ",null]");
|
||||
} else {
|
||||
int32_t rounded = round_power_w(value);
|
||||
server.sendContent(String("[") + bin.ts + "," + String(rounded) + "]");
|
||||
}
|
||||
}
|
||||
server.sendContent("]}");
|
||||
}
|
||||
|
||||
static void handle_sd_download() {
|
||||
if (!ensure_auth()) {
|
||||
return;
|
||||
}
|
||||
if (!sd_logger_is_ready()) {
|
||||
server.send(404, "text/plain", "SD not ready");
|
||||
return;
|
||||
}
|
||||
String path = server.arg("path");
|
||||
String error;
|
||||
if (!sanitize_sd_download_path(path, error)) {
|
||||
if (SERIAL_DEBUG_MODE) {
|
||||
Serial.printf("sd: reject path '%s' reason=%s\n", path.c_str(), error.c_str());
|
||||
}
|
||||
server.send(400, "text/plain", "Invalid path");
|
||||
return;
|
||||
}
|
||||
File f = SD.open(path.c_str(), FILE_READ);
|
||||
if (!f) {
|
||||
server.send(404, "text/plain", "Not found");
|
||||
return;
|
||||
}
|
||||
size_t size = f.size();
|
||||
String filename = path.substring(path.lastIndexOf('/') + 1);
|
||||
bool name_clean = true;
|
||||
(void)name_clean;
|
||||
String safe_name = sanitize_download_filename(filename, name_clean);
|
||||
String cd = "attachment; filename=\"" + safe_name + "\"; filename*=UTF-8''" + url_encode_component(safe_name);
|
||||
server.sendHeader("Content-Disposition", cd);
|
||||
server.setContentLength(size);
|
||||
const char *content_type = "application/octet-stream";
|
||||
if (filename.endsWith(".csv")) {
|
||||
content_type = "text/csv";
|
||||
} else if (filename.endsWith(".txt")) {
|
||||
content_type = "text/plain";
|
||||
}
|
||||
server.send(200, content_type, "");
|
||||
WiFiClient client = server.client();
|
||||
uint8_t buf[512];
|
||||
while (f.available()) {
|
||||
size_t n = f.read(buf, sizeof(buf));
|
||||
if (n == 0) {
|
||||
break;
|
||||
}
|
||||
client.write(buf, n);
|
||||
delay(0);
|
||||
}
|
||||
f.close();
|
||||
}
|
||||
|
||||
void web_server_set_config(const WifiMqttConfig &config) {
|
||||
g_config = config;
|
||||
g_web_user = config.web_user;
|
||||
g_web_pass = config.web_pass;
|
||||
}
|
||||
|
||||
void web_server_set_sender_faults(const FaultCounters *faults, const FaultType *last_errors) {
|
||||
g_sender_faults = faults;
|
||||
g_sender_last_errors = last_errors;
|
||||
}
|
||||
|
||||
void web_server_set_last_batch(uint8_t sender_index, const MeterData *samples, size_t count) {
|
||||
if (!samples || sender_index >= NUM_SENDERS) {
|
||||
return;
|
||||
}
|
||||
if (count > METER_BATCH_MAX_SAMPLES) {
|
||||
count = METER_BATCH_MAX_SAMPLES;
|
||||
}
|
||||
g_last_batch_count[sender_index] = static_cast<uint8_t>(count);
|
||||
for (size_t i = 0; i < count; ++i) {
|
||||
g_last_batch[sender_index][i] = samples[i];
|
||||
}
|
||||
}
|
||||
|
||||
void web_server_begin_ap(const SenderStatus *statuses, uint8_t count) {
|
||||
@@ -118,6 +887,10 @@ void web_server_begin_ap(const SenderStatus *statuses, uint8_t count) {
|
||||
g_is_ap = true;
|
||||
|
||||
server.on("/", handle_root);
|
||||
server.on("/manual", handle_manual);
|
||||
server.on("/history/start", handle_history_start);
|
||||
server.on("/history/data", handle_history_data);
|
||||
server.on("/sd/download", handle_sd_download);
|
||||
server.on("/wifi", HTTP_GET, handle_wifi_get);
|
||||
server.on("/wifi", HTTP_POST, handle_wifi_post);
|
||||
server.on("/sender/", handle_sender);
|
||||
@@ -137,7 +910,11 @@ void web_server_begin_sta(const SenderStatus *statuses, uint8_t count) {
|
||||
g_is_ap = false;
|
||||
|
||||
server.on("/", handle_root);
|
||||
server.on("/manual", handle_manual);
|
||||
server.on("/sender/", handle_sender);
|
||||
server.on("/history/start", handle_history_start);
|
||||
server.on("/history/data", handle_history_data);
|
||||
server.on("/sd/download", handle_sd_download);
|
||||
server.on("/wifi", HTTP_GET, handle_wifi_get);
|
||||
server.on("/wifi", HTTP_POST, handle_wifi_post);
|
||||
server.onNotFound([]() {
|
||||
@@ -151,5 +928,6 @@ void web_server_begin_sta(const SenderStatus *statuses, uint8_t count) {
|
||||
}
|
||||
|
||||
void web_server_loop() {
|
||||
history_tick();
|
||||
server.handleClient();
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "wifi_manager.h"
|
||||
#include "config.h"
|
||||
#include <WiFi.h>
|
||||
#include <esp_wifi.h>
|
||||
|
||||
@@ -10,9 +11,6 @@ void wifi_manager_init() {
|
||||
|
||||
bool wifi_load_config(WifiMqttConfig &config) {
|
||||
config.valid = prefs.getBool("valid", false);
|
||||
if (!config.valid) {
|
||||
return false;
|
||||
}
|
||||
config.ssid = prefs.getString("ssid", "");
|
||||
config.password = prefs.getString("pass", "");
|
||||
config.mqtt_host = prefs.getString("mqhost", "");
|
||||
@@ -21,6 +19,11 @@ bool wifi_load_config(WifiMqttConfig &config) {
|
||||
config.mqtt_pass = prefs.getString("mqpass", "");
|
||||
config.ntp_server_1 = prefs.getString("ntp1", "pool.ntp.org");
|
||||
config.ntp_server_2 = prefs.getString("ntp2", "time.nist.gov");
|
||||
config.web_user = prefs.getString("webuser", WEB_AUTH_DEFAULT_USER);
|
||||
config.web_pass = prefs.getString("webpass", WEB_AUTH_DEFAULT_PASS);
|
||||
if (!config.valid) {
|
||||
return false;
|
||||
}
|
||||
return config.ssid.length() > 0 && config.mqtt_host.length() > 0;
|
||||
}
|
||||
|
||||
@@ -34,6 +37,8 @@ bool wifi_save_config(const WifiMqttConfig &config) {
|
||||
prefs.putString("mqpass", config.mqtt_pass);
|
||||
prefs.putString("ntp1", config.ntp_server_1);
|
||||
prefs.putString("ntp2", config.ntp_server_2);
|
||||
prefs.putString("webuser", config.web_user);
|
||||
prefs.putString("webpass", config.web_pass);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
37
test/test_html_escape/test_html_escape.cpp
Normal file
37
test/test_html_escape/test_html_escape.cpp
Normal file
@@ -0,0 +1,37 @@
|
||||
#include <Arduino.h>
|
||||
#include <unity.h>
|
||||
#include "html_util.h"
|
||||
|
||||
static void test_html_escape_basic() {
|
||||
TEST_ASSERT_EQUAL_STRING("", html_escape("").c_str());
|
||||
TEST_ASSERT_EQUAL_STRING("plain", html_escape("plain").c_str());
|
||||
TEST_ASSERT_EQUAL_STRING("a&b", html_escape("a&b").c_str());
|
||||
TEST_ASSERT_EQUAL_STRING("<tag>", html_escape("<tag>").c_str());
|
||||
TEST_ASSERT_EQUAL_STRING(""hi"", html_escape("\"hi\"").c_str());
|
||||
TEST_ASSERT_EQUAL_STRING("it's", html_escape("it's").c_str());
|
||||
TEST_ASSERT_EQUAL_STRING("&<>"'", html_escape("&<>\"'").c_str());
|
||||
}
|
||||
|
||||
static void test_sanitize_device_id() {
|
||||
String out;
|
||||
TEST_ASSERT_TRUE(sanitize_device_id("F19C", out));
|
||||
TEST_ASSERT_EQUAL_STRING("dd3-F19C", out.c_str());
|
||||
TEST_ASSERT_TRUE(sanitize_device_id("dd3-f19c", out));
|
||||
TEST_ASSERT_EQUAL_STRING("dd3-F19C", out.c_str());
|
||||
TEST_ASSERT_FALSE(sanitize_device_id("F19G", out));
|
||||
TEST_ASSERT_FALSE(sanitize_device_id("dd3-12", out));
|
||||
TEST_ASSERT_FALSE(sanitize_device_id("dd3-12345", out));
|
||||
TEST_ASSERT_FALSE(sanitize_device_id("../F19C", out));
|
||||
TEST_ASSERT_FALSE(sanitize_device_id("dd3-%2f", out));
|
||||
TEST_ASSERT_FALSE(sanitize_device_id("dd3-12/3", out));
|
||||
TEST_ASSERT_FALSE(sanitize_device_id("dd3-12\\3", out));
|
||||
}
|
||||
|
||||
void setup() {
|
||||
UNITY_BEGIN();
|
||||
RUN_TEST(test_html_escape_basic);
|
||||
RUN_TEST(test_sanitize_device_id);
|
||||
UNITY_END();
|
||||
}
|
||||
|
||||
void loop() {}
|
||||
Reference in New Issue
Block a user