Browse Source

Add mode to do filling and watering in one step. Added missing Influx lib to ESP8266 build.

Thomas Buck 3 years ago
parent
commit
e3a0f762a0
3 changed files with 174 additions and 33 deletions
  1. 6
    1
      include/Statemachine.h
  2. 1
    0
      platformio.ini
  3. 167
    32
      src/Statemachine.cpp

+ 6
- 1
include/Statemachine.h View File

@@ -31,13 +31,18 @@ public:
31 31
         init = 0,
32 32
         menu, // auto, pumps, valves
33 33
         
34
-        auto_mode, // select mode
34
+        auto_mode_a, // select mode 1
35
+        auto_mode_b, // select mode 2
35 36
         auto_fert, // select fertilizer
36 37
         auto_fert_run,
37 38
         auto_tank_run,
38 39
         auto_plant, // select plant
39 40
         auto_plant_run,
40 41
         auto_done,
42
+
43
+        fillnwater_plant, // select plants
44
+        fillnwater_tank_run,
45
+        fillnwater_plant_run,
41 46
         
42 47
         menu_pumps, // selet pump
43 48
         menu_pumps_time, // set runtime

+ 1
- 0
platformio.ini View File

@@ -20,6 +20,7 @@ lib_deps =
20 20
     Wire
21 21
     https://github.com/Links2004/arduinoWebSockets
22 22
     https://github.com/rlogiacco/CircularBuffer
23
+    https://github.com/tobiasschuerg/InfluxDB-Client-for-Arduino.git
23 24
 
24 25
 [env:esp32_main]
25 26
 platform = espressif32

+ 167
- 32
src/Statemachine.cpp View File

@@ -75,13 +75,17 @@ uint32_t Statemachine::DigitBuffer::getNumber(void) {
75 75
 static const char *state_names[] = {
76 76
     stringify(init),
77 77
     stringify(menu),
78
-    stringify(auto_mode),
78
+    stringify(auto_mode_a),
79
+    stringify(auto_mode_b),
79 80
     stringify(auto_fert),
80 81
     stringify(auto_fert_run),
81 82
     stringify(auto_tank_run),
82 83
     stringify(auto_plant),
83 84
     stringify(auto_plant_run),
84 85
     stringify(auto_done),
86
+    stringify(fillnwater_plant),
87
+    stringify(fillnwater_tank_run),
88
+    stringify(fillnwater_plant_run),
85 89
     stringify(menu_pumps),
86 90
     stringify(menu_pumps_time),
87 91
     stringify(menu_pumps_go),
@@ -123,7 +127,7 @@ void Statemachine::input(int n) {
123 127
         switch_to(menu);
124 128
     } else if (state == menu) {
125 129
         if (n == 1) {
126
-            switch_to(auto_mode);
130
+            switch_to(auto_mode_a);
127 131
         } else if (n == 2) {
128 132
             switch_to(menu_pumps);
129 133
         } else if (n == 3) {
@@ -131,10 +135,13 @@ void Statemachine::input(int n) {
131 135
         } else if ((n == -1) || (n == -2)) {
132 136
             switch_to(init);
133 137
         }
134
-    } else if (state == auto_mode) {
138
+    } else if ((state == auto_mode_a) || (state == auto_mode_b)) {
135 139
         if (n == 1) {
136 140
             switch_to(auto_fert);
137 141
         } else if (n == 2) {
142
+            selected_plants.clear();
143
+            switch_to(fillnwater_plant);
144
+        } else if (n == 3) {
138 145
             auto wl = plants.getWaterlevel();
139 146
             if ((wl != Plants::full) && (wl != Plants::invalid)) {
140 147
                 plants.openWaterInlet();
@@ -144,17 +151,19 @@ void Statemachine::input(int n) {
144 151
                 switch_to(auto_tank_run);
145 152
             } else if (wl == Plants::full) {
146 153
                 stop_time = millis();
147
-                switch_to(auto_mode);
154
+                switch_to(auto_mode_a);
148 155
             } else if (wl == Plants::invalid) {
149 156
                 error_condition = "Invalid sensor state";
150
-                state = auto_mode;
157
+                state = auto_mode_a;
151 158
                 switch_to(error);
152 159
             }
153
-        } else if (n == 3) {
160
+        } else if (n == 4) {
154 161
             selected_plants.clear();
155 162
             switch_to(auto_plant);
156
-        } else if ((n == -1) || (n == -2)) {
163
+        } else if (n == -1) {
157 164
             switch_to(menu);
165
+        } else if (n == -2) {
166
+            switch_to((state == auto_mode_a) ? auto_mode_b : auto_mode_a);
158 167
         }
159 168
     } else if (state == auto_fert) {
160 169
         if ((n >= 1) && (n <= 3)) {
@@ -167,14 +176,14 @@ void Statemachine::input(int n) {
167 176
                 switch_to(auto_fert_run);
168 177
             } else if (wl == Plants::full) {
169 178
                 stop_time = millis();
170
-                switch_to(auto_mode);
179
+                switch_to(auto_mode_a);
171 180
             } else if (wl == Plants::invalid) {
172 181
                 error_condition = "Invalid sensor state";
173
-                state = auto_mode;
182
+                state = auto_mode_a;
174 183
                 switch_to(error);
175 184
             }
176 185
         } else if ((n == -1) || (n == -2)) {
177
-            switch_to(auto_mode);
186
+            switch_to(auto_mode_a);
178 187
         }
179 188
     } else if (state == auto_fert_run) {
180 189
             plants.abort();
@@ -190,7 +199,7 @@ void Statemachine::input(int n) {
190 199
                 backspace();
191 200
                 db.removeDigit();
192 201
             } else {
193
-                switch_to(auto_mode);
202
+                switch_to(auto_mode_b);
194 203
             }
195 204
         } else if (n == -2) {
196 205
             if (!db.hasDigits()) {
@@ -207,10 +216,10 @@ void Statemachine::input(int n) {
207 216
                     switch_to(auto_plant_run);
208 217
                 } else if (wl == Plants::empty) {
209 218
                     stop_time = millis();
210
-                    switch_to(auto_mode);
219
+                    switch_to(auto_mode_b);
211 220
                 } else if (wl == Plants::invalid) {
212 221
                     error_condition = "Invalid sensor state";
213
-                    state = auto_mode;
222
+                    state = auto_mode_b;
214 223
                     switch_to(error);
215 224
                 }
216 225
             } else {
@@ -235,7 +244,7 @@ void Statemachine::input(int n) {
235 244
         stop_time = millis();
236 245
         switch_to(auto_done);
237 246
     } else if (state == auto_done) {
238
-        switch_to(auto_mode);
247
+        switch_to(auto_mode_a);
239 248
     } else if (state == menu_pumps) {
240 249
         if (n == -1) {
241 250
             if (db.hasDigits()) {
@@ -264,6 +273,101 @@ void Statemachine::input(int n) {
264 273
                 backspace();
265 274
             }
266 275
         }
276
+    } else if (state == fillnwater_plant) {
277
+        if (n == -1) {
278
+            if (db.hasDigits()) {
279
+                backspace();
280
+                db.removeDigit();
281
+            } else {
282
+                switch_to(auto_mode_a);
283
+            }
284
+        } else if (n == -2) {
285
+            if (!db.hasDigits()) {
286
+                int found = 0;
287
+                for (int i = 0; i < plants.countPlants(); i++) {
288
+                    if (selected_plants.isSet(i)) {
289
+                        found = 1;
290
+                        break;
291
+                    }
292
+                }
293
+                if (found != 0) {
294
+                    auto wl = plants.getWaterlevel();
295
+                    if ((wl != Plants::full) && (wl != Plants::invalid)) {
296
+                        plants.openWaterInlet();
297
+                        selected_id = plants.countPlants() + 1;
298
+                        selected_time = MAX_TANK_FILL_TIME;
299
+                        start_time = millis();
300
+                        switch_to(fillnwater_tank_run);
301
+                    } else if (wl == Plants::full) {
302
+                        stop_time = millis();
303
+                        auto wl = plants.getWaterlevel();
304
+                        if ((wl != Plants::empty) && (wl != Plants::invalid)) {
305
+                            for (int i = 0; i < plants.countPlants(); i++) {
306
+                                if (selected_plants.isSet(i)) {
307
+                                    plants.startPlant(i);
308
+                                }
309
+                            }
310
+
311
+                            selected_time = MAX_AUTO_PLANT_RUNTIME;
312
+                            start_time = millis();
313
+                            switch_to(fillnwater_plant_run);
314
+                        } else if (wl == Plants::empty) {
315
+                            stop_time = millis();
316
+                            switch_to(auto_mode_a);
317
+                        } else if (wl == Plants::invalid) {
318
+                            error_condition = "Invalid sensor state";
319
+                            state = auto_mode_a;
320
+                            switch_to(error);
321
+                        }
322
+                    } else if (wl == Plants::invalid) {
323
+                        error_condition = "Invalid sensor state";
324
+                        state = auto_mode_a;
325
+                        switch_to(error);
326
+                    }
327
+                }
328
+            } else {
329
+                selected_id = number_input();
330
+                if ((selected_id <= 0) || (selected_id > plants.countPlants())) {
331
+                    error_condition = "Invalid plant ID!";
332
+                    switch_to(error);
333
+                } else {
334
+                    selected_plants.set(selected_id - 1);
335
+                    switch_to(fillnwater_plant);
336
+                }
337
+            }
338
+        } else {
339
+            if (db.spaceLeft()) {
340
+                db.addDigit(n);
341
+            } else {
342
+                backspace();
343
+            }
344
+        }
345
+    } else if (state == fillnwater_tank_run) {
346
+        plants.abort();
347
+        stop_time = millis();
348
+        auto wl = plants.getWaterlevel();
349
+        if ((wl != Plants::empty) && (wl != Plants::invalid)) {
350
+            for (int i = 0; i < plants.countPlants(); i++) {
351
+                if (selected_plants.isSet(i)) {
352
+                    plants.startPlant(i);
353
+                }
354
+            }
355
+
356
+            selected_time = MAX_AUTO_PLANT_RUNTIME;
357
+            start_time = millis();
358
+            switch_to(fillnwater_plant_run);
359
+        } else if (wl == Plants::empty) {
360
+            stop_time = millis();
361
+            switch_to(auto_mode_a);
362
+        } else if (wl == Plants::invalid) {
363
+            error_condition = "Invalid sensor state";
364
+            state = auto_mode_a;
365
+            switch_to(error);
366
+        }
367
+    } else if (state == fillnwater_plant_run) {
368
+        plants.abort();
369
+        stop_time = millis();
370
+        switch_to(auto_done);
267 371
     } else if (state == menu_pumps_time) {
268 372
         if (n == -1) {
269 373
             if (db.hasDigits()) {
@@ -313,9 +417,9 @@ void Statemachine::input(int n) {
313 417
             switch_to(menu_pumps_time);
314 418
         }
315 419
     } else if (state == menu_pumps_run) {
316
-            plants.abort();
317
-            stop_time = millis();
318
-            switch_to(menu_pumps_done);
420
+        plants.abort();
421
+        stop_time = millis();
422
+        switch_to(menu_pumps_done);
319 423
     } else if (state == menu_pumps_done) {
320 424
         switch_to(menu);
321 425
     } else if (state == menu_valves) {
@@ -477,7 +581,7 @@ void Statemachine::act(void) {
477 581
     }
478 582
 #endif // CHECK_SENSORS_VALVE_PUMP_MENU_EMPTY
479 583
     
480
-    if ((state == auto_fert_run) || (state == auto_tank_run)) {
584
+    if ((state == auto_fert_run) || (state == auto_tank_run) || (state == fillnwater_tank_run)) {
481 585
         unsigned long runtime = millis() - start_time;
482 586
         if ((runtime / 1000UL) >= selected_time) {
483 587
             // stop if timeout has been reached
@@ -495,16 +599,38 @@ void Statemachine::act(void) {
495 599
         if (wl == Plants::full) {
496 600
             plants.abort();
497 601
             stop_time = millis();
498
-            switch_to(auto_done);
602
+            if (state == fillnwater_tank_run) {
603
+                auto wl = plants.getWaterlevel();
604
+                if ((wl != Plants::empty) && (wl != Plants::invalid)) {
605
+                    for (int i = 0; i < plants.countPlants(); i++) {
606
+                        if (selected_plants.isSet(i)) {
607
+                            plants.startPlant(i);
608
+                        }
609
+                    }
610
+
611
+                    selected_time = MAX_AUTO_PLANT_RUNTIME;
612
+                    start_time = millis();
613
+                    switch_to(fillnwater_plant_run);
614
+                } else if (wl == Plants::empty) {
615
+                    stop_time = millis();
616
+                    switch_to(auto_mode_a);
617
+                } else if (wl == Plants::invalid) {
618
+                    error_condition = "Invalid sensor state";
619
+                    state = auto_mode_a;
620
+                    switch_to(error);
621
+                }
622
+            } else {
623
+                switch_to(auto_done);
624
+            }
499 625
         } else if (wl == Plants::invalid) {
500 626
             plants.abort();
501 627
             error_condition = "Invalid sensor state";
502
-            state = auto_mode;
628
+            state = auto_mode_a;
503 629
             switch_to(error);
504 630
         }
505 631
     }
506
-        
507
-    if (state == auto_plant_run) {
632
+
633
+    if ((state == auto_plant_run) || (state == fillnwater_plant_run)) {
508 634
         unsigned long runtime = millis() - start_time;
509 635
         if ((runtime / 1000UL) >= selected_time) {
510 636
             // stop if timeout has been reached
@@ -516,7 +642,7 @@ void Statemachine::act(void) {
516 642
             last_animation_time = millis();
517 643
             switch_to(state);
518 644
         }
519
-        
645
+
520 646
         // check water level state
521 647
         auto wl = plants.getWaterlevel();
522 648
         if (wl == Plants::empty) {
@@ -526,7 +652,7 @@ void Statemachine::act(void) {
526 652
         } else if (wl == Plants::invalid) {
527 653
             plants.abort();
528 654
             error_condition = "Invalid sensor state";
529
-            state = auto_mode;
655
+            state = auto_mode_a;
530 656
             switch_to(error);
531 657
         }
532 658
     }
@@ -550,11 +676,17 @@ void Statemachine::switch_to(States s) {
550 676
               "2: Fertilizer pumps",
551 677
               "3: Outlet valves",
552 678
               -1);
553
-    } else if (s == auto_mode) {
554
-        print("------- Auto -------",
679
+    } else if (s == auto_mode_a) {
680
+        print("----- Auto 1/2 -----",
555 681
               "1: Add Fertilizer",
556
-              "2: Fill Reservoir",
557
-              "3: Water a plant",
682
+              "2: Fill 'n' Water",
683
+              "#: Go to page 2/2...",
684
+              -1);
685
+    } else if (s == auto_mode_b) {
686
+        print("----- Auto 2/2 -----",
687
+              "3: Fill Reservoir",
688
+              "4: Water a plant",
689
+              "#: Go to page 1/2...",
558 690
               -1);
559 691
     } else if (s == auto_fert) {
560 692
         print("---- Fertilizer ----",
@@ -577,7 +709,7 @@ void Statemachine::switch_to(States s) {
577 709
               b.c_str(),
578 710
               "Hit any key to stop!",
579 711
               -1);
580
-    } else if (s == auto_tank_run) {
712
+    } else if ((s == auto_tank_run) || (s == fillnwater_tank_run)) {
581 713
         unsigned long runtime = millis() - start_time;
582 714
         String a = String("Time: ") + String(runtime / 1000UL) + String("s / ") + String(selected_time) + String('s');
583 715
         
@@ -592,7 +724,7 @@ void Statemachine::switch_to(States s) {
592 724
               b.c_str(),
593 725
               "Hit any key to stop!",
594 726
               -1);
595
-    } else if (s == auto_plant) {
727
+    } else if ((s == auto_plant) || (s == fillnwater_plant)) {
596 728
         String a = String("(Input 1 to ") + String(plants.countPlants()) + String(")");
597 729
         
598 730
         print("--- Select Plant ---",
@@ -600,7 +732,7 @@ void Statemachine::switch_to(States s) {
600 732
               a.c_str(),
601 733
               "Plant: ",
602 734
               3);
603
-    } else if (s == auto_plant_run) {
735
+    } else if ((s == auto_plant_run) || (s == fillnwater_plant_run)) {
604 736
         unsigned long runtime = millis() - start_time;
605 737
         String a = String("Time: ") + String(runtime / 1000UL) + String("s / ") + String(selected_time) + String('s');
606 738
         
@@ -626,7 +758,7 @@ void Statemachine::switch_to(States s) {
626 758
 
627 759
 #if defined(PLATFORM_ESP)
628 760
         unsigned long runtime = stop_time - start_time;
629
-        if (old_state == auto_plant_run) {
761
+        if ((old_state == auto_plant_run) || (old_state == fillnwater_plant_run)) {
630 762
             for (int i = 0; i < plants.countPlants(); i++) {
631 763
                 if (selected_plants.isSet(i)) {
632 764
                     bool success = wifi_write_database(runtime / 1000, "plant", i + 1);
@@ -786,5 +918,8 @@ void Statemachine::switch_to(States s) {
786 918
               error_condition.c_str(),
787 919
               "    Press any key...",
788 920
               -1);
921
+    } else {
922
+        debug.print("Invalid state ");
923
+        debug.println(s);
789 924
     }
790 925
 }

Loading…
Cancel
Save