From 5f42575b10fc66e634fe0bcfffbc3d60bdb414ee Mon Sep 17 00:00:00 2001 From: acidburns Date: Wed, 21 Jan 2026 21:14:48 +0100 Subject: [PATCH] Add 868MHz envs, fix MAC id, offset timesync --- README.md | 2 ++ include/config.h | 8 ++++++-- platformio.ini | 27 +++++++++++++++++++++++++++ src/data_model.cpp | 5 +++-- src/main.cpp | 4 ++++ src/test_mode.cpp | 4 ++++ 6 files changed, 46 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 360dcbd..8f608d7 100644 --- a/README.md +++ b/README.md @@ -224,6 +224,8 @@ inline constexpr uint16_t EXPECTED_SENDER_IDS[NUM_SENDERS] = { 0xF19C }; ## Build Environments - `lilygo-t3-v1-6-1`: production build - `lilygo-t3-v1-6-1-test`: test build with `ENABLE_TEST_MODE` +- `lilygo-t3-v1-6-1-868`: production build for 868 MHz modules +- `lilygo-t3-v1-6-1-868-test`: test build for 868 MHz modules ## Limits & Known Constraints - **Compression**: uses lightweight RLE (good for JSON but not optimal). diff --git a/include/config.h b/include/config.h index 02b530a..2c0fffe 100644 --- a/include/config.h +++ b/include/config.h @@ -38,7 +38,10 @@ constexpr uint8_t PIN_OLED_CTRL = 14; constexpr uint8_t PIN_METER_RX = 34; // LoRa settings -constexpr long LORA_FREQUENCY = 433E6; +#ifndef LORA_FREQUENCY_HZ +#define LORA_FREQUENCY_HZ 433E6 +#endif +constexpr long LORA_FREQUENCY = LORA_FREQUENCY_HZ; constexpr uint8_t LORA_SPREADING_FACTOR = 12; constexpr long LORA_BANDWIDTH = 125E3; constexpr uint8_t LORA_CODING_RATE = 5; @@ -52,7 +55,8 @@ constexpr uint32_t OLED_AUTO_OFF_MS = 10UL * 60UL * 1000UL; constexpr uint8_t NUM_SENDERS = 1; inline constexpr uint16_t EXPECTED_SENDER_IDS[NUM_SENDERS] = { - 0xF19C + //0xF19C //433mhz sender + 0x7EB4 //868mhz sender }; DeviceRole detect_role(); diff --git a/platformio.ini b/platformio.ini index d7f14d6..52f1e2d 100644 --- a/platformio.ini +++ b/platformio.ini @@ -31,3 +31,30 @@ lib_deps = knolleary/PubSubClient@^2.8 build_flags = -DENABLE_TEST_MODE + +[env:lilygo-t3-v1-6-1-868] +platform = espressif32 +board = ttgo-lora32-v1 +framework = arduino +lib_deps = + sandeepmistry/LoRa@^0.8.0 + bblanchon/ArduinoJson@^6.21.5 + adafruit/Adafruit SSD1306@^2.5.9 + adafruit/Adafruit GFX Library@^1.11.9 + knolleary/PubSubClient@^2.8 +build_flags = + -DLORA_FREQUENCY_HZ=868E6 + +[env:lilygo-t3-v1-6-1-868-test] +platform = espressif32 +board = ttgo-lora32-v1 +framework = arduino +lib_deps = + sandeepmistry/LoRa@^0.8.0 + bblanchon/ArduinoJson@^6.21.5 + adafruit/Adafruit SSD1306@^2.5.9 + adafruit/Adafruit GFX Library@^1.11.9 + knolleary/PubSubClient@^2.8 +build_flags = + -DENABLE_TEST_MODE + -DLORA_FREQUENCY_HZ=868E6 diff --git a/src/data_model.cpp b/src/data_model.cpp index 73dbb96..f1bc694 100644 --- a/src/data_model.cpp +++ b/src/data_model.cpp @@ -1,9 +1,10 @@ #include "data_model.h" -#include +#include void init_device_ids(uint16_t &short_id, char *device_id, size_t device_id_len) { uint8_t mac[6] = {0}; - WiFi.macAddress(mac); + // Read base MAC without needing WiFi to be started. + esp_read_mac(mac, ESP_MAC_WIFI_STA); short_id = (static_cast(mac[4]) << 8) | mac[5]; snprintf(device_id, device_id_len, "dd3-%04X", short_id); } diff --git a/src/main.cpp b/src/main.cpp index 8b50199..e6449ff 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -21,6 +21,7 @@ static SenderStatus g_sender_statuses[NUM_SENDERS]; static bool g_ap_mode = false; static WifiMqttConfig g_cfg; static uint32_t g_last_timesync_ms = 0; +static constexpr uint32_t TIME_SYNC_OFFSET_MS = 15000; static void init_sender_statuses() { for (uint8_t i = 0; i < NUM_SENDERS; ++i) { @@ -124,6 +125,9 @@ static void sender_cycle() { } static void receiver_loop() { + if (g_last_timesync_ms == 0) { + g_last_timesync_ms = millis() - (TIME_SYNC_INTERVAL_SEC * 1000UL - TIME_SYNC_OFFSET_MS); + } LoraPacket pkt = {}; if (lora_receive(pkt, 0) && pkt.protocol_version == PROTOCOL_VERSION && pkt.payload_type == PayloadType::MeterData) { uint8_t decompressed[256]; diff --git a/src/test_mode.cpp b/src/test_mode.cpp index e70d023..8710338 100644 --- a/src/test_mode.cpp +++ b/src/test_mode.cpp @@ -16,6 +16,7 @@ static uint32_t g_last_test_ms = 0; static uint16_t g_test_code_counter = 1000; static uint32_t g_last_timesync_ms = 0; static constexpr uint32_t TEST_SEND_INTERVAL_MS = 30000; +static constexpr uint32_t TEST_TIMESYNC_OFFSET_MS = 15000; void test_sender_loop(uint16_t short_id, const char *device_id) { LoraPacket rx = {}; @@ -76,6 +77,9 @@ void test_sender_loop(uint16_t short_id, const char *device_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);