Browse Source

post sml measurements to influxdb

Thomas Buck 4 months ago
parent
commit
5b89716c53
5 changed files with 71 additions and 10 deletions
  1. 1
    1
      compile_commands.json
  2. 2
    0
      include/influx.h
  3. 3
    3
      platformio.ini
  4. 11
    0
      src/influx.cpp
  5. 54
    6
      src/lora.cpp

+ 1
- 1
compile_commands.json View File

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

+ 2
- 0
include/influx.h View File

@@ -18,4 +18,6 @@ void initInflux();
18 18
 void runInflux();
19 19
 void writeDatabase();
20 20
 
21
+void writeSensorDatum(String measurement, String sensor, String placement, String key, double value);
22
+
21 23
 #endif // __INFLUX_H__

+ 3
- 3
platformio.ini View File

@@ -48,9 +48,7 @@ extra_scripts = pre:extra_script.py
48 48
 build_flags =
49 49
   -DSENSOR_HOSTNAME_PREFIX=\"lora-\"
50 50
   "-DNAME_OF_FEATURE=\"Lora Gatway\""
51
-  -DENABLE_WEBSOCKETS
52 51
   -DENABLE_DEBUGLOG
53
-  -DENABLE_MQTT
54 52
   -DNEW_ESP32_LIB
55 53
   -DFEATURE_LORA
56 54
   -DFEATURE_SML
@@ -77,10 +75,12 @@ build_flags =
77 75
   "-DNAME_OF_FEATURE=\"Lora Gatway\""
78 76
   -DENABLE_WEBSOCKETS
79 77
   -DENABLE_DEBUGLOG
80
-  -DENABLE_MQTT
81 78
   -DNEW_ESP32_LIB
82 79
   -DFEATURE_LORA
80
+  -DENABLE_INFLUXDB_LOGGING
81
+  -DUSE_INFLUXDB_LIB
83 82
 lib_deps =
83
+    ESP8266 Influxdb
84 84
     https://github.com/knolleary/pubsubclient.git#2d228f2f862a95846c65a8518c79f48dfc8f188c
85 85
     https://github.com/rlogiacco/CircularBuffer.git#f29cf01b6e8603422f3668d51036ac124f803404
86 86
     https://github.com/Links2004/arduinoWebSockets.git#30d5e136665a52880f641ddd7245b3ba05ecd32b

+ 11
- 0
src/influx.cpp View File

@@ -112,6 +112,17 @@ static void addTagsRelais(InfluxData &measurement, String id, String name) {
112 112
 }
113 113
 #endif
114 114
 
115
+void writeSensorDatum(String measurement, String sensor, String placement, String key, double value) {
116
+    InfluxData ms(measurement);
117
+    addTagsSensor(ms, sensor, placement);
118
+
119
+    ms.addValue(key, value);
120
+
121
+    debug.printf("Writing %s %s %s %s %.2lf\n", measurement.c_str(), sensor.c_str(), placement.c_str(), key.c_str(), value);
122
+    writeMeasurement(ms);
123
+    debug.println(F("Done!"));
124
+}
125
+
115 126
 void writeDatabase() {
116 127
 #ifdef ENABLE_BME280
117 128
 

+ 54
- 6
src/lora.cpp View File

@@ -22,6 +22,7 @@
22 22
 
23 23
 #include "config.h"
24 24
 #include "DebugLog.h"
25
+#include "influx.h"
25 26
 #include "lora.h"
26 27
 
27 28
 // define LORA_TEST_TX to periodically transmit a test message
@@ -31,7 +32,8 @@
31 32
 
32 33
 #ifdef FEATURE_SML
33 34
 #define LORA_LED_BRIGHTNESS 1 // in percent, 50% brightness is plenty for this LED
34
-#define OLED_BAT_INTERVAL (10UL * 1000UL) // in ms
35
+#define OLED_BAT_INTERVAL (2UL * 60UL * 1000UL) // in ms
36
+#define FORCE_BAT_SEND_AT_OLED_INTERVAL
35 37
 #else // FEATURE_SML
36 38
 #define LORA_LED_BRIGHTNESS 25 // in percent, 50% brightness is plenty for this LED
37 39
 #endif // FEATURE_SML
@@ -135,7 +137,7 @@ static bool lora_tx(uint8_t *data, size_t len) {
135 137
         return false;
136 138
     }
137 139
 
138
-    debug.printf("TX [%lu] ", len);
140
+    debug.printf("TX [%d] (%lu) ", data[0], len);
139 141
     radio.clearDio1Action();
140 142
 
141 143
     heltec_led(LORA_LED_BRIGHTNESS);
@@ -292,9 +294,13 @@ void lora_run(void) {
292 294
 
293 295
 #ifdef OLED_BAT_INTERVAL
294 296
     unsigned long time = millis();
295
-    if ((time - last_bat_time) >= OLED_BAT_INTERVAL) {
297
+    if (((time - last_bat_time) >= OLED_BAT_INTERVAL) || (last_bat_time == 0)) {
296 298
         last_bat_time = time;
297 299
         print_bat();
300
+
301
+#ifdef FORCE_BAT_SEND_AT_OLED_INTERVAL
302
+        lora_sml_send(LORA_SML_BAT_V, lora_get_mangled_bat(), 0);
303
+#endif // FORCE_BAT_SEND_AT_OLED_INTERVAL
298 304
     }
299 305
 #endif
300 306
 
@@ -314,7 +320,7 @@ void lora_run(void) {
314 320
             debug.printf("  RSSI: %.2f dBm\n", radio.getRSSI());
315 321
             debug.printf("  SNR: %.2f dB\n", radio.getSNR());
316 322
 
317
-#ifdef DEBUG_LORA_RX_HEXDUMP
323
+#if defined(DEBUG_LORA_RX_HEXDUMP) || (!defined(ENABLE_INFLUXDB_LOGGING))
318 324
             for (int i = 0; i < sizeof(data); i++) {
319 325
                 debug.printf("%02X", data[i]);
320 326
                 if (i < (sizeof(data) - 1)) {
@@ -325,6 +331,7 @@ void lora_run(void) {
325 331
             }
326 332
 #endif
327 333
 
334
+#ifdef ENABLE_INFLUXDB_LOGGING
328 335
             if (data[0] == LORA_SML_BAT_V) {
329 336
                 float vbat = NAN;
330 337
                 int percent = -1;
@@ -332,14 +339,55 @@ void lora_run(void) {
332 339
                 memcpy(&percent, data + 1 + sizeof(float), sizeof(int));
333 340
                 debug.printf("  Vbat: %.2f (%d%%)\n", vbat, percent);
334 341
 
335
-                // TODO payload to influxdb
342
+                writeSensorDatum("environment", "sml", SENSOR_LOCATION, "vbat", vbat);
343
+                writeSensorDatum("environment", "sml", SENSOR_LOCATION, "percent", percent);
336 344
             } else {
337 345
                 double val = NAN;
338 346
                 memcpy(&val, data + 1, sizeof(double));
339 347
                 debug.printf("  Value: %.2f\n", val);
340 348
 
341
-                // TODO payload to influxdb
349
+                String key;
350
+                switch (data[0]) {
351
+                    case LORA_SML_HELLO:
352
+                        key = "hello";
353
+                        break;
354
+
355
+                    case LORA_SML_SUM_WH:
356
+                        key = "Sum_Wh";
357
+                        break;
358
+
359
+                    case LORA_SML_T1_WH:
360
+                        key = "T1_Wh";
361
+                        break;
362
+
363
+                    case LORA_SML_T2_WH:
364
+                        key = "T2_Wh";
365
+                        break;
366
+
367
+                    case LORA_SML_SUM_W:
368
+                        key = "Sum_W";
369
+                        break;
370
+
371
+                    case LORA_SML_L1_W:
372
+                        key = "L1_W";
373
+                        break;
374
+
375
+                    case LORA_SML_L2_W:
376
+                        key = "L2_W";
377
+                        break;
378
+
379
+                    case LORA_SML_L3_W:
380
+                        key = "L3_W";
381
+                        break;
382
+
383
+                    default:
384
+                        key = "unknown";
385
+                        break;
386
+                }
387
+
388
+                writeSensorDatum("environment", "sml", SENSOR_LOCATION, key, val);
342 389
             }
390
+#endif // ENABLE_INFLUXDB_LOGGING
343 391
         }
344 392
 
345 393
         success = true;

Loading…
Cancel
Save