Kaynağa Gözat

also send battery state via lora

Thomas Buck 4 ay önce
ebeveyn
işleme
eac6a944dd
3 değiştirilmiş dosya ile 40 ekleme ve 11 silme
  1. 3
    3
      include/lora.h
  2. 33
    8
      src/lora.cpp
  3. 4
    0
      src/smart_meter.cpp

+ 3
- 3
include/lora.h Dosyayı Görüntüle

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

+ 33
- 8
src/lora.cpp Dosyayı Görüntüle

@@ -115,6 +115,15 @@ static void print_bat(void) {
115 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 127
 static void lora_rx(void) {
119 128
     rx_flag = true;
120 129
 }
@@ -305,10 +314,6 @@ void lora_run(void) {
305 314
             debug.printf("  RSSI: %.2f dBm\n", radio.getRSSI());
306 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 317
 #ifdef DEBUG_LORA_RX_HEXDUMP
313 318
             for (int i = 0; i < sizeof(data); i++) {
314 319
                 debug.printf("%02X", data[i]);
@@ -320,7 +325,21 @@ void lora_run(void) {
320 325
             }
321 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 345
         success = true;
@@ -350,7 +369,6 @@ void lora_run(void) {
350 369
         lora_tx(s.c_str(), s.length());
351 370
     }
352 371
 #else // LORA_TEST_TX
353
-#ifdef FEATURE_SML
354 372
     if (button.isSingleClick()) {
355 373
         // In case of button click, tell user to wait
356 374
         if (!tx_legal) {
@@ -358,9 +376,16 @@ void lora_run(void) {
358 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 387
 #endif // FEATURE_SML
388
+    }
364 389
 #endif // LORA_TEST_TX
365 390
 }
366 391
 

+ 4
- 0
src/smart_meter.cpp Dosyayı Görüntüle

@@ -162,6 +162,10 @@ void sml_run(void) {
162 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 169
         debug.println();
166 170
     }
167 171
 }

Loading…
İptal
Kaydet