Browse Source

also send battery state via lora

Thomas Buck 4 months ago
parent
commit
eac6a944dd
3 changed files with 40 additions and 11 deletions
  1. 3
    3
      include/lora.h
  2. 33
    8
      src/lora.cpp
  3. 4
    0
      src/smart_meter.cpp

+ 3
- 3
include/lora.h View File

22
 void lora_init(void);
22
 void lora_init(void);
23
 void lora_run(void);
23
 void lora_run(void);
24
 
24
 
25
-#ifdef FEATURE_SML
26
-
27
 enum lora_sml_type {
25
 enum lora_sml_type {
28
     LORA_SML_HELLO = 0,
26
     LORA_SML_HELLO = 0,
29
     LORA_SML_SUM_WH,
27
     LORA_SML_SUM_WH,
38
     LORA_SML_NUM_MESSAGES
36
     LORA_SML_NUM_MESSAGES
39
 };
37
 };
40
 
38
 
39
+#ifdef FEATURE_SML
41
 void lora_sml_send(enum lora_sml_type msg, double value, unsigned long counter);
40
 void lora_sml_send(enum lora_sml_type msg, double value, unsigned long counter);
42
-
43
 #endif // FEATURE_SML
41
 #endif // FEATURE_SML
44
 
42
 
43
+double lora_get_mangled_bat(void);
44
+
45
 #endif // FEATURE_LORA
45
 #endif // FEATURE_LORA
46
 
46
 
47
 #endif // __ESP_ENV_LORA__
47
 #endif // __ESP_ENV_LORA__

+ 33
- 8
src/lora.cpp View File

115
     debug.printf("Vbat: %.2fV (%d%%)\n", vbat, heltec_battery_percent(vbat));
115
     debug.printf("Vbat: %.2fV (%d%%)\n", vbat, heltec_battery_percent(vbat));
116
 }
116
 }
117
 
117
 
118
+double lora_get_mangled_bat(void) {
119
+    uint8_t data[sizeof(double)];
120
+    float vbat = heltec_vbat();
121
+    int percent = heltec_battery_percent(vbat);
122
+    memcpy(data, &vbat, sizeof(float));
123
+    memcpy(data + sizeof(float), &percent, sizeof(int));
124
+    return *((double *)data);
125
+}
126
+
118
 static void lora_rx(void) {
127
 static void lora_rx(void) {
119
     rx_flag = true;
128
     rx_flag = true;
120
 }
129
 }
305
             debug.printf("  RSSI: %.2f dBm\n", radio.getRSSI());
314
             debug.printf("  RSSI: %.2f dBm\n", radio.getRSSI());
306
             debug.printf("  SNR: %.2f dB\n", radio.getSNR());
315
             debug.printf("  SNR: %.2f dB\n", radio.getSNR());
307
 
316
 
308
-            double val = NAN;
309
-            memcpy(&val, data + 1, sizeof(double));
310
-            debug.printf("  Value: %.2f\n", val);
311
-
312
 #ifdef DEBUG_LORA_RX_HEXDUMP
317
 #ifdef DEBUG_LORA_RX_HEXDUMP
313
             for (int i = 0; i < sizeof(data); i++) {
318
             for (int i = 0; i < sizeof(data); i++) {
314
                 debug.printf("%02X", data[i]);
319
                 debug.printf("%02X", data[i]);
320
             }
325
             }
321
 #endif
326
 #endif
322
 
327
 
323
-            // TODO payload to influxdb
328
+            if (data[0] == LORA_SML_BAT_V) {
329
+                float vbat = NAN;
330
+                int percent = -1;
331
+                memcpy(&vbat, data + 1, sizeof(float));
332
+                memcpy(&percent, data + 1 + sizeof(float), sizeof(int));
333
+                debug.printf("  Vbat: %.2f (%d%%)\n", vbat, percent);
334
+
335
+                // TODO payload to influxdb
336
+            } else {
337
+                double val = NAN;
338
+                memcpy(&val, data + 1, sizeof(double));
339
+                debug.printf("  Value: %.2f\n", val);
340
+
341
+                // TODO payload to influxdb
342
+            }
324
         }
343
         }
325
 
344
 
326
         success = true;
345
         success = true;
350
         lora_tx(s.c_str(), s.length());
369
         lora_tx(s.c_str(), s.length());
351
     }
370
     }
352
 #else // LORA_TEST_TX
371
 #else // LORA_TEST_TX
353
-#ifdef FEATURE_SML
354
     if (button.isSingleClick()) {
372
     if (button.isSingleClick()) {
355
         // In case of button click, tell user to wait
373
         // In case of button click, tell user to wait
356
         if (!tx_legal) {
374
         if (!tx_legal) {
358
             return;
376
             return;
359
         }
377
         }
360
 
378
 
361
-        lora_sml_send(LORA_SML_HELLO, -23.42, 0);
362
-    }
379
+#ifdef FEATURE_SML
380
+        lora_sml_send(LORA_SML_BAT_V, lora_get_mangled_bat(), 0);
381
+#else // FEATURE_SML
382
+        uint8_t data[sizeof(double) + 1];
383
+        data[0] = LORA_SML_HELLO;
384
+        double tmp = -23.42;
385
+        memcpy(data + 1, &tmp, sizeof(double));
386
+        lora_tx(data, sizeof(data));
363
 #endif // FEATURE_SML
387
 #endif // FEATURE_SML
388
+    }
364
 #endif // LORA_TEST_TX
389
 #endif // LORA_TEST_TX
365
 }
390
 }
366
 
391
 

+ 4
- 0
src/smart_meter.cpp View File

162
 #endif // FEATURE_LORA
162
 #endif // FEATURE_LORA
163
         }
163
         }
164
 
164
 
165
+#ifdef FEATURE_LORA
166
+        lora_sml_send(LORA_SML_BAT_V, lora_get_mangled_bat(), counter);
167
+#endif // FEATURE_LORA
168
+
165
         debug.println();
169
         debug.println();
166
     }
170
     }
167
 }
171
 }

Loading…
Cancel
Save