Pārlūkot izejas kodu

Added support for InfluxDB logging of fertilizer and plant watering events

Thomas Buck 3 gadus atpakaļ
vecāks
revīzija
a0082fb343
5 mainītis faili ar 118 papildinājumiem un 5 dzēšanām
  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 Parādīt failu

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

+ 10
- 0
include/config.h Parādīt failu

@@ -56,4 +56,14 @@
56 56
 #define I2C_BUS_SPEED 400000
57 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 69
 #endif // _CONFIG_H_

+ 1
- 0
platformio.ini Parādīt failu

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

+ 62
- 1
src/Statemachine.cpp Parādīt failu

@@ -19,6 +19,7 @@
19 19
 
20 20
 #include "Plants.h"
21 21
 #include "DebugLog.h"
22
+#include "WifiStuff.h"
22 23
 #include "Statemachine.h"
23 24
 #include "config.h"
24 25
 
@@ -200,7 +201,6 @@ void Statemachine::input(int n) {
200 201
                             plants.startPlant(i);
201 202
                         }
202 203
                     }
203
-                    selected_plants.clear();
204 204
                     
205 205
                     selected_time = MAX_AUTO_PLANT_RUNTIME;
206 206
                     start_time = millis();
@@ -623,6 +623,37 @@ void Statemachine::switch_to(States s) {
623 623
               a.c_str(),
624 624
               "Hit any key for menu",
625 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 657
     } else if (s == menu_pumps) {
627 658
         String a = String("(Input 1 to ") + String(plants.countFertilizers()) + String(")");
628 659
         
@@ -671,6 +702,20 @@ void Statemachine::switch_to(States s) {
671 702
               a.c_str(),
672 703
               "Hit any key for menu",
673 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 719
     } else if (s == menu_valves) {
675 720
         String a = String("(Input 1 to ") + String(plants.countPlants() + 1) + String(")");
676 721
         
@@ -719,6 +764,22 @@ void Statemachine::switch_to(States s) {
719 764
               a.c_str(),
720 765
               "Hit any key for menu",
721 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 783
     } else if (s == error) {
723 784
         print("------ Error! ------",
724 785
               "There is a problem:",

+ 43
- 4
src/WifiStuff.cpp Parādīt failu

@@ -56,14 +56,18 @@ String message_buffer_b;
56 56
 String message_buffer_c;
57 57
 String message_buffer_d;
58 58
 
59
-//#define ENABLE_GPIO_TEST
60 59
 #ifdef ENABLE_GPIO_TEST
61
-
62
-#define GPIO_TEST_INTERVAL 4000
63
-#define GPIO_TEST_DELAY 200
64 60
 static bool runningGpioTest = false;
65 61
 static bool gpioTestState = false;
66 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 72
 void runGpioTest(bool state) {
69 73
     lastGpioTime = millis();
@@ -95,6 +99,21 @@ void handleGpioTest() {
95 99
 
96 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 117
 void wifi_set_message_buffer(String a, String b, String c, String d) {
99 118
     message_buffer_a = a;
100 119
     message_buffer_b = b;
@@ -488,6 +507,20 @@ void handleRoot() {
488 507
     
489 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 524
     message += F("<p>Try <a href='/update'>/update</a> for OTA firmware updates!</p>\n");
492 525
     message += F("<p>Made by <a href='https://xythobuz.de'>xythobuz</a></p>\n");
493 526
     message += F("<p><a href='https://git.xythobuz.de/thomas/giess-o-mat'>Project Repository</a></p>\n");
@@ -647,6 +680,12 @@ void wifi_setup() {
647 680
 
648 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 689
     // Setup HTTP Server
651 690
     debug.println("WiFi: initializing HTTP server");
652 691
     MDNS.begin(hostname.c_str());

Notiek ielāde…
Atcelt
Saglabāt