2 Commits

Author SHA1 Message Date
  Thomas Buck 476f8db02f sml_data_received() only returned false with empty rtc data area. 1 month ago
  Thomas Buck 84199ccfc8 some lora tweaks 1 month ago
4 changed files with 23 additions and 6 deletions
  1. 1
    1
      compile_commands.json
  2. 1
    0
      include/config.h
  3. 17
    4
      src/lora.cpp
  4. 4
    1
      src/smart_meter.cpp

+ 1
- 1
compile_commands.json View File

@@ -1 +1 @@
1
-.pio/build/cyd/compile_commands.json
1
+.pio/build/loratx/compile_commands.json

+ 1
- 0
include/config.h View File

@@ -45,6 +45,7 @@
45 45
 // LoRa SML Bridge "Crypto"
46 46
 // needs to be sizeof(struct lora_sml_msg) bytes long
47 47
 #define LORA_XOR_KEY "_SUPER_SECRET_KEY_HERE_"
48
+#define LORA_CLIENT_CHECKSUM
48 49
 
49 50
 // all given in milliseconds
50 51
 #define SERVER_HANDLE_INTERVAL 10

+ 17
- 4
src/lora.cpp View File

@@ -33,9 +33,9 @@
33 33
 
34 34
 #ifdef FEATURE_SML
35 35
 #define LORA_LED_BRIGHTNESS 0 // in percent, 50% brightness is plenty for this LED
36
-#define DEEP_SLEEP_DURATION_S 60
37
-#define DEEP_SLEEP_TIMEOUT_MS (30UL * 1000UL)
38
-#define DEEP_SLEEP_ABORT_NO_DATA_MS DEEP_SLEEP_TIMEOUT_MS
36
+#define DEEP_SLEEP_DURATION_S (60 - (millis() / 1000)) // 60s cycle time
37
+#define DEEP_SLEEP_TIMEOUT_MS (30UL * 1000UL) // fallback sleep timeout when sml data was received
38
+#define DEEP_SLEEP_ABORT_NO_DATA_MS DEEP_SLEEP_TIMEOUT_MS // sleep timeout when no sml data received
39 39
 #else // FEATURE_SML
40 40
 #define LORA_LED_BRIGHTNESS 25 // in percent, 50% brightness is plenty for this LED
41 41
 #endif // FEATURE_SML
@@ -59,7 +59,7 @@
59 59
 // transmissting without an antenna can damage your hardware.
60 60
 // 25mW = 14dBm
61 61
 #define MAX_TX_POWER        14
62
-#define ANTENNA_GAIN        5
62
+#define ANTENNA_GAIN        (5 - 3) // 3dB for the coax extensions and their SMA connectors
63 63
 #define TRANSMIT_POWER      (MAX_TX_POWER - ANTENNA_GAIN)
64 64
 
65 65
 #define RADIOLIB_xy(action) \
@@ -85,7 +85,9 @@ static unsigned long tx_time = 0, minimum_pause = 0;
85 85
 static volatile bool rx_flag = false;
86 86
 
87 87
 #ifndef LORA_KEEP_SENDING_CACHE
88
+#define BAT_MSG_EVERY_NTH 5
88 89
 RTC_DATA_ATTR static uint8_t last_tx_msg = LORA_SML_HELLO;
90
+RTC_DATA_ATTR static uint8_t bat_msg_cnt = 0;
89 91
 #endif // ! LORA_KEEP_SENDING_CACHE
90 92
 
91 93
 #ifdef FEATURE_SML
@@ -259,6 +261,13 @@ void lora_sml_done(void) {
259 261
     do {
260 262
         last_tx_msg++;
261 263
         n++;
264
+        if (last_tx_msg == LORA_SML_BAT_V) {
265
+            bat_msg_cnt++;
266
+            if (bat_msg_cnt < BAT_MSG_EVERY_NTH) {
267
+                continue; // skip bat message this time
268
+            }
269
+            bat_msg_cnt = 0;
270
+        }
262 271
         if (last_tx_msg >= LORA_SML_NUM_MESSAGES) {
263 272
             last_tx_msg = 0;
264 273
         }
@@ -270,6 +279,7 @@ void lora_sml_done(void) {
270 279
         lora_tx(msg, cache[msg].value);
271 280
     }
272 281
 
282
+    debug.println("sleep");
273 283
     heltec_deep_sleep(DEEP_SLEEP_DURATION_S < (minimum_pause / 1000) ? (minimum_pause / 1000) : DEEP_SLEEP_DURATION_S);
274 284
 #endif // ! LORA_KEEP_SENDING_CACHE
275 285
 }
@@ -363,6 +373,7 @@ void lora_run(void) {
363 373
     bool got_sml = sml_data_received();
364 374
     if ((got_sml && (time >= DEEP_SLEEP_TIMEOUT_MS))
365 375
             || ((!got_sml) && (time >= DEEP_SLEEP_ABORT_NO_DATA_MS))) {
376
+        debug.printf("falllback sleep %d %lu\n", got_sml, time);
366 377
         heltec_deep_sleep(DEEP_SLEEP_DURATION_S < (minimum_pause / 1000) ? (minimum_pause / 1000) : DEEP_SLEEP_DURATION_S);
367 378
     }
368 379
 #endif // DEEP_SLEEP_TIMEOUT_MS && DEEP_SLEEP_DURATION_S
@@ -371,6 +382,7 @@ void lora_run(void) {
371 382
     if ((time >= (6UL * 60UL * 60UL * 1000UL) // running for at least 6h
372 383
         && ((time - last_rx) >= (30UL * 1000UL))) // and last lora rx at least 30s ago
373 384
         || ((time - last_rx) >= (4UL * 60UL * 1000UL))) { // or last message longer than 4min ago
385
+        debug.println("hang sleep");
374 386
         heltec_deep_sleep(5); // attempt reset to avoid lorarx hanging
375 387
     }
376 388
 #endif // ! FEATURE_SML
@@ -503,6 +515,7 @@ void lora_run(void) {
503 515
             return;
504 516
         }
505 517
 
518
+        debug.println("click");
506 519
         lora_tx(LORA_SML_HELLO, heltec_temperature());
507 520
     }
508 521
 #endif // ! FEATURE_SML

+ 4
- 1
src/smart_meter.cpp View File

@@ -29,12 +29,13 @@
29 29
 
30 30
 static EspSoftwareSerial::UART port;
31 31
 RTC_DATA_ATTR static unsigned long counter = 0;
32
+bool got_data = false;
32 33
 
33 34
 static double SumWh = NAN, T1Wh = NAN, T2Wh = NAN;
34 35
 static double SumW = NAN, L1W = NAN, L2W = NAN, L3W = NAN;
35 36
 
36 37
 bool sml_data_received(void) {
37
-    return counter > 0;
38
+    return got_data;
38 39
 }
39 40
 
40 41
 static void EnergySum(void) {
@@ -164,6 +165,8 @@ void sml_run(void) {
164 165
 
165 166
         debug.println();
166 167
 
168
+        got_data = true;
169
+
167 170
 #ifdef FEATURE_LORA
168 171
         // the power readings (Watt) are just sent as is, if available.
169 172
         // the energy readings are consolidated if possible, to avoid

Loading…
Cancel
Save