2 Commits

Author SHA1 Message Date
  Thomas Buck 476f8db02f sml_data_received() only returned false with empty rtc data area. 2 months ago
  Thomas Buck 84199ccfc8 some lora tweaks 2 months 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
-.pio/build/cyd/compile_commands.json
1
+.pio/build/loratx/compile_commands.json

+ 1
- 0
include/config.h View File

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

+ 17
- 4
src/lora.cpp View File

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

+ 4
- 1
src/smart_meter.cpp View File

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

Loading…
Cancel
Save