Compare commits
55 Commits
main
...
multi-mete
| Author | SHA1 | Date | |
|---|---|---|---|
| eb80f49046 | |||
|
|
31a3eea5dd | ||
| c62f07bf44 | |||
| 938f490a32 | |||
| 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 |
356
README.md
356
README.md
@@ -1,309 +1,129 @@
|
|||||||
# 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 multiple IEC 62056-21 meters, batches data, sends over LoRa.
|
||||||
|
- `Receiver` (PIN `GPIO14` LOW): receives/ACKs batches, publishes MQTT, serves web UI, logs to SD.
|
||||||
|
|
||||||
## Hardware
|
## Current Architecture
|
||||||
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)
|
|
||||||
|
|
||||||
### Pin Mapping
|
- Single codebase, role selected at boot via `detect_role()` (`include/config.h`, `src/config.cpp`).
|
||||||
- LoRa (SX1276)
|
- LoRa link uses explicit CRC16 frame protection in firmware (`src/lora_transport.cpp`), in addition to LoRa PHY CRC.
|
||||||
- SCK: GPIO5
|
- Sender batches up to `30` samples and retransmits on missing ACK (`BATCH_MAX_RETRIES=2`, policy `Keep`).
|
||||||
- MISO: GPIO19
|
- Receiver handles AP fallback when STA config is missing/invalid and exposes a config/status web UI.
|
||||||
- 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)
|
|
||||||
|
|
||||||
### Notes on GPIOs
|
## LoRa Frame Protocol (Current)
|
||||||
- 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.
|
|
||||||
|
|
||||||
## Firmware Roles
|
Frame format on-air:
|
||||||
### 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.
|
|
||||||
|
|
||||||
**Sender flow (pseudo-code)**:
|
`[msg_kind:1][device_short_id:2][payload...][crc16:2]`
|
||||||
```cpp
|
|
||||||
void sender_loop() {
|
|
||||||
meter_read_every_second(); // SML/OBIS -> MeterData samples
|
|
||||||
read_battery(data); // VBAT + SoC
|
|
||||||
|
|
||||||
if (time_to_send_batch()) {
|
`msg_kind`:
|
||||||
json = meterBatchToJson(samples);
|
- `0` = `BatchUp`
|
||||||
compressed = compressBuffer(json);
|
- `1` = `AckDown`
|
||||||
lora_send(packet(MeterBatch, compressed));
|
|
||||||
}
|
|
||||||
|
|
||||||
display_set_last_meter(data);
|
### `BatchUp`
|
||||||
display_set_last_read(ok);
|
|
||||||
display_set_last_tx(ok);
|
|
||||||
display_tick();
|
|
||||||
|
|
||||||
lora_receive_time_sync(); // optional
|
`BatchUp` is chunked in transport (`batch_id`, `chunk_index`, `chunk_count`, `total_len`) and then decoded via `payload_codec`.
|
||||||
light_sleep_until_next_event();
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
**Key sender functions**:
|
Payload header contains:
|
||||||
```cpp
|
- fixed magic/schema fields (`kMagic=0xDDB3`, `kSchema=2`)
|
||||||
bool meter_read(MeterData &data); // parse SML frame, set OBIS fields
|
- `schema_id`
|
||||||
void read_battery(MeterData &data); // ADC -> volts + percent
|
- sender/batch/time/error metadata
|
||||||
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
|
|
||||||
```
|
|
||||||
|
|
||||||
### Receiver (USB-powered)
|
Supported payload schemas in this branch:
|
||||||
- WiFi STA connect using stored config; if not available/fails, starts AP.
|
- `schema_id=1` (`EnergyMulti`): integer kWh for up to 3 meters (`energy1_kwh`, `energy2_kwh`, `energy3_kwh`)
|
||||||
- NTP sync (UTC) and local display in Europe/Berlin.
|
- `schema_id=0` (legacy): older energy/power delta encoding path remains decode-compatible
|
||||||
- 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.
|
|
||||||
|
|
||||||
**Receiver loop (pseudo-code)**:
|
`n == 0` is used as sync request (no meter samples).
|
||||||
```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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (time_to_send_timesync()) {
|
### `AckDown` (7 bytes)
|
||||||
time_send_timesync(self_short_id); // 60s for first 10 min, then hourly if RTC is present
|
|
||||||
}
|
|
||||||
|
|
||||||
mqtt_loop();
|
`[flags:1][batch_id_be:2][epoch_utc_be:4]`
|
||||||
web_server_loop();
|
|
||||||
display_set_receiver_status(...);
|
|
||||||
display_tick();
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
**Key receiver functions**:
|
- `flags bit0`: `time_valid`
|
||||||
```cpp
|
- Receiver sends ACK repeatedly (`ACK_REPEAT_COUNT=3`, `ACK_REPEAT_DELAY_MS=200`).
|
||||||
bool lora_receive(LoraPacket &pkt, uint32_t timeout_ms);
|
- Sender accepts time only if `time_valid=1` and `epoch >= MIN_ACCEPTED_EPOCH_UTC` (`2026-02-01 00:00:00 UTC`).
|
||||||
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);
|
|
||||||
```
|
|
||||||
|
|
||||||
## Test Mode (compile-time)
|
## Time Bootstrap Guardrail
|
||||||
Enabled by `-DENABLE_TEST_MODE` (see `platformio.ini` test environment).
|
|
||||||
|
|
||||||
- Sender: sends 4-digit test code every ~30s in JSON.
|
On sender boot:
|
||||||
- Receiver: shows last test code per sender and publishes to `/test` topic.
|
- `g_time_acquired=false`
|
||||||
- Normal behavior is excluded from test builds.
|
- only sync requests every `SYNC_REQUEST_INTERVAL_MS` (15s)
|
||||||
|
- no normal sampling/transmit until valid ACK time received
|
||||||
|
|
||||||
**Test sender (pseudo-code)**:
|
This prevents publishing/storing pre-threshold timestamps.
|
||||||
```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);
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
**Test receiver (pseudo-code)**:
|
## Multi-Meter Sender Behavior
|
||||||
```cpp
|
|
||||||
void test_receiver_loop() {
|
|
||||||
if (pkt.type == TestCode) {
|
|
||||||
json = decompress(pkt.payload);
|
|
||||||
update_sender_test_code(json);
|
|
||||||
mqtt_publish_test(id, json);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## LoRa Protocol
|
Implemented in `src/meter_driver.cpp` + sender path in `src/main.cpp`:
|
||||||
Packet layout:
|
|
||||||
|
|
||||||
```
|
- Meter protocol: IEC 62056-21 ASCII, Mode D style framing (`/ ... !`)
|
||||||
[0] protocol_version (1)
|
- UART settings: `9600 7E1`
|
||||||
[1] role (0=sender, 1=receiver)
|
- Parsed OBIS: `1-0:1.8.0`
|
||||||
[2..3] device_id_short (uint16)
|
- Conversion: floor to integer kWh (`floorf`)
|
||||||
[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)
|
|
||||||
```
|
|
||||||
|
|
||||||
LoRa radio settings:
|
Meter count is build-dependent (`include/config.h`):
|
||||||
- Frequency: **433 MHz** or **868 MHz** (set by build env via `LORA_FREQUENCY_HZ`)
|
- Debug builds (`SERIAL_DEBUG_MODE=1`): `METER_COUNT=2`
|
||||||
- SF12, BW 125 kHz, CR 4/5, CRC on, Sync Word 0x34
|
- Prod builds (`SERIAL_DEBUG_MODE=0`): `METER_COUNT=3`
|
||||||
|
|
||||||
## Data Format
|
Default RX pins:
|
||||||
JSON payload (sender + MQTT):
|
- Meter1: `GPIO34` (`Serial2`)
|
||||||
|
- Meter2: `GPIO25` (`Serial1`)
|
||||||
|
- Meter3: `GPIO3` (`Serial`, prod only because debug serial is disabled)
|
||||||
|
|
||||||
```json
|
## Receiver Behavior
|
||||||
{
|
|
||||||
"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
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## Device IDs
|
For valid `BatchUp` decode:
|
||||||
- Derived from WiFi STA MAC.
|
1. Reassemble chunks and decode payload.
|
||||||
- `short_id = (MAC[4] << 8) | MAC[5]`
|
2. Send `AckDown` immediately.
|
||||||
- `device_id = dd3-%04X`
|
3. Drop duplicate batches per sender (`batch_id` tracking).
|
||||||
- JSON `id` uses only the last 4 hex digits (e.g., `F19C`) to save airtime.
|
4. If `n==0`: treat as sync request only.
|
||||||
|
5. Else convert to `MeterData`, log to SD, update web UI, publish MQTT.
|
||||||
|
|
||||||
Receiver expects known senders in `include/config.h` via:
|
## MQTT Topics and Payloads
|
||||||
```cpp
|
|
||||||
constexpr uint8_t NUM_SENDERS = 1;
|
|
||||||
inline constexpr uint16_t EXPECTED_SENDER_IDS[NUM_SENDERS] = { 0xF19C };
|
|
||||||
```
|
|
||||||
|
|
||||||
## OLED Behavior
|
State topic:
|
||||||
- Sender: OLED stays **ON for 10 seconds** on each wake, then powers down for sleep.
|
- `smartmeter/<device_id>/state`
|
||||||
- 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.
|
|
||||||
|
|
||||||
## Power & Battery
|
Fault topic (retained):
|
||||||
- Sender disables WiFi/BLE, reads VBAT via ADC, uses linear SoC map:
|
- `smartmeter/<device_id>/faults`
|
||||||
- 3.0 V = 0%
|
|
||||||
- 4.2 V = 100%
|
|
||||||
- Uses deep sleep between cycles (`SENDER_WAKE_INTERVAL_SEC`).
|
|
||||||
|
|
||||||
## Web UI
|
For `EnergyMulti` samples, state JSON includes:
|
||||||
- AP SSID: `DD3-Bridge-<short_id>`
|
- `id`, `ts`
|
||||||
- AP password: `changeme123`
|
- `energy1_kwh`, `energy2_kwh`, optional `energy3_kwh`
|
||||||
- Endpoints:
|
- `bat_v`, `bat_pct`
|
||||||
- `/`: status overview
|
- optional link fields: `rssi`, `snr`
|
||||||
- `/wifi`: WiFi/MQTT/NTP config (AP and STA)
|
- fault/reject fields: `err_last`, `rx_reject`, `rx_reject_text` (+ non-zero counters)
|
||||||
- `/sender/<device_id>`: per-sender details
|
|
||||||
|
|
||||||
## MQTT
|
Home Assistant discovery publishing is enabled (`ENABLE_HA_DISCOVERY=true`) but still advertises legacy keys (`e_kwh`, `p_w`, `p1_w`, `p2_w`, `p3_w`) in `src/mqtt_client.cpp`.
|
||||||
- Topic: `smartmeter/<deviceId>/state`
|
|
||||||
- QoS 0
|
|
||||||
- Test mode: `smartmeter/<deviceId>/test`
|
|
||||||
- Client ID: `dd3-bridge-<device_id>` (stable, derived from MAC)
|
|
||||||
|
|
||||||
## NTP
|
## Web UI, Wi-Fi, Storage
|
||||||
- NTP servers are configurable in the web UI (`/wifi`).
|
|
||||||
- Defaults: `pool.ntp.org` and `time.nist.gov`.
|
- STA config is stored in Preferences (`wifi_manager`).
|
||||||
## RTC (DS3231)
|
- If STA/MQTT config is unavailable, receiver starts AP mode with SSID prefix `DD3-Bridge-`.
|
||||||
- Optional DS3231 on the I2C bus. Connect SDA to GPIO21 and SCL to GPIO22 (same bus as the OLED).
|
- Web auth defaults are `admin/admin` (`WEB_AUTH_DEFAULT_USER/PASS`).
|
||||||
- Enable/disable with `ENABLE_DS3231` in `include/config.h`.
|
- SD logging is enabled (`ENABLE_SD_LOGGING=true`).
|
||||||
- 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.
|
|
||||||
|
|
||||||
## Build Environments
|
## 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
|
From `platformio.ini`:
|
||||||
- **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).
|
|
||||||
|
|
||||||
## Files & Modules
|
- `lilygo-t3-v1-6-1`
|
||||||
- `include/config.h`, `src/config.cpp`: pins, radio settings, sender IDs
|
- `lilygo-t3-v1-6-1-test`
|
||||||
- `include/data_model.h`, `src/data_model.cpp`: MeterData + ID init
|
- `lilygo-t3-v1-6-1-868`
|
||||||
- `include/json_codec.h`, `src/json_codec.cpp`: JSON encode/decode
|
- `lilygo-t3-v1-6-1-868-test`
|
||||||
- `include/compressor.h`, `src/compressor.cpp`: RLE compression
|
- `lilygo-t3-v1-6-1-payload-test`
|
||||||
- `include/lora_transport.h`, `src/lora_transport.cpp`: LoRa packet + CRC
|
- `lilygo-t3-v1-6-1-868-payload-test`
|
||||||
- `include/meter_driver.h`, `src/meter_driver.cpp`: IEC 62056-21 ASCII + SML parse
|
- `lilygo-t3-v1-6-1-prod`
|
||||||
- `include/power_manager.h`, `src/power_manager.cpp`: ADC + sleep
|
- `lilygo-t3-v1-6-1-868-prod`
|
||||||
- `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
|
|
||||||
|
|
||||||
## Quick Start
|
Example:
|
||||||
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:
|
|
||||||
|
|
||||||
```bash
|
```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:
|
## 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
|
|
||||||
```
|
|
||||||
|
|
||||||
|
`ENABLE_TEST_MODE` replaces normal sender/receiver loops with dedicated test loops (`src/test_mode.cpp`).
|
||||||
|
It sends/receives plain JSON test frames and publishes to `smartmeter/<device_id>/test`.
|
||||||
|
|||||||
@@ -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
|
Receiver = 1
|
||||||
};
|
};
|
||||||
|
|
||||||
enum class PayloadType : uint8_t {
|
enum class BatchRetryPolicy : uint8_t {
|
||||||
MeterData = 0,
|
Keep = 0,
|
||||||
TestCode = 1,
|
Drop = 1
|
||||||
TimeSync = 2,
|
|
||||||
MeterBatch = 3
|
|
||||||
};
|
};
|
||||||
|
|
||||||
constexpr uint8_t PROTOCOL_VERSION = 1;
|
|
||||||
|
|
||||||
// Pin definitions
|
// Pin definitions
|
||||||
constexpr uint8_t PIN_LORA_SCK = 5;
|
constexpr uint8_t PIN_LORA_SCK = 5;
|
||||||
constexpr uint8_t PIN_LORA_MISO = 19;
|
constexpr uint8_t PIN_LORA_MISO = 19;
|
||||||
@@ -33,10 +29,12 @@ constexpr uint8_t OLED_HEIGHT = 64;
|
|||||||
|
|
||||||
constexpr uint8_t PIN_BAT_ADC = 35;
|
constexpr uint8_t PIN_BAT_ADC = 35;
|
||||||
|
|
||||||
constexpr uint8_t PIN_ROLE = 13;
|
constexpr uint8_t PIN_ROLE = 14;
|
||||||
constexpr uint8_t PIN_OLED_CTRL = 14;
|
constexpr uint8_t PIN_OLED_CTRL = 13;
|
||||||
|
|
||||||
constexpr uint8_t PIN_METER_RX = 34;
|
constexpr uint8_t PIN_METER1_RX = 34; // UART2 RX
|
||||||
|
constexpr uint8_t PIN_METER2_RX = 25; // UART1 RX
|
||||||
|
constexpr uint8_t PIN_METER3_RX = 3; // UART0 RX (prod only, when serial debug is off)
|
||||||
|
|
||||||
// LoRa settings
|
// LoRa settings
|
||||||
#ifndef LORA_FREQUENCY_HZ
|
#ifndef LORA_FREQUENCY_HZ
|
||||||
@@ -47,23 +45,54 @@ constexpr uint8_t LORA_SPREADING_FACTOR = 12;
|
|||||||
constexpr long LORA_BANDWIDTH = 125E3;
|
constexpr long LORA_BANDWIDTH = 125E3;
|
||||||
constexpr uint8_t LORA_CODING_RATE = 5;
|
constexpr uint8_t LORA_CODING_RATE = 5;
|
||||||
constexpr uint8_t LORA_SYNC_WORD = 0x34;
|
constexpr uint8_t LORA_SYNC_WORD = 0x34;
|
||||||
|
constexpr uint8_t LORA_PREAMBLE_LEN = 8;
|
||||||
|
|
||||||
// Timing
|
// Timing
|
||||||
constexpr uint32_t SENDER_WAKE_INTERVAL_SEC = 30;
|
constexpr uint32_t SENDER_WAKE_INTERVAL_SEC = 30;
|
||||||
constexpr uint32_t TIME_SYNC_INTERVAL_SEC = 60;
|
constexpr uint32_t SYNC_REQUEST_INTERVAL_MS = 15000;
|
||||||
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 OLED_PAGE_INTERVAL_MS = 4000;
|
constexpr uint32_t OLED_PAGE_INTERVAL_MS = 4000;
|
||||||
constexpr uint32_t OLED_AUTO_OFF_MS = 10UL * 60UL * 1000UL;
|
constexpr uint32_t OLED_AUTO_OFF_MS = 10UL * 60UL * 1000UL;
|
||||||
constexpr uint32_t SENDER_OLED_READ_MS = 10000;
|
constexpr uint32_t SENDER_OLED_READ_MS = 10000;
|
||||||
constexpr uint32_t METER_SAMPLE_INTERVAL_MS = 1000;
|
constexpr uint32_t METER_SAMPLE_INTERVAL_MS = 1000;
|
||||||
constexpr uint32_t METER_SEND_INTERVAL_MS = 30000;
|
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 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 uint32_t WATCHDOG_TIMEOUT_SEC = 120;
|
||||||
constexpr bool ENABLE_HA_DISCOVERY = true;
|
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 uint8_t METER_COUNT_DEBUG = 2;
|
||||||
|
constexpr uint8_t METER_COUNT_PROD = 3;
|
||||||
|
constexpr uint8_t METER_COUNT = SERIAL_DEBUG_MODE ? METER_COUNT_DEBUG : METER_COUNT_PROD;
|
||||||
|
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 *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 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] = {
|
inline constexpr uint16_t EXPECTED_SENDER_IDS[NUM_SENDERS] = {
|
||||||
0xF19C //433mhz sender
|
0xF19C //433mhz sender
|
||||||
//0x7EB4 //868mhz sender
|
//0x7EB4 //868mhz sender
|
||||||
|
|||||||
@@ -9,6 +9,15 @@ enum class FaultType : uint8_t {
|
|||||||
LoraTx = 3
|
LoraTx = 3
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum class RxRejectReason : uint8_t {
|
||||||
|
None = 0,
|
||||||
|
CrcFail = 1,
|
||||||
|
InvalidMsgKind = 2,
|
||||||
|
LengthMismatch = 3,
|
||||||
|
DeviceIdMismatch = 4,
|
||||||
|
BatchIdMismatch = 5
|
||||||
|
};
|
||||||
|
|
||||||
struct FaultCounters {
|
struct FaultCounters {
|
||||||
uint32_t meter_read_fail;
|
uint32_t meter_read_fail;
|
||||||
uint32_t decode_fail;
|
uint32_t decode_fail;
|
||||||
@@ -19,10 +28,12 @@ struct MeterData {
|
|||||||
uint32_t ts_utc;
|
uint32_t ts_utc;
|
||||||
uint16_t short_id;
|
uint16_t short_id;
|
||||||
char device_id[16];
|
char device_id[16];
|
||||||
|
bool energy_multi;
|
||||||
|
uint8_t energy_meter_count;
|
||||||
|
uint32_t energy_kwh_int[3];
|
||||||
float energy_total_kwh;
|
float energy_total_kwh;
|
||||||
float phase_power_w[3];
|
float phase_power_w[3];
|
||||||
float total_power_w;
|
float total_power_w;
|
||||||
float phase_voltage_v[3];
|
|
||||||
float battery_voltage_v;
|
float battery_voltage_v;
|
||||||
uint8_t battery_percent;
|
uint8_t battery_percent;
|
||||||
bool valid;
|
bool valid;
|
||||||
@@ -33,12 +44,15 @@ struct MeterData {
|
|||||||
uint32_t err_decode;
|
uint32_t err_decode;
|
||||||
uint32_t err_lora_tx;
|
uint32_t err_lora_tx;
|
||||||
FaultType last_error;
|
FaultType last_error;
|
||||||
|
uint8_t rx_reject_reason;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct SenderStatus {
|
struct SenderStatus {
|
||||||
MeterData last_data;
|
MeterData last_data;
|
||||||
uint32_t last_update_ts_utc;
|
uint32_t last_update_ts_utc;
|
||||||
|
uint16_t last_acked_batch_id;
|
||||||
bool has_data;
|
bool has_data;
|
||||||
};
|
};
|
||||||
|
|
||||||
void init_device_ids(uint16_t &short_id, char *device_id, size_t device_id_len);
|
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_meter(const MeterData &data);
|
||||||
void display_set_last_read(bool ok, uint32_t ts_utc);
|
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_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_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_set_receiver_status(bool ap_mode, const char *ssid, bool mqtt_ok);
|
||||||
void display_power_down();
|
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"
|
#include "data_model.h"
|
||||||
|
|
||||||
bool meterDataToJson(const MeterData &data, String &out_json);
|
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 <Arduino.h>
|
||||||
#include "config.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 {
|
struct LoraPacket {
|
||||||
uint8_t protocol_version;
|
LoraMsgKind msg_kind;
|
||||||
DeviceRole role;
|
|
||||||
uint16_t device_id_short;
|
uint16_t device_id_short;
|
||||||
PayloadType payload_type;
|
|
||||||
uint8_t payload[LORA_MAX_PAYLOAD];
|
uint8_t payload[LORA_MAX_PAYLOAD];
|
||||||
size_t payload_len;
|
size_t payload_len;
|
||||||
int16_t rssi_dbm;
|
int16_t rssi_dbm;
|
||||||
@@ -19,4 +31,10 @@ struct LoraPacket {
|
|||||||
void lora_init();
|
void lora_init();
|
||||||
bool lora_send(const LoraPacket &pkt);
|
bool lora_send(const LoraPacket &pkt);
|
||||||
bool lora_receive(LoraPacket &pkt, uint32_t timeout_ms);
|
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_sleep();
|
||||||
|
void lora_receive_continuous();
|
||||||
|
bool lora_receive_window(LoraPacket &pkt, uint32_t timeout_ms);
|
||||||
|
uint32_t lora_airtime_ms(size_t packet_len);
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include "data_model.h"
|
|
||||||
|
|
||||||
void meter_init();
|
void meter_init();
|
||||||
bool meter_read(MeterData &data);
|
void meter_poll();
|
||||||
|
uint8_t meter_count();
|
||||||
|
bool meter_get_last_energy_kwh(uint8_t meter_idx, uint32_t &out_energy_kwh);
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
void power_sender_init();
|
void power_sender_init();
|
||||||
void power_receiver_init();
|
void power_receiver_init();
|
||||||
|
void power_configure_unused_pins_sender();
|
||||||
void read_battery(MeterData &data);
|
void read_battery(MeterData &data);
|
||||||
uint8_t battery_percent_from_voltage(float voltage_v);
|
uint8_t battery_percent_from_voltage(float voltage_v);
|
||||||
void light_sleep_ms(uint32_t ms);
|
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
|
#pragma once
|
||||||
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include "lora_transport.h"
|
|
||||||
|
|
||||||
void time_receiver_init(const char *ntp_server_1, const char *ntp_server_2);
|
void time_receiver_init(const char *ntp_server_1, const char *ntp_server_2);
|
||||||
uint32_t time_get_utc();
|
uint32_t time_get_utc();
|
||||||
bool time_is_synced();
|
bool time_is_synced();
|
||||||
void time_set_utc(uint32_t epoch);
|
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_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_utc();
|
||||||
uint32_t time_get_last_sync_age_sec();
|
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_ap(const SenderStatus *statuses, uint8_t count);
|
||||||
void web_server_begin_sta(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_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();
|
void web_server_loop();
|
||||||
|
|||||||
@@ -12,6 +12,8 @@ struct WifiMqttConfig {
|
|||||||
String mqtt_pass;
|
String mqtt_pass;
|
||||||
String ntp_server_1;
|
String ntp_server_1;
|
||||||
String ntp_server_2;
|
String ntp_server_2;
|
||||||
|
String web_user;
|
||||||
|
String web_pass;
|
||||||
bool valid;
|
bool valid;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -9,18 +9,7 @@
|
|||||||
; https://docs.platformio.org/page/projectconf.html
|
; https://docs.platformio.org/page/projectconf.html
|
||||||
|
|
||||||
[env:lilygo-t3-v1-6-1]
|
[env:lilygo-t3-v1-6-1]
|
||||||
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 =
|
|
||||||
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
|
|
||||||
board = ttgo-lora32-v1
|
board = ttgo-lora32-v1
|
||||||
framework = arduino
|
framework = arduino
|
||||||
lib_deps =
|
lib_deps =
|
||||||
@@ -30,10 +19,24 @@ lib_deps =
|
|||||||
adafruit/Adafruit GFX Library@^1.11.9
|
adafruit/Adafruit GFX Library@^1.11.9
|
||||||
knolleary/PubSubClient@^2.8
|
knolleary/PubSubClient@^2.8
|
||||||
build_flags =
|
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
|
-DENABLE_TEST_MODE
|
||||||
|
|
||||||
[env:lilygo-t3-v1-6-1-868]
|
[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
|
board = ttgo-lora32-v1
|
||||||
framework = arduino
|
framework = arduino
|
||||||
lib_deps =
|
lib_deps =
|
||||||
@@ -43,10 +46,11 @@ lib_deps =
|
|||||||
adafruit/Adafruit GFX Library@^1.11.9
|
adafruit/Adafruit GFX Library@^1.11.9
|
||||||
knolleary/PubSubClient@^2.8
|
knolleary/PubSubClient@^2.8
|
||||||
build_flags =
|
build_flags =
|
||||||
|
-DSERIAL_DEBUG_MODE_FLAG=1
|
||||||
-DLORA_FREQUENCY_HZ=868E6
|
-DLORA_FREQUENCY_HZ=868E6
|
||||||
|
|
||||||
[env:lilygo-t3-v1-6-1-868-test]
|
[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
|
board = ttgo-lora32-v1
|
||||||
framework = arduino
|
framework = arduino
|
||||||
lib_deps =
|
lib_deps =
|
||||||
@@ -56,5 +60,62 @@ lib_deps =
|
|||||||
adafruit/Adafruit GFX Library@^1.11.9
|
adafruit/Adafruit GFX Library@^1.11.9
|
||||||
knolleary/PubSubClient@^2.8
|
knolleary/PubSubClient@^2.8
|
||||||
build_flags =
|
build_flags =
|
||||||
|
-DSERIAL_DEBUG_MODE_FLAG=1
|
||||||
-DENABLE_TEST_MODE
|
-DENABLE_TEST_MODE
|
||||||
-DLORA_FREQUENCY_HZ=868E6
|
-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() {
|
DeviceRole detect_role() {
|
||||||
pinMode(PIN_ROLE, INPUT_PULLDOWN);
|
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];
|
short_id = (static_cast<uint16_t>(mac[4]) << 8) | mac[5];
|
||||||
snprintf(device_id, device_id_len, "dd3-%04X", short_id);
|
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 <Wire.h>
|
||||||
#include <Adafruit_GFX.h>
|
#include <Adafruit_GFX.h>
|
||||||
#include <Adafruit_SSD1306.h>
|
#include <Adafruit_SSD1306.h>
|
||||||
|
#include <limits.h>
|
||||||
|
#include <math.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
static Adafruit_SSD1306 display(OLED_WIDTH, OLED_HEIGHT, &Wire, -1);
|
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_tx_ts = 0;
|
||||||
static uint32_t g_last_read_ms = 0;
|
static uint32_t g_last_read_ms = 0;
|
||||||
static uint32_t g_last_tx_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 FaultType g_last_error = FaultType::None;
|
||||||
static uint32_t g_last_error_ts = 0;
|
static uint32_t g_last_error_ts = 0;
|
||||||
static uint32_t g_last_error_ms = 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_oled_on = true;
|
||||||
static bool g_prev_ctrl_high = false;
|
static bool g_prev_ctrl_high = false;
|
||||||
static uint32_t g_oled_off_start = 0;
|
|
||||||
static uint32_t g_last_page_ms = 0;
|
static uint32_t g_last_page_ms = 0;
|
||||||
static uint8_t g_page = 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 bool g_display_ready = false;
|
||||||
static uint32_t g_last_init_attempt_ms = 0;
|
static uint32_t g_last_init_attempt_ms = 0;
|
||||||
static bool g_last_oled_on = true;
|
static bool g_last_oled_on = true;
|
||||||
@@ -65,7 +70,9 @@ void display_power_down() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void display_init() {
|
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.begin(PIN_OLED_SDA, PIN_OLED_SCL);
|
||||||
Wire.setClock(100000);
|
Wire.setClock(100000);
|
||||||
g_display_ready = display.begin(SSD1306_SWITCHCAPVCC, OLED_I2C_ADDR);
|
g_display_ready = display.begin(SSD1306_SWITCHCAPVCC, OLED_I2C_ADDR);
|
||||||
@@ -77,7 +84,7 @@ void display_init() {
|
|||||||
display.display();
|
display.display();
|
||||||
}
|
}
|
||||||
g_last_init_attempt_ms = millis();
|
g_last_init_attempt_ms = millis();
|
||||||
g_boot_ms = millis();
|
g_last_activity_ms = millis();
|
||||||
}
|
}
|
||||||
|
|
||||||
void display_set_role(DeviceRole role) {
|
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();
|
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) {
|
void display_set_last_error(FaultType type, uint32_t ts_utc, uint32_t ts_ms) {
|
||||||
g_last_error = type;
|
g_last_error = type;
|
||||||
g_last_error_ts = ts_utc;
|
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;
|
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) {
|
static bool render_last_error_line(uint8_t y) {
|
||||||
if (g_last_error == FaultType::None) {
|
if (g_last_error == FaultType::None) {
|
||||||
return false;
|
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.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.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
|
#ifdef ENABLE_TEST_MODE
|
||||||
if (strlen(g_test_code) > 0) {
|
if (strlen(g_test_code) > 0) {
|
||||||
@@ -215,15 +252,15 @@ static void render_sender_status() {
|
|||||||
static void render_sender_measurement() {
|
static void render_sender_measurement() {
|
||||||
display.clearDisplay();
|
display.clearDisplay();
|
||||||
display.setCursor(0, 0);
|
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.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.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.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.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();
|
display.display();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -248,16 +285,10 @@ static void render_receiver_status() {
|
|||||||
display.printf("Time: %s", time_buf);
|
display.printf("Time: %s", time_buf);
|
||||||
|
|
||||||
uint32_t latest = 0;
|
uint32_t latest = 0;
|
||||||
bool link_valid = false;
|
|
||||||
int16_t link_rssi = 0;
|
|
||||||
float link_snr = 0.0f;
|
|
||||||
if (g_statuses) {
|
if (g_statuses) {
|
||||||
for (uint8_t i = 0; i < g_status_count; ++i) {
|
for (uint8_t i = 0; i < g_status_count; ++i) {
|
||||||
if (g_statuses[i].has_data && g_statuses[i].last_update_ts_utc > latest) {
|
if (g_statuses[i].has_data && g_statuses[i].last_update_ts_utc > latest) {
|
||||||
latest = g_statuses[i].last_update_ts_utc;
|
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);
|
localtime_r(&t, &timeinfo);
|
||||||
display.printf("Upd %02d:%02d", timeinfo.tm_hour, timeinfo.tm_min);
|
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);
|
render_last_error_line(56);
|
||||||
display.display();
|
display.display();
|
||||||
@@ -292,7 +320,15 @@ static void render_receiver_sender(uint8_t index) {
|
|||||||
display.setCursor(0, 0);
|
display.setCursor(0, 0);
|
||||||
uint8_t bat = status.has_data ? status.last_data.battery_percent : 0;
|
uint8_t bat = status.has_data ? status.last_data.battery_percent : 0;
|
||||||
if (status.has_data) {
|
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 {
|
} else {
|
||||||
display.printf("%s B--", status.last_data.device_id);
|
display.printf("%s B--", status.last_data.device_id);
|
||||||
}
|
}
|
||||||
@@ -314,19 +350,37 @@ static void render_receiver_sender(uint8_t index) {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
display.setCursor(0, 12);
|
display.setCursor(0, 12);
|
||||||
display.printf("E %.1f kWh", status.last_data.energy_total_kwh);
|
if (status.last_data.energy_multi) {
|
||||||
display.setCursor(0, 24);
|
display.printf("E1 %lu E2 %lu", static_cast<unsigned long>(status.last_data.energy_kwh_int[0]),
|
||||||
display.printf("P %.0fW", status.last_data.total_power_w);
|
static_cast<unsigned long>(status.last_data.energy_kwh_int[1]));
|
||||||
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 {
|
} 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);
|
||||||
|
if (status.last_data.energy_multi && status.last_data.energy_meter_count >= 3) {
|
||||||
|
display.printf("E3 %lu", static_cast<unsigned long>(status.last_data.energy_kwh_int[2]));
|
||||||
|
} else {
|
||||||
|
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();
|
display.display();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -345,26 +399,21 @@ void display_tick() {
|
|||||||
}
|
}
|
||||||
return;
|
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;
|
uint32_t now_ms = millis();
|
||||||
if (in_boot_window) {
|
bool ctrl_falling_edge = g_prev_ctrl_high && !ctrl_high;
|
||||||
|
if (g_role == DeviceRole::Receiver) {
|
||||||
g_oled_on = true;
|
g_oled_on = true;
|
||||||
|
g_last_activity_ms = now_ms;
|
||||||
} else {
|
} else {
|
||||||
if (ctrl_high) {
|
if (ctrl_high || ctrl_falling_edge) {
|
||||||
g_oled_on = true;
|
g_last_activity_ms = now_ms;
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
g_oled_on = (now_ms - g_last_activity_ms) < OLED_AUTO_OFF_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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (g_oled_on) {
|
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,7 @@
|
|||||||
#include "json_codec.h"
|
#include "json_codec.h"
|
||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
|
#include <limits.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include "power_manager.h"
|
|
||||||
|
|
||||||
static float round2(float value) {
|
static float round2(float value) {
|
||||||
if (isnan(value)) {
|
if (isnan(value)) {
|
||||||
@@ -10,6 +10,20 @@ static float round2(float value) {
|
|||||||
return roundf(value * 100.0f) / 100.0f;
|
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) {
|
static const char *short_id_from_device_id(const char *device_id) {
|
||||||
if (!device_id) {
|
if (!device_id) {
|
||||||
return "";
|
return "";
|
||||||
@@ -32,27 +46,36 @@ static void format_float_2(char *buf, size_t buf_len, float value) {
|
|||||||
snprintf(buf, buf_len, "%.2f", round2(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) {
|
bool meterDataToJson(const MeterData &data, String &out_json) {
|
||||||
StaticJsonDocument<256> doc;
|
StaticJsonDocument<320> doc;
|
||||||
doc["id"] = short_id_from_device_id(data.device_id);
|
doc["id"] = short_id_from_device_id(data.device_id);
|
||||||
doc["ts"] = data.ts_utc;
|
doc["ts"] = data.ts_utc;
|
||||||
char buf[16];
|
char buf[16];
|
||||||
format_float_2(buf, sizeof(buf), data.energy_total_kwh);
|
if (data.energy_multi) {
|
||||||
doc["e_kwh"] = serialized(buf);
|
doc["energy1_kwh"] = data.energy_kwh_int[0];
|
||||||
format_float_2(buf, sizeof(buf), data.total_power_w);
|
doc["energy2_kwh"] = data.energy_kwh_int[1];
|
||||||
doc["p_w"] = serialized(buf);
|
if (data.energy_meter_count >= 3) {
|
||||||
format_float_2(buf, sizeof(buf), data.phase_power_w[0]);
|
doc["energy3_kwh"] = data.energy_kwh_int[2];
|
||||||
doc["p1_w"] = serialized(buf);
|
}
|
||||||
format_float_2(buf, sizeof(buf), data.phase_power_w[1]);
|
} else {
|
||||||
doc["p2_w"] = serialized(buf);
|
format_float_2(buf, sizeof(buf), data.energy_total_kwh);
|
||||||
format_float_2(buf, sizeof(buf), data.phase_power_w[2]);
|
doc["e_kwh"] = serialized(buf);
|
||||||
doc["p3_w"] = serialized(buf);
|
set_int_or_null(doc, "p_w", data.total_power_w);
|
||||||
format_float_2(buf, sizeof(buf), data.phase_voltage_v[0]);
|
set_int_or_null(doc, "p1_w", data.phase_power_w[0]);
|
||||||
doc["v1_v"] = serialized(buf);
|
set_int_or_null(doc, "p2_w", data.phase_power_w[1]);
|
||||||
format_float_2(buf, sizeof(buf), data.phase_voltage_v[1]);
|
set_int_or_null(doc, "p3_w", data.phase_power_w[2]);
|
||||||
doc["v2_v"] = serialized(buf);
|
}
|
||||||
format_float_2(buf, sizeof(buf), data.phase_voltage_v[2]);
|
|
||||||
doc["v3_v"] = serialized(buf);
|
|
||||||
format_float_2(buf, sizeof(buf), data.battery_voltage_v);
|
format_float_2(buf, sizeof(buf), data.battery_voltage_v);
|
||||||
doc["bat_v"] = serialized(buf);
|
doc["bat_v"] = serialized(buf);
|
||||||
doc["bat_pct"] = data.battery_percent;
|
doc["bat_pct"] = data.battery_percent;
|
||||||
@@ -69,177 +92,11 @@ bool meterDataToJson(const MeterData &data, String &out_json) {
|
|||||||
if (data.err_lora_tx > 0) {
|
if (data.err_lora_tx > 0) {
|
||||||
doc["err_tx"] = data.err_lora_tx;
|
doc["err_tx"] = data.err_lora_tx;
|
||||||
}
|
}
|
||||||
if (data.last_error != FaultType::None) {
|
doc["err_last"] = static_cast<uint8_t>(data.last_error);
|
||||||
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 = "";
|
out_json = "";
|
||||||
size_t len = serializeJson(doc, out_json);
|
size_t len = serializeJson(doc, out_json);
|
||||||
return len > 0 && len < 256;
|
return len > 0 && len < 320;
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
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_transport.h"
|
||||||
#include <LoRa.h>
|
#include <LoRa.h>
|
||||||
#include <SPI.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) {
|
static uint16_t crc16_ccitt(const uint8_t *data, size_t len) {
|
||||||
uint16_t crc = 0xFFFF;
|
uint16_t crc = 0xFFFF;
|
||||||
@@ -29,13 +62,26 @@ void lora_init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool lora_send(const LoraPacket &pkt) {
|
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;
|
size_t idx = 0;
|
||||||
buffer[idx++] = pkt.protocol_version;
|
buffer[idx++] = static_cast<uint8_t>(pkt.msg_kind);
|
||||||
buffer[idx++] = static_cast<uint8_t>(pkt.role);
|
|
||||||
buffer[idx++] = static_cast<uint8_t>(pkt.device_id_short >> 8);
|
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.device_id_short & 0xFF);
|
||||||
buffer[idx++] = static_cast<uint8_t>(pkt.payload_type);
|
|
||||||
|
|
||||||
if (pkt.payload_len > LORA_MAX_PAYLOAD) {
|
if (pkt.payload_len > LORA_MAX_PAYLOAD) {
|
||||||
return false;
|
return false;
|
||||||
@@ -49,8 +95,24 @@ bool lora_send(const LoraPacket &pkt) {
|
|||||||
buffer[idx++] = static_cast<uint8_t>(crc & 0xFF);
|
buffer[idx++] = static_cast<uint8_t>(crc & 0xFF);
|
||||||
|
|
||||||
LoRa.beginPacket();
|
LoRa.beginPacket();
|
||||||
|
if (SERIAL_DEBUG_MODE) {
|
||||||
|
t2 = millis();
|
||||||
|
}
|
||||||
LoRa.write(buffer, idx);
|
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;
|
return result == 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,40 +121,61 @@ bool lora_receive(LoraPacket &pkt, uint32_t timeout_ms) {
|
|||||||
while (true) {
|
while (true) {
|
||||||
int packet_size = LoRa.parsePacket();
|
int packet_size = LoRa.parsePacket();
|
||||||
if (packet_size > 0) {
|
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()) {
|
while (LoRa.available()) {
|
||||||
LoRa.read();
|
LoRa.read();
|
||||||
}
|
}
|
||||||
|
note_reject(RxRejectReason::LengthMismatch);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t buffer[5 + LORA_MAX_PAYLOAD + 2];
|
uint8_t buffer[1 + 2 + LORA_MAX_PAYLOAD + 2];
|
||||||
size_t len = 0;
|
size_t len = 0;
|
||||||
while (LoRa.available() && len < sizeof(buffer)) {
|
while (LoRa.available() && len < sizeof(buffer)) {
|
||||||
buffer[len++] = LoRa.read();
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t crc_calc = crc16_ccitt(buffer, len - 2);
|
uint16_t crc_calc = crc16_ccitt(buffer, len - 2);
|
||||||
uint16_t crc_rx = static_cast<uint16_t>(buffer[len - 2] << 8) | buffer[len - 1];
|
uint16_t crc_rx = static_cast<uint16_t>(buffer[len - 2] << 8) | buffer[len - 1];
|
||||||
if (crc_calc != crc_rx) {
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
pkt.protocol_version = buffer[0];
|
pkt.msg_kind = static_cast<LoraMsgKind>(msg_kind);
|
||||||
pkt.role = static_cast<DeviceRole>(buffer[1]);
|
pkt.device_id_short = static_cast<uint16_t>(buffer[1] << 8) | buffer[2];
|
||||||
pkt.device_id_short = static_cast<uint16_t>(buffer[2] << 8) | buffer[3];
|
pkt.payload_len = len - 5;
|
||||||
pkt.payload_type = static_cast<PayloadType>(buffer[4]);
|
|
||||||
pkt.payload_len = len - 7;
|
|
||||||
if (pkt.payload_len > LORA_MAX_PAYLOAD) {
|
if (pkt.payload_len > LORA_MAX_PAYLOAD) {
|
||||||
|
note_reject(RxRejectReason::LengthMismatch);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
memcpy(pkt.payload, &buffer[5], pkt.payload_len);
|
memcpy(pkt.payload, &buffer[3], pkt.payload_len);
|
||||||
pkt.rssi_dbm = static_cast<int16_t>(LoRa.packetRssi());
|
pkt.rssi_dbm = g_last_rx_rssi_dbm;
|
||||||
pkt.snr_db = LoRa.packetSnr();
|
pkt.snr_db = g_last_rx_snr_db;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -103,6 +186,52 @@ bool lora_receive(LoraPacket &pkt, uint32_t timeout_ms) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void lora_idle() {
|
||||||
|
LoRa.idle();
|
||||||
|
}
|
||||||
|
|
||||||
void lora_sleep() {
|
void lora_sleep() {
|
||||||
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));
|
||||||
|
}
|
||||||
|
|||||||
924
src/main.cpp
924
src/main.cpp
File diff suppressed because it is too large
Load Diff
@@ -4,167 +4,31 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
static constexpr uint32_t METER_READ_TIMEOUT_MS = 2000;
|
static constexpr uint32_t METER_FRAME_TIMEOUT_MS = 1500;
|
||||||
static constexpr size_t SML_BUFFER_SIZE = 2048;
|
static constexpr size_t METER_FRAME_MAX = 512;
|
||||||
|
|
||||||
static const uint8_t OBIS_ENERGY_TOTAL[6] = {0x01, 0x00, 0x01, 0x08, 0x00, 0xFF};
|
enum class MeterRxState : uint8_t {
|
||||||
static const uint8_t OBIS_TOTAL_POWER[6] = {0x01, 0x00, 0x10, 0x07, 0x00, 0xFF};
|
WaitStart = 0,
|
||||||
static const uint8_t OBIS_P1[6] = {0x01, 0x00, 0x24, 0x07, 0x00, 0xFF};
|
InFrame = 1
|
||||||
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};
|
|
||||||
|
|
||||||
static bool find_obis_value(const uint8_t *buf, size_t len, const uint8_t *obis, float &out_value) {
|
struct MeterPort {
|
||||||
for (size_t i = 0; i + 6 < len; ++i) {
|
HardwareSerial *serial;
|
||||||
if (memcmp(&buf[i], obis, 6) == 0) {
|
MeterRxState state;
|
||||||
int8_t scaler = 0;
|
char frame_buf[METER_FRAME_MAX + 1];
|
||||||
bool scaler_found = false;
|
size_t frame_len;
|
||||||
bool value_found = false;
|
uint32_t last_rx_ms;
|
||||||
int64_t value = 0;
|
uint32_t bytes_rx;
|
||||||
size_t cursor = i + 6;
|
uint32_t frames_ok;
|
||||||
size_t limit = (i + 6 + 120 < len) ? i + 6 + 120 : len;
|
uint32_t frames_parse_fail;
|
||||||
|
uint32_t rx_overflow;
|
||||||
|
uint32_t rx_timeout;
|
||||||
|
uint32_t last_energy_kwh;
|
||||||
|
bool has_energy;
|
||||||
|
};
|
||||||
|
|
||||||
while (cursor < limit) {
|
static MeterPort g_ports[METER_COUNT] = {};
|
||||||
uint8_t tl = buf[cursor++];
|
static uint32_t g_last_log_ms = 0;
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
void meter_init() {
|
|
||||||
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) {
|
static bool parse_obis_ascii_value(const char *line, const char *obis, float &out_value) {
|
||||||
const char *p = strstr(line, obis);
|
const char *p = strstr(line, obis);
|
||||||
@@ -201,146 +65,142 @@ static bool parse_obis_ascii_value(const char *line, const char *obis, float &ou
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool parse_obis_ascii_unit_scale(const char *line, const char *obis, float &value) {
|
static bool parse_energy_kwh_floor(const char *frame, size_t len, uint32_t &out_kwh) {
|
||||||
const char *p = strstr(line, obis);
|
char line[128];
|
||||||
if (!p) {
|
size_t line_len = 0;
|
||||||
return false;
|
for (size_t i = 0; i < len; ++i) {
|
||||||
}
|
char c = frame[i];
|
||||||
const char *asterisk = strchr(p, '*');
|
if (c == '\r') {
|
||||||
if (!asterisk) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
const char *end = strchr(asterisk, ')');
|
|
||||||
if (!end) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
char unit_buf[8];
|
|
||||||
size_t ulen = 0;
|
|
||||||
for (const char *c = asterisk + 1; c < end && ulen + 1 < sizeof(unit_buf); ++c) {
|
|
||||||
if (*c == ' ') {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
unit_buf[ulen++] = *c;
|
if (c == '\n' || c == '!') {
|
||||||
}
|
line[line_len] = '\0';
|
||||||
unit_buf[ulen] = '\0';
|
float value = NAN;
|
||||||
if (ulen == 0) {
|
if (parse_obis_ascii_value(line, "1-0:1.8.0", value) && !isnan(value) && value >= 0.0f) {
|
||||||
return false;
|
out_kwh = static_cast<uint32_t>(floorf(value));
|
||||||
}
|
return true;
|
||||||
if (strcmp(unit_buf, "Wh") == 0) {
|
}
|
||||||
value *= 0.001f;
|
line_len = 0;
|
||||||
return true;
|
if (c == '!') {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (line_len + 1 < sizeof(line)) {
|
||||||
|
line[line_len++] = c;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool meter_read_ascii(MeterData &data) {
|
static void meter_debug_log() {
|
||||||
const uint32_t start_ms = millis();
|
if (!SERIAL_DEBUG_MODE) {
|
||||||
bool in_telegram = false;
|
return;
|
||||||
bool got_any = false;
|
}
|
||||||
|
uint32_t now_ms = millis();
|
||||||
|
if (now_ms - g_last_log_ms < 60000) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
g_last_log_ms = now_ms;
|
||||||
|
for (uint8_t i = 0; i < METER_COUNT; ++i) {
|
||||||
|
const MeterPort &p = g_ports[i];
|
||||||
|
Serial.printf("meter%u: ok=%lu parse_fail=%lu overflow=%lu timeout=%lu bytes=%lu e=%lu valid=%u\n",
|
||||||
|
static_cast<unsigned>(i + 1),
|
||||||
|
static_cast<unsigned long>(p.frames_ok),
|
||||||
|
static_cast<unsigned long>(p.frames_parse_fail),
|
||||||
|
static_cast<unsigned long>(p.rx_overflow),
|
||||||
|
static_cast<unsigned long>(p.rx_timeout),
|
||||||
|
static_cast<unsigned long>(p.bytes_rx),
|
||||||
|
static_cast<unsigned long>(p.last_energy_kwh),
|
||||||
|
p.has_energy ? 1 : 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool energy_ok = false;
|
void meter_init() {
|
||||||
bool total_p_ok = false;
|
g_ports[0].serial = &Serial2;
|
||||||
bool p1_ok = false;
|
g_ports[0].serial->begin(9600, SERIAL_7E1, PIN_METER1_RX, -1);
|
||||||
bool p2_ok = false;
|
g_ports[0].state = MeterRxState::WaitStart;
|
||||||
bool p3_ok = false;
|
|
||||||
bool v1_ok = false;
|
|
||||||
bool v2_ok = false;
|
|
||||||
bool v3_ok = false;
|
|
||||||
|
|
||||||
char line[128];
|
if (METER_COUNT >= 2) {
|
||||||
size_t line_len = 0;
|
g_ports[1].serial = &Serial1;
|
||||||
|
g_ports[1].serial->begin(9600, SERIAL_7E1, PIN_METER2_RX, -1);
|
||||||
|
g_ports[1].state = MeterRxState::WaitStart;
|
||||||
|
}
|
||||||
|
|
||||||
while (millis() - start_ms < METER_READ_TIMEOUT_MS) {
|
if (METER_COUNT >= 3) {
|
||||||
while (Serial2.available()) {
|
g_ports[2].serial = &Serial;
|
||||||
char c = static_cast<char>(Serial2.read());
|
g_ports[2].serial->begin(9600, SERIAL_7E1, PIN_METER3_RX, -1);
|
||||||
if (!in_telegram) {
|
g_ports[2].state = MeterRxState::WaitStart;
|
||||||
if (c == '/') {
|
}
|
||||||
in_telegram = true;
|
}
|
||||||
line_len = 0;
|
|
||||||
line[line_len++] = c;
|
static void meter_poll_port(MeterPort &port) {
|
||||||
}
|
if (!port.serial) {
|
||||||
continue;
|
return;
|
||||||
}
|
}
|
||||||
|
uint32_t now_ms = millis();
|
||||||
if (c == '\r') {
|
if (port.state == MeterRxState::InFrame && (now_ms - port.last_rx_ms > METER_FRAME_TIMEOUT_MS)) {
|
||||||
continue;
|
port.rx_timeout++;
|
||||||
}
|
port.state = MeterRxState::WaitStart;
|
||||||
if (c == '\n') {
|
port.frame_len = 0;
|
||||||
line[line_len] = '\0';
|
}
|
||||||
if (line[0] == '!') {
|
|
||||||
return got_any;
|
while (port.serial->available()) {
|
||||||
}
|
char c = static_cast<char>(port.serial->read());
|
||||||
|
port.bytes_rx++;
|
||||||
float value = NAN;
|
port.last_rx_ms = now_ms;
|
||||||
if (parse_obis_ascii_value(line, "1-0:1.8.0", value)) {
|
|
||||||
parse_obis_ascii_unit_scale(line, "1-0:1.8.0", value);
|
if (port.state == MeterRxState::WaitStart) {
|
||||||
data.energy_total_kwh = value;
|
if (c == '/') {
|
||||||
energy_ok = true;
|
port.state = MeterRxState::InFrame;
|
||||||
got_any = true;
|
port.frame_len = 0;
|
||||||
}
|
port.frame_buf[port.frame_len++] = c;
|
||||||
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;
|
|
||||||
}
|
|
||||||
if (line_len + 1 < sizeof(line)) {
|
|
||||||
line[line_len++] = c;
|
|
||||||
}
|
}
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
delay(5);
|
|
||||||
}
|
|
||||||
|
|
||||||
data.valid = energy_ok || total_p_ok || p1_ok || p2_ok || p3_ok || v1_ok || v2_ok || v3_ok;
|
if (port.frame_len + 1 >= sizeof(port.frame_buf)) {
|
||||||
return data.valid;
|
port.rx_overflow++;
|
||||||
|
port.state = MeterRxState::WaitStart;
|
||||||
|
port.frame_len = 0;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
port.frame_buf[port.frame_len++] = c;
|
||||||
|
if (c == '!') {
|
||||||
|
port.frame_buf[port.frame_len] = '\0';
|
||||||
|
uint32_t energy_kwh = 0;
|
||||||
|
if (parse_energy_kwh_floor(port.frame_buf, port.frame_len, energy_kwh)) {
|
||||||
|
port.last_energy_kwh = energy_kwh;
|
||||||
|
port.has_energy = true;
|
||||||
|
port.frames_ok++;
|
||||||
|
} else {
|
||||||
|
port.frames_parse_fail++;
|
||||||
|
}
|
||||||
|
port.state = MeterRxState::WaitStart;
|
||||||
|
port.frame_len = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool meter_read(MeterData &data) {
|
void meter_poll() {
|
||||||
data.energy_total_kwh = NAN;
|
for (uint8_t i = 0; i < METER_COUNT; ++i) {
|
||||||
data.total_power_w = NAN;
|
meter_poll_port(g_ports[i]);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
return meter_read_sml(data);
|
meter_debug_log();
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t meter_count() {
|
||||||
|
return METER_COUNT;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool meter_get_last_energy_kwh(uint8_t meter_idx, uint32_t &out_energy_kwh) {
|
||||||
|
if (meter_idx >= METER_COUNT) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!g_ports[meter_idx].has_energy) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
out_energy_kwh = g_ports[meter_idx].last_energy_kwh;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,19 @@ static PubSubClient mqtt_client(wifi_client);
|
|||||||
static WifiMqttConfig g_cfg;
|
static WifiMqttConfig g_cfg;
|
||||||
static String g_client_id;
|
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) {
|
void mqtt_init(const WifiMqttConfig &config, const char *device_id) {
|
||||||
g_cfg = config;
|
g_cfg = config;
|
||||||
mqtt_client.setServer(config.mqtt_host.c_str(), config.mqtt_port);
|
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_m"] = counters.meter_read_fail;
|
||||||
doc["err_d"] = counters.decode_fail;
|
doc["err_d"] = counters.decode_fail;
|
||||||
doc["err_tx"] = counters.lora_tx_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"] = static_cast<uint8_t>(last_error);
|
doc["err_last_text"] = fault_text(last_error);
|
||||||
doc["err_last_age"] = last_error_age_sec;
|
doc["err_last_age"] = last_error != FaultType::None ? last_error_age_sec : 0;
|
||||||
}
|
|
||||||
|
|
||||||
String payload;
|
String payload;
|
||||||
size_t len = serializeJson(doc, payload);
|
size_t len = serializeJson(doc, payload);
|
||||||
@@ -126,9 +138,6 @@ bool mqtt_publish_discovery(const char *device_id) {
|
|||||||
bool ok = true;
|
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, "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, "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, "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, "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 }}");
|
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_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_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_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 }}");
|
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;
|
return ok;
|
||||||
}
|
}
|
||||||
|
|||||||
406
src/payload_codec.cpp
Normal file
406
src/payload_codec.cpp
Normal file
@@ -0,0 +1,406 @@
|
|||||||
|
#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 constexpr uint8_t kPayloadSchemaLegacy = 0;
|
||||||
|
static constexpr uint8_t kPayloadSchemaEnergyMulti = 1;
|
||||||
|
|
||||||
|
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(23, out_cap, pos)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
write_u16_le(&out[pos], kMagic);
|
||||||
|
pos += 2;
|
||||||
|
out[pos++] = kSchema;
|
||||||
|
out[pos++] = kFlags;
|
||||||
|
out[pos++] = in.schema_id;
|
||||||
|
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;
|
||||||
|
out[pos++] = in.meter_count;
|
||||||
|
|
||||||
|
if (in.n == 0) {
|
||||||
|
*out_len = pos;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (in.schema_id == kPayloadSchemaEnergyMulti) {
|
||||||
|
if (in.meter_count == 0 || in.meter_count > 3) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!ensure_capacity(static_cast<size_t>(in.n) * 12, out_cap, pos)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for (uint8_t i = 0; i < in.n; ++i) {
|
||||||
|
write_u32_le(&out[pos], in.energy1_kwh[i]);
|
||||||
|
pos += 4;
|
||||||
|
write_u32_le(&out[pos], in.energy2_kwh[i]);
|
||||||
|
pos += 4;
|
||||||
|
write_u32_le(&out[pos], in.energy3_kwh[i]);
|
||||||
|
pos += 4;
|
||||||
|
}
|
||||||
|
*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 < 23) {
|
||||||
|
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->schema_id = buf[pos++];
|
||||||
|
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++];
|
||||||
|
out->meter_count = 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 (out->schema_id == kPayloadSchemaEnergyMulti) {
|
||||||
|
if (out->meter_count == 0 || out->meter_count > 3) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (pos + static_cast<size_t>(out->n) * 12 > len) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for (uint8_t i = 0; i < out->n; ++i) {
|
||||||
|
out->energy1_kwh[i] = read_u32_le(&buf[pos]);
|
||||||
|
pos += 4;
|
||||||
|
out->energy2_kwh[i] = read_u32_le(&buf[pos]);
|
||||||
|
pos += 4;
|
||||||
|
out->energy3_kwh[i] = read_u32_le(&buf[pos]);
|
||||||
|
pos += 4;
|
||||||
|
}
|
||||||
|
for (uint8_t i = out->n; i < kMaxSamples; ++i) {
|
||||||
|
out->energy1_kwh[i] = 0;
|
||||||
|
out->energy2_kwh[i] = 0;
|
||||||
|
out->energy3_kwh[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.schema_id = kPayloadSchemaLegacy;
|
||||||
|
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.meter_count = 0;
|
||||||
|
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
|
||||||
42
src/payload_codec.h
Normal file
42
src/payload_codec.h
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <Arduino.h>
|
||||||
|
|
||||||
|
struct BatchInput {
|
||||||
|
uint8_t schema_id;
|
||||||
|
uint16_t sender_id;
|
||||||
|
uint16_t batch_id;
|
||||||
|
uint32_t t_last;
|
||||||
|
uint8_t dt_s;
|
||||||
|
uint8_t n;
|
||||||
|
uint8_t meter_count;
|
||||||
|
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 energy1_kwh[30];
|
||||||
|
uint32_t energy2_kwh[30];
|
||||||
|
uint32_t energy3_kwh[30];
|
||||||
|
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>
|
#include <esp_sleep.h>
|
||||||
|
|
||||||
static constexpr float BATTERY_DIVIDER = 2.0f;
|
static constexpr float BATTERY_DIVIDER = 2.0f;
|
||||||
static constexpr float BATTERY_CAL = 1.0f;
|
|
||||||
static constexpr float ADC_REF_V = 3.3f;
|
static constexpr float ADC_REF_V = 3.3f;
|
||||||
|
|
||||||
void power_sender_init() {
|
void power_sender_init() {
|
||||||
|
setCpuFrequencyMhz(80);
|
||||||
|
WiFi.mode(WIFI_OFF);
|
||||||
esp_wifi_stop();
|
esp_wifi_stop();
|
||||||
|
esp_wifi_deinit();
|
||||||
btStop();
|
btStop();
|
||||||
analogReadResolution(12);
|
analogReadResolution(12);
|
||||||
pinMode(PIN_BAT_ADC, INPUT);
|
pinMode(PIN_BAT_ADC, INPUT);
|
||||||
@@ -22,29 +24,89 @@ void power_receiver_init() {
|
|||||||
pinMode(PIN_BAT_ADC, INPUT);
|
pinMode(PIN_BAT_ADC, INPUT);
|
||||||
}
|
}
|
||||||
|
|
||||||
void read_battery(MeterData &data) {
|
void power_configure_unused_pins_sender() {
|
||||||
const int samples = 8;
|
// Board-specific: only touch pins that are known unused and safe on TTGO LoRa32 v1.6.1
|
||||||
uint32_t sum = 0;
|
const uint8_t pins[] = {32, 33};
|
||||||
for (int i = 0; i < samples; ++i) {
|
for (uint8_t pin : pins) {
|
||||||
sum += analogRead(PIN_BAT_ADC);
|
pinMode(pin, INPUT_PULLDOWN);
|
||||||
delay(5);
|
|
||||||
}
|
}
|
||||||
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;
|
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_voltage_v = v;
|
||||||
data.battery_percent = battery_percent_from_voltage(v);
|
data.battery_percent = battery_percent_from_voltage(v);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t battery_percent_from_voltage(float voltage_v) {
|
uint8_t battery_percent_from_voltage(float voltage_v) {
|
||||||
float pct = (voltage_v - 3.0f) / (4.2f - 3.0f) * 100.0f;
|
if (isnan(voltage_v)) {
|
||||||
if (pct < 0.0f) {
|
return 0;
|
||||||
pct = 0.0f;
|
|
||||||
}
|
}
|
||||||
if (pct > 100.0f) {
|
struct LutPoint {
|
||||||
pct = 100.0f;
|
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) {
|
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
|
#ifdef ENABLE_TEST_MODE
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "compressor.h"
|
|
||||||
#include "lora_transport.h"
|
#include "lora_transport.h"
|
||||||
#include "json_codec.h"
|
|
||||||
#include "time_manager.h"
|
#include "time_manager.h"
|
||||||
#include "display_ui.h"
|
#include "display_ui.h"
|
||||||
#include "mqtt_client.h"
|
#include "mqtt_client.h"
|
||||||
@@ -14,16 +12,94 @@
|
|||||||
|
|
||||||
static uint32_t g_last_test_ms = 0;
|
static uint32_t g_last_test_ms = 0;
|
||||||
static uint16_t g_test_code_counter = 1000;
|
static uint16_t g_test_code_counter = 1000;
|
||||||
static uint32_t g_last_timesync_ms = 0;
|
static uint16_t g_test_batch_id = 1;
|
||||||
|
static uint16_t g_test_last_acked_batch_id = 0;
|
||||||
static constexpr uint32_t TEST_SEND_INTERVAL_MS = 30000;
|
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) {
|
static void write_u16_be(uint8_t *dst, uint16_t value) {
|
||||||
LoraPacket rx = {};
|
dst[0] = static_cast<uint8_t>((value >> 8) & 0xFF);
|
||||||
if (lora_receive(rx, 0) && rx.payload_type == PayloadType::TimeSync) {
|
dst[1] = static_cast<uint8_t>(value & 0xFF);
|
||||||
time_handle_timesync_payload(rx.payload, rx.payload_len);
|
}
|
||||||
|
|
||||||
|
static uint16_t read_u16_be(const uint8_t *src) {
|
||||||
|
return static_cast<uint16_t>(src[0] << 8) | static_cast<uint16_t>(src[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void write_u32_be(uint8_t *dst, uint32_t value) {
|
||||||
|
dst[0] = static_cast<uint8_t>((value >> 24) & 0xFF);
|
||||||
|
dst[1] = static_cast<uint8_t>((value >> 16) & 0xFF);
|
||||||
|
dst[2] = static_cast<uint8_t>((value >> 8) & 0xFF);
|
||||||
|
dst[3] = static_cast<uint8_t>(value & 0xFF);
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint32_t read_u32_be(const uint8_t *src) {
|
||||||
|
return (static_cast<uint32_t>(src[0]) << 24) |
|
||||||
|
(static_cast<uint32_t>(src[1]) << 16) |
|
||||||
|
(static_cast<uint32_t>(src[2]) << 8) |
|
||||||
|
static_cast<uint32_t>(src[3]);
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint32_t ack_window_ms() {
|
||||||
|
uint32_t air_ms = lora_airtime_ms(lora_frame_size(LORA_ACK_DOWN_PAYLOAD_LEN));
|
||||||
|
uint32_t window_ms = air_ms + 300;
|
||||||
|
if (window_ms < 1200) {
|
||||||
|
window_ms = 1200;
|
||||||
|
}
|
||||||
|
if (window_ms > 4000) {
|
||||||
|
window_ms = 4000;
|
||||||
|
}
|
||||||
|
return window_ms;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool receive_ack_for_batch(uint16_t batch_id, uint8_t &time_valid, uint32_t &ack_epoch, int16_t &rssi_dbm, float &snr_db) {
|
||||||
|
LoraPacket ack_pkt = {};
|
||||||
|
uint32_t window_ms = ack_window_ms();
|
||||||
|
bool got_ack = lora_receive_window(ack_pkt, window_ms);
|
||||||
|
if (!got_ack) {
|
||||||
|
got_ack = lora_receive_window(ack_pkt, window_ms / 2);
|
||||||
|
}
|
||||||
|
if (!got_ack || ack_pkt.msg_kind != LoraMsgKind::AckDown || ack_pkt.payload_len < LORA_ACK_DOWN_PAYLOAD_LEN) {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint16_t ack_id = read_u16_be(&ack_pkt.payload[1]);
|
||||||
|
if (ack_id != batch_id) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
time_valid = ack_pkt.payload[0] & 0x01;
|
||||||
|
ack_epoch = read_u32_be(&ack_pkt.payload[3]);
|
||||||
|
rssi_dbm = ack_pkt.rssi_dbm;
|
||||||
|
snr_db = ack_pkt.snr_db;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void send_test_ack(uint16_t self_short_id, uint16_t batch_id, uint8_t &time_valid, uint32_t &ack_epoch) {
|
||||||
|
ack_epoch = time_get_utc();
|
||||||
|
time_valid = (time_is_synced() && ack_epoch >= MIN_ACCEPTED_EPOCH_UTC) ? 1 : 0;
|
||||||
|
if (!time_valid) {
|
||||||
|
ack_epoch = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
LoraPacket ack = {};
|
||||||
|
ack.msg_kind = LoraMsgKind::AckDown;
|
||||||
|
ack.device_id_short = self_short_id;
|
||||||
|
ack.payload_len = LORA_ACK_DOWN_PAYLOAD_LEN;
|
||||||
|
ack.payload[0] = time_valid;
|
||||||
|
write_u16_be(&ack.payload[1], batch_id);
|
||||||
|
write_u32_be(&ack.payload[3], ack_epoch);
|
||||||
|
|
||||||
|
uint8_t repeats = ACK_REPEAT_COUNT == 0 ? 1 : ACK_REPEAT_COUNT;
|
||||||
|
for (uint8_t i = 0; i < repeats; ++i) {
|
||||||
|
lora_send(ack);
|
||||||
|
if (i + 1 < repeats && ACK_REPEAT_DELAY_MS > 0) {
|
||||||
|
delay(ACK_REPEAT_DELAY_MS);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
lora_receive_continuous();
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_sender_loop(uint16_t short_id, const char *device_id) {
|
||||||
if (millis() - g_last_test_ms < TEST_SEND_INTERVAL_MS) {
|
if (millis() - g_last_test_ms < TEST_SEND_INTERVAL_MS) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -45,11 +121,13 @@ void test_sender_loop(uint16_t short_id, const char *device_id) {
|
|||||||
uint32_t now_utc = time_get_utc();
|
uint32_t now_utc = time_get_utc();
|
||||||
uint32_t ts = now_utc > 0 ? now_utc : millis() / 1000;
|
uint32_t ts = now_utc > 0 ? now_utc : millis() / 1000;
|
||||||
|
|
||||||
StaticJsonDocument<128> doc;
|
StaticJsonDocument<192> doc;
|
||||||
doc["id"] = device_id;
|
doc["id"] = device_id;
|
||||||
doc["role"] = "sender";
|
doc["role"] = "sender";
|
||||||
doc["test_code"] = code;
|
doc["test_code"] = code;
|
||||||
doc["ts"] = ts;
|
doc["ts"] = ts;
|
||||||
|
doc["batch_id"] = g_test_batch_id;
|
||||||
|
doc["last_acked"] = g_test_last_acked_batch_id;
|
||||||
char bat_buf[8];
|
char bat_buf[8];
|
||||||
snprintf(bat_buf, sizeof(bat_buf), "%.2f", data.battery_voltage_v);
|
snprintf(bat_buf, sizeof(bat_buf), "%.2f", data.battery_voltage_v);
|
||||||
doc["bat_v"] = serialized(bat_buf);
|
doc["bat_v"] = serialized(bat_buf);
|
||||||
@@ -60,58 +138,71 @@ void test_sender_loop(uint16_t short_id, const char *device_id) {
|
|||||||
data.ts_utc = ts;
|
data.ts_utc = ts;
|
||||||
display_set_last_meter(data);
|
display_set_last_meter(data);
|
||||||
|
|
||||||
uint8_t compressed[LORA_MAX_PAYLOAD];
|
if (json.length() > 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)) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
LoraPacket pkt = {};
|
LoraPacket pkt = {};
|
||||||
pkt.protocol_version = PROTOCOL_VERSION;
|
pkt.msg_kind = LoraMsgKind::BatchUp;
|
||||||
pkt.role = DeviceRole::Sender;
|
|
||||||
pkt.device_id_short = short_id;
|
pkt.device_id_short = short_id;
|
||||||
pkt.payload_type = PayloadType::TestCode;
|
pkt.payload_len = json.length();
|
||||||
pkt.payload_len = compressed_len;
|
memcpy(pkt.payload, json.c_str(), pkt.payload_len);
|
||||||
memcpy(pkt.payload, compressed, compressed_len);
|
if (!lora_send(pkt)) {
|
||||||
lora_send(pkt);
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t time_valid = 0;
|
||||||
|
uint32_t ack_epoch = 0;
|
||||||
|
int16_t ack_rssi = 0;
|
||||||
|
float ack_snr = 0.0f;
|
||||||
|
if (receive_ack_for_batch(g_test_batch_id, time_valid, ack_epoch, ack_rssi, ack_snr)) {
|
||||||
|
if (time_valid == 1 && ack_epoch >= MIN_ACCEPTED_EPOCH_UTC) {
|
||||||
|
time_set_utc(ack_epoch);
|
||||||
|
}
|
||||||
|
g_test_last_acked_batch_id = g_test_batch_id;
|
||||||
|
g_test_batch_id++;
|
||||||
|
if (SERIAL_DEBUG_MODE) {
|
||||||
|
Serial.printf("test ack: batch=%u time_valid=%u epoch=%lu rssi=%d snr=%.1f\n",
|
||||||
|
static_cast<unsigned>(g_test_last_acked_batch_id),
|
||||||
|
static_cast<unsigned>(time_valid),
|
||||||
|
static_cast<unsigned long>(ack_epoch),
|
||||||
|
static_cast<int>(ack_rssi),
|
||||||
|
static_cast<double>(ack_snr));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_receiver_loop(SenderStatus *statuses, uint8_t count, uint16_t self_short_id) {
|
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
LoraPacket pkt = {};
|
LoraPacket pkt = {};
|
||||||
if (!lora_receive(pkt, 0)) {
|
if (!lora_receive(pkt, 0)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (pkt.payload_type != PayloadType::TestCode) {
|
if (pkt.msg_kind != LoraMsgKind::BatchUp) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t decompressed[160];
|
uint8_t decompressed[192];
|
||||||
size_t decompressed_len = 0;
|
if (pkt.payload_len >= sizeof(decompressed)) {
|
||||||
if (!decompressBuffer(pkt.payload, pkt.payload_len, decompressed, sizeof(decompressed) - 1, decompressed_len)) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (decompressed_len >= sizeof(decompressed)) {
|
memcpy(decompressed, pkt.payload, pkt.payload_len);
|
||||||
return;
|
decompressed[pkt.payload_len] = '\0';
|
||||||
}
|
|
||||||
decompressed[decompressed_len] = '\0';
|
|
||||||
|
|
||||||
StaticJsonDocument<128> doc;
|
StaticJsonDocument<192> doc;
|
||||||
if (deserializeJson(doc, reinterpret_cast<const char *>(decompressed)) != DeserializationError::Ok) {
|
if (deserializeJson(doc, reinterpret_cast<const char *>(decompressed)) != DeserializationError::Ok) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *id = doc["id"] | "";
|
const char *id = doc["id"] | "";
|
||||||
const char *code = doc["test_code"] | "";
|
const char *code = doc["test_code"] | "";
|
||||||
|
uint16_t batch_id = static_cast<uint16_t>(doc["batch_id"] | 0);
|
||||||
|
uint32_t ts = doc["ts"] | 0;
|
||||||
float bat_v = doc["bat_v"] | NAN;
|
float bat_v = doc["bat_v"] | NAN;
|
||||||
|
|
||||||
|
uint8_t time_valid = 0;
|
||||||
|
uint32_t ack_epoch = 0;
|
||||||
|
send_test_ack(self_short_id, batch_id, time_valid, ack_epoch);
|
||||||
|
|
||||||
for (uint8_t i = 0; i < count; ++i) {
|
for (uint8_t i = 0; i < count; ++i) {
|
||||||
if (strncmp(statuses[i].last_data.device_id, id, sizeof(statuses[i].last_data.device_id)) == 0) {
|
if (strncmp(statuses[i].last_data.device_id, id, sizeof(statuses[i].last_data.device_id)) == 0) {
|
||||||
display_set_test_code_for_sender(i, code);
|
display_set_test_code_for_sender(i, code);
|
||||||
@@ -119,12 +210,34 @@ void test_receiver_loop(SenderStatus *statuses, uint8_t count, uint16_t self_sho
|
|||||||
statuses[i].last_data.battery_voltage_v = bat_v;
|
statuses[i].last_data.battery_voltage_v = bat_v;
|
||||||
statuses[i].last_data.battery_percent = battery_percent_from_voltage(bat_v);
|
statuses[i].last_data.battery_percent = battery_percent_from_voltage(bat_v);
|
||||||
}
|
}
|
||||||
|
statuses[i].last_data.link_valid = true;
|
||||||
|
statuses[i].last_data.link_rssi_dbm = pkt.rssi_dbm;
|
||||||
|
statuses[i].last_data.link_snr_db = pkt.snr_db;
|
||||||
|
statuses[i].last_data.ts_utc = ts;
|
||||||
|
statuses[i].last_acked_batch_id = batch_id;
|
||||||
statuses[i].has_data = true;
|
statuses[i].has_data = true;
|
||||||
statuses[i].last_update_ts_utc = time_get_utc();
|
statuses[i].last_update_ts_utc = time_get_utc();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mqtt_publish_test(id, String(reinterpret_cast<const char *>(decompressed)));
|
StaticJsonDocument<256> mqtt_doc;
|
||||||
|
mqtt_doc["id"] = id;
|
||||||
|
mqtt_doc["role"] = "receiver";
|
||||||
|
mqtt_doc["test_code"] = code;
|
||||||
|
mqtt_doc["ts"] = ts;
|
||||||
|
mqtt_doc["batch_id"] = batch_id;
|
||||||
|
mqtt_doc["acked_batch_id"] = batch_id;
|
||||||
|
if (!isnan(bat_v)) {
|
||||||
|
mqtt_doc["bat_v"] = bat_v;
|
||||||
|
}
|
||||||
|
mqtt_doc["rssi"] = pkt.rssi_dbm;
|
||||||
|
mqtt_doc["snr"] = pkt.snr_db;
|
||||||
|
mqtt_doc["time_valid"] = time_valid;
|
||||||
|
mqtt_doc["ack_epoch"] = ack_epoch;
|
||||||
|
|
||||||
|
String mqtt_payload;
|
||||||
|
serializeJson(mqtt_doc, mqtt_payload);
|
||||||
|
mqtt_publish_test(id, mqtt_payload);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,12 +1,8 @@
|
|||||||
#include "time_manager.h"
|
#include "time_manager.h"
|
||||||
#include "compressor.h"
|
|
||||||
#include "config.h"
|
|
||||||
#include "rtc_ds3231.h"
|
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
static bool g_time_synced = false;
|
static bool g_time_synced = false;
|
||||||
static bool g_tz_set = false;
|
static bool g_tz_set = false;
|
||||||
static bool g_rtc_present = false;
|
|
||||||
static uint32_t g_last_sync_utc = 0;
|
static uint32_t g_last_sync_utc = 0;
|
||||||
|
|
||||||
static void note_last_sync(uint32_t epoch) {
|
static void note_last_sync(uint32_t epoch) {
|
||||||
@@ -55,59 +51,6 @@ void time_set_utc(uint32_t epoch) {
|
|||||||
settimeofday(&tv, nullptr);
|
settimeofday(&tv, nullptr);
|
||||||
g_time_synced = true;
|
g_time_synced = true;
|
||||||
note_last_sync(epoch);
|
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) {
|
void time_get_local_hhmm(char *out, size_t out_len) {
|
||||||
@@ -121,33 +64,6 @@ void time_get_local_hhmm(char *out, size_t out_len) {
|
|||||||
snprintf(out, out_len, "%02d:%02d", timeinfo.tm_hour, timeinfo.tm_min);
|
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() {
|
uint32_t time_get_last_sync_utc() {
|
||||||
return g_last_sync_utc;
|
return g_last_sync_utc;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,17 +1,111 @@
|
|||||||
#include "web_server.h"
|
#include "web_server.h"
|
||||||
#include <WebServer.h>
|
#include <WebServer.h>
|
||||||
#include "wifi_manager.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 WebServer server(80);
|
||||||
static const SenderStatus *g_statuses = nullptr;
|
static const SenderStatus *g_statuses = nullptr;
|
||||||
static uint8_t g_status_count = 0;
|
static uint8_t g_status_count = 0;
|
||||||
static WifiMqttConfig g_config;
|
static WifiMqttConfig g_config;
|
||||||
static bool g_is_ap = false;
|
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 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) {
|
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'>";
|
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 += "<title>" + safe_title + "</title></head><body>";
|
||||||
h += "<h2>" + title + "</h2>";
|
h += "<h2>" + safe_title + "</h2>";
|
||||||
return h;
|
return h;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -19,22 +113,328 @@ static String html_footer() {
|
|||||||
return "</body></html>";
|
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) {
|
static String render_sender_block(const SenderStatus &status) {
|
||||||
String s;
|
String s;
|
||||||
s += "<div style='margin-bottom:10px;padding:6px;border:1px solid #ccc'>";
|
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 += " ack:" + String(status.last_acked_batch_id);
|
||||||
|
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) {
|
if (!status.has_data) {
|
||||||
s += "No data";
|
s += "No data";
|
||||||
} else {
|
} else {
|
||||||
s += "Energy: " + String(status.last_data.energy_total_kwh, 3) + " kWh<br>";
|
if (status.last_data.energy_multi) {
|
||||||
s += "Power: " + String(status.last_data.total_power_w, 1) + " W<br>";
|
s += "Energy1: " + String(status.last_data.energy_kwh_int[0]) + " kWh<br>";
|
||||||
s += "Battery: " + String(status.last_data.battery_voltage_v, 2) + " V (" + String(status.last_data.battery_percent) + ")";
|
s += "Energy2: " + String(status.last_data.energy_kwh_int[1]) + " kWh<br>";
|
||||||
|
if (status.last_data.energy_meter_count >= 3) {
|
||||||
|
s += "Energy3: " + String(status.last_data.energy_kwh_int[2]) + " kWh<br>";
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
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>";
|
s += "</div>";
|
||||||
return s;
|
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() {
|
static void handle_root() {
|
||||||
|
if (!ensure_auth()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
String html = html_header("DD3 Bridge Status");
|
String html = html_header("DD3 Bridge Status");
|
||||||
html += g_is_ap ? "<p>Mode: AP</p>" : "<p>Mode: STA</p>";
|
html += g_is_ap ? "<p>Mode: AP</p>" : "<p>Mode: STA</p>";
|
||||||
|
|
||||||
@@ -44,22 +444,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='/wifi'>Configure WiFi/MQTT/NTP</a></p>";
|
||||||
|
html += "<p><a href='/manual'>Manual</a></p>";
|
||||||
html += html_footer();
|
html += html_footer();
|
||||||
server.send(200, "text/html", html);
|
server.send(200, "text/html", html);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void handle_wifi_get() {
|
static void handle_wifi_get() {
|
||||||
|
if (!ensure_auth()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
String html = html_header("WiFi/MQTT Config");
|
String html = html_header("WiFi/MQTT Config");
|
||||||
html += "<form method='POST' action='/wifi'>";
|
html += "<form method='POST' action='/wifi'>";
|
||||||
html += "SSID: <input name='ssid' value='" + g_config.ssid + "'><br>";
|
html += "SSID: <input name='ssid' value='" + html_escape(g_config.ssid) + "'><br>";
|
||||||
html += "Password: <input name='pass' type='password' value='" + g_config.password + "'><br>";
|
html += "Password: <input name='pass' type='password'> ";
|
||||||
html += "MQTT Host: <input name='mqhost' value='" + g_config.mqtt_host + "'><br>";
|
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 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 User: <input name='mquser' value='" + html_escape(g_config.mqtt_user) + "'><br>";
|
||||||
html += "MQTT Pass: <input name='mqpass' type='password' value='" + g_config.mqtt_pass + "'><br>";
|
html += "MQTT Pass: <input name='mqpass' type='password'> ";
|
||||||
html += "NTP Server 1: <input name='ntp1' value='" + g_config.ntp_server_1 + "'><br>";
|
html += "<label><input type='checkbox' name='clear_mqtt_pass'> Clear password</label><br>";
|
||||||
html += "NTP Server 2: <input name='ntp2' value='" + g_config.ntp_server_2 + "'><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 += "<button type='submit'>Save</button>";
|
||||||
html += "</form>";
|
html += "</form>";
|
||||||
html += html_footer();
|
html += html_footer();
|
||||||
@@ -67,15 +490,38 @@ static void handle_wifi_get() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void handle_wifi_post() {
|
static void handle_wifi_post() {
|
||||||
WifiMqttConfig cfg;
|
if (!ensure_auth()) {
|
||||||
cfg.ntp_server_1 = "pool.ntp.org";
|
return;
|
||||||
cfg.ntp_server_2 = "time.nist.gov";
|
}
|
||||||
|
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.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_host = server.arg("mqhost");
|
||||||
cfg.mqtt_port = static_cast<uint16_t>(server.arg("mqport").toInt());
|
cfg.mqtt_port = static_cast<uint16_t>(server.arg("mqport").toInt());
|
||||||
cfg.mqtt_user = server.arg("mquser");
|
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) {
|
if (server.arg("ntp1").length() > 0) {
|
||||||
cfg.ntp_server_1 = server.arg("ntp1");
|
cfg.ntp_server_1 = server.arg("ntp1");
|
||||||
}
|
}
|
||||||
@@ -83,6 +529,9 @@ static void handle_wifi_post() {
|
|||||||
cfg.ntp_server_2 = server.arg("ntp2");
|
cfg.ntp_server_2 = server.arg("ntp2");
|
||||||
}
|
}
|
||||||
cfg.valid = true;
|
cfg.valid = true;
|
||||||
|
g_config = cfg;
|
||||||
|
g_web_user = cfg.web_user;
|
||||||
|
g_web_pass = cfg.web_pass;
|
||||||
wifi_save_config(cfg);
|
wifi_save_config(cfg);
|
||||||
server.send(200, "text/html", "<html><body>Saved. Rebooting...</body></html>");
|
server.send(200, "text/html", "<html><body>Saved. Rebooting...</body></html>");
|
||||||
delay(1000);
|
delay(1000);
|
||||||
@@ -90,16 +539,107 @@ static void handle_wifi_post() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void handle_sender() {
|
static void handle_sender() {
|
||||||
|
if (!ensure_auth()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (!g_statuses) {
|
if (!g_statuses) {
|
||||||
server.send(404, "text/plain", "No senders");
|
server.send(404, "text/plain", "No senders");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
String uri = server.uri();
|
String uri = server.uri();
|
||||||
String device_id = uri.substring(String("/sender/").length());
|
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) {
|
for (uint8_t i = 0; i < g_status_count; ++i) {
|
||||||
if (device_id.equalsIgnoreCase(g_statuses[i].last_data.device_id)) {
|
if (device_id.equalsIgnoreCase(g_statuses[i].last_data.device_id)) {
|
||||||
String html = html_header("Sender " + device_id);
|
String html = html_header("Sender " + device_id);
|
||||||
html += render_sender_block(g_statuses[i]);
|
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>energy1_kwh</th><th>energy2_kwh</th><th>energy3_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_kwh_int[0]) + "</td>";
|
||||||
|
html += "<td>" + String(d.energy_kwh_int[1]) + "</td>";
|
||||||
|
html += "<td>" + String(d.energy_kwh_int[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();
|
html += html_footer();
|
||||||
server.send(200, "text/html", html);
|
server.send(200, "text/html", html);
|
||||||
return;
|
return;
|
||||||
@@ -108,8 +648,217 @@ static void handle_sender() {
|
|||||||
server.send(404, "text/plain", "Not found");
|
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) {
|
void web_server_set_config(const WifiMqttConfig &config) {
|
||||||
g_config = 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) {
|
void web_server_begin_ap(const SenderStatus *statuses, uint8_t count) {
|
||||||
@@ -118,6 +867,10 @@ void web_server_begin_ap(const SenderStatus *statuses, uint8_t count) {
|
|||||||
g_is_ap = true;
|
g_is_ap = true;
|
||||||
|
|
||||||
server.on("/", handle_root);
|
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_GET, handle_wifi_get);
|
||||||
server.on("/wifi", HTTP_POST, handle_wifi_post);
|
server.on("/wifi", HTTP_POST, handle_wifi_post);
|
||||||
server.on("/sender/", handle_sender);
|
server.on("/sender/", handle_sender);
|
||||||
@@ -137,7 +890,11 @@ void web_server_begin_sta(const SenderStatus *statuses, uint8_t count) {
|
|||||||
g_is_ap = false;
|
g_is_ap = false;
|
||||||
|
|
||||||
server.on("/", handle_root);
|
server.on("/", handle_root);
|
||||||
|
server.on("/manual", handle_manual);
|
||||||
server.on("/sender/", handle_sender);
|
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_GET, handle_wifi_get);
|
||||||
server.on("/wifi", HTTP_POST, handle_wifi_post);
|
server.on("/wifi", HTTP_POST, handle_wifi_post);
|
||||||
server.onNotFound([]() {
|
server.onNotFound([]() {
|
||||||
@@ -151,5 +908,6 @@ void web_server_begin_sta(const SenderStatus *statuses, uint8_t count) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void web_server_loop() {
|
void web_server_loop() {
|
||||||
|
history_tick();
|
||||||
server.handleClient();
|
server.handleClient();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#include "wifi_manager.h"
|
#include "wifi_manager.h"
|
||||||
|
#include "config.h"
|
||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
#include <esp_wifi.h>
|
#include <esp_wifi.h>
|
||||||
|
|
||||||
@@ -10,9 +11,6 @@ void wifi_manager_init() {
|
|||||||
|
|
||||||
bool wifi_load_config(WifiMqttConfig &config) {
|
bool wifi_load_config(WifiMqttConfig &config) {
|
||||||
config.valid = prefs.getBool("valid", false);
|
config.valid = prefs.getBool("valid", false);
|
||||||
if (!config.valid) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
config.ssid = prefs.getString("ssid", "");
|
config.ssid = prefs.getString("ssid", "");
|
||||||
config.password = prefs.getString("pass", "");
|
config.password = prefs.getString("pass", "");
|
||||||
config.mqtt_host = prefs.getString("mqhost", "");
|
config.mqtt_host = prefs.getString("mqhost", "");
|
||||||
@@ -21,6 +19,11 @@ bool wifi_load_config(WifiMqttConfig &config) {
|
|||||||
config.mqtt_pass = prefs.getString("mqpass", "");
|
config.mqtt_pass = prefs.getString("mqpass", "");
|
||||||
config.ntp_server_1 = prefs.getString("ntp1", "pool.ntp.org");
|
config.ntp_server_1 = prefs.getString("ntp1", "pool.ntp.org");
|
||||||
config.ntp_server_2 = prefs.getString("ntp2", "time.nist.gov");
|
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;
|
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("mqpass", config.mqtt_pass);
|
||||||
prefs.putString("ntp1", config.ntp_server_1);
|
prefs.putString("ntp1", config.ntp_server_1);
|
||||||
prefs.putString("ntp2", config.ntp_server_2);
|
prefs.putString("ntp2", config.ntp_server_2);
|
||||||
|
prefs.putString("webuser", config.web_user);
|
||||||
|
prefs.putString("webpass", config.web_pass);
|
||||||
return true;
|
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