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
-.pio/build/loratx/compile_commands.json
1
+.pio/build/lorarx/compile_commands.json

+ 2
- 0
include/influx.h View File

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

+ 3
- 3
platformio.ini View File

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

+ 11
- 0
src/influx.cpp View File

112
 }
112
 }
113
 #endif
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
 void writeDatabase() {
126
 void writeDatabase() {
116
 #ifdef ENABLE_BME280
127
 #ifdef ENABLE_BME280
117
 
128
 

+ 54
- 6
src/lora.cpp View File

22
 
22
 
23
 #include "config.h"
23
 #include "config.h"
24
 #include "DebugLog.h"
24
 #include "DebugLog.h"
25
+#include "influx.h"
25
 #include "lora.h"
26
 #include "lora.h"
26
 
27
 
27
 // define LORA_TEST_TX to periodically transmit a test message
28
 // define LORA_TEST_TX to periodically transmit a test message
31
 
32
 
32
 #ifdef FEATURE_SML
33
 #ifdef FEATURE_SML
33
 #define LORA_LED_BRIGHTNESS 1 // in percent, 50% brightness is plenty for this LED
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
 #else // FEATURE_SML
37
 #else // FEATURE_SML
36
 #define LORA_LED_BRIGHTNESS 25 // in percent, 50% brightness is plenty for this LED
38
 #define LORA_LED_BRIGHTNESS 25 // in percent, 50% brightness is plenty for this LED
37
 #endif // FEATURE_SML
39
 #endif // FEATURE_SML
135
         return false;
137
         return false;
136
     }
138
     }
137
 
139
 
138
-    debug.printf("TX [%lu] ", len);
140
+    debug.printf("TX [%d] (%lu) ", data[0], len);
139
     radio.clearDio1Action();
141
     radio.clearDio1Action();
140
 
142
 
141
     heltec_led(LORA_LED_BRIGHTNESS);
143
     heltec_led(LORA_LED_BRIGHTNESS);
292
 
294
 
293
 #ifdef OLED_BAT_INTERVAL
295
 #ifdef OLED_BAT_INTERVAL
294
     unsigned long time = millis();
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
         last_bat_time = time;
298
         last_bat_time = time;
297
         print_bat();
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
 #endif
305
 #endif
300
 
306
 
314
             debug.printf("  RSSI: %.2f dBm\n", radio.getRSSI());
320
             debug.printf("  RSSI: %.2f dBm\n", radio.getRSSI());
315
             debug.printf("  SNR: %.2f dB\n", radio.getSNR());
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
             for (int i = 0; i < sizeof(data); i++) {
324
             for (int i = 0; i < sizeof(data); i++) {
319
                 debug.printf("%02X", data[i]);
325
                 debug.printf("%02X", data[i]);
320
                 if (i < (sizeof(data) - 1)) {
326
                 if (i < (sizeof(data) - 1)) {
325
             }
331
             }
326
 #endif
332
 #endif
327
 
333
 
334
+#ifdef ENABLE_INFLUXDB_LOGGING
328
             if (data[0] == LORA_SML_BAT_V) {
335
             if (data[0] == LORA_SML_BAT_V) {
329
                 float vbat = NAN;
336
                 float vbat = NAN;
330
                 int percent = -1;
337
                 int percent = -1;
332
                 memcpy(&percent, data + 1 + sizeof(float), sizeof(int));
339
                 memcpy(&percent, data + 1 + sizeof(float), sizeof(int));
333
                 debug.printf("  Vbat: %.2f (%d%%)\n", vbat, percent);
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
             } else {
344
             } else {
337
                 double val = NAN;
345
                 double val = NAN;
338
                 memcpy(&val, data + 1, sizeof(double));
346
                 memcpy(&val, data + 1, sizeof(double));
339
                 debug.printf("  Value: %.2f\n", val);
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
         success = true;
393
         success = true;

Loading…
Cancel
Save