Compare commits
7 Commits
multi-mete
...
lora-refac
| Author | SHA1 | Date | |
|---|---|---|---|
| 195cce2bcf | |||
| 382e506f14 | |||
| 64afa8621e | |||
| 7540af7d71 | |||
| 9d5f5ed513 | |||
| 3a1de36b75 | |||
|
|
595a31f278 |
90
README.md
90
README.md
@@ -1,19 +1,21 @@
|
|||||||
# DD3-LoRa-Bridge-MultiSender
|
# DD3-LoRa-Bridge-MultiSender
|
||||||
|
|
||||||
Firmware for LilyGO T3 v1.6.1 (`ESP32 + SX1276 + SSD1306`) that runs as either:
|
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.
|
- `Sender` (PIN `GPIO14` HIGH): reads one IEC 62056-21 meter, batches samples, sends over LoRa.
|
||||||
- `Receiver` (PIN `GPIO14` LOW): receives/ACKs batches, publishes MQTT, serves web UI, logs to SD.
|
- `Receiver` (PIN `GPIO14` LOW): receives/ACKs batches, publishes MQTT, serves web UI, logs to SD.
|
||||||
|
|
||||||
## Current Architecture
|
## Current Architecture
|
||||||
|
|
||||||
- Single codebase, role selected at boot via `detect_role()` (`include/config.h`, `src/config.cpp`).
|
- Single codebase, role selected at boot via `detect_role()` (`include/config.h`, `src/config.cpp`).
|
||||||
- LoRa link uses explicit CRC16 frame protection in firmware (`src/lora_transport.cpp`), in addition to LoRa PHY CRC.
|
- LoRa link uses explicit CRC16 frame protection in firmware (`src/lora_transport.cpp`).
|
||||||
- Sender batches up to `30` samples and retransmits on missing ACK (`BATCH_MAX_RETRIES=2`, policy `Keep`).
|
- Sender batches up to `30` samples and retries on missing ACK (`BATCH_MAX_RETRIES=2`, retry policy `Keep`).
|
||||||
- Receiver handles AP fallback when STA config is missing/invalid and exposes a config/status web UI.
|
- Sender meter parsing is decoupled from LoRa ACK waits using a dedicated FreeRTOS reader task + queue (`src/main.cpp`).
|
||||||
|
- Receiver uses STA mode when config is valid, otherwise AP fallback with web config.
|
||||||
|
- No debug auto-reboot timer is active in normal firmware loops.
|
||||||
|
|
||||||
## LoRa Frame Protocol (Current)
|
## LoRa Frame Protocol (Current)
|
||||||
|
|
||||||
Frame format on-air:
|
On-air frame format:
|
||||||
|
|
||||||
`[msg_kind:1][device_short_id:2][payload...][crc16:2]`
|
`[msg_kind:1][device_short_id:2][payload...][crc16:2]`
|
||||||
|
|
||||||
@@ -23,53 +25,56 @@ Frame format on-air:
|
|||||||
|
|
||||||
### `BatchUp`
|
### `BatchUp`
|
||||||
|
|
||||||
`BatchUp` is chunked in transport (`batch_id`, `chunk_index`, `chunk_count`, `total_len`) and then decoded via `payload_codec`.
|
Transport is chunked (`batch_id`, `chunk_index`, `chunk_count`, `total_len`) and reassembled before payload decode.
|
||||||
|
|
||||||
Payload header contains:
|
Payload codec (`src/payload_codec.cpp`) currently uses:
|
||||||
- fixed magic/schema fields (`kMagic=0xDDB3`, `kSchema=2`)
|
- `kMagic=0xDDB3`
|
||||||
- `schema_id`
|
- `kSchema=2`
|
||||||
- sender/batch/time/error metadata
|
- metadata: sender, batch, timestamp, interval, battery, fault counters
|
||||||
|
- data arrays: `energy_wh[]`, `p1_w[]`, `p2_w[]`, `p3_w[]`
|
||||||
|
|
||||||
Supported payload schemas in this branch:
|
`n == 0` is valid and used for sync request packets.
|
||||||
- `schema_id=1` (`EnergyMulti`): integer kWh for up to 3 meters (`energy1_kwh`, `energy2_kwh`, `energy3_kwh`)
|
|
||||||
- `schema_id=0` (legacy): older energy/power delta encoding path remains decode-compatible
|
|
||||||
|
|
||||||
`n == 0` is used as sync request (no meter samples).
|
|
||||||
|
|
||||||
### `AckDown` (7 bytes)
|
### `AckDown` (7 bytes)
|
||||||
|
|
||||||
`[flags:1][batch_id_be:2][epoch_utc_be:4]`
|
`[flags:1][batch_id_be:2][epoch_utc_be:4]`
|
||||||
|
|
||||||
- `flags bit0`: `time_valid`
|
- `flags bit0`: `time_valid`
|
||||||
- Receiver sends ACK repeatedly (`ACK_REPEAT_COUNT=3`, `ACK_REPEAT_DELAY_MS=200`).
|
- Receiver repeats ACK (`ACK_REPEAT_COUNT=3`, `ACK_REPEAT_DELAY_MS=200`).
|
||||||
- Sender accepts time only if `time_valid=1` and `epoch >= MIN_ACCEPTED_EPOCH_UTC` (`2026-02-01 00:00:00 UTC`).
|
- Sender accepts time only if `time_valid=1` and `epoch >= MIN_ACCEPTED_EPOCH_UTC` (`2026-02-01 00:00:00 UTC`).
|
||||||
|
|
||||||
## Time Bootstrap Guardrail
|
## Time Bootstrap Guardrail
|
||||||
|
|
||||||
On sender boot:
|
On sender boot:
|
||||||
- `g_time_acquired=false`
|
- `g_time_acquired=false`
|
||||||
- only sync requests every `SYNC_REQUEST_INTERVAL_MS` (15s)
|
- no normal sampling/transmit yet
|
||||||
- no normal sampling/transmit until valid ACK time received
|
- sync request every `SYNC_REQUEST_INTERVAL_MS` (15s)
|
||||||
|
|
||||||
This prevents publishing/storing pre-threshold timestamps.
|
Only after valid ACK time is received:
|
||||||
|
- system time is set
|
||||||
|
- normal 1 Hz sampling and periodic LoRa batch transmit start
|
||||||
|
|
||||||
## Multi-Meter Sender Behavior
|
This blocks pre-threshold timestamps from MQTT/SD paths.
|
||||||
|
|
||||||
Implemented in `src/meter_driver.cpp` + sender path in `src/main.cpp`:
|
Timezone handling:
|
||||||
|
- Local time rendering uses `TIMEZONE_TZ` from `include/config.h`.
|
||||||
|
- Default value is `CET-1CEST,M3.5.0/2,M10.5.0/3` and can be changed at compile time.
|
||||||
|
|
||||||
- Meter protocol: IEC 62056-21 ASCII, Mode D style framing (`/ ... !`)
|
## Sender Meter Path
|
||||||
- UART settings: `9600 7E1`
|
|
||||||
- Parsed OBIS: `1-0:1.8.0`
|
|
||||||
- Conversion: floor to integer kWh (`floorf`)
|
|
||||||
|
|
||||||
Meter count is build-dependent (`include/config.h`):
|
Implemented in `src/meter_driver.cpp` + sender loop in `src/main.cpp`:
|
||||||
- Debug builds (`SERIAL_DEBUG_MODE=1`): `METER_COUNT=2`
|
|
||||||
- Prod builds (`SERIAL_DEBUG_MODE=0`): `METER_COUNT=3`
|
|
||||||
|
|
||||||
Default RX pins:
|
- UART: `Serial2`, RX pin `GPIO34` (`PIN_METER_RX`), `9600 7E1`
|
||||||
- Meter1: `GPIO34` (`Serial2`)
|
- ESP32 RX buffer is enlarged to `8192` bytes to survive long LoRa blocking sections.
|
||||||
- Meter2: `GPIO25` (`Serial1`)
|
- Frame detection: starts at `'/'`, ends at `'!'`, timeout protection included (`METER_FRAME_TIMEOUT_MS=20000`).
|
||||||
- Meter3: `GPIO3` (`Serial`, prod only because debug serial is disabled)
|
- Parsing runs in a dedicated sender task and is handed to the main sender loop via queue.
|
||||||
|
- Parsed OBIS values:
|
||||||
|
- `1-0:1.8.0` (total energy)
|
||||||
|
- `1-0:16.7.0` (total power)
|
||||||
|
- `1-0:36.7.0`, `56.7.0`, `76.7.0` (phase powers)
|
||||||
|
- `1-0:1.8.0*Wh` is automatically scaled to kWh
|
||||||
|
|
||||||
|
Sender samples every second and transmits batches every 30 seconds.
|
||||||
|
|
||||||
## Receiver Behavior
|
## Receiver Behavior
|
||||||
|
|
||||||
@@ -78,7 +83,7 @@ For valid `BatchUp` decode:
|
|||||||
2. Send `AckDown` immediately.
|
2. Send `AckDown` immediately.
|
||||||
3. Drop duplicate batches per sender (`batch_id` tracking).
|
3. Drop duplicate batches per sender (`batch_id` tracking).
|
||||||
4. If `n==0`: treat as sync request only.
|
4. If `n==0`: treat as sync request only.
|
||||||
5. Else convert to `MeterData`, log to SD, update web UI, publish MQTT.
|
5. Else convert samples, log to SD, update web UI, publish MQTT.
|
||||||
|
|
||||||
## MQTT Topics and Payloads
|
## MQTT Topics and Payloads
|
||||||
|
|
||||||
@@ -88,21 +93,22 @@ State topic:
|
|||||||
Fault topic (retained):
|
Fault topic (retained):
|
||||||
- `smartmeter/<device_id>/faults`
|
- `smartmeter/<device_id>/faults`
|
||||||
|
|
||||||
For `EnergyMulti` samples, state JSON includes:
|
State JSON fields (`src/json_codec.cpp`):
|
||||||
- `id`, `ts`
|
- `id`, `ts`, `e_kwh`
|
||||||
- `energy1_kwh`, `energy2_kwh`, optional `energy3_kwh`
|
- `p_w`, `p1_w`, `p2_w`, `p3_w`
|
||||||
- `bat_v`, `bat_pct`
|
- `bat_v`, `bat_pct`
|
||||||
- optional link fields: `rssi`, `snr`
|
- optional link fields: `rssi`, `snr`
|
||||||
- fault/reject fields: `err_last`, `rx_reject`, `rx_reject_text` (+ non-zero counters)
|
- fault/reject fields: `err_last`, `rx_reject`, `rx_reject_text` (+ non-zero counters)
|
||||||
|
|
||||||
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`.
|
Home Assistant discovery is enabled (`ENABLE_HA_DISCOVERY=true`) and publishes config topics under:
|
||||||
|
- `homeassistant/sensor/<device_id>/<key>/config`
|
||||||
|
|
||||||
## Web UI, Wi-Fi, Storage
|
## Web UI, Wi-Fi, Storage
|
||||||
|
|
||||||
- STA config is stored in Preferences (`wifi_manager`).
|
- Wi-Fi/MQTT/NTP/web-auth config persists in Preferences (`wifi_manager`).
|
||||||
- If STA/MQTT config is unavailable, receiver starts AP mode with SSID prefix `DD3-Bridge-`.
|
- AP fallback SSID prefix: `DD3-Bridge-`.
|
||||||
- Web auth defaults are `admin/admin` (`WEB_AUTH_DEFAULT_USER/PASS`).
|
- Default web credentials: `admin/admin`.
|
||||||
- SD logging is enabled (`ENABLE_SD_LOGGING=true`).
|
- SD logging enabled (`ENABLE_SD_LOGGING=true`).
|
||||||
|
|
||||||
## Build Environments
|
## Build Environments
|
||||||
|
|
||||||
@@ -126,4 +132,4 @@ Example:
|
|||||||
## Test Mode
|
## Test Mode
|
||||||
|
|
||||||
`ENABLE_TEST_MODE` replaces normal sender/receiver loops with dedicated test loops (`src/test_mode.cpp`).
|
`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`.
|
It sends/receives JSON test frames and publishes to `smartmeter/<device_id>/test`.
|
||||||
|
|||||||
@@ -32,9 +32,7 @@ constexpr uint8_t PIN_BAT_ADC = 35;
|
|||||||
constexpr uint8_t PIN_ROLE = 14;
|
constexpr uint8_t PIN_ROLE = 14;
|
||||||
constexpr uint8_t PIN_OLED_CTRL = 13;
|
constexpr uint8_t PIN_OLED_CTRL = 13;
|
||||||
|
|
||||||
constexpr uint8_t PIN_METER1_RX = 34; // UART2 RX
|
constexpr uint8_t PIN_METER_RX = 34;
|
||||||
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
|
||||||
@@ -70,9 +68,6 @@ constexpr bool ENABLE_HA_DISCOVERY = true;
|
|||||||
#define SERIAL_DEBUG_MODE_FLAG 0
|
#define SERIAL_DEBUG_MODE_FLAG 0
|
||||||
#endif
|
#endif
|
||||||
constexpr bool SERIAL_DEBUG_MODE = SERIAL_DEBUG_MODE_FLAG != 0;
|
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 SERIAL_DEBUG_DUMP_JSON = false;
|
||||||
constexpr bool LORA_SEND_BYPASS = false;
|
constexpr bool LORA_SEND_BYPASS = false;
|
||||||
constexpr bool ENABLE_SD_LOGGING = true;
|
constexpr bool ENABLE_SD_LOGGING = true;
|
||||||
@@ -84,6 +79,7 @@ constexpr uint16_t SD_HISTORY_MAX_DAYS = 30;
|
|||||||
constexpr uint16_t SD_HISTORY_MIN_RES_MIN = 1;
|
constexpr uint16_t SD_HISTORY_MIN_RES_MIN = 1;
|
||||||
constexpr uint16_t SD_HISTORY_MAX_BINS = 4000;
|
constexpr uint16_t SD_HISTORY_MAX_BINS = 4000;
|
||||||
constexpr uint16_t SD_HISTORY_TIME_BUDGET_MS = 10;
|
constexpr uint16_t SD_HISTORY_TIME_BUDGET_MS = 10;
|
||||||
|
constexpr const char *TIMEZONE_TZ = "CET-1CEST,M3.5.0/2,M10.5.0/3";
|
||||||
constexpr const char *AP_SSID_PREFIX = "DD3-Bridge-";
|
constexpr const char *AP_SSID_PREFIX = "DD3-Bridge-";
|
||||||
constexpr const char *AP_PASSWORD = "changeme123";
|
constexpr const char *AP_PASSWORD = "changeme123";
|
||||||
constexpr bool WEB_AUTH_REQUIRE_STA = true;
|
constexpr bool WEB_AUTH_REQUIRE_STA = true;
|
||||||
|
|||||||
@@ -28,9 +28,6 @@ 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;
|
||||||
@@ -50,7 +47,6 @@ struct MeterData {
|
|||||||
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;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
#include "data_model.h"
|
||||||
|
|
||||||
void meter_init();
|
void meter_init();
|
||||||
void meter_poll();
|
bool meter_read(MeterData &data);
|
||||||
uint8_t meter_count();
|
bool meter_poll_frame(const char *&frame, size_t &len);
|
||||||
bool meter_get_last_energy_kwh(uint8_t meter_idx, uint32_t &out_energy_kwh);
|
bool meter_parse_frame(const char *frame, size_t len, MeterData &data);
|
||||||
|
|||||||
@@ -350,18 +350,9 @@ static void render_receiver_sender(uint8_t index) {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
display.setCursor(0, 12);
|
display.setCursor(0, 12);
|
||||||
if (status.last_data.energy_multi) {
|
display.printf("E %.2f kWh", status.last_data.energy_total_kwh);
|
||||||
display.printf("E1 %lu E2 %lu", static_cast<unsigned long>(status.last_data.energy_kwh_int[0]),
|
|
||||||
static_cast<unsigned long>(status.last_data.energy_kwh_int[1]));
|
|
||||||
} else {
|
|
||||||
display.printf("E %.2f kWh", status.last_data.energy_total_kwh);
|
|
||||||
}
|
|
||||||
display.setCursor(0, 22);
|
display.setCursor(0, 22);
|
||||||
if (status.last_data.energy_multi && status.last_data.energy_meter_count >= 3) {
|
display.printf("L1 %dW", static_cast<int>(round_power_w(status.last_data.phase_power_w[0])));
|
||||||
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.setCursor(0, 32);
|
||||||
display.printf("L2 %dW", static_cast<int>(round_power_w(status.last_data.phase_power_w[1])));
|
display.printf("L2 %dW", static_cast<int>(round_power_w(status.last_data.phase_power_w[1])));
|
||||||
display.setCursor(0, 42);
|
display.setCursor(0, 42);
|
||||||
|
|||||||
@@ -3,6 +3,8 @@
|
|||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
|
static constexpr size_t STATE_JSON_DOC_CAPACITY = 512;
|
||||||
|
|
||||||
static float round2(float value) {
|
static float round2(float value) {
|
||||||
if (isnan(value)) {
|
if (isnan(value)) {
|
||||||
return value;
|
return value;
|
||||||
@@ -58,24 +60,16 @@ static void set_int_or_null(JsonDocument &doc, const char *key, float value) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool meterDataToJson(const MeterData &data, String &out_json) {
|
bool meterDataToJson(const MeterData &data, String &out_json) {
|
||||||
StaticJsonDocument<320> doc;
|
StaticJsonDocument<STATE_JSON_DOC_CAPACITY> 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];
|
||||||
if (data.energy_multi) {
|
format_float_2(buf, sizeof(buf), data.energy_total_kwh);
|
||||||
doc["energy1_kwh"] = data.energy_kwh_int[0];
|
doc["e_kwh"] = serialized(buf);
|
||||||
doc["energy2_kwh"] = data.energy_kwh_int[1];
|
set_int_or_null(doc, "p_w", data.total_power_w);
|
||||||
if (data.energy_meter_count >= 3) {
|
set_int_or_null(doc, "p1_w", data.phase_power_w[0]);
|
||||||
doc["energy3_kwh"] = data.energy_kwh_int[2];
|
set_int_or_null(doc, "p2_w", data.phase_power_w[1]);
|
||||||
}
|
set_int_or_null(doc, "p3_w", data.phase_power_w[2]);
|
||||||
} else {
|
|
||||||
format_float_2(buf, sizeof(buf), data.energy_total_kwh);
|
|
||||||
doc["e_kwh"] = serialized(buf);
|
|
||||||
set_int_or_null(doc, "p_w", data.total_power_w);
|
|
||||||
set_int_or_null(doc, "p1_w", data.phase_power_w[0]);
|
|
||||||
set_int_or_null(doc, "p2_w", data.phase_power_w[1]);
|
|
||||||
set_int_or_null(doc, "p3_w", data.phase_power_w[2]);
|
|
||||||
}
|
|
||||||
format_float_2(buf, sizeof(buf), data.battery_voltage_v);
|
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;
|
||||||
@@ -98,5 +92,5 @@ bool meterDataToJson(const MeterData &data, String &out_json) {
|
|||||||
|
|
||||||
out_json = "";
|
out_json = "";
|
||||||
size_t len = serializeJson(doc, out_json);
|
size_t len = serializeJson(doc, out_json);
|
||||||
return len > 0 && len < 320;
|
return len > 0;
|
||||||
}
|
}
|
||||||
|
|||||||
447
src/main.cpp
447
src/main.cpp
@@ -17,6 +17,9 @@
|
|||||||
#ifdef ARDUINO_ARCH_ESP32
|
#ifdef ARDUINO_ARCH_ESP32
|
||||||
#include <esp_task_wdt.h>
|
#include <esp_task_wdt.h>
|
||||||
#include <esp_system.h>
|
#include <esp_system.h>
|
||||||
|
#include <freertos/FreeRTOS.h>
|
||||||
|
#include <freertos/queue.h>
|
||||||
|
#include <freertos/task.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static DeviceRole g_role = DeviceRole::Sender;
|
static DeviceRole g_role = DeviceRole::Sender;
|
||||||
@@ -26,7 +29,6 @@ static char g_device_id[16] = "";
|
|||||||
static SenderStatus g_sender_statuses[NUM_SENDERS];
|
static SenderStatus g_sender_statuses[NUM_SENDERS];
|
||||||
static bool g_ap_mode = false;
|
static bool g_ap_mode = false;
|
||||||
static WifiMqttConfig g_cfg;
|
static WifiMqttConfig g_cfg;
|
||||||
static uint32_t g_boot_ms = 0;
|
|
||||||
static FaultCounters g_sender_faults = {};
|
static FaultCounters g_sender_faults = {};
|
||||||
static FaultCounters g_receiver_faults = {};
|
static FaultCounters g_receiver_faults = {};
|
||||||
static FaultCounters g_receiver_faults_published = {};
|
static FaultCounters g_receiver_faults_published = {};
|
||||||
@@ -55,10 +57,11 @@ struct BatchBuffer {
|
|||||||
uint16_t batch_id;
|
uint16_t batch_id;
|
||||||
bool batch_id_valid;
|
bool batch_id_valid;
|
||||||
uint8_t count;
|
uint8_t count;
|
||||||
struct EnergySample {
|
uint16_t attempt_count;
|
||||||
uint32_t ts_utc;
|
uint16_t valid_count;
|
||||||
uint32_t energy_kwh[3];
|
uint16_t invalid_count;
|
||||||
} samples[METER_BATCH_MAX_SAMPLES];
|
FaultType last_error;
|
||||||
|
MeterData samples[METER_BATCH_MAX_SAMPLES];
|
||||||
};
|
};
|
||||||
|
|
||||||
static BatchBuffer g_batch_queue[BATCH_QUEUE_DEPTH];
|
static BatchBuffer g_batch_queue[BATCH_QUEUE_DEPTH];
|
||||||
@@ -66,7 +69,7 @@ static uint8_t g_batch_head = 0;
|
|||||||
static uint8_t g_batch_tail = 0;
|
static uint8_t g_batch_tail = 0;
|
||||||
static uint8_t g_batch_count = 0;
|
static uint8_t g_batch_count = 0;
|
||||||
|
|
||||||
static BatchBuffer::EnergySample g_build_samples[METER_BATCH_MAX_SAMPLES];
|
static MeterData g_build_samples[METER_BATCH_MAX_SAMPLES];
|
||||||
static uint8_t g_build_count = 0;
|
static uint8_t g_build_count = 0;
|
||||||
|
|
||||||
static uint32_t g_last_sample_ms = 0;
|
static uint32_t g_last_sample_ms = 0;
|
||||||
@@ -82,7 +85,7 @@ static uint16_t g_last_acked_batch_id = 0;
|
|||||||
static uint8_t g_batch_retry_count = 0;
|
static uint8_t g_batch_retry_count = 0;
|
||||||
static bool g_batch_ack_pending = false;
|
static bool g_batch_ack_pending = false;
|
||||||
static uint32_t g_batch_ack_timeout_ms = BATCH_ACK_TIMEOUT_MS;
|
static uint32_t g_batch_ack_timeout_ms = BATCH_ACK_TIMEOUT_MS;
|
||||||
static BatchBuffer::EnergySample g_inflight_samples[METER_BATCH_MAX_SAMPLES];
|
static MeterData g_inflight_samples[METER_BATCH_MAX_SAMPLES];
|
||||||
static uint8_t g_inflight_count = 0;
|
static uint8_t g_inflight_count = 0;
|
||||||
static uint16_t g_inflight_batch_id = 0;
|
static uint16_t g_inflight_batch_id = 0;
|
||||||
static bool g_inflight_active = false;
|
static bool g_inflight_active = false;
|
||||||
@@ -93,12 +96,39 @@ static uint32_t g_sender_sleep_ms = 0;
|
|||||||
static uint32_t g_sender_power_log_ms = 0;
|
static uint32_t g_sender_power_log_ms = 0;
|
||||||
static RxRejectReason g_sender_rx_reject_reason = RxRejectReason::None;
|
static RxRejectReason g_sender_rx_reject_reason = RxRejectReason::None;
|
||||||
static uint32_t g_sender_rx_reject_log_ms = 0;
|
static uint32_t g_sender_rx_reject_log_ms = 0;
|
||||||
static uint32_t g_last_energy_kwh[3] = {};
|
static MeterData g_last_meter_data = {};
|
||||||
static bool g_last_energy_valid[3] = {};
|
static bool g_last_meter_valid = false;
|
||||||
|
static uint32_t g_last_meter_rx_ms = 0;
|
||||||
|
static uint32_t g_meter_stale_seconds = 0;
|
||||||
static bool g_time_acquired = false;
|
static bool g_time_acquired = false;
|
||||||
static uint32_t g_last_sync_request_ms = 0;
|
static uint32_t g_last_sync_request_ms = 0;
|
||||||
|
static uint32_t g_build_attempts = 0;
|
||||||
|
static uint32_t g_build_valid = 0;
|
||||||
|
static uint32_t g_build_invalid = 0;
|
||||||
|
static constexpr uint32_t METER_SAMPLE_MAX_AGE_MS = 15000;
|
||||||
|
struct MeterSampleEvent {
|
||||||
|
MeterData data;
|
||||||
|
uint32_t rx_ms;
|
||||||
|
};
|
||||||
|
#ifdef ARDUINO_ARCH_ESP32
|
||||||
|
static QueueHandle_t g_meter_sample_queue = nullptr;
|
||||||
|
static TaskHandle_t g_meter_reader_task = nullptr;
|
||||||
|
static bool g_meter_reader_task_running = false;
|
||||||
|
static constexpr UBaseType_t METER_SAMPLE_QUEUE_LEN = 8;
|
||||||
|
static constexpr uint32_t METER_READER_TASK_STACK_WORDS = 4096;
|
||||||
|
static constexpr UBaseType_t METER_READER_TASK_PRIORITY = 2;
|
||||||
|
static constexpr BaseType_t METER_READER_TASK_CORE = 0;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
enum class TxBuildError : uint8_t {
|
||||||
|
None = 0,
|
||||||
|
Encode = 1
|
||||||
|
};
|
||||||
|
|
||||||
|
static TxBuildError g_last_tx_build_error = TxBuildError::None;
|
||||||
|
|
||||||
static void watchdog_kick();
|
static void watchdog_kick();
|
||||||
|
static void finish_inflight_batch();
|
||||||
|
|
||||||
static void serial_debug_printf(const char *fmt, ...) {
|
static void serial_debug_printf(const char *fmt, ...) {
|
||||||
if (!SERIAL_DEBUG_MODE) {
|
if (!SERIAL_DEBUG_MODE) {
|
||||||
@@ -112,6 +142,117 @@ static void serial_debug_printf(const char *fmt, ...) {
|
|||||||
Serial.println(buf);
|
Serial.println(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void set_last_meter_sample(const MeterData &parsed, uint32_t rx_ms) {
|
||||||
|
g_last_meter_data = parsed;
|
||||||
|
g_last_meter_valid = true;
|
||||||
|
g_last_meter_rx_ms = rx_ms;
|
||||||
|
g_meter_stale_seconds = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool parse_meter_frame_sample(const char *frame, size_t frame_len, MeterData &parsed) {
|
||||||
|
parsed = {};
|
||||||
|
parsed.energy_total_kwh = NAN;
|
||||||
|
parsed.total_power_w = NAN;
|
||||||
|
parsed.phase_power_w[0] = NAN;
|
||||||
|
parsed.phase_power_w[1] = NAN;
|
||||||
|
parsed.phase_power_w[2] = NAN;
|
||||||
|
parsed.valid = false;
|
||||||
|
return meter_parse_frame(frame, frame_len, parsed);
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef ARDUINO_ARCH_ESP32
|
||||||
|
static void meter_queue_push_latest(const MeterSampleEvent &event) {
|
||||||
|
if (!g_meter_sample_queue) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (xQueueSend(g_meter_sample_queue, &event, 0) == pdTRUE) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
MeterSampleEvent dropped = {};
|
||||||
|
xQueueReceive(g_meter_sample_queue, &dropped, 0);
|
||||||
|
if (xQueueSend(g_meter_sample_queue, &event, 0) != pdTRUE && SERIAL_DEBUG_MODE) {
|
||||||
|
serial_debug_printf("meter: queue push failed");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void meter_reader_task_entry(void *arg) {
|
||||||
|
(void)arg;
|
||||||
|
for (;;) {
|
||||||
|
const char *frame = nullptr;
|
||||||
|
size_t frame_len = 0;
|
||||||
|
if (!meter_poll_frame(frame, frame_len)) {
|
||||||
|
vTaskDelay(pdMS_TO_TICKS(5));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
MeterData parsed = {};
|
||||||
|
if (parse_meter_frame_sample(frame, frame_len, parsed)) {
|
||||||
|
MeterSampleEvent event = {};
|
||||||
|
event.data = parsed;
|
||||||
|
event.rx_ms = millis();
|
||||||
|
meter_queue_push_latest(event);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool meter_reader_start() {
|
||||||
|
if (g_meter_reader_task_running) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (!g_meter_sample_queue) {
|
||||||
|
g_meter_sample_queue = xQueueCreate(METER_SAMPLE_QUEUE_LEN, sizeof(MeterSampleEvent));
|
||||||
|
if (!g_meter_sample_queue) {
|
||||||
|
if (SERIAL_DEBUG_MODE) {
|
||||||
|
serial_debug_printf("meter: queue alloc failed");
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
BaseType_t rc = xTaskCreatePinnedToCore(
|
||||||
|
meter_reader_task_entry,
|
||||||
|
"meter_reader",
|
||||||
|
METER_READER_TASK_STACK_WORDS,
|
||||||
|
nullptr,
|
||||||
|
METER_READER_TASK_PRIORITY,
|
||||||
|
&g_meter_reader_task,
|
||||||
|
METER_READER_TASK_CORE);
|
||||||
|
if (rc != pdPASS) {
|
||||||
|
if (SERIAL_DEBUG_MODE) {
|
||||||
|
serial_debug_printf("meter: task start failed rc=%ld", static_cast<long>(rc));
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
g_meter_reader_task_running = true;
|
||||||
|
serial_debug_printf("meter: reader task core=%ld queue=%u",
|
||||||
|
static_cast<long>(METER_READER_TASK_CORE),
|
||||||
|
static_cast<unsigned>(METER_SAMPLE_QUEUE_LEN));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static void meter_reader_pump(uint32_t now_ms) {
|
||||||
|
#ifdef ARDUINO_ARCH_ESP32
|
||||||
|
if (g_meter_reader_task_running && g_meter_sample_queue) {
|
||||||
|
MeterSampleEvent event = {};
|
||||||
|
while (xQueueReceive(g_meter_sample_queue, &event, 0) == pdTRUE) {
|
||||||
|
set_last_meter_sample(event.data, event.rx_ms);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
const char *frame = nullptr;
|
||||||
|
size_t frame_len = 0;
|
||||||
|
if (!meter_poll_frame(frame, frame_len)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
MeterData parsed = {};
|
||||||
|
if (parse_meter_frame_sample(frame, frame_len, parsed)) {
|
||||||
|
set_last_meter_sample(parsed, now_ms);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static uint16_t g_last_batch_id_rx[NUM_SENDERS] = {};
|
static uint16_t g_last_batch_id_rx[NUM_SENDERS] = {};
|
||||||
|
|
||||||
struct BatchRxState {
|
struct BatchRxState {
|
||||||
@@ -133,7 +274,6 @@ static void init_sender_statuses() {
|
|||||||
g_sender_statuses[i] = {};
|
g_sender_statuses[i] = {};
|
||||||
g_sender_statuses[i].has_data = false;
|
g_sender_statuses[i].has_data = false;
|
||||||
g_sender_statuses[i].last_update_ts_utc = 0;
|
g_sender_statuses[i].last_update_ts_utc = 0;
|
||||||
g_sender_statuses[i].last_acked_batch_id = 0;
|
|
||||||
g_sender_statuses[i].last_data.short_id = EXPECTED_SENDER_IDS[i];
|
g_sender_statuses[i].last_data.short_id = EXPECTED_SENDER_IDS[i];
|
||||||
snprintf(g_sender_statuses[i].last_data.device_id, sizeof(g_sender_statuses[i].last_data.device_id), "dd3-%04X", EXPECTED_SENDER_IDS[i]);
|
snprintf(g_sender_statuses[i].last_data.device_id, sizeof(g_sender_statuses[i].last_data.device_id), "dd3-%04X", EXPECTED_SENDER_IDS[i]);
|
||||||
g_sender_faults_remote[i] = {};
|
g_sender_faults_remote[i] = {};
|
||||||
@@ -196,7 +336,7 @@ static BatchBuffer *batch_queue_peek() {
|
|||||||
return &g_batch_queue[g_batch_tail];
|
return &g_batch_queue[g_batch_tail];
|
||||||
}
|
}
|
||||||
|
|
||||||
static void batch_queue_enqueue(const BatchBuffer::EnergySample *samples, uint8_t count) {
|
static void batch_queue_enqueue(const MeterData *samples, uint8_t count) {
|
||||||
if (!samples || count == 0) {
|
if (!samples || count == 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -209,6 +349,10 @@ static void batch_queue_enqueue(const BatchBuffer::EnergySample *samples, uint8_
|
|||||||
slot.batch_id = 0;
|
slot.batch_id = 0;
|
||||||
slot.batch_id_valid = false;
|
slot.batch_id_valid = false;
|
||||||
slot.count = count;
|
slot.count = count;
|
||||||
|
slot.attempt_count = static_cast<uint16_t>(g_build_attempts);
|
||||||
|
slot.valid_count = static_cast<uint16_t>(g_build_valid);
|
||||||
|
slot.invalid_count = static_cast<uint16_t>(g_build_invalid);
|
||||||
|
slot.last_error = g_sender_last_error;
|
||||||
for (uint8_t i = 0; i < count; ++i) {
|
for (uint8_t i = 0; i < count; ++i) {
|
||||||
slot.samples[i] = samples[i];
|
slot.samples[i] = samples[i];
|
||||||
}
|
}
|
||||||
@@ -216,6 +360,28 @@ static void batch_queue_enqueue(const BatchBuffer::EnergySample *samples, uint8_
|
|||||||
g_batch_count++;
|
g_batch_count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void reset_build_counters() {
|
||||||
|
g_build_attempts = 0;
|
||||||
|
g_build_valid = 0;
|
||||||
|
g_build_invalid = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool append_meter_sample(const MeterData &data, bool meter_ok) {
|
||||||
|
if (!meter_ok) {
|
||||||
|
g_build_invalid++;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
g_last_sample_ts_utc = data.ts_utc;
|
||||||
|
g_build_samples[g_build_count++] = data;
|
||||||
|
g_build_valid++;
|
||||||
|
if (g_build_count >= METER_BATCH_MAX_SAMPLES) {
|
||||||
|
batch_queue_enqueue(g_build_samples, g_build_count);
|
||||||
|
g_build_count = 0;
|
||||||
|
reset_build_counters();
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
static uint32_t last_sample_ts() {
|
static uint32_t last_sample_ts() {
|
||||||
if (g_last_sample_ts_utc == 0) {
|
if (g_last_sample_ts_utc == 0) {
|
||||||
uint32_t now_utc = time_get_utc();
|
uint32_t now_utc = time_get_utc();
|
||||||
@@ -331,6 +497,50 @@ static uint16_t short_id_from_sender_id(uint16_t sender_id) {
|
|||||||
return EXPECTED_SENDER_IDS[sender_id - 1];
|
return EXPECTED_SENDER_IDS[sender_id - 1];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static uint32_t kwh_to_wh_from_float(float value) {
|
||||||
|
if (isnan(value)) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
double wh = static_cast<double>(value) * 1000.0;
|
||||||
|
if (wh < 0.0) {
|
||||||
|
wh = 0.0;
|
||||||
|
}
|
||||||
|
if (wh > static_cast<double>(UINT32_MAX)) {
|
||||||
|
wh = static_cast<double>(UINT32_MAX);
|
||||||
|
}
|
||||||
|
return static_cast<uint32_t>(llround(wh));
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool float_to_i16_w(float value, int16_t &out) {
|
||||||
|
if (isnan(value)) {
|
||||||
|
out = 0;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
long rounded = lroundf(value);
|
||||||
|
if (rounded < INT16_MIN || rounded > INT16_MAX) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
out = static_cast<int16_t>(rounded);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int16_t float_to_i16_w_clamped(float value, bool &clamped) {
|
||||||
|
clamped = false;
|
||||||
|
if (isnan(value)) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
long rounded = lroundf(value);
|
||||||
|
if (rounded < INT16_MIN) {
|
||||||
|
clamped = true;
|
||||||
|
return INT16_MIN;
|
||||||
|
}
|
||||||
|
if (rounded > INT16_MAX) {
|
||||||
|
clamped = true;
|
||||||
|
return INT16_MAX;
|
||||||
|
}
|
||||||
|
return static_cast<int16_t>(rounded);
|
||||||
|
}
|
||||||
|
|
||||||
static uint16_t battery_mv_from_voltage(float value) {
|
static uint16_t battery_mv_from_voltage(float value) {
|
||||||
if (isnan(value) || value <= 0.0f) {
|
if (isnan(value) || value <= 0.0f) {
|
||||||
return 0;
|
return 0;
|
||||||
@@ -457,6 +667,15 @@ static bool prepare_inflight_from_queue() {
|
|||||||
batch->batch_id = g_batch_id;
|
batch->batch_id = g_batch_id;
|
||||||
batch->batch_id_valid = true;
|
batch->batch_id_valid = true;
|
||||||
}
|
}
|
||||||
|
if (SERIAL_DEBUG_MODE) {
|
||||||
|
serial_debug_printf("batch: id=%u desired=%u attempts=%u valid=%u invalid=%u err_last=%u",
|
||||||
|
batch->batch_id,
|
||||||
|
static_cast<unsigned>(METER_BATCH_MAX_SAMPLES),
|
||||||
|
static_cast<unsigned>(batch->attempt_count),
|
||||||
|
static_cast<unsigned>(batch->valid_count),
|
||||||
|
static_cast<unsigned>(batch->invalid_count),
|
||||||
|
static_cast<unsigned>(batch->last_error));
|
||||||
|
}
|
||||||
g_inflight_count = batch->count;
|
g_inflight_count = batch->count;
|
||||||
g_inflight_batch_id = batch->batch_id;
|
g_inflight_batch_id = batch->batch_id;
|
||||||
for (uint8_t i = 0; i < g_inflight_count; ++i) {
|
for (uint8_t i = 0; i < g_inflight_count; ++i) {
|
||||||
@@ -467,11 +686,11 @@ static bool prepare_inflight_from_queue() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static bool send_inflight_batch(uint32_t ts_for_display) {
|
static bool send_inflight_batch(uint32_t ts_for_display) {
|
||||||
|
g_last_tx_build_error = TxBuildError::None;
|
||||||
if (!g_inflight_active) {
|
if (!g_inflight_active) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
BatchInput input = {};
|
BatchInput input = {};
|
||||||
input.schema_id = 1;
|
|
||||||
input.sender_id = sender_id_from_short_id(g_short_id);
|
input.sender_id = sender_id_from_short_id(g_short_id);
|
||||||
input.batch_id = g_inflight_batch_id;
|
input.batch_id = g_inflight_batch_id;
|
||||||
input.t_last = g_inflight_sync_request ? time_get_utc() :
|
input.t_last = g_inflight_sync_request ? time_get_utc() :
|
||||||
@@ -479,23 +698,51 @@ static bool send_inflight_batch(uint32_t ts_for_display) {
|
|||||||
uint32_t dt_s = METER_SAMPLE_INTERVAL_MS / 1000;
|
uint32_t dt_s = METER_SAMPLE_INTERVAL_MS / 1000;
|
||||||
input.dt_s = dt_s > 0 ? static_cast<uint8_t>(dt_s) : 1;
|
input.dt_s = dt_s > 0 ? static_cast<uint8_t>(dt_s) : 1;
|
||||||
input.n = g_inflight_sync_request ? 0 : g_inflight_count;
|
input.n = g_inflight_sync_request ? 0 : g_inflight_count;
|
||||||
input.meter_count = METER_COUNT;
|
input.battery_mV = g_inflight_sync_request ? battery_mv_from_voltage(g_last_battery_voltage_v) :
|
||||||
input.battery_mV = battery_mv_from_voltage(g_last_battery_voltage_v);
|
battery_mv_from_voltage(g_inflight_samples[g_inflight_count - 1].battery_voltage_v);
|
||||||
input.err_m = g_sender_faults.meter_read_fail > 255 ? 255 : static_cast<uint8_t>(g_sender_faults.meter_read_fail);
|
input.err_m = g_sender_faults.meter_read_fail > 255 ? 255 : static_cast<uint8_t>(g_sender_faults.meter_read_fail);
|
||||||
input.err_d = g_sender_faults.decode_fail > 255 ? 255 : static_cast<uint8_t>(g_sender_faults.decode_fail);
|
input.err_d = g_sender_faults.decode_fail > 255 ? 255 : static_cast<uint8_t>(g_sender_faults.decode_fail);
|
||||||
input.err_tx = g_sender_faults.lora_tx_fail > 255 ? 255 : static_cast<uint8_t>(g_sender_faults.lora_tx_fail);
|
input.err_tx = g_sender_faults.lora_tx_fail > 255 ? 255 : static_cast<uint8_t>(g_sender_faults.lora_tx_fail);
|
||||||
input.err_last = static_cast<uint8_t>(g_sender_last_error);
|
input.err_last = static_cast<uint8_t>(g_sender_last_error);
|
||||||
input.err_rx_reject = static_cast<uint8_t>(g_sender_rx_reject_reason);
|
input.err_rx_reject = static_cast<uint8_t>(g_sender_rx_reject_reason);
|
||||||
|
uint8_t energy_regressions = 0;
|
||||||
|
uint8_t phase_clamps = 0;
|
||||||
for (uint8_t i = 0; i < input.n; ++i) {
|
for (uint8_t i = 0; i < input.n; ++i) {
|
||||||
input.energy1_kwh[i] = g_inflight_samples[i].energy_kwh[0];
|
input.energy_wh[i] = kwh_to_wh_from_float(g_inflight_samples[i].energy_total_kwh);
|
||||||
input.energy2_kwh[i] = g_inflight_samples[i].energy_kwh[1];
|
if (i > 0 && input.energy_wh[i] < input.energy_wh[i - 1]) {
|
||||||
input.energy3_kwh[i] = g_inflight_samples[i].energy_kwh[2];
|
input.energy_wh[i] = input.energy_wh[i - 1];
|
||||||
|
if (energy_regressions < 255) {
|
||||||
|
energy_regressions++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
bool c1 = false;
|
||||||
|
bool c2 = false;
|
||||||
|
bool c3 = false;
|
||||||
|
input.p1_w[i] = float_to_i16_w_clamped(g_inflight_samples[i].phase_power_w[0], c1);
|
||||||
|
input.p2_w[i] = float_to_i16_w_clamped(g_inflight_samples[i].phase_power_w[1], c2);
|
||||||
|
input.p3_w[i] = float_to_i16_w_clamped(g_inflight_samples[i].phase_power_w[2], c3);
|
||||||
|
if (c1 && phase_clamps < 255) {
|
||||||
|
phase_clamps++;
|
||||||
|
}
|
||||||
|
if (c2 && phase_clamps < 255) {
|
||||||
|
phase_clamps++;
|
||||||
|
}
|
||||||
|
if (c3 && phase_clamps < 255) {
|
||||||
|
phase_clamps++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (SERIAL_DEBUG_MODE && (energy_regressions > 0 || phase_clamps > 0)) {
|
||||||
|
serial_debug_printf("tx: sanitize batch_id=%u energy_regress=%u phase_clamps=%u",
|
||||||
|
g_inflight_batch_id,
|
||||||
|
static_cast<unsigned>(energy_regressions),
|
||||||
|
static_cast<unsigned>(phase_clamps));
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint8_t encoded[BATCH_MAX_COMPRESSED];
|
static uint8_t encoded[BATCH_MAX_COMPRESSED];
|
||||||
size_t encoded_len = 0;
|
size_t encoded_len = 0;
|
||||||
uint32_t encode_start = millis();
|
uint32_t encode_start = millis();
|
||||||
if (!encode_batch(input, encoded, sizeof(encoded), &encoded_len)) {
|
if (!encode_batch(input, encoded, sizeof(encoded), &encoded_len)) {
|
||||||
|
g_last_tx_build_error = TxBuildError::Encode;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
uint32_t encode_ms = millis() - encode_start;
|
uint32_t encode_ms = millis() - encode_start;
|
||||||
@@ -541,6 +788,13 @@ static bool send_meter_batch(uint32_t ts_for_display) {
|
|||||||
g_last_sent_batch_id = g_inflight_batch_id;
|
g_last_sent_batch_id = g_inflight_batch_id;
|
||||||
g_batch_ack_pending = true;
|
g_batch_ack_pending = true;
|
||||||
} else {
|
} else {
|
||||||
|
if (g_last_tx_build_error == TxBuildError::Encode) {
|
||||||
|
serial_debug_printf("tx: encode failed batch_id=%u dropped", g_inflight_batch_id);
|
||||||
|
note_fault(g_sender_faults, g_sender_last_error, g_sender_last_error_utc, g_sender_last_error_ms, FaultType::Decode);
|
||||||
|
display_set_last_error(g_sender_last_error, g_sender_last_error_utc, g_sender_last_error_ms);
|
||||||
|
finish_inflight_batch();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
g_inflight_active = false;
|
g_inflight_active = false;
|
||||||
g_inflight_count = 0;
|
g_inflight_count = 0;
|
||||||
g_inflight_batch_id = 0;
|
g_inflight_batch_id = 0;
|
||||||
@@ -560,6 +814,15 @@ static bool send_sync_request() {
|
|||||||
g_inflight_sync_request = true;
|
g_inflight_sync_request = true;
|
||||||
g_inflight_count = 0;
|
g_inflight_count = 0;
|
||||||
g_inflight_batch_id = g_batch_id;
|
g_inflight_batch_id = g_batch_id;
|
||||||
|
if (SERIAL_DEBUG_MODE && g_build_attempts > 0) {
|
||||||
|
serial_debug_printf("batch: id=%u desired=%u attempts=%u valid=%u invalid=%u err_last=%u sync=1",
|
||||||
|
g_inflight_batch_id,
|
||||||
|
static_cast<unsigned>(METER_BATCH_MAX_SAMPLES),
|
||||||
|
static_cast<unsigned>(g_build_attempts),
|
||||||
|
static_cast<unsigned>(g_build_valid),
|
||||||
|
static_cast<unsigned>(g_build_invalid),
|
||||||
|
static_cast<unsigned>(g_sender_last_error));
|
||||||
|
}
|
||||||
bool ok = send_inflight_batch(time_get_utc());
|
bool ok = send_inflight_batch(time_get_utc());
|
||||||
if (ok) {
|
if (ok) {
|
||||||
g_last_sent_batch_id = g_inflight_batch_id;
|
g_last_sent_batch_id = g_inflight_batch_id;
|
||||||
@@ -664,16 +927,13 @@ static bool process_batch_packet(const LoraPacket &pkt, BatchInput &out_batch, b
|
|||||||
}
|
}
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
if (SERIAL_DEBUG_MODE) {
|
Serial.begin(115200);
|
||||||
Serial.begin(115200);
|
delay(200);
|
||||||
delay(200);
|
|
||||||
}
|
|
||||||
#ifdef PAYLOAD_CODEC_TEST
|
#ifdef PAYLOAD_CODEC_TEST
|
||||||
payload_codec_self_test();
|
payload_codec_self_test();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
watchdog_init();
|
watchdog_init();
|
||||||
g_boot_ms = millis();
|
|
||||||
g_role = detect_role();
|
g_role = detect_role();
|
||||||
init_device_ids(g_short_id, g_device_id, sizeof(g_device_id));
|
init_device_ids(g_short_id, g_device_id, sizeof(g_device_id));
|
||||||
display_set_role(g_role);
|
display_set_role(g_role);
|
||||||
@@ -693,6 +953,11 @@ void setup() {
|
|||||||
power_sender_init();
|
power_sender_init();
|
||||||
power_configure_unused_pins_sender();
|
power_configure_unused_pins_sender();
|
||||||
meter_init();
|
meter_init();
|
||||||
|
#ifdef ARDUINO_ARCH_ESP32
|
||||||
|
if (!meter_reader_start()) {
|
||||||
|
serial_debug_printf("meter: using inline polling fallback");
|
||||||
|
}
|
||||||
|
#endif
|
||||||
g_last_sample_ms = millis() - METER_SAMPLE_INTERVAL_MS;
|
g_last_sample_ms = millis() - METER_SAMPLE_INTERVAL_MS;
|
||||||
g_last_send_ms = millis();
|
g_last_send_ms = millis();
|
||||||
g_last_sync_request_ms = millis() - SYNC_REQUEST_INTERVAL_MS;
|
g_last_sync_request_ms = millis() - SYNC_REQUEST_INTERVAL_MS;
|
||||||
@@ -750,66 +1015,84 @@ static void sender_loop() {
|
|||||||
g_batch_retry_count);
|
g_batch_retry_count);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (g_time_acquired) {
|
meter_reader_pump(now_ms);
|
||||||
meter_poll();
|
|
||||||
for (uint8_t i = 0; i < meter_count() && i < 3; ++i) {
|
|
||||||
uint32_t e_kwh = 0;
|
|
||||||
if (meter_get_last_energy_kwh(i, e_kwh)) {
|
|
||||||
g_last_energy_kwh[i] = e_kwh;
|
|
||||||
g_last_energy_valid[i] = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (now_ms - g_last_sample_ms >= METER_SAMPLE_INTERVAL_MS) {
|
if (g_time_acquired) {
|
||||||
g_last_sample_ms = now_ms;
|
while (now_ms - g_last_sample_ms >= METER_SAMPLE_INTERVAL_MS) {
|
||||||
bool any_meter_valid = false;
|
g_last_sample_ms += METER_SAMPLE_INTERVAL_MS;
|
||||||
for (uint8_t i = 0; i < meter_count() && i < 3; ++i) {
|
MeterData data = {};
|
||||||
if (g_last_energy_valid[i]) {
|
data.short_id = g_short_id;
|
||||||
any_meter_valid = true;
|
strncpy(data.device_id, g_device_id, sizeof(data.device_id));
|
||||||
break;
|
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;
|
||||||
|
|
||||||
|
g_build_attempts++;
|
||||||
|
uint32_t meter_age_ms = g_last_meter_valid ? (now_ms - g_last_meter_rx_ms) : UINT32_MAX;
|
||||||
|
// Reuse recent good samples to bridge short parser gaps without accepting stale data forever.
|
||||||
|
bool meter_ok = g_last_meter_valid && meter_age_ms <= METER_SAMPLE_MAX_AGE_MS;
|
||||||
|
if (meter_ok) {
|
||||||
|
data.energy_total_kwh = g_last_meter_data.energy_total_kwh;
|
||||||
|
data.total_power_w = g_last_meter_data.total_power_w;
|
||||||
|
data.phase_power_w[0] = g_last_meter_data.phase_power_w[0];
|
||||||
|
data.phase_power_w[1] = g_last_meter_data.phase_power_w[1];
|
||||||
|
data.phase_power_w[2] = g_last_meter_data.phase_power_w[2];
|
||||||
|
g_meter_stale_seconds = meter_age_ms >= 1000 ? (meter_age_ms / 1000) : 0;
|
||||||
|
} else {
|
||||||
|
g_meter_stale_seconds = g_last_meter_valid ? (meter_age_ms / 1000) : (g_meter_stale_seconds + 1);
|
||||||
}
|
}
|
||||||
if (!any_meter_valid) {
|
if (!meter_ok) {
|
||||||
note_fault(g_sender_faults, g_sender_last_error, g_sender_last_error_utc, g_sender_last_error_ms, FaultType::MeterRead);
|
note_fault(g_sender_faults, g_sender_last_error, g_sender_last_error_utc, g_sender_last_error_ms, FaultType::MeterRead);
|
||||||
display_set_last_error(g_sender_last_error, g_sender_last_error_utc, g_sender_last_error_ms);
|
display_set_last_error(g_sender_last_error, g_sender_last_error_utc, g_sender_last_error_ms);
|
||||||
} else {
|
|
||||||
BatchBuffer::EnergySample sample = {};
|
|
||||||
sample.ts_utc = time_get_utc();
|
|
||||||
sample.energy_kwh[0] = g_last_energy_valid[0] ? g_last_energy_kwh[0] : 0;
|
|
||||||
sample.energy_kwh[1] = g_last_energy_valid[1] ? g_last_energy_kwh[1] : 0;
|
|
||||||
sample.energy_kwh[2] = (meter_count() >= 3 && g_last_energy_valid[2]) ? g_last_energy_kwh[2] : 0;
|
|
||||||
g_last_sample_ts_utc = sample.ts_utc;
|
|
||||||
g_build_samples[g_build_count++] = sample;
|
|
||||||
if (g_build_count >= METER_BATCH_MAX_SAMPLES) {
|
|
||||||
batch_queue_enqueue(g_build_samples, g_build_count);
|
|
||||||
g_build_count = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
MeterData view = {};
|
|
||||||
view.short_id = g_short_id;
|
|
||||||
strncpy(view.device_id, g_device_id, sizeof(view.device_id));
|
|
||||||
view.ts_utc = sample.ts_utc;
|
|
||||||
view.energy_multi = true;
|
|
||||||
view.energy_meter_count = meter_count();
|
|
||||||
view.energy_kwh_int[0] = sample.energy_kwh[0];
|
|
||||||
view.energy_kwh_int[1] = sample.energy_kwh[1];
|
|
||||||
view.energy_kwh_int[2] = sample.energy_kwh[2];
|
|
||||||
view.energy_total_kwh = static_cast<float>(sample.energy_kwh[0]);
|
|
||||||
view.valid = true;
|
|
||||||
view.battery_voltage_v = g_last_battery_voltage_v;
|
|
||||||
view.battery_percent = g_last_battery_percent;
|
|
||||||
display_set_last_meter(view);
|
|
||||||
display_set_last_read(true, view.ts_utc);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (g_build_count == 0 && battery_sample_due(now_ms)) {
|
if (g_build_count == 0 && battery_sample_due(now_ms)) {
|
||||||
update_battery_cache();
|
update_battery_cache();
|
||||||
}
|
}
|
||||||
|
data.battery_voltage_v = g_last_battery_voltage_v;
|
||||||
|
data.battery_percent = g_last_battery_percent;
|
||||||
|
data.rx_reject_reason = static_cast<uint8_t>(g_sender_rx_reject_reason);
|
||||||
|
uint32_t sample_ts_utc = time_get_utc();
|
||||||
|
if (sample_ts_utc > 0 && now_ms > g_last_sample_ms) {
|
||||||
|
uint32_t lag_s = (now_ms - g_last_sample_ms) / 1000;
|
||||||
|
if (sample_ts_utc > lag_s) {
|
||||||
|
sample_ts_utc -= lag_s;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
data.ts_utc = sample_ts_utc;
|
||||||
|
data.valid = meter_ok;
|
||||||
|
|
||||||
|
bool appended = append_meter_sample(data, meter_ok);
|
||||||
|
if (SERIAL_DEBUG_MODE) {
|
||||||
|
serial_debug_printf("sample: i=%lu ok=%u appended=%u e_kwh=%.3f p1=%.1f p2=%.1f p3=%.1f ms=%lu",
|
||||||
|
static_cast<unsigned long>(g_build_attempts),
|
||||||
|
meter_ok ? 1U : 0U,
|
||||||
|
appended ? 1U : 0U,
|
||||||
|
static_cast<double>(data.energy_total_kwh),
|
||||||
|
static_cast<double>(data.phase_power_w[0]),
|
||||||
|
static_cast<double>(data.phase_power_w[1]),
|
||||||
|
static_cast<double>(data.phase_power_w[2]),
|
||||||
|
static_cast<unsigned long>(now_ms));
|
||||||
|
}
|
||||||
|
display_set_last_meter(data);
|
||||||
|
display_set_last_read(meter_ok, data.ts_utc);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!g_batch_ack_pending && now_ms - g_last_send_ms >= METER_SEND_INTERVAL_MS) {
|
if (!g_batch_ack_pending && now_ms - g_last_send_ms >= METER_SEND_INTERVAL_MS) {
|
||||||
g_last_send_ms = now_ms;
|
g_last_send_ms = now_ms;
|
||||||
send_meter_batch(last_sample_ts());
|
if (g_build_count > 0) {
|
||||||
|
batch_queue_enqueue(g_build_samples, g_build_count);
|
||||||
|
g_build_count = 0;
|
||||||
|
reset_build_counters();
|
||||||
|
}
|
||||||
|
if (g_batch_count > 0) {
|
||||||
|
send_meter_batch(last_sample_ts());
|
||||||
|
} else if (g_build_attempts > 0) {
|
||||||
|
if (send_sync_request()) {
|
||||||
|
reset_build_counters();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (!g_batch_ack_pending && now_ms - g_last_sync_request_ms >= SYNC_REQUEST_INTERVAL_MS) {
|
if (!g_batch_ack_pending && now_ms - g_last_sync_request_ms >= SYNC_REQUEST_INTERVAL_MS) {
|
||||||
@@ -1024,24 +1307,11 @@ static void receiver_loop() {
|
|||||||
snprintf(data.device_id, sizeof(data.device_id), "dd3-0000");
|
snprintf(data.device_id, sizeof(data.device_id), "dd3-0000");
|
||||||
}
|
}
|
||||||
data.ts_utc = t_first + static_cast<uint32_t>(s) * batch.dt_s;
|
data.ts_utc = t_first + static_cast<uint32_t>(s) * batch.dt_s;
|
||||||
if (batch.schema_id == 1) {
|
data.energy_total_kwh = static_cast<float>(batch.energy_wh[s]) / 1000.0f;
|
||||||
data.energy_multi = true;
|
data.phase_power_w[0] = static_cast<float>(batch.p1_w[s]);
|
||||||
data.energy_meter_count = batch.meter_count;
|
data.phase_power_w[1] = static_cast<float>(batch.p2_w[s]);
|
||||||
data.energy_kwh_int[0] = batch.energy1_kwh[s];
|
data.phase_power_w[2] = static_cast<float>(batch.p3_w[s]);
|
||||||
data.energy_kwh_int[1] = batch.energy2_kwh[s];
|
data.total_power_w = data.phase_power_w[0] + data.phase_power_w[1] + data.phase_power_w[2];
|
||||||
data.energy_kwh_int[2] = batch.energy3_kwh[s];
|
|
||||||
data.energy_total_kwh = static_cast<float>(batch.energy1_kwh[s]);
|
|
||||||
data.phase_power_w[0] = 0.0f;
|
|
||||||
data.phase_power_w[1] = 0.0f;
|
|
||||||
data.phase_power_w[2] = 0.0f;
|
|
||||||
data.total_power_w = 0.0f;
|
|
||||||
} else {
|
|
||||||
data.energy_total_kwh = static_cast<float>(batch.energy_wh[s]) / 1000.0f;
|
|
||||||
data.phase_power_w[0] = static_cast<float>(batch.p1_w[s]);
|
|
||||||
data.phase_power_w[1] = static_cast<float>(batch.p2_w[s]);
|
|
||||||
data.phase_power_w[2] = static_cast<float>(batch.p3_w[s]);
|
|
||||||
data.total_power_w = data.phase_power_w[0] + data.phase_power_w[1] + data.phase_power_w[2];
|
|
||||||
}
|
|
||||||
data.battery_voltage_v = bat_v;
|
data.battery_voltage_v = bat_v;
|
||||||
data.battery_percent = !isnan(bat_v) ? battery_percent_from_voltage(bat_v) : 0;
|
data.battery_percent = !isnan(bat_v) ? battery_percent_from_voltage(bat_v) : 0;
|
||||||
data.valid = true;
|
data.valid = true;
|
||||||
@@ -1057,7 +1327,6 @@ static void receiver_loop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (sender_idx >= 0) {
|
if (sender_idx >= 0) {
|
||||||
g_sender_statuses[sender_idx].last_acked_batch_id = batch_id;
|
|
||||||
web_server_set_last_batch(static_cast<uint8_t>(sender_idx), samples, count);
|
web_server_set_last_batch(static_cast<uint8_t>(sender_idx), samples, count);
|
||||||
for (size_t s = 0; s < count; ++s) {
|
for (size_t s = 0; s < count; ++s) {
|
||||||
mqtt_publish_state(samples[s]);
|
mqtt_publish_state(samples[s]);
|
||||||
|
|||||||
@@ -4,7 +4,9 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
static constexpr uint32_t METER_FRAME_TIMEOUT_MS = 1500;
|
// LoRa TX/RX windows can block the main loop for several seconds at SF12.
|
||||||
|
// Keep partial frame state long enough so valid telegrams are not dropped.
|
||||||
|
static constexpr uint32_t METER_FRAME_TIMEOUT_MS = 20000;
|
||||||
static constexpr size_t METER_FRAME_MAX = 512;
|
static constexpr size_t METER_FRAME_MAX = 512;
|
||||||
|
|
||||||
enum class MeterRxState : uint8_t {
|
enum class MeterRxState : uint8_t {
|
||||||
@@ -12,24 +14,25 @@ enum class MeterRxState : uint8_t {
|
|||||||
InFrame = 1
|
InFrame = 1
|
||||||
};
|
};
|
||||||
|
|
||||||
struct MeterPort {
|
static MeterRxState g_rx_state = MeterRxState::WaitStart;
|
||||||
HardwareSerial *serial;
|
static char g_frame_buf[METER_FRAME_MAX + 1];
|
||||||
MeterRxState state;
|
static size_t g_frame_len = 0;
|
||||||
char frame_buf[METER_FRAME_MAX + 1];
|
static uint32_t g_last_rx_ms = 0;
|
||||||
size_t frame_len;
|
static uint32_t g_bytes_rx = 0;
|
||||||
uint32_t last_rx_ms;
|
static uint32_t g_frames_ok = 0;
|
||||||
uint32_t bytes_rx;
|
static uint32_t g_frames_parse_fail = 0;
|
||||||
uint32_t frames_ok;
|
static uint32_t g_rx_overflow = 0;
|
||||||
uint32_t frames_parse_fail;
|
static uint32_t g_rx_timeout = 0;
|
||||||
uint32_t rx_overflow;
|
|
||||||
uint32_t rx_timeout;
|
|
||||||
uint32_t last_energy_kwh;
|
|
||||||
bool has_energy;
|
|
||||||
};
|
|
||||||
|
|
||||||
static MeterPort g_ports[METER_COUNT] = {};
|
|
||||||
static uint32_t g_last_log_ms = 0;
|
static uint32_t g_last_log_ms = 0;
|
||||||
|
|
||||||
|
void meter_init() {
|
||||||
|
#ifdef ARDUINO_ARCH_ESP32
|
||||||
|
// Buffer enough serial data to survive long LoRa blocking sections.
|
||||||
|
Serial2.setRxBufferSize(8192);
|
||||||
|
#endif
|
||||||
|
Serial2.begin(9600, SERIAL_7E1, PIN_METER_RX, -1);
|
||||||
|
}
|
||||||
|
|
||||||
static bool 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);
|
||||||
if (!p) {
|
if (!p) {
|
||||||
@@ -65,30 +68,34 @@ static bool parse_obis_ascii_value(const char *line, const char *obis, float &ou
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool parse_energy_kwh_floor(const char *frame, size_t len, uint32_t &out_kwh) {
|
static bool parse_obis_ascii_unit_scale(const char *line, const char *obis, float &value) {
|
||||||
char line[128];
|
const char *p = strstr(line, obis);
|
||||||
size_t line_len = 0;
|
if (!p) {
|
||||||
for (size_t i = 0; i < len; ++i) {
|
return false;
|
||||||
char c = frame[i];
|
}
|
||||||
if (c == '\r') {
|
const char *asterisk = strchr(p, '*');
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
if (c == '\n' || c == '!') {
|
unit_buf[ulen++] = *c;
|
||||||
line[line_len] = '\0';
|
}
|
||||||
float value = NAN;
|
unit_buf[ulen] = '\0';
|
||||||
if (parse_obis_ascii_value(line, "1-0:1.8.0", value) && !isnan(value) && value >= 0.0f) {
|
if (ulen == 0) {
|
||||||
out_kwh = static_cast<uint32_t>(floorf(value));
|
return false;
|
||||||
return true;
|
}
|
||||||
}
|
if (strcmp(unit_buf, "Wh") == 0) {
|
||||||
line_len = 0;
|
value *= 0.001f;
|
||||||
if (c == '!') {
|
return true;
|
||||||
break;
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (line_len + 1 < sizeof(line)) {
|
|
||||||
line[line_len++] = c;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -102,105 +109,162 @@ static void meter_debug_log() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
g_last_log_ms = now_ms;
|
g_last_log_ms = now_ms;
|
||||||
for (uint8_t i = 0; i < METER_COUNT; ++i) {
|
Serial.printf("meter: ok=%lu parse_fail=%lu overflow=%lu timeout=%lu bytes=%lu\n",
|
||||||
const MeterPort &p = g_ports[i];
|
static_cast<unsigned long>(g_frames_ok),
|
||||||
Serial.printf("meter%u: ok=%lu parse_fail=%lu overflow=%lu timeout=%lu bytes=%lu e=%lu valid=%u\n",
|
static_cast<unsigned long>(g_frames_parse_fail),
|
||||||
static_cast<unsigned>(i + 1),
|
static_cast<unsigned long>(g_rx_overflow),
|
||||||
static_cast<unsigned long>(p.frames_ok),
|
static_cast<unsigned long>(g_rx_timeout),
|
||||||
static_cast<unsigned long>(p.frames_parse_fail),
|
static_cast<unsigned long>(g_bytes_rx));
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void meter_init() {
|
bool meter_poll_frame(const char *&frame, size_t &len) {
|
||||||
g_ports[0].serial = &Serial2;
|
frame = nullptr;
|
||||||
g_ports[0].serial->begin(9600, SERIAL_7E1, PIN_METER1_RX, -1);
|
len = 0;
|
||||||
g_ports[0].state = MeterRxState::WaitStart;
|
|
||||||
|
|
||||||
if (METER_COUNT >= 2) {
|
|
||||||
g_ports[1].serial = &Serial1;
|
|
||||||
g_ports[1].serial->begin(9600, SERIAL_7E1, PIN_METER2_RX, -1);
|
|
||||||
g_ports[1].state = MeterRxState::WaitStart;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (METER_COUNT >= 3) {
|
|
||||||
g_ports[2].serial = &Serial;
|
|
||||||
g_ports[2].serial->begin(9600, SERIAL_7E1, PIN_METER3_RX, -1);
|
|
||||||
g_ports[2].state = MeterRxState::WaitStart;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void meter_poll_port(MeterPort &port) {
|
|
||||||
if (!port.serial) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
uint32_t now_ms = millis();
|
uint32_t now_ms = millis();
|
||||||
if (port.state == MeterRxState::InFrame && (now_ms - port.last_rx_ms > METER_FRAME_TIMEOUT_MS)) {
|
|
||||||
port.rx_timeout++;
|
if (g_rx_state == MeterRxState::InFrame && (now_ms - g_last_rx_ms > METER_FRAME_TIMEOUT_MS)) {
|
||||||
port.state = MeterRxState::WaitStart;
|
g_rx_timeout++;
|
||||||
port.frame_len = 0;
|
g_rx_state = MeterRxState::WaitStart;
|
||||||
|
g_frame_len = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (port.serial->available()) {
|
while (Serial2.available()) {
|
||||||
char c = static_cast<char>(port.serial->read());
|
char c = static_cast<char>(Serial2.read());
|
||||||
port.bytes_rx++;
|
g_bytes_rx++;
|
||||||
port.last_rx_ms = now_ms;
|
g_last_rx_ms = now_ms;
|
||||||
|
|
||||||
if (port.state == MeterRxState::WaitStart) {
|
if (g_rx_state == MeterRxState::WaitStart) {
|
||||||
if (c == '/') {
|
if (c == '/') {
|
||||||
port.state = MeterRxState::InFrame;
|
g_rx_state = MeterRxState::InFrame;
|
||||||
port.frame_len = 0;
|
g_frame_len = 0;
|
||||||
port.frame_buf[port.frame_len++] = c;
|
g_frame_buf[g_frame_len++] = c;
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (port.frame_len + 1 >= sizeof(port.frame_buf)) {
|
if (g_frame_len + 1 >= sizeof(g_frame_buf)) {
|
||||||
port.rx_overflow++;
|
g_rx_overflow++;
|
||||||
port.state = MeterRxState::WaitStart;
|
g_rx_state = MeterRxState::WaitStart;
|
||||||
port.frame_len = 0;
|
g_frame_len = 0;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
port.frame_buf[port.frame_len++] = c;
|
g_frame_buf[g_frame_len++] = c;
|
||||||
if (c == '!') {
|
if (c == '!') {
|
||||||
port.frame_buf[port.frame_len] = '\0';
|
g_frame_buf[g_frame_len] = '\0';
|
||||||
uint32_t energy_kwh = 0;
|
frame = g_frame_buf;
|
||||||
if (parse_energy_kwh_floor(port.frame_buf, port.frame_len, energy_kwh)) {
|
len = g_frame_len;
|
||||||
port.last_energy_kwh = energy_kwh;
|
g_rx_state = MeterRxState::WaitStart;
|
||||||
port.has_energy = true;
|
g_frame_len = 0;
|
||||||
port.frames_ok++;
|
meter_debug_log();
|
||||||
} else {
|
return true;
|
||||||
port.frames_parse_fail++;
|
|
||||||
}
|
|
||||||
port.state = MeterRxState::WaitStart;
|
|
||||||
port.frame_len = 0;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void meter_poll() {
|
|
||||||
for (uint8_t i = 0; i < METER_COUNT; ++i) {
|
|
||||||
meter_poll_port(g_ports[i]);
|
|
||||||
}
|
|
||||||
meter_debug_log();
|
meter_debug_log();
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t meter_count() {
|
bool meter_parse_frame(const char *frame, size_t len, MeterData &data) {
|
||||||
return METER_COUNT;
|
if (!frame || len == 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
bool got_any = false;
|
||||||
|
bool energy_ok = false;
|
||||||
|
bool total_p_ok = false;
|
||||||
|
bool p1_ok = false;
|
||||||
|
bool p2_ok = false;
|
||||||
|
bool p3_ok = false;
|
||||||
|
char line[128];
|
||||||
|
size_t line_len = 0;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < len; ++i) {
|
||||||
|
char c = frame[i];
|
||||||
|
if (c == '\r') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (c == '!') {
|
||||||
|
if (line_len + 1 < sizeof(line)) {
|
||||||
|
line[line_len++] = c;
|
||||||
|
}
|
||||||
|
line[line_len] = '\0';
|
||||||
|
data.valid = energy_ok || total_p_ok || p1_ok || p2_ok || p3_ok;
|
||||||
|
if (data.valid) {
|
||||||
|
g_frames_ok++;
|
||||||
|
} else {
|
||||||
|
g_frames_parse_fail++;
|
||||||
|
}
|
||||||
|
return data.valid;
|
||||||
|
}
|
||||||
|
if (c == '\n') {
|
||||||
|
line[line_len] = '\0';
|
||||||
|
if (line[0] == '!') {
|
||||||
|
data.valid = energy_ok || total_p_ok || p1_ok || p2_ok || p3_ok;
|
||||||
|
if (data.valid) {
|
||||||
|
g_frames_ok++;
|
||||||
|
} else {
|
||||||
|
g_frames_parse_fail++;
|
||||||
|
}
|
||||||
|
return data.valid;
|
||||||
|
}
|
||||||
|
|
||||||
|
float value = NAN;
|
||||||
|
if (parse_obis_ascii_value(line, "1-0:1.8.0", value)) {
|
||||||
|
parse_obis_ascii_unit_scale(line, "1-0:1.8.0", value);
|
||||||
|
data.energy_total_kwh = value;
|
||||||
|
energy_ok = true;
|
||||||
|
got_any = true;
|
||||||
|
}
|
||||||
|
if (parse_obis_ascii_value(line, "1-0:16.7.0", value)) {
|
||||||
|
data.total_power_w = value;
|
||||||
|
total_p_ok = true;
|
||||||
|
got_any = true;
|
||||||
|
}
|
||||||
|
if (parse_obis_ascii_value(line, "1-0:36.7.0", value)) {
|
||||||
|
data.phase_power_w[0] = value;
|
||||||
|
p1_ok = true;
|
||||||
|
got_any = true;
|
||||||
|
}
|
||||||
|
if (parse_obis_ascii_value(line, "1-0:56.7.0", value)) {
|
||||||
|
data.phase_power_w[1] = value;
|
||||||
|
p2_ok = true;
|
||||||
|
got_any = true;
|
||||||
|
}
|
||||||
|
if (parse_obis_ascii_value(line, "1-0:76.7.0", value)) {
|
||||||
|
data.phase_power_w[2] = value;
|
||||||
|
p3_ok = true;
|
||||||
|
got_any = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
line_len = 0;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (line_len + 1 < sizeof(line)) {
|
||||||
|
line[line_len++] = c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
data.valid = got_any;
|
||||||
|
if (data.valid) {
|
||||||
|
g_frames_ok++;
|
||||||
|
} else {
|
||||||
|
g_frames_parse_fail++;
|
||||||
|
}
|
||||||
|
return data.valid;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool meter_get_last_energy_kwh(uint8_t meter_idx, uint32_t &out_energy_kwh) {
|
bool meter_read(MeterData &data) {
|
||||||
if (meter_idx >= METER_COUNT) {
|
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.valid = false;
|
||||||
|
|
||||||
|
const char *frame = nullptr;
|
||||||
|
size_t len = 0;
|
||||||
|
if (!meter_poll_frame(frame, len)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (!g_ports[meter_idx].has_energy) {
|
return meter_parse_frame(frame, len, data);
|
||||||
return false;
|
|
||||||
}
|
|
||||||
out_energy_kwh = g_ports[meter_idx].last_energy_kwh;
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,8 +5,6 @@ static constexpr uint16_t kMagic = 0xDDB3;
|
|||||||
static constexpr uint8_t kSchema = 2;
|
static constexpr uint8_t kSchema = 2;
|
||||||
static constexpr uint8_t kFlags = 0x01;
|
static constexpr uint8_t kFlags = 0x01;
|
||||||
static constexpr size_t kMaxSamples = 30;
|
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) {
|
static void write_u16_le(uint8_t *dst, uint16_t value) {
|
||||||
dst[0] = static_cast<uint8_t>(value & 0xFF);
|
dst[0] = static_cast<uint8_t>(value & 0xFF);
|
||||||
@@ -110,14 +108,13 @@ bool encode_batch(const BatchInput &in, uint8_t *out, size_t out_cap, size_t *ou
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
size_t pos = 0;
|
size_t pos = 0;
|
||||||
if (!ensure_capacity(23, out_cap, pos)) {
|
if (!ensure_capacity(21, out_cap, pos)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
write_u16_le(&out[pos], kMagic);
|
write_u16_le(&out[pos], kMagic);
|
||||||
pos += 2;
|
pos += 2;
|
||||||
out[pos++] = kSchema;
|
out[pos++] = kSchema;
|
||||||
out[pos++] = kFlags;
|
out[pos++] = kFlags;
|
||||||
out[pos++] = in.schema_id;
|
|
||||||
write_u16_le(&out[pos], in.sender_id);
|
write_u16_le(&out[pos], in.sender_id);
|
||||||
pos += 2;
|
pos += 2;
|
||||||
write_u16_le(&out[pos], in.batch_id);
|
write_u16_le(&out[pos], in.batch_id);
|
||||||
@@ -133,32 +130,12 @@ bool encode_batch(const BatchInput &in, uint8_t *out, size_t out_cap, size_t *ou
|
|||||||
out[pos++] = in.err_tx;
|
out[pos++] = in.err_tx;
|
||||||
out[pos++] = in.err_last;
|
out[pos++] = in.err_last;
|
||||||
out[pos++] = in.err_rx_reject;
|
out[pos++] = in.err_rx_reject;
|
||||||
out[pos++] = in.meter_count;
|
|
||||||
|
|
||||||
if (in.n == 0) {
|
if (in.n == 0) {
|
||||||
*out_len = pos;
|
*out_len = pos;
|
||||||
return true;
|
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)) {
|
if (!ensure_capacity(4, out_cap, pos)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -212,7 +189,7 @@ bool decode_batch(const uint8_t *buf, size_t len, BatchInput *out) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
size_t pos = 0;
|
size_t pos = 0;
|
||||||
if (len < 23) {
|
if (len < 21) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
uint16_t magic = read_u16_le(&buf[pos]);
|
uint16_t magic = read_u16_le(&buf[pos]);
|
||||||
@@ -222,7 +199,6 @@ bool decode_batch(const uint8_t *buf, size_t len, BatchInput *out) {
|
|||||||
if (magic != kMagic || schema != kSchema || (flags & 0x01) == 0) {
|
if (magic != kMagic || schema != kSchema || (flags & 0x01) == 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
out->schema_id = buf[pos++];
|
|
||||||
out->sender_id = read_u16_le(&buf[pos]);
|
out->sender_id = read_u16_le(&buf[pos]);
|
||||||
pos += 2;
|
pos += 2;
|
||||||
out->batch_id = read_u16_le(&buf[pos]);
|
out->batch_id = read_u16_le(&buf[pos]);
|
||||||
@@ -238,7 +214,6 @@ bool decode_batch(const uint8_t *buf, size_t len, BatchInput *out) {
|
|||||||
out->err_tx = buf[pos++];
|
out->err_tx = buf[pos++];
|
||||||
out->err_last = buf[pos++];
|
out->err_last = buf[pos++];
|
||||||
out->err_rx_reject = buf[pos++];
|
out->err_rx_reject = buf[pos++];
|
||||||
out->meter_count = buf[pos++];
|
|
||||||
|
|
||||||
if (out->n > kMaxSamples || out->dt_s == 0) {
|
if (out->n > kMaxSamples || out->dt_s == 0) {
|
||||||
return false;
|
return false;
|
||||||
@@ -252,29 +227,6 @@ bool decode_batch(const uint8_t *buf, size_t len, BatchInput *out) {
|
|||||||
}
|
}
|
||||||
return pos == len;
|
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) {
|
if (pos + 4 > len) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -337,7 +289,6 @@ bool decode_batch(const uint8_t *buf, size_t len, BatchInput *out) {
|
|||||||
#ifdef PAYLOAD_CODEC_TEST
|
#ifdef PAYLOAD_CODEC_TEST
|
||||||
bool payload_codec_self_test() {
|
bool payload_codec_self_test() {
|
||||||
BatchInput in = {};
|
BatchInput in = {};
|
||||||
in.schema_id = kPayloadSchemaLegacy;
|
|
||||||
in.sender_id = 1;
|
in.sender_id = 1;
|
||||||
in.batch_id = 42;
|
in.batch_id = 42;
|
||||||
in.t_last = 1700000000;
|
in.t_last = 1700000000;
|
||||||
@@ -349,7 +300,6 @@ bool payload_codec_self_test() {
|
|||||||
in.err_tx = 3;
|
in.err_tx = 3;
|
||||||
in.err_last = 2;
|
in.err_last = 2;
|
||||||
in.err_rx_reject = 1;
|
in.err_rx_reject = 1;
|
||||||
in.meter_count = 0;
|
|
||||||
in.energy_wh[0] = 100000;
|
in.energy_wh[0] = 100000;
|
||||||
in.energy_wh[1] = 100001;
|
in.energy_wh[1] = 100001;
|
||||||
in.energy_wh[2] = 100050;
|
in.energy_wh[2] = 100050;
|
||||||
|
|||||||
@@ -3,22 +3,17 @@
|
|||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
|
||||||
struct BatchInput {
|
struct BatchInput {
|
||||||
uint8_t schema_id;
|
|
||||||
uint16_t sender_id;
|
uint16_t sender_id;
|
||||||
uint16_t batch_id;
|
uint16_t batch_id;
|
||||||
uint32_t t_last;
|
uint32_t t_last;
|
||||||
uint8_t dt_s;
|
uint8_t dt_s;
|
||||||
uint8_t n;
|
uint8_t n;
|
||||||
uint8_t meter_count;
|
|
||||||
uint16_t battery_mV;
|
uint16_t battery_mV;
|
||||||
uint8_t err_m;
|
uint8_t err_m;
|
||||||
uint8_t err_d;
|
uint8_t err_d;
|
||||||
uint8_t err_tx;
|
uint8_t err_tx;
|
||||||
uint8_t err_last;
|
uint8_t err_last;
|
||||||
uint8_t err_rx_reject;
|
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];
|
uint32_t energy_wh[30];
|
||||||
int16_t p1_w[30];
|
int16_t p1_w[30];
|
||||||
int16_t p2_w[30];
|
int16_t p2_w[30];
|
||||||
|
|||||||
@@ -12,93 +12,8 @@
|
|||||||
|
|
||||||
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 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 void write_u16_be(uint8_t *dst, uint16_t value) {
|
|
||||||
dst[0] = static_cast<uint8_t>((value >> 8) & 0xFF);
|
|
||||||
dst[1] = static_cast<uint8_t>(value & 0xFF);
|
|
||||||
}
|
|
||||||
|
|
||||||
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) {
|
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;
|
||||||
@@ -121,13 +36,11 @@ 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<192> doc;
|
StaticJsonDocument<128> 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);
|
||||||
@@ -147,32 +60,11 @@ void test_sender_loop(uint16_t short_id, const char *device_id) {
|
|||||||
pkt.device_id_short = short_id;
|
pkt.device_id_short = short_id;
|
||||||
pkt.payload_len = json.length();
|
pkt.payload_len = json.length();
|
||||||
memcpy(pkt.payload, json.c_str(), pkt.payload_len);
|
memcpy(pkt.payload, json.c_str(), pkt.payload_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) {
|
||||||
|
(void)self_short_id;
|
||||||
LoraPacket pkt = {};
|
LoraPacket pkt = {};
|
||||||
if (!lora_receive(pkt, 0)) {
|
if (!lora_receive(pkt, 0)) {
|
||||||
return;
|
return;
|
||||||
@@ -181,28 +73,22 @@ void test_receiver_loop(SenderStatus *statuses, uint8_t count, uint16_t self_sho
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t decompressed[192];
|
uint8_t decompressed[160];
|
||||||
if (pkt.payload_len >= sizeof(decompressed)) {
|
if (pkt.payload_len >= sizeof(decompressed)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
memcpy(decompressed, pkt.payload, pkt.payload_len);
|
memcpy(decompressed, pkt.payload, pkt.payload_len);
|
||||||
decompressed[pkt.payload_len] = '\0';
|
decompressed[pkt.payload_len] = '\0';
|
||||||
|
|
||||||
StaticJsonDocument<192> doc;
|
StaticJsonDocument<128> 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);
|
||||||
@@ -210,34 +96,12 @@ 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
StaticJsonDocument<256> mqtt_doc;
|
mqtt_publish_test(id, String(reinterpret_cast<const char *>(decompressed)));
|
||||||
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,4 +1,5 @@
|
|||||||
#include "time_manager.h"
|
#include "time_manager.h"
|
||||||
|
#include "config.h"
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
static bool g_time_synced = false;
|
static bool g_time_synced = false;
|
||||||
@@ -12,15 +13,20 @@ static void note_last_sync(uint32_t epoch) {
|
|||||||
g_last_sync_utc = epoch;
|
g_last_sync_utc = epoch;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void ensure_timezone_set() {
|
||||||
|
if (g_tz_set) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
setenv("TZ", TIMEZONE_TZ, 1);
|
||||||
|
tzset();
|
||||||
|
g_tz_set = true;
|
||||||
|
}
|
||||||
|
|
||||||
void time_receiver_init(const char *ntp_server_1, const char *ntp_server_2) {
|
void time_receiver_init(const char *ntp_server_1, const char *ntp_server_2) {
|
||||||
const char *server1 = (ntp_server_1 && ntp_server_1[0] != '\0') ? ntp_server_1 : "pool.ntp.org";
|
const char *server1 = (ntp_server_1 && ntp_server_1[0] != '\0') ? ntp_server_1 : "pool.ntp.org";
|
||||||
const char *server2 = (ntp_server_2 && ntp_server_2[0] != '\0') ? ntp_server_2 : "time.nist.gov";
|
const char *server2 = (ntp_server_2 && ntp_server_2[0] != '\0') ? ntp_server_2 : "time.nist.gov";
|
||||||
configTime(0, 0, server1, server2);
|
configTime(0, 0, server1, server2);
|
||||||
if (!g_tz_set) {
|
ensure_timezone_set();
|
||||||
setenv("TZ", "CET-1CEST,M3.5.0/2,M10.5.0/3", 1);
|
|
||||||
tzset();
|
|
||||||
g_tz_set = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t time_get_utc() {
|
uint32_t time_get_utc() {
|
||||||
@@ -40,11 +46,7 @@ bool time_is_synced() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void time_set_utc(uint32_t epoch) {
|
void time_set_utc(uint32_t epoch) {
|
||||||
if (!g_tz_set) {
|
ensure_timezone_set();
|
||||||
setenv("TZ", "CET-1CEST,M3.5.0/2,M10.5.0/3", 1);
|
|
||||||
tzset();
|
|
||||||
g_tz_set = true;
|
|
||||||
}
|
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
tv.tv_sec = epoch;
|
tv.tv_sec = epoch;
|
||||||
tv.tv_usec = 0;
|
tv.tv_usec = 0;
|
||||||
|
|||||||
@@ -57,6 +57,32 @@ static HistoryJob g_history = {};
|
|||||||
static constexpr size_t SD_LIST_MAX_FILES = 200;
|
static constexpr size_t SD_LIST_MAX_FILES = 200;
|
||||||
static constexpr size_t SD_DOWNLOAD_MAX_PATH = 160;
|
static constexpr size_t SD_DOWNLOAD_MAX_PATH = 160;
|
||||||
|
|
||||||
|
static String format_utc_timestamp(uint32_t ts_utc) {
|
||||||
|
if (ts_utc == 0) {
|
||||||
|
return "n/a";
|
||||||
|
}
|
||||||
|
time_t t = static_cast<time_t>(ts_utc);
|
||||||
|
struct tm tm_utc;
|
||||||
|
gmtime_r(&t, &tm_utc);
|
||||||
|
char buf[32];
|
||||||
|
snprintf(buf, sizeof(buf), "%04d-%02d-%02d %02d:%02d:%02d UTC",
|
||||||
|
tm_utc.tm_year + 1900,
|
||||||
|
tm_utc.tm_mon + 1,
|
||||||
|
tm_utc.tm_mday,
|
||||||
|
tm_utc.tm_hour,
|
||||||
|
tm_utc.tm_min,
|
||||||
|
tm_utc.tm_sec);
|
||||||
|
return String(buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint32_t timestamp_age_seconds(uint32_t ts_utc) {
|
||||||
|
uint32_t now_utc = time_get_utc();
|
||||||
|
if (ts_utc == 0 || now_utc < ts_utc) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return now_utc - ts_utc;
|
||||||
|
}
|
||||||
|
|
||||||
static int32_t round_power_w(float value) {
|
static int32_t round_power_w(float value) {
|
||||||
if (isnan(value)) {
|
if (isnan(value)) {
|
||||||
return 0;
|
return 0;
|
||||||
@@ -363,7 +389,6 @@ static String render_sender_block(const SenderStatus &status) {
|
|||||||
s += " RSSI:" + String(status.last_data.link_rssi_dbm) + " SNR:" + String(status.last_data.link_snr_db, 1);
|
s += " RSSI:" + String(status.last_data.link_rssi_dbm) + " SNR:" + String(status.last_data.link_snr_db, 1);
|
||||||
}
|
}
|
||||||
if (status.has_data) {
|
if (status.has_data) {
|
||||||
s += " ack:" + String(status.last_acked_batch_id);
|
|
||||||
s += " err_tx:" + String(status.last_data.err_lora_tx);
|
s += " err_tx:" + String(status.last_data.err_lora_tx);
|
||||||
s += " err_last:" + String(static_cast<uint8_t>(status.last_data.last_error));
|
s += " err_last:" + String(static_cast<uint8_t>(status.last_data.last_error));
|
||||||
s += " (" + String(fault_text(status.last_data.last_error)) + ")";
|
s += " (" + String(fault_text(status.last_data.last_error)) + ")";
|
||||||
@@ -375,19 +400,16 @@ static String render_sender_block(const SenderStatus &status) {
|
|||||||
if (!status.has_data) {
|
if (!status.has_data) {
|
||||||
s += "No data";
|
s += "No data";
|
||||||
} else {
|
} else {
|
||||||
if (status.last_data.energy_multi) {
|
s += "Last update: " + format_utc_timestamp(status.last_update_ts_utc);
|
||||||
s += "Energy1: " + String(status.last_data.energy_kwh_int[0]) + " kWh<br>";
|
if (time_is_synced()) {
|
||||||
s += "Energy2: " + String(status.last_data.energy_kwh_int[1]) + " kWh<br>";
|
s += " (" + String(timestamp_age_seconds(status.last_update_ts_utc)) + "s ago)";
|
||||||
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 += "<br>";
|
||||||
|
s += "Energy: " + String(status.last_data.energy_total_kwh, 2) + " kWh<br>";
|
||||||
|
s += "Power: " + String(round_power_w(status.last_data.total_power_w)) + " W<br>";
|
||||||
|
s += "P1/P2/P3: " + String(round_power_w(status.last_data.phase_power_w[0])) + " / " +
|
||||||
|
String(round_power_w(status.last_data.phase_power_w[1])) + " / " +
|
||||||
|
String(round_power_w(status.last_data.phase_power_w[2])) + " W<br>";
|
||||||
s += "Battery: " + String(status.last_data.battery_percent) + "% (" + String(status.last_data.battery_voltage_v, 2) + " V)";
|
s += "Battery: " + String(status.last_data.battery_percent) + "% (" + String(status.last_data.battery_voltage_v, 2) + " V)";
|
||||||
}
|
}
|
||||||
s += "</div>";
|
s += "</div>";
|
||||||
@@ -614,16 +636,14 @@ static void handle_sender() {
|
|||||||
if (g_last_batch_count[i] > 0) {
|
if (g_last_batch_count[i] > 0) {
|
||||||
html += "<h3>Last batch (" + String(g_last_batch_count[i]) + " samples)</h3>";
|
html += "<h3>Last batch (" + String(g_last_batch_count[i]) + " samples)</h3>";
|
||||||
html += "<table border='1' cellspacing='0' cellpadding='3'>";
|
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 += "<tr><th>#</th><th>ts</th><th>e_kwh</th><th>p_w</th><th>p1_w</th><th>p2_w</th><th>p3_w</th>";
|
||||||
html += "<th>bat_v</th><th>bat_pct</th><th>rssi</th><th>snr</th><th>err_tx</th><th>err_last</th><th>rx_reject</th></tr>";
|
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) {
|
for (uint8_t r = 0; r < g_last_batch_count[i]; ++r) {
|
||||||
const MeterData &d = g_last_batch[i][r];
|
const MeterData &d = g_last_batch[i][r];
|
||||||
html += "<tr>";
|
html += "<tr>";
|
||||||
html += "<td>" + String(r) + "</td>";
|
html += "<td>" + String(r) + "</td>";
|
||||||
html += "<td>" + String(d.ts_utc) + "</td>";
|
html += "<td>" + String(d.ts_utc) + "</td>";
|
||||||
html += "<td>" + String(d.energy_kwh_int[0]) + "</td>";
|
html += "<td>" + String(d.energy_total_kwh, 2) + "</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.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[0])) + "</td>";
|
||||||
html += "<td>" + String(round_power_w(d.phase_power_w[1])) + "</td>";
|
html += "<td>" + String(round_power_w(d.phase_power_w[1])) + "</td>";
|
||||||
|
|||||||
Reference in New Issue
Block a user