Browse Source

Added support for InfluxDB logging of fertilizer and plant watering events

Thomas Buck 2 years ago
parent
commit
a0082fb343
5 changed files with 118 additions and 5 deletions
  1. 2
    0
      include/WifiStuff.h
  2. 10
    0
      include/config.h
  3. 1
    0
      platformio.ini
  4. 62
    1
      src/Statemachine.cpp
  5. 43
    4
      src/WifiStuff.cpp

+ 2
- 0
include/WifiStuff.h View File

31
 
31
 
32
 void wifi_send_websocket(String s);
32
 void wifi_send_websocket(String s);
33
 
33
 
34
+bool wifi_write_database(int duration, const char *type, int id);
35
+
34
 #endif
36
 #endif
35
 
37
 
36
 #endif // _WIFI_STUFF_H_
38
 #endif // _WIFI_STUFF_H_

+ 10
- 0
include/config.h View File

56
 #define I2C_BUS_SPEED 400000
56
 #define I2C_BUS_SPEED 400000
57
 #define I2C_BUF_SIZE 32
57
 #define I2C_BUF_SIZE 32
58
 
58
 
59
+//#define ENABLE_GPIO_TEST
60
+#define GPIO_TEST_INTERVAL 4000
61
+#define GPIO_TEST_DELAY 200
62
+
63
+// InfluxDB settings
64
+#define ENABLE_INFLUXDB_LOGGING
65
+#define INFLUXDB_HOST "10.23.42.14"
66
+#define INFLUXDB_PORT 8086
67
+#define INFLUXDB_DATABASE "giessomat"
68
+
59
 #endif // _CONFIG_H_
69
 #endif // _CONFIG_H_

+ 1
- 0
platformio.ini View File

32
     Wire
32
     Wire
33
     https://github.com/Links2004/arduinoWebSockets
33
     https://github.com/Links2004/arduinoWebSockets
34
     https://github.com/rlogiacco/CircularBuffer
34
     https://github.com/rlogiacco/CircularBuffer
35
+    https://github.com/tobiasschuerg/InfluxDB-Client-for-Arduino.git
35
 
36
 
36
 [env:arduino_ui]
37
 [env:arduino_ui]
37
 platform = atmelavr
38
 platform = atmelavr

+ 62
- 1
src/Statemachine.cpp View File

19
 
19
 
20
 #include "Plants.h"
20
 #include "Plants.h"
21
 #include "DebugLog.h"
21
 #include "DebugLog.h"
22
+#include "WifiStuff.h"
22
 #include "Statemachine.h"
23
 #include "Statemachine.h"
23
 #include "config.h"
24
 #include "config.h"
24
 
25
 
200
                             plants.startPlant(i);
201
                             plants.startPlant(i);
201
                         }
202
                         }
202
                     }
203
                     }
203
-                    selected_plants.clear();
204
                     
204
                     
205
                     selected_time = MAX_AUTO_PLANT_RUNTIME;
205
                     selected_time = MAX_AUTO_PLANT_RUNTIME;
206
                     start_time = millis();
206
                     start_time = millis();
623
               a.c_str(),
623
               a.c_str(),
624
               "Hit any key for menu",
624
               "Hit any key for menu",
625
               -1);
625
               -1);
626
+
627
+#if defined(PLATFORM_ESP)
628
+        unsigned long runtime = stop_time - start_time;
629
+        if (old_state == auto_plant_run) {
630
+            for (int i = 0; i < plants.countPlants(); i++) {
631
+                if (selected_plants.isSet(i)) {
632
+                    bool success = wifi_write_database(runtime / 1000, "plant", i + 1);
633
+                    if (!success) {
634
+                        debug.print("Error writing to InfluxDB ");
635
+                        debug.print(INFLUXDB_HOST);
636
+                        debug.print(":");
637
+                        debug.print(INFLUXDB_PORT);
638
+                        debug.print("/");
639
+                        debug.print(INFLUXDB_DATABASE);
640
+                        debug.println("/plant");
641
+                    }
642
+                }
643
+            }
644
+        } else if (old_state == auto_fert_run) {
645
+            bool success = wifi_write_database(runtime / 1000, "fertilizer", selected_id);
646
+            if (!success) {
647
+                debug.print("Error writing to InfluxDB ");
648
+                debug.print(INFLUXDB_HOST);
649
+                debug.print(":");
650
+                debug.print(INFLUXDB_PORT);
651
+                debug.print("/");
652
+                debug.print(INFLUXDB_DATABASE);
653
+                debug.println("/fertilizer");
654
+            }
655
+        }
656
+#endif // PLATFORM_ESP
626
     } else if (s == menu_pumps) {
657
     } else if (s == menu_pumps) {
627
         String a = String("(Input 1 to ") + String(plants.countFertilizers()) + String(")");
658
         String a = String("(Input 1 to ") + String(plants.countFertilizers()) + String(")");
628
         
659
         
671
               a.c_str(),
702
               a.c_str(),
672
               "Hit any key for menu",
703
               "Hit any key for menu",
673
               -1);
704
               -1);
705
+
706
+#if defined(PLATFORM_ESP)
707
+        unsigned long runtime = stop_time - start_time;
708
+        bool success = wifi_write_database(runtime / 1000, "fertilizer", selected_id);
709
+        if (!success) {
710
+            debug.print("Error writing to InfluxDB ");
711
+            debug.print(INFLUXDB_HOST);
712
+            debug.print(":");
713
+            debug.print(INFLUXDB_PORT);
714
+            debug.print("/");
715
+            debug.print(INFLUXDB_DATABASE);
716
+            debug.println("/fertilizer");
717
+        }
718
+#endif // PLATFORM_ESP
674
     } else if (s == menu_valves) {
719
     } else if (s == menu_valves) {
675
         String a = String("(Input 1 to ") + String(plants.countPlants() + 1) + String(")");
720
         String a = String("(Input 1 to ") + String(plants.countPlants() + 1) + String(")");
676
         
721
         
719
               a.c_str(),
764
               a.c_str(),
720
               "Hit any key for menu",
765
               "Hit any key for menu",
721
               -1);
766
               -1);
767
+
768
+#if defined(PLATFORM_ESP)
769
+        unsigned long runtime = stop_time - start_time;
770
+        if (selected_id <= plants.countPlants()) {
771
+            bool success = wifi_write_database(runtime / 1000, "plant", selected_id);
772
+            if (!success) {
773
+                debug.print("Error writing to InfluxDB ");
774
+                debug.print(INFLUXDB_HOST);
775
+                debug.print(":");
776
+                debug.print(INFLUXDB_PORT);
777
+                debug.print("/");
778
+                debug.print(INFLUXDB_DATABASE);
779
+                debug.println("/plant");
780
+            }
781
+        }
782
+#endif // PLATFORM_ESP
722
     } else if (s == error) {
783
     } else if (s == error) {
723
         print("------ Error! ------",
784
         print("------ Error! ------",
724
               "There is a problem:",
785
               "There is a problem:",

+ 43
- 4
src/WifiStuff.cpp View File

56
 String message_buffer_c;
56
 String message_buffer_c;
57
 String message_buffer_d;
57
 String message_buffer_d;
58
 
58
 
59
-//#define ENABLE_GPIO_TEST
60
 #ifdef ENABLE_GPIO_TEST
59
 #ifdef ENABLE_GPIO_TEST
61
-
62
-#define GPIO_TEST_INTERVAL 4000
63
-#define GPIO_TEST_DELAY 200
64
 static bool runningGpioTest = false;
60
 static bool runningGpioTest = false;
65
 static bool gpioTestState = false;
61
 static bool gpioTestState = false;
66
 unsigned long lastGpioTime = 0;
62
 unsigned long lastGpioTime = 0;
63
+#endif // ENABLE_GPIO_TEST
64
+
65
+#ifdef ENABLE_INFLUXDB_LOGGING
66
+#include <InfluxDb.h>
67
+Influxdb influx(INFLUXDB_HOST, INFLUXDB_PORT);
68
+#endif // ENABLE_INFLUXDB_LOGGING
69
+
70
+#ifdef ENABLE_GPIO_TEST
67
 
71
 
68
 void runGpioTest(bool state) {
72
 void runGpioTest(bool state) {
69
     lastGpioTime = millis();
73
     lastGpioTime = millis();
95
 
99
 
96
 #endif // ENABLE_GPIO_TEST
100
 #endif // ENABLE_GPIO_TEST
97
 
101
 
102
+bool wifi_write_database(int duration, const char *type, int id) {
103
+    bool success = false;
104
+
105
+#ifdef ENABLE_INFLUXDB_LOGGING
106
+    InfluxData measurement(type);
107
+    measurement.addTag("version", FIRMWARE_VERSION);
108
+    measurement.addTag("device", WiFi.macAddress());
109
+    measurement.addTag("id", String(id));
110
+    measurement.addValue("duration", duration);
111
+    success = influx.write(measurement);
112
+#endif // ENABLE_INFLUXDB_LOGGING
113
+
114
+    return success;
115
+}
116
+
98
 void wifi_set_message_buffer(String a, String b, String c, String d) {
117
 void wifi_set_message_buffer(String a, String b, String c, String d) {
99
     message_buffer_a = a;
118
     message_buffer_a = a;
100
     message_buffer_b = b;
119
     message_buffer_b = b;
488
     
507
     
489
 #endif
508
 #endif
490
 
509
 
510
+    message += F("<p>\n");
511
+#ifdef ENABLE_INFLUXDB_LOGGING
512
+    message += F("InfluxDB: ");
513
+    message += INFLUXDB_DATABASE;
514
+    message += F(" @ ");
515
+    message += INFLUXDB_HOST;
516
+    message += F(":");
517
+    message += String(INFLUXDB_PORT);
518
+    message += F("\n");
519
+#else
520
+    message += F("InfluxDB logging not enabled!\n");
521
+#endif
522
+    message += F("</p>\n");
523
+
491
     message += F("<p>Try <a href='/update'>/update</a> for OTA firmware updates!</p>\n");
524
     message += F("<p>Try <a href='/update'>/update</a> for OTA firmware updates!</p>\n");
492
     message += F("<p>Made by <a href='https://xythobuz.de'>xythobuz</a></p>\n");
525
     message += F("<p>Made by <a href='https://xythobuz.de'>xythobuz</a></p>\n");
493
     message += F("<p><a href='https://git.xythobuz.de/thomas/giess-o-mat'>Project Repository</a></p>\n");
526
     message += F("<p><a href='https://git.xythobuz.de/thomas/giess-o-mat'>Project Repository</a></p>\n");
647
 
680
 
648
 #endif
681
 #endif
649
 
682
 
683
+#ifdef ENABLE_INFLUXDB_LOGGING
684
+    // Setup InfluxDB Client
685
+    debug.println("WiFi: set InfluxDB database");
686
+    influx.setDb(INFLUXDB_DATABASE);
687
+#endif // ENABLE_INFLUXDB_LOGGING
688
+
650
     // Setup HTTP Server
689
     // Setup HTTP Server
651
     debug.println("WiFi: initializing HTTP server");
690
     debug.println("WiFi: initializing HTTP server");
652
     MDNS.begin(hostname.c_str());
691
     MDNS.begin(hostname.c_str());

Loading…
Cancel
Save