Browse Source

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

Thomas Buck 2 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
         init = 0,
31
         init = 0,
32
         menu, // auto, pumps, valves
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
         auto_fert, // select fertilizer
36
         auto_fert, // select fertilizer
36
         auto_fert_run,
37
         auto_fert_run,
37
         auto_tank_run,
38
         auto_tank_run,
38
         auto_plant, // select plant
39
         auto_plant, // select plant
39
         auto_plant_run,
40
         auto_plant_run,
40
         auto_done,
41
         auto_done,
42
+
43
+        fillnwater_plant, // select plants
44
+        fillnwater_tank_run,
45
+        fillnwater_plant_run,
41
         
46
         
42
         menu_pumps, // selet pump
47
         menu_pumps, // selet pump
43
         menu_pumps_time, // set runtime
48
         menu_pumps_time, // set runtime

+ 1
- 0
platformio.ini View File

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

+ 167
- 32
src/Statemachine.cpp View File

75
 static const char *state_names[] = {
75
 static const char *state_names[] = {
76
     stringify(init),
76
     stringify(init),
77
     stringify(menu),
77
     stringify(menu),
78
-    stringify(auto_mode),
78
+    stringify(auto_mode_a),
79
+    stringify(auto_mode_b),
79
     stringify(auto_fert),
80
     stringify(auto_fert),
80
     stringify(auto_fert_run),
81
     stringify(auto_fert_run),
81
     stringify(auto_tank_run),
82
     stringify(auto_tank_run),
82
     stringify(auto_plant),
83
     stringify(auto_plant),
83
     stringify(auto_plant_run),
84
     stringify(auto_plant_run),
84
     stringify(auto_done),
85
     stringify(auto_done),
86
+    stringify(fillnwater_plant),
87
+    stringify(fillnwater_tank_run),
88
+    stringify(fillnwater_plant_run),
85
     stringify(menu_pumps),
89
     stringify(menu_pumps),
86
     stringify(menu_pumps_time),
90
     stringify(menu_pumps_time),
87
     stringify(menu_pumps_go),
91
     stringify(menu_pumps_go),
123
         switch_to(menu);
127
         switch_to(menu);
124
     } else if (state == menu) {
128
     } else if (state == menu) {
125
         if (n == 1) {
129
         if (n == 1) {
126
-            switch_to(auto_mode);
130
+            switch_to(auto_mode_a);
127
         } else if (n == 2) {
131
         } else if (n == 2) {
128
             switch_to(menu_pumps);
132
             switch_to(menu_pumps);
129
         } else if (n == 3) {
133
         } else if (n == 3) {
131
         } else if ((n == -1) || (n == -2)) {
135
         } else if ((n == -1) || (n == -2)) {
132
             switch_to(init);
136
             switch_to(init);
133
         }
137
         }
134
-    } else if (state == auto_mode) {
138
+    } else if ((state == auto_mode_a) || (state == auto_mode_b)) {
135
         if (n == 1) {
139
         if (n == 1) {
136
             switch_to(auto_fert);
140
             switch_to(auto_fert);
137
         } else if (n == 2) {
141
         } else if (n == 2) {
142
+            selected_plants.clear();
143
+            switch_to(fillnwater_plant);
144
+        } else if (n == 3) {
138
             auto wl = plants.getWaterlevel();
145
             auto wl = plants.getWaterlevel();
139
             if ((wl != Plants::full) && (wl != Plants::invalid)) {
146
             if ((wl != Plants::full) && (wl != Plants::invalid)) {
140
                 plants.openWaterInlet();
147
                 plants.openWaterInlet();
144
                 switch_to(auto_tank_run);
151
                 switch_to(auto_tank_run);
145
             } else if (wl == Plants::full) {
152
             } else if (wl == Plants::full) {
146
                 stop_time = millis();
153
                 stop_time = millis();
147
-                switch_to(auto_mode);
154
+                switch_to(auto_mode_a);
148
             } else if (wl == Plants::invalid) {
155
             } else if (wl == Plants::invalid) {
149
                 error_condition = "Invalid sensor state";
156
                 error_condition = "Invalid sensor state";
150
-                state = auto_mode;
157
+                state = auto_mode_a;
151
                 switch_to(error);
158
                 switch_to(error);
152
             }
159
             }
153
-        } else if (n == 3) {
160
+        } else if (n == 4) {
154
             selected_plants.clear();
161
             selected_plants.clear();
155
             switch_to(auto_plant);
162
             switch_to(auto_plant);
156
-        } else if ((n == -1) || (n == -2)) {
163
+        } else if (n == -1) {
157
             switch_to(menu);
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
     } else if (state == auto_fert) {
168
     } else if (state == auto_fert) {
160
         if ((n >= 1) && (n <= 3)) {
169
         if ((n >= 1) && (n <= 3)) {
167
                 switch_to(auto_fert_run);
176
                 switch_to(auto_fert_run);
168
             } else if (wl == Plants::full) {
177
             } else if (wl == Plants::full) {
169
                 stop_time = millis();
178
                 stop_time = millis();
170
-                switch_to(auto_mode);
179
+                switch_to(auto_mode_a);
171
             } else if (wl == Plants::invalid) {
180
             } else if (wl == Plants::invalid) {
172
                 error_condition = "Invalid sensor state";
181
                 error_condition = "Invalid sensor state";
173
-                state = auto_mode;
182
+                state = auto_mode_a;
174
                 switch_to(error);
183
                 switch_to(error);
175
             }
184
             }
176
         } else if ((n == -1) || (n == -2)) {
185
         } else if ((n == -1) || (n == -2)) {
177
-            switch_to(auto_mode);
186
+            switch_to(auto_mode_a);
178
         }
187
         }
179
     } else if (state == auto_fert_run) {
188
     } else if (state == auto_fert_run) {
180
             plants.abort();
189
             plants.abort();
190
                 backspace();
199
                 backspace();
191
                 db.removeDigit();
200
                 db.removeDigit();
192
             } else {
201
             } else {
193
-                switch_to(auto_mode);
202
+                switch_to(auto_mode_b);
194
             }
203
             }
195
         } else if (n == -2) {
204
         } else if (n == -2) {
196
             if (!db.hasDigits()) {
205
             if (!db.hasDigits()) {
207
                     switch_to(auto_plant_run);
216
                     switch_to(auto_plant_run);
208
                 } else if (wl == Plants::empty) {
217
                 } else if (wl == Plants::empty) {
209
                     stop_time = millis();
218
                     stop_time = millis();
210
-                    switch_to(auto_mode);
219
+                    switch_to(auto_mode_b);
211
                 } else if (wl == Plants::invalid) {
220
                 } else if (wl == Plants::invalid) {
212
                     error_condition = "Invalid sensor state";
221
                     error_condition = "Invalid sensor state";
213
-                    state = auto_mode;
222
+                    state = auto_mode_b;
214
                     switch_to(error);
223
                     switch_to(error);
215
                 }
224
                 }
216
             } else {
225
             } else {
235
         stop_time = millis();
244
         stop_time = millis();
236
         switch_to(auto_done);
245
         switch_to(auto_done);
237
     } else if (state == auto_done) {
246
     } else if (state == auto_done) {
238
-        switch_to(auto_mode);
247
+        switch_to(auto_mode_a);
239
     } else if (state == menu_pumps) {
248
     } else if (state == menu_pumps) {
240
         if (n == -1) {
249
         if (n == -1) {
241
             if (db.hasDigits()) {
250
             if (db.hasDigits()) {
264
                 backspace();
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
     } else if (state == menu_pumps_time) {
371
     } else if (state == menu_pumps_time) {
268
         if (n == -1) {
372
         if (n == -1) {
269
             if (db.hasDigits()) {
373
             if (db.hasDigits()) {
313
             switch_to(menu_pumps_time);
417
             switch_to(menu_pumps_time);
314
         }
418
         }
315
     } else if (state == menu_pumps_run) {
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
     } else if (state == menu_pumps_done) {
423
     } else if (state == menu_pumps_done) {
320
         switch_to(menu);
424
         switch_to(menu);
321
     } else if (state == menu_valves) {
425
     } else if (state == menu_valves) {
477
     }
581
     }
478
 #endif // CHECK_SENSORS_VALVE_PUMP_MENU_EMPTY
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
         unsigned long runtime = millis() - start_time;
585
         unsigned long runtime = millis() - start_time;
482
         if ((runtime / 1000UL) >= selected_time) {
586
         if ((runtime / 1000UL) >= selected_time) {
483
             // stop if timeout has been reached
587
             // stop if timeout has been reached
495
         if (wl == Plants::full) {
599
         if (wl == Plants::full) {
496
             plants.abort();
600
             plants.abort();
497
             stop_time = millis();
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
         } else if (wl == Plants::invalid) {
625
         } else if (wl == Plants::invalid) {
500
             plants.abort();
626
             plants.abort();
501
             error_condition = "Invalid sensor state";
627
             error_condition = "Invalid sensor state";
502
-            state = auto_mode;
628
+            state = auto_mode_a;
503
             switch_to(error);
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
         unsigned long runtime = millis() - start_time;
634
         unsigned long runtime = millis() - start_time;
509
         if ((runtime / 1000UL) >= selected_time) {
635
         if ((runtime / 1000UL) >= selected_time) {
510
             // stop if timeout has been reached
636
             // stop if timeout has been reached
516
             last_animation_time = millis();
642
             last_animation_time = millis();
517
             switch_to(state);
643
             switch_to(state);
518
         }
644
         }
519
-        
645
+
520
         // check water level state
646
         // check water level state
521
         auto wl = plants.getWaterlevel();
647
         auto wl = plants.getWaterlevel();
522
         if (wl == Plants::empty) {
648
         if (wl == Plants::empty) {
526
         } else if (wl == Plants::invalid) {
652
         } else if (wl == Plants::invalid) {
527
             plants.abort();
653
             plants.abort();
528
             error_condition = "Invalid sensor state";
654
             error_condition = "Invalid sensor state";
529
-            state = auto_mode;
655
+            state = auto_mode_a;
530
             switch_to(error);
656
             switch_to(error);
531
         }
657
         }
532
     }
658
     }
550
               "2: Fertilizer pumps",
676
               "2: Fertilizer pumps",
551
               "3: Outlet valves",
677
               "3: Outlet valves",
552
               -1);
678
               -1);
553
-    } else if (s == auto_mode) {
554
-        print("------- Auto -------",
679
+    } else if (s == auto_mode_a) {
680
+        print("----- Auto 1/2 -----",
555
               "1: Add Fertilizer",
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
               -1);
690
               -1);
559
     } else if (s == auto_fert) {
691
     } else if (s == auto_fert) {
560
         print("---- Fertilizer ----",
692
         print("---- Fertilizer ----",
577
               b.c_str(),
709
               b.c_str(),
578
               "Hit any key to stop!",
710
               "Hit any key to stop!",
579
               -1);
711
               -1);
580
-    } else if (s == auto_tank_run) {
712
+    } else if ((s == auto_tank_run) || (s == fillnwater_tank_run)) {
581
         unsigned long runtime = millis() - start_time;
713
         unsigned long runtime = millis() - start_time;
582
         String a = String("Time: ") + String(runtime / 1000UL) + String("s / ") + String(selected_time) + String('s');
714
         String a = String("Time: ") + String(runtime / 1000UL) + String("s / ") + String(selected_time) + String('s');
583
         
715
         
592
               b.c_str(),
724
               b.c_str(),
593
               "Hit any key to stop!",
725
               "Hit any key to stop!",
594
               -1);
726
               -1);
595
-    } else if (s == auto_plant) {
727
+    } else if ((s == auto_plant) || (s == fillnwater_plant)) {
596
         String a = String("(Input 1 to ") + String(plants.countPlants()) + String(")");
728
         String a = String("(Input 1 to ") + String(plants.countPlants()) + String(")");
597
         
729
         
598
         print("--- Select Plant ---",
730
         print("--- Select Plant ---",
600
               a.c_str(),
732
               a.c_str(),
601
               "Plant: ",
733
               "Plant: ",
602
               3);
734
               3);
603
-    } else if (s == auto_plant_run) {
735
+    } else if ((s == auto_plant_run) || (s == fillnwater_plant_run)) {
604
         unsigned long runtime = millis() - start_time;
736
         unsigned long runtime = millis() - start_time;
605
         String a = String("Time: ") + String(runtime / 1000UL) + String("s / ") + String(selected_time) + String('s');
737
         String a = String("Time: ") + String(runtime / 1000UL) + String("s / ") + String(selected_time) + String('s');
606
         
738
         
626
 
758
 
627
 #if defined(PLATFORM_ESP)
759
 #if defined(PLATFORM_ESP)
628
         unsigned long runtime = stop_time - start_time;
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
             for (int i = 0; i < plants.countPlants(); i++) {
762
             for (int i = 0; i < plants.countPlants(); i++) {
631
                 if (selected_plants.isSet(i)) {
763
                 if (selected_plants.isSet(i)) {
632
                     bool success = wifi_write_database(runtime / 1000, "plant", i + 1);
764
                     bool success = wifi_write_database(runtime / 1000, "plant", i + 1);
786
               error_condition.c_str(),
918
               error_condition.c_str(),
787
               "    Press any key...",
919
               "    Press any key...",
788
               -1);
920
               -1);
921
+    } else {
922
+        debug.print("Invalid state ");
923
+        debug.println(s);
789
     }
924
     }
790
 }
925
 }

Loading…
Cancel
Save