DIY fertilizer mixer and plant watering machine https://www.xythobuz.de/giessomat.html
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Statemachine.cpp 53KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527
  1. /*
  2. * Copyright (c) 2021 Thomas Buck <thomas@xythobuz.de>
  3. *
  4. * This file is part of Giess-o-mat.
  5. *
  6. * Giess-o-mat is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Giess-o-mat is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Giess-o-mat. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. #include "Plants.h"
  20. #include "DebugLog.h"
  21. #include "WifiStuff.h"
  22. #include "Statemachine.h"
  23. #include "config.h"
  24. #include "config_pins.h"
  25. #ifdef FUNCTION_CONTROL
  26. Statemachine::DigitBuffer::DigitBuffer(int _size) {
  27. size = _size;
  28. pos = 0;
  29. digits = new int[size];
  30. }
  31. Statemachine::DigitBuffer::~DigitBuffer() {
  32. delete digits;
  33. }
  34. bool Statemachine::DigitBuffer::spaceLeft(void) {
  35. return (pos < size);
  36. }
  37. bool Statemachine::DigitBuffer::hasDigits(void) {
  38. return (pos > 0);
  39. }
  40. int Statemachine::DigitBuffer::countDigits(void) {
  41. return pos;
  42. }
  43. void Statemachine::DigitBuffer::addDigit(int d) {
  44. if (spaceLeft()) {
  45. digits[pos] = d;
  46. pos++;
  47. }
  48. }
  49. void Statemachine::DigitBuffer::removeDigit(void) {
  50. if (hasDigits()) {
  51. pos--;
  52. }
  53. }
  54. void Statemachine::DigitBuffer::clear(void) {
  55. pos = 0;
  56. }
  57. uint32_t Statemachine::DigitBuffer::getNumber(void) {
  58. uint32_t fact = 1;
  59. uint32_t sum = 0;
  60. for (int i = (pos - 1); i >= 0; i--) {
  61. sum += digits[i] * fact;
  62. fact *= 10;
  63. }
  64. return sum;
  65. }
  66. static const char *state_names[] = {
  67. stringify(init),
  68. stringify(door_select),
  69. stringify(menu_a),
  70. stringify(menu_b),
  71. stringify(menu_c),
  72. stringify(auto_mode_a),
  73. stringify(auto_mode_b),
  74. stringify(auto_fert_a),
  75. stringify(auto_fert_b),
  76. stringify(auto_fert_run),
  77. stringify(auto_tank_run),
  78. stringify(auto_stirr_run),
  79. stringify(auto_plant),
  80. stringify(auto_plant_kickstart_run),
  81. stringify(auto_plant_run),
  82. stringify(auto_done),
  83. stringify(fillnwater_plant),
  84. stringify(fillnwater_tank_run),
  85. stringify(fillnwater_kickstart_run),
  86. stringify(fillnwater_plant_run),
  87. stringify(automation_mode),
  88. stringify(menu_pumps),
  89. stringify(menu_pumps_time),
  90. stringify(menu_pumps_go),
  91. stringify(menu_pumps_run),
  92. stringify(menu_pumps_done),
  93. stringify(menu_valves),
  94. stringify(menu_valves_time),
  95. stringify(menu_valves_go),
  96. stringify(menu_valves_run),
  97. stringify(menu_valves_done),
  98. stringify(menu_aux),
  99. stringify(menu_aux_time),
  100. stringify(menu_aux_go),
  101. stringify(menu_aux_run),
  102. stringify(menu_aux_done),
  103. stringify(error)
  104. };
  105. const char *Statemachine::getStateName(void) {
  106. return state_names[state];
  107. }
  108. bool Statemachine::isIdle(void) {
  109. return state == init;
  110. }
  111. Statemachine::Statemachine(print_fn _print, backspace_fn _backspace)
  112. : db(7), selected_plants(plants.countPlants()) {
  113. state = init;
  114. old_state = init;
  115. print = _print;
  116. backspace = _backspace;
  117. selected_id = 0;
  118. selected_time = 0;
  119. start_time = 0;
  120. stop_time = 0;
  121. last_animation_time = 0;
  122. error_condition = "";
  123. into_state_time = 0;
  124. filling_started_empty = false;
  125. watering_started_full = false;
  126. menu_entered_digits = "";
  127. }
  128. void Statemachine::begin(void) {
  129. switch_to(init);
  130. }
  131. void Statemachine::input(int n) {
  132. if (state == init) {
  133. #if (LOCK_COUNT > 0) && defined(DOOR_LOCK_PIN)
  134. if (n == -1) {
  135. if (db.hasDigits()) {
  136. backspace();
  137. db.removeDigit();
  138. if (menu_entered_digits.length() > 0) {
  139. menu_entered_digits.remove(menu_entered_digits.length() - 1);
  140. switch_to(state);
  141. }
  142. } else {
  143. switch_to(menu_a);
  144. }
  145. } else if (n == -2) {
  146. switch_to(menu_a);
  147. } else {
  148. if (db.spaceLeft()) {
  149. db.addDigit(n);
  150. //menu_entered_digits += String(n);
  151. menu_entered_digits += String("*");
  152. switch_to(state);
  153. } else {
  154. backspace();
  155. }
  156. uint32_t n = db.getNumber();
  157. if (n == DOOR_LOCK_PIN) {
  158. db.clear();
  159. menu_entered_digits = "";
  160. selected_plants.clear();
  161. switch_to(door_select);
  162. } else if (db.countDigits() >= DOOR_LOCK_PIN_MAX_DIGITS) {
  163. db.clear();
  164. menu_entered_digits = "";
  165. switch_to(state);
  166. }
  167. }
  168. #else
  169. switch_to(menu_a);
  170. #endif
  171. } else if (state == door_select) {
  172. #if (LOCK_COUNT > 0)
  173. if (n == -1) {
  174. if (db.hasDigits()) {
  175. backspace();
  176. db.removeDigit();
  177. if (menu_entered_digits.length() > 0) {
  178. menu_entered_digits.remove(menu_entered_digits.length() - 1);
  179. switch_to(state);
  180. }
  181. } else {
  182. switch_to(init);
  183. }
  184. } else if (n == -2) {
  185. if (!db.hasDigits()) {
  186. for (int i = 0; i < LOCK_COUNT; i++) {
  187. if (selected_plants.isSet(i)) {
  188. plants.startAux(STIRRER_COUNT + i);
  189. delay(DOOR_LOCK_ON_TIME);
  190. plants.stopAux(STIRRER_COUNT + i);
  191. delay(DOOR_LOCK_NEXT_DELAY);
  192. }
  193. }
  194. plants.stopAllAux();
  195. switch_to(menu_a);
  196. } else {
  197. selected_id = number_input();
  198. if ((selected_id <= 0) || (selected_id > LOCK_COUNT)) {
  199. error_condition = F("Invalid lock ID!");
  200. switch_to(error);
  201. } else {
  202. selected_plants.set(selected_id - 1);
  203. menu_entered_digits = "";
  204. switch_to(state);
  205. }
  206. }
  207. } else {
  208. if (db.spaceLeft()) {
  209. db.addDigit(n);
  210. menu_entered_digits += String(n);
  211. switch_to(state);
  212. } else {
  213. backspace();
  214. }
  215. }
  216. #else
  217. // should never be reached
  218. switch_to(menu_a);
  219. #endif
  220. } else if ((state == menu_a) || (state == menu_b) || (state == menu_c)) {
  221. if (n == 1) {
  222. switch_to(auto_mode_a);
  223. } else if (n == 2) {
  224. switch_to(automation_mode);
  225. } else if (n == 3) {
  226. switch_to(menu_pumps);
  227. } else if (n == 4) {
  228. switch_to(menu_valves);
  229. } else if (n == 5) {
  230. switch_to(menu_aux);
  231. #if (LOCK_COUNT > 0) && !defined(DOOR_LOCK_PIN)
  232. } else if (n == 6) {
  233. switch_to(door_select);
  234. #endif
  235. } else if (n == -1) {
  236. switch_to(init);
  237. } else if (n == -2) {
  238. switch_to((state == menu_a) ? menu_b : ((state == menu_b) ? menu_c : menu_a));
  239. }
  240. } else if (state == automation_mode) {
  241. // TODO
  242. switch_to(menu_a);
  243. } else if ((state == auto_mode_a) || (state == auto_mode_b)) {
  244. if (n == 1) {
  245. switch_to(auto_fert_a);
  246. } else if (n == 2) {
  247. selected_plants.clear();
  248. switch_to(fillnwater_plant);
  249. } else if (n == 3) {
  250. auto wl = plants.getWaterlevel();
  251. if ((wl != Plants::full) && (wl != Plants::invalid)) {
  252. plants.openWaterInlet();
  253. selected_id = plants.countPlants() + 1;
  254. selected_time = MAX_TANK_FILL_TIME;
  255. start_time = millis();
  256. switch_to(auto_tank_run);
  257. } else if (wl == Plants::full) {
  258. stop_time = millis();
  259. switch_to(auto_mode_a);
  260. } else if (wl == Plants::invalid) {
  261. error_condition = F("Invalid sensor state");
  262. state = auto_mode_a;
  263. switch_to(error);
  264. }
  265. } else if (n == 4) {
  266. selected_plants.clear();
  267. switch_to(auto_plant);
  268. } else if (n == -1) {
  269. switch_to(menu_a);
  270. } else if (n == -2) {
  271. switch_to((state == auto_mode_a) ? auto_mode_b : auto_mode_a);
  272. }
  273. } else if ((state == auto_fert_a) || (state == auto_fert_b)) {
  274. if ((n >= 1) && (n <= 3)) {
  275. auto wl = plants.getWaterlevel();
  276. if ((wl != Plants::full) && (wl != Plants::invalid)) {
  277. plants.startFertilizer(n - 1);
  278. selected_id = n;
  279. selected_time = AUTO_PUMP_RUNTIME;
  280. start_time = millis();
  281. switch_to(auto_fert_run);
  282. } else if (wl == Plants::full) {
  283. stop_time = millis();
  284. switch_to(auto_mode_a);
  285. } else if (wl == Plants::invalid) {
  286. error_condition = F("Invalid sensor state");
  287. state = auto_mode_a;
  288. switch_to(error);
  289. }
  290. } else if (n == 4) {
  291. for (int i = 0; i < STIRRER_COUNT; i++) {
  292. plants.startAux(i);
  293. }
  294. selected_id = 1;
  295. selected_time = AUTO_STIRR_RUNTIME;
  296. start_time = millis();
  297. switch_to(auto_stirr_run);
  298. } else if (n == -1) {
  299. switch_to(auto_mode_a);
  300. } else if (n == -2) {
  301. switch_to((state == auto_fert_a) ? auto_fert_b : auto_fert_a);
  302. }
  303. } else if ((state == auto_fert_run) || (state == auto_tank_run)
  304. || (state == auto_stirr_run) || (state == auto_plant_run)
  305. || (state == auto_plant_kickstart_run)
  306. || (state == fillnwater_kickstart_run)) {
  307. plants.abort();
  308. stop_time = millis();
  309. switch_to(auto_done);
  310. } else if (state == auto_plant) {
  311. if (n == -1) {
  312. if (db.hasDigits()) {
  313. backspace();
  314. db.removeDigit();
  315. if (menu_entered_digits.length() > 0) {
  316. menu_entered_digits.remove(menu_entered_digits.length() - 1);
  317. switch_to(state);
  318. }
  319. } else {
  320. switch_to(auto_mode_b);
  321. }
  322. } else if (n == -2) {
  323. if (!db.hasDigits()) {
  324. auto wl = plants.getWaterlevel();
  325. if ((wl != Plants::empty) && (wl != Plants::invalid)) {
  326. // check if kickstart is required for this
  327. bool need_kickstart = false;
  328. for (int i = 0; i < plants.countPlants(); i++) {
  329. if (selected_plants.isSet(i)) {
  330. if (plants.getKickstart()->getPinNumber(i) >= 0) {
  331. need_kickstart = true;
  332. }
  333. }
  334. }
  335. // start kickstart/valve as needed
  336. for (int i = 0; i < plants.countPlants(); i++) {
  337. if (selected_plants.isSet(i)) {
  338. plants.startPlant(i, need_kickstart);
  339. }
  340. }
  341. selected_time = MAX_AUTO_PLANT_RUNTIME;
  342. start_time = millis();
  343. if (need_kickstart) {
  344. switch_to(auto_plant_kickstart_run);
  345. } else {
  346. switch_to(auto_plant_run);
  347. }
  348. } else if (wl == Plants::empty) {
  349. stop_time = millis();
  350. switch_to(auto_mode_b);
  351. } else if (wl == Plants::invalid) {
  352. error_condition = F("Invalid sensor state");
  353. state = auto_mode_b;
  354. switch_to(error);
  355. }
  356. } else {
  357. selected_id = number_input();
  358. if ((selected_id <= 0) || (selected_id > plants.countPlants())) {
  359. error_condition = F("Invalid plant ID!");
  360. switch_to(error);
  361. } else {
  362. selected_plants.set(selected_id - 1);
  363. menu_entered_digits = "";
  364. switch_to(auto_plant);
  365. }
  366. }
  367. } else {
  368. if (db.spaceLeft()) {
  369. db.addDigit(n);
  370. menu_entered_digits += String(n);
  371. switch_to(state);
  372. } else {
  373. backspace();
  374. }
  375. }
  376. } else if (state == auto_done) {
  377. switch_to(auto_mode_a);
  378. } else if (state == menu_pumps) {
  379. if (n == -1) {
  380. if (db.hasDigits()) {
  381. backspace();
  382. db.removeDigit();
  383. if (menu_entered_digits.length() > 0) {
  384. menu_entered_digits.remove(menu_entered_digits.length() - 1);
  385. switch_to(state);
  386. }
  387. } else {
  388. switch_to(menu_b);
  389. }
  390. } else if (n == -2) {
  391. if (!db.hasDigits()) {
  392. return;
  393. }
  394. selected_id = number_input();
  395. if ((selected_id <= 0) || (selected_id > plants.countFertilizers())) {
  396. error_condition = F("Invalid pump ID!");
  397. switch_to(error);
  398. } else {
  399. switch_to(menu_pumps_time);
  400. }
  401. } else {
  402. if (db.spaceLeft()) {
  403. db.addDigit(n);
  404. menu_entered_digits += String(n);
  405. switch_to(state);
  406. } else {
  407. backspace();
  408. }
  409. }
  410. } else if (state == fillnwater_plant) {
  411. if (n == -1) {
  412. if (db.hasDigits()) {
  413. backspace();
  414. db.removeDigit();
  415. if (menu_entered_digits.length() > 0) {
  416. menu_entered_digits.remove(menu_entered_digits.length() - 1);
  417. switch_to(state);
  418. }
  419. } else {
  420. switch_to(auto_mode_a);
  421. }
  422. } else if (n == -2) {
  423. if (!db.hasDigits()) {
  424. int found = 0;
  425. for (int i = 0; i < plants.countPlants(); i++) {
  426. if (selected_plants.isSet(i)) {
  427. found = 1;
  428. break;
  429. }
  430. }
  431. if (found != 0) {
  432. auto wl = plants.getWaterlevel();
  433. if ((wl != Plants::full) && (wl != Plants::invalid)) {
  434. // if the waterlevel is currently empty, we
  435. // set a flag to record the time to fill
  436. filling_started_empty = (wl == Plants::empty);
  437. plants.openWaterInlet();
  438. selected_id = plants.countPlants() + 1;
  439. selected_time = MAX_TANK_FILL_TIME;
  440. start_time = millis();
  441. switch_to(fillnwater_tank_run);
  442. } else if (wl == Plants::full) {
  443. stop_time = millis();
  444. auto wl = plants.getWaterlevel();
  445. if ((wl != Plants::empty) && (wl != Plants::invalid)) {
  446. // check if kickstart is required for this
  447. bool need_kickstart = false;
  448. for (int i = 0; i < plants.countPlants(); i++) {
  449. if (selected_plants.isSet(i)) {
  450. if (plants.getKickstart()->getPinNumber(i) >= 0) {
  451. need_kickstart = true;
  452. }
  453. }
  454. }
  455. // start kickstart/valve as needed
  456. for (int i = 0; i < plants.countPlants(); i++) {
  457. if (selected_plants.isSet(i)) {
  458. plants.startPlant(i, need_kickstart);
  459. }
  460. }
  461. // for recording the flowrate
  462. watering_started_full = (wl == Plants::full);
  463. selected_time = MAX_AUTO_PLANT_RUNTIME;
  464. start_time = millis();
  465. if (need_kickstart) {
  466. switch_to(fillnwater_kickstart_run);
  467. } else {
  468. switch_to(fillnwater_plant_run);
  469. }
  470. } else if (wl == Plants::empty) {
  471. stop_time = millis();
  472. switch_to(auto_mode_a);
  473. } else if (wl == Plants::invalid) {
  474. error_condition = F("Invalid sensor state");
  475. state = auto_mode_a;
  476. switch_to(error);
  477. }
  478. } else if (wl == Plants::invalid) {
  479. error_condition = F("Invalid sensor state");
  480. state = auto_mode_a;
  481. switch_to(error);
  482. }
  483. }
  484. } else {
  485. selected_id = number_input();
  486. if ((selected_id <= 0) || (selected_id > plants.countPlants())) {
  487. error_condition = F("Invalid plant ID!");
  488. switch_to(error);
  489. } else {
  490. selected_plants.set(selected_id - 1);
  491. menu_entered_digits = "";
  492. switch_to(fillnwater_plant);
  493. }
  494. }
  495. } else {
  496. if (db.spaceLeft()) {
  497. db.addDigit(n);
  498. menu_entered_digits += String(n);
  499. switch_to(state);
  500. } else {
  501. backspace();
  502. }
  503. }
  504. } else if (state == fillnwater_tank_run) {
  505. plants.abort();
  506. stop_time = millis();
  507. auto wl = plants.getWaterlevel();
  508. if ((wl != Plants::empty) && (wl != Plants::invalid)) {
  509. // check if kickstart is required for this
  510. bool need_kickstart = false;
  511. for (int i = 0; i < plants.countPlants(); i++) {
  512. if (selected_plants.isSet(i)) {
  513. if (plants.getKickstart()->getPinNumber(i) >= 0) {
  514. need_kickstart = true;
  515. }
  516. }
  517. }
  518. // start kickstart/valve as needed
  519. for (int i = 0; i < plants.countPlants(); i++) {
  520. if (selected_plants.isSet(i)) {
  521. plants.startPlant(i, need_kickstart);
  522. }
  523. }
  524. watering_started_full = (wl == Plants::full);
  525. selected_time = MAX_AUTO_PLANT_RUNTIME;
  526. start_time = millis();
  527. if (need_kickstart) {
  528. switch_to(fillnwater_kickstart_run);
  529. } else {
  530. switch_to(fillnwater_plant_run);
  531. }
  532. } else if (wl == Plants::empty) {
  533. switch_to(auto_mode_a);
  534. } else if (wl == Plants::invalid) {
  535. error_condition = F("Invalid sensor state");
  536. state = auto_mode_a;
  537. switch_to(error);
  538. }
  539. } else if (state == fillnwater_plant_run) {
  540. plants.abort();
  541. stop_time = millis();
  542. switch_to(auto_done);
  543. } else if (state == menu_pumps_time) {
  544. if (n == -1) {
  545. if (db.hasDigits()) {
  546. backspace();
  547. db.removeDigit();
  548. if (menu_entered_digits.length() > 0) {
  549. menu_entered_digits.remove(menu_entered_digits.length() - 1);
  550. switch_to(state);
  551. }
  552. } else {
  553. switch_to(menu_pumps);
  554. }
  555. } else if (n == -2) {
  556. if (!db.hasDigits()) {
  557. return;
  558. }
  559. selected_time = number_input();
  560. if ((selected_time <= 0) || (selected_time > MAX_PUMP_RUNTIME)) {
  561. error_condition = F("Invalid time range!");
  562. switch_to(error);
  563. } else {
  564. switch_to(menu_pumps_go);
  565. }
  566. } else {
  567. if (db.spaceLeft()) {
  568. db.addDigit(n);
  569. menu_entered_digits += String(n);
  570. switch_to(state);
  571. } else {
  572. backspace();
  573. }
  574. }
  575. } else if (state == menu_pumps_go) {
  576. if (n == -2) {
  577. start_time = millis();
  578. last_animation_time = start_time;
  579. auto wl = plants.getWaterlevel();
  580. if ((wl != Plants::full) && (wl != Plants::invalid)) {
  581. plants.startFertilizer(selected_id - 1);
  582. switch_to(menu_pumps_run);
  583. } else if (wl == Plants::full) {
  584. stop_time = millis();
  585. switch_to(menu_pumps_done);
  586. } else if (wl == Plants::invalid) {
  587. error_condition = F("Invalid sensor state");
  588. state = menu_pumps;
  589. switch_to(error);
  590. }
  591. } else {
  592. switch_to(menu_pumps_time);
  593. }
  594. } else if (state == menu_pumps_run) {
  595. plants.abort();
  596. stop_time = millis();
  597. switch_to(menu_pumps_done);
  598. } else if (state == menu_pumps_done) {
  599. switch_to(menu_b);
  600. } else if (state == menu_valves) {
  601. if (n == -1) {
  602. if (db.hasDigits()) {
  603. backspace();
  604. db.removeDigit();
  605. if (menu_entered_digits.length() > 0) {
  606. menu_entered_digits.remove(menu_entered_digits.length() - 1);
  607. switch_to(state);
  608. }
  609. } else {
  610. switch_to(menu_b);
  611. }
  612. } else if (n == -2) {
  613. if (!db.hasDigits()) {
  614. return;
  615. }
  616. selected_id = number_input();
  617. if ((selected_id <= 0) || (selected_id > (plants.countPlants() + 1))) {
  618. error_condition = F("Invalid valve ID!");
  619. switch_to(error);
  620. } else {
  621. switch_to(menu_valves_time);
  622. }
  623. } else {
  624. if (db.spaceLeft()) {
  625. db.addDigit(n);
  626. menu_entered_digits += String(n);
  627. switch_to(state);
  628. } else {
  629. backspace();
  630. }
  631. }
  632. } else if (state == menu_valves_time) {
  633. if (n == -1) {
  634. if (db.hasDigits()) {
  635. backspace();
  636. db.removeDigit();
  637. if (menu_entered_digits.length() > 0) {
  638. menu_entered_digits.remove(menu_entered_digits.length() - 1);
  639. switch_to(state);
  640. }
  641. } else {
  642. switch_to(menu_valves);
  643. }
  644. } else if (n == -2) {
  645. if (!db.hasDigits()) {
  646. return;
  647. }
  648. selected_time = number_input();
  649. if ((selected_time <= 0) || (selected_time > MAX_VALVE_RUNTIME)) {
  650. error_condition = F("Invalid time range!");
  651. switch_to(error);
  652. } else {
  653. switch_to(menu_valves_go);
  654. }
  655. } else {
  656. if (db.spaceLeft()) {
  657. db.addDigit(n);
  658. menu_entered_digits += String(n);
  659. switch_to(state);
  660. } else {
  661. backspace();
  662. }
  663. }
  664. } else if (state == menu_valves_go) {
  665. if (n == -2) {
  666. start_time = millis();
  667. last_animation_time = start_time;
  668. auto wl = plants.getWaterlevel();
  669. if ((wl != Plants::full) && (wl != Plants::invalid)) {
  670. if (selected_id >= (plants.countPlants() + 1)) {
  671. plants.openWaterInlet();
  672. } else {
  673. // TODO support testing kickstart
  674. plants.startPlant(selected_id - 1, false);
  675. }
  676. switch_to(menu_valves_run);
  677. } else if (wl == Plants::full) {
  678. stop_time = millis();
  679. switch_to(menu_valves_done);
  680. } else if (wl == Plants::invalid) {
  681. error_condition = F("Invalid sensor state");
  682. state = menu_valves;
  683. switch_to(error);
  684. }
  685. } else {
  686. switch_to(menu_valves_time);
  687. }
  688. } else if (state == menu_valves_run) {
  689. plants.abort();
  690. stop_time = millis();
  691. switch_to(menu_valves_done);
  692. } else if (state == menu_valves_done) {
  693. switch_to(menu_b);
  694. } else if (state == menu_aux) {
  695. if (n == -1) {
  696. if (db.hasDigits()) {
  697. backspace();
  698. db.removeDigit();
  699. if (menu_entered_digits.length() > 0) {
  700. menu_entered_digits.remove(menu_entered_digits.length() - 1);
  701. switch_to(state);
  702. }
  703. } else {
  704. switch_to(menu_c);
  705. }
  706. } else if (n == -2) {
  707. if (!db.hasDigits()) {
  708. return;
  709. }
  710. selected_id = number_input();
  711. if ((selected_id <= 0) || (selected_id > plants.countAux())) {
  712. error_condition = F("Invalid valve ID!");
  713. switch_to(error);
  714. } else {
  715. switch_to(menu_aux_time);
  716. }
  717. } else {
  718. if (db.spaceLeft()) {
  719. db.addDigit(n);
  720. menu_entered_digits += String(n);
  721. switch_to(state);
  722. } else {
  723. backspace();
  724. }
  725. }
  726. } else if (state == menu_aux_time) {
  727. if (n == -1) {
  728. if (db.hasDigits()) {
  729. backspace();
  730. db.removeDigit();
  731. if (menu_entered_digits.length() > 0) {
  732. menu_entered_digits.remove(menu_entered_digits.length() - 1);
  733. switch_to(state);
  734. }
  735. } else {
  736. switch_to(menu_aux);
  737. }
  738. } else if (n == -2) {
  739. if (!db.hasDigits()) {
  740. return;
  741. }
  742. selected_time = number_input();
  743. if ((selected_time <= 0) || (selected_time > MAX_AUX_RUNTIME)) {
  744. error_condition = F("Invalid time range!");
  745. switch_to(error);
  746. } else {
  747. switch_to(menu_aux_go);
  748. }
  749. } else {
  750. if (db.spaceLeft()) {
  751. db.addDigit(n);
  752. menu_entered_digits += String(n);
  753. switch_to(state);
  754. } else {
  755. backspace();
  756. }
  757. }
  758. } else if (state == menu_aux_go) {
  759. if (n == -2) {
  760. start_time = millis();
  761. last_animation_time = start_time;
  762. plants.startAux(selected_id - 1);
  763. switch_to(menu_aux_run);
  764. } else {
  765. switch_to(menu_aux_time);
  766. }
  767. } else if (state == menu_aux_run) {
  768. plants.abort();
  769. stop_time = millis();
  770. switch_to(menu_aux_done);
  771. } else if (state == menu_aux_done) {
  772. switch_to(menu_c);
  773. } else if (state == error) {
  774. if (old_state != error) {
  775. switch_to(old_state);
  776. } else {
  777. switch_to(menu_a);
  778. }
  779. }
  780. }
  781. uint32_t Statemachine::number_input(void) {
  782. for (int i = 0; i < db.countDigits(); i++) {
  783. backspace();
  784. }
  785. uint32_t n = db.getNumber();
  786. db.clear();
  787. debug.print("Whole number input: ");
  788. debug.println(n);
  789. return n;
  790. }
  791. void Statemachine::act(void) {
  792. if ((state == menu_pumps_run) || (state == menu_valves_run)
  793. || (state == menu_aux_run)
  794. || (state == auto_stirr_run)) {
  795. unsigned long runtime = millis() - start_time;
  796. if ((runtime / 1000UL) >= selected_time) {
  797. // stop if timeout has been reached
  798. plants.abort();
  799. stop_time = millis();
  800. switch_to((state == menu_pumps_run) ? menu_pumps_done : ((state == menu_valves_run) ? menu_valves_done : ((state == menu_aux_run) ? menu_aux_done : auto_done)));
  801. } else if ((millis() - last_animation_time) >= 500) {
  802. // update animation if needed
  803. last_animation_time = millis();
  804. switch_to(state);
  805. }
  806. }
  807. #ifdef CHECK_SENSORS_VALVE_PUMP_MENU_FULL
  808. if ((state == menu_pumps_run) || ((state == menu_valves_run) && (selected_id == (plants.countPlants() + 1)))) {
  809. // check water level state
  810. auto wl = plants.getWaterlevel();
  811. if (wl == Plants::full) {
  812. plants.abort();
  813. stop_time = millis();
  814. switch_to((state == menu_pumps_run) ? menu_pumps_done : menu_valves_done);
  815. } else if (wl == Plants::invalid) {
  816. plants.abort();
  817. error_condition = F("Invalid sensor state");
  818. state = (state == menu_pumps_run) ? menu_pumps : menu_valves;
  819. switch_to(error);
  820. }
  821. }
  822. #endif // CHECK_SENSORS_VALVE_PUMP_MENU_FULL
  823. #ifdef CHECK_SENSORS_VALVE_PUMP_MENU_EMPTY
  824. if ((state == menu_valves_run) && (selected_id <= plants.countPlants())) {
  825. // check water level state
  826. auto wl = plants.getWaterlevel();
  827. if (wl == Plants::empty) {
  828. plants.abort();
  829. stop_time = millis();
  830. switch_to(menu_valves_done);
  831. } else if (wl == Plants::invalid) {
  832. plants.abort();
  833. error_condition = F("Invalid sensor state");
  834. state = menu_valves;
  835. switch_to(error);
  836. }
  837. }
  838. #endif // CHECK_SENSORS_VALVE_PUMP_MENU_EMPTY
  839. if ((state == auto_plant_kickstart_run) || (state == fillnwater_kickstart_run)) {
  840. unsigned long runtime = millis() - start_time;
  841. if ((runtime / 1000UL) >= KICKSTART_RUNTIME) {
  842. // kickstart is done, switch over to valves
  843. plants.abort();
  844. start_time = millis();
  845. // start required valves
  846. for (int i = 0; i < plants.countPlants(); i++) {
  847. if (selected_plants.isSet(i)) {
  848. plants.startPlant(i, false);
  849. }
  850. }
  851. if (state == auto_plant_kickstart_run) {
  852. switch_to(auto_plant_run);
  853. } else {
  854. switch_to(fillnwater_plant_run);
  855. }
  856. } else if ((millis() - last_animation_time) >= 500) {
  857. // update animation if needed
  858. last_animation_time = millis();
  859. switch_to(state);
  860. }
  861. }
  862. if ((state == auto_fert_run) || (state == auto_tank_run) || (state == fillnwater_tank_run)) {
  863. unsigned long runtime = millis() - start_time;
  864. if ((runtime / 1000UL) >= selected_time) {
  865. // stop if timeout has been reached
  866. plants.abort();
  867. stop_time = millis();
  868. if (state == fillnwater_tank_run) {
  869. auto wl = plants.getWaterlevel();
  870. if ((wl != Plants::empty) && (wl != Plants::invalid)) {
  871. // check if kickstart is required for this
  872. bool need_kickstart = false;
  873. for (int i = 0; i < plants.countPlants(); i++) {
  874. if (selected_plants.isSet(i)) {
  875. if (plants.getKickstart()->getPinNumber(i) >= 0) {
  876. need_kickstart = true;
  877. }
  878. }
  879. }
  880. // start kickstart/valve as needed
  881. for (int i = 0; i < plants.countPlants(); i++) {
  882. if (selected_plants.isSet(i)) {
  883. plants.startPlant(i, need_kickstart);
  884. }
  885. }
  886. watering_started_full = (wl == Plants::full);
  887. selected_time = MAX_AUTO_PLANT_RUNTIME;
  888. start_time = millis();
  889. if (need_kickstart) {
  890. switch_to(fillnwater_kickstart_run);
  891. } else {
  892. switch_to(fillnwater_plant_run);
  893. }
  894. } else if (wl == Plants::empty) {
  895. stop_time = millis();
  896. switch_to(auto_done);
  897. } else if (wl == Plants::invalid) {
  898. error_condition = F("Invalid sensor state");
  899. state = auto_mode_a;
  900. switch_to(error);
  901. }
  902. } else {
  903. switch_to(auto_done);
  904. }
  905. } else if ((millis() - last_animation_time) >= 500) {
  906. // update animation if needed
  907. last_animation_time = millis();
  908. switch_to(state);
  909. }
  910. // check water level state
  911. auto wl = plants.getWaterlevel();
  912. if (wl == Plants::full) {
  913. plants.abort();
  914. stop_time = millis();
  915. if (state == fillnwater_tank_run) {
  916. // record time to fill here, if we started with
  917. // an empty tank at the start of filling
  918. if (filling_started_empty) {
  919. unsigned long time_to_fill = stop_time - start_time;
  920. debug.print("Filling tank took ");
  921. debug.print(String(time_to_fill));
  922. debug.println("ms");
  923. #if defined(PLATFORM_ESP)
  924. bool success = wifi_write_database(time_to_fill, "calibrated_filling", -1);
  925. if (!success) {
  926. debug.print("Error writing to InfluxDB ");
  927. debug.print(INFLUXDB_HOST);
  928. debug.print(":");
  929. debug.print(INFLUXDB_PORT);
  930. debug.print("/");
  931. debug.print(INFLUXDB_DATABASE);
  932. debug.println("/calibrated_filling");
  933. }
  934. #endif // PLATFORM_ESP
  935. }
  936. auto wl = plants.getWaterlevel();
  937. if ((wl != Plants::empty) && (wl != Plants::invalid)) {
  938. // check if kickstart is required for this
  939. bool need_kickstart = false;
  940. for (int i = 0; i < plants.countPlants(); i++) {
  941. if (selected_plants.isSet(i)) {
  942. if (plants.getKickstart()->getPinNumber(i) >= 0) {
  943. need_kickstart = true;
  944. }
  945. }
  946. }
  947. // start kickstart/valve as needed
  948. for (int i = 0; i < plants.countPlants(); i++) {
  949. if (selected_plants.isSet(i)) {
  950. plants.startPlant(i, need_kickstart);
  951. }
  952. }
  953. watering_started_full = (wl == Plants::full);
  954. selected_time = MAX_AUTO_PLANT_RUNTIME;
  955. start_time = millis();
  956. if (need_kickstart) {
  957. switch_to(fillnwater_kickstart_run);
  958. } else {
  959. switch_to(fillnwater_plant_run);
  960. }
  961. } else if (wl == Plants::empty) {
  962. stop_time = millis();
  963. switch_to(auto_mode_a);
  964. } else if (wl == Plants::invalid) {
  965. error_condition = F("Invalid sensor state");
  966. state = auto_mode_a;
  967. switch_to(error);
  968. }
  969. } else {
  970. switch_to(auto_done);
  971. }
  972. } else if (wl == Plants::invalid) {
  973. plants.abort();
  974. error_condition = F("Invalid sensor state");
  975. state = auto_mode_a;
  976. switch_to(error);
  977. }
  978. }
  979. if ((state == auto_plant_run) || (state == fillnwater_plant_run)) {
  980. unsigned long runtime = millis() - start_time;
  981. if ((runtime / 1000UL) >= selected_time) {
  982. // stop if timeout has been reached
  983. plants.abort();
  984. stop_time = millis();
  985. switch_to(auto_done);
  986. } else if ((millis() - last_animation_time) >= 500) {
  987. // update animation if needed
  988. last_animation_time = millis();
  989. switch_to(state);
  990. }
  991. // check water level state
  992. auto wl = plants.getWaterlevel();
  993. if (wl == Plants::empty) {
  994. plants.abort();
  995. stop_time = millis();
  996. // if we started watering with a full tank
  997. // and then finished watering when it was empty
  998. // and we were only watering a single plant
  999. // look at this as a "calibration run" and record
  1000. // the time it took to empty the tank
  1001. if ((state == fillnwater_plant_run) && watering_started_full && (selected_plants.countSet() == 1)) {
  1002. unsigned long time_to_water = stop_time - start_time;
  1003. debug.print("Watering plant ");
  1004. debug.print(selected_plants.getFirstSet() + 1);
  1005. debug.print(" with the complete tank took ");
  1006. debug.print(String(time_to_water));
  1007. debug.println("ms");
  1008. #if defined(PLATFORM_ESP)
  1009. bool success = wifi_write_database(time_to_water, "calibrated_watering", selected_plants.getFirstSet() + 1);
  1010. if (!success) {
  1011. debug.print("Error writing to InfluxDB ");
  1012. debug.print(INFLUXDB_HOST);
  1013. debug.print(":");
  1014. debug.print(INFLUXDB_PORT);
  1015. debug.print("/");
  1016. debug.print(INFLUXDB_DATABASE);
  1017. debug.println("/calibrated_watering");
  1018. }
  1019. #endif // PLATFORM_ESP
  1020. }
  1021. switch_to(auto_done);
  1022. } else if (wl == Plants::invalid) {
  1023. plants.abort();
  1024. error_condition = F("Invalid sensor state");
  1025. state = auto_mode_a;
  1026. switch_to(error);
  1027. }
  1028. }
  1029. if ((state == menu_a) || (state == menu_b) || (state == menu_c)
  1030. || (state == automation_mode) || (state == auto_done)
  1031. || (state == auto_mode_a) || (state == auto_mode_b)
  1032. || (state == auto_fert_a) || (state == auto_fert_b)
  1033. || (state == auto_plant) || (state == fillnwater_plant)
  1034. || (state == menu_pumps) || (state == menu_pumps_time)
  1035. || (state == menu_pumps_go) || (state == menu_pumps_done)
  1036. || (state == menu_valves) || (state == menu_valves_time)
  1037. || (state == menu_valves_go) || (state == menu_valves_done)
  1038. || (state == menu_aux) || (state == menu_aux_time)
  1039. || (state == menu_aux_go) || (state == menu_aux_done)) {
  1040. unsigned long runtime = millis() - into_state_time;
  1041. if (runtime >= BACK_TO_IDLE_TIMEOUT) {
  1042. debug.print("Idle timeout reached in state ");
  1043. debug.println(state_names[state]);
  1044. switch_to(init);
  1045. }
  1046. }
  1047. }
  1048. void Statemachine::switch_to(States s) {
  1049. old_state = state;
  1050. state = s;
  1051. into_state_time = millis();
  1052. if (old_state != state) {
  1053. // don't spam log with every animation state "change"
  1054. debug.print("switch_to ");
  1055. debug.print(state_names[old_state]);
  1056. debug.print(" --> ");
  1057. debug.println(state_names[state]);
  1058. menu_entered_digits = "";
  1059. }
  1060. if (s == init) {
  1061. String a = String(F("- Giess-o-mat V")) + FIRMWARE_VERSION + String(F(" -"));
  1062. #if (LOCK_COUNT > 0) && defined(DOOR_LOCK_PIN)
  1063. String b = String(F("PIN: ")) + menu_entered_digits;
  1064. print(a.c_str(),
  1065. "* or # to enter menu",
  1066. "Enter PIN for locks:",
  1067. b.c_str(),
  1068. 3);
  1069. #else
  1070. print(a.c_str(),
  1071. "Usage: Enter number",
  1072. "* Delete prev. digit",
  1073. "# Execute input num.",
  1074. -1);
  1075. #endif
  1076. } else if (s == door_select) {
  1077. String a = String("(Input 1 to ") + String(LOCK_COUNT) + String(")");
  1078. String b = String(F("Door: ")) + menu_entered_digits;
  1079. print("- Select Door Lock -",
  1080. "Leave empty if done!",
  1081. a.c_str(),
  1082. b.c_str(),
  1083. 3);
  1084. } else if (s == menu_a) {
  1085. print("----- Menu 1/3 -----",
  1086. "1: Manual Operation",
  1087. "2: Automation",
  1088. "#: Go to page 2/3...",
  1089. -1);
  1090. } else if (s == menu_b) {
  1091. print("----- Menu 2/3 -----",
  1092. "3: Fertilizer pumps",
  1093. "4: Outlet valves",
  1094. "#: Go to page 3/3...",
  1095. -1);
  1096. } else if (s == menu_c) {
  1097. print("----- Menu 3/3 -----",
  1098. "5: Aux. Outputs",
  1099. #if (LOCK_COUNT > 0) && !defined(DOOR_LOCK_PIN)
  1100. "6: Door Locks",
  1101. #else
  1102. "",
  1103. #endif
  1104. "#: Go to page 1/3...",
  1105. -1);
  1106. } else if (state == automation_mode) {
  1107. // TODO
  1108. print("---- Automation ----",
  1109. "TODO NOT IMPLEMENTED",
  1110. "TODO NOT IMPLEMENTED",
  1111. "TODO NOT IMPLEMENTED",
  1112. -1);
  1113. } else if (s == auto_mode_a) {
  1114. print("---- Manual 1/2 ----",
  1115. "1: Add Fertilizer",
  1116. "2: Fill 'n' Water",
  1117. "#: Go to page 2/2...",
  1118. -1);
  1119. } else if (s == auto_mode_b) {
  1120. print("---- Manual 2/2 ----",
  1121. "3: Fill Reservoir",
  1122. "4: Water a plant",
  1123. "#: Go to page 1/2...",
  1124. -1);
  1125. } else if (s == auto_fert_a) {
  1126. print("-- Fertilizer 1/2 --",
  1127. "1: Vegetation Phase",
  1128. "2: Bloom Phase",
  1129. "#: Go to page 2/2...",
  1130. -1);
  1131. } else if (s == auto_fert_b) {
  1132. print("-- Fertilizer 2/2 --",
  1133. "3: Special Fert.",
  1134. "4: Run Stirrer",
  1135. "#: Go to page 1/2...",
  1136. -1);
  1137. } else if (s == auto_fert_run) {
  1138. unsigned long runtime = millis() - start_time;
  1139. String a = String("Time: ") + String(runtime / 1000UL) + String("s / ") + String(selected_time) + String('s');
  1140. unsigned long anim = runtime * 20UL / (selected_time * 1000UL);
  1141. String b;
  1142. for (unsigned long i = 0; i < anim; i++) {
  1143. b += '#';
  1144. }
  1145. print("---- Dispensing ----",
  1146. a.c_str(),
  1147. b.c_str(),
  1148. "Hit any key to stop!",
  1149. -1);
  1150. } else if (s == auto_stirr_run) {
  1151. unsigned long runtime = millis() - start_time;
  1152. String a = String("Time: ") + String(runtime / 1000UL) + String("s / ") + String(selected_time) + String('s');
  1153. unsigned long anim = runtime * 20UL / (selected_time * 1000UL);
  1154. String b;
  1155. for (unsigned long i = 0; i < anim; i++) {
  1156. b += '#';
  1157. }
  1158. print("----- Stirring -----",
  1159. a.c_str(),
  1160. b.c_str(),
  1161. "Hit any key to stop!",
  1162. -1);
  1163. } else if ((s == auto_tank_run) || (s == fillnwater_tank_run)) {
  1164. unsigned long runtime = millis() - start_time;
  1165. String a = String("Time: ") + String(runtime / 1000UL) + String("s / ") + String(selected_time) + String('s');
  1166. unsigned long anim = runtime * 20UL / (selected_time * 1000UL);
  1167. String b;
  1168. for (unsigned long i = 0; i < anim; i++) {
  1169. b += '#';
  1170. }
  1171. print("--- Filling Tank ---",
  1172. a.c_str(),
  1173. b.c_str(),
  1174. "Hit any key to stop!",
  1175. -1);
  1176. } else if ((s == auto_plant) || (s == fillnwater_plant)) {
  1177. String a = String("(Input 1 to ") + String(plants.countPlants()) + String(")");
  1178. String b = String(F("Plant: ")) + menu_entered_digits;
  1179. print("--- Select Plant ---",
  1180. "Leave empty if done!",
  1181. a.c_str(),
  1182. b.c_str(),
  1183. 3);
  1184. } else if ((s == auto_plant_kickstart_run) || (s == fillnwater_kickstart_run)) {
  1185. unsigned long runtime = millis() - start_time;
  1186. String a = String("Time: ") + String(runtime / 1000UL) + String("s / ") + String(KICKSTART_RUNTIME) + String('s');
  1187. unsigned long anim = runtime * 20UL / (KICKSTART_RUNTIME * 1000UL);
  1188. String b;
  1189. for (unsigned long i = 0; i < anim; i++) {
  1190. b += '#';
  1191. }
  1192. print("---- Kick-Start ----",
  1193. a.c_str(),
  1194. b.c_str(),
  1195. "Hit any key to stop!",
  1196. -1);
  1197. } else if ((s == auto_plant_run) || (s == fillnwater_plant_run)) {
  1198. unsigned long runtime = millis() - start_time;
  1199. String a = String("Time: ") + String(runtime / 1000UL) + String("s / ") + String(selected_time) + String('s');
  1200. unsigned long anim = runtime * 20UL / (selected_time * 1000UL);
  1201. String b;
  1202. for (unsigned long i = 0; i < anim; i++) {
  1203. b += '#';
  1204. }
  1205. print("----- Watering -----",
  1206. a.c_str(),
  1207. b.c_str(),
  1208. "Hit any key to stop!",
  1209. -1);
  1210. } else if (s == auto_done) {
  1211. String a = String("after ") + String((stop_time - start_time) / 1000UL) + String("s.");
  1212. print("------- Done -------",
  1213. "Dispensing finished",
  1214. a.c_str(),
  1215. "Hit any key for menu",
  1216. -1);
  1217. #if defined(PLATFORM_ESP)
  1218. unsigned long runtime = stop_time - start_time;
  1219. if ((old_state == auto_plant_run) || (old_state == fillnwater_plant_run)) {
  1220. for (int i = 0; i < plants.countPlants(); i++) {
  1221. if (selected_plants.isSet(i)) {
  1222. bool success = wifi_write_database(runtime / 1000, "plant", i + 1);
  1223. if (!success) {
  1224. debug.print("Error writing to InfluxDB ");
  1225. debug.print(INFLUXDB_HOST);
  1226. debug.print(":");
  1227. debug.print(INFLUXDB_PORT);
  1228. debug.print("/");
  1229. debug.print(INFLUXDB_DATABASE);
  1230. debug.println("/plant");
  1231. }
  1232. }
  1233. }
  1234. } else if (old_state == auto_fert_run) {
  1235. bool success = wifi_write_database(runtime / 1000, "fertilizer", selected_id);
  1236. if (!success) {
  1237. debug.print("Error writing to InfluxDB ");
  1238. debug.print(INFLUXDB_HOST);
  1239. debug.print(":");
  1240. debug.print(INFLUXDB_PORT);
  1241. debug.print("/");
  1242. debug.print(INFLUXDB_DATABASE);
  1243. debug.println("/fertilizer");
  1244. }
  1245. }
  1246. #endif // PLATFORM_ESP
  1247. } else if (s == menu_pumps) {
  1248. String a = String("(Input 1 to ") + String(plants.countFertilizers()) + String(")");
  1249. String b = String(F("Pump: ")) + menu_entered_digits;
  1250. print("------- Pump -------",
  1251. "Please select pump",
  1252. a.c_str(),
  1253. b.c_str(),
  1254. 3);
  1255. } else if (s == menu_pumps_time) {
  1256. String header = String("------ Pump ") + String(selected_id) + String(" ------");
  1257. String b = String(F("Runtime: ")) + menu_entered_digits;
  1258. print(header.c_str(),
  1259. "Please set runtime",
  1260. "(Input in seconds)",
  1261. b.c_str(),
  1262. 3);
  1263. } else if (s == menu_pumps_go) {
  1264. String a = String("Pump No. ") + String(selected_id);
  1265. String b = String("Runtime ") + String(selected_time) + String('s');
  1266. print("----- Confirm? -----",
  1267. a.c_str(),
  1268. b.c_str(),
  1269. " # Confirm",
  1270. -1);
  1271. } else if (s == menu_pumps_run) {
  1272. unsigned long runtime = millis() - start_time;
  1273. String a = String("Time: ") + String(runtime / 1000UL) + String("s / ") + String(selected_time) + String('s');
  1274. unsigned long anim = runtime * 20UL / (selected_time * 1000UL);
  1275. String b;
  1276. for (unsigned long i = 0; i < anim; i++) {
  1277. b += '#';
  1278. }
  1279. print("---- Dispensing ----",
  1280. a.c_str(),
  1281. b.c_str(),
  1282. "Hit any key to stop!",
  1283. -1);
  1284. } else if (s == menu_pumps_done) {
  1285. String a = String("after ") + String((stop_time - start_time) / 1000UL) + String("s.");
  1286. print("------- Done -------",
  1287. "Dispensing finished",
  1288. a.c_str(),
  1289. "Hit any key for menu",
  1290. -1);
  1291. #if defined(PLATFORM_ESP)
  1292. unsigned long runtime = stop_time - start_time;
  1293. bool success = wifi_write_database(runtime / 1000, "fertilizer", selected_id);
  1294. if (!success) {
  1295. debug.print("Error writing to InfluxDB ");
  1296. debug.print(INFLUXDB_HOST);
  1297. debug.print(":");
  1298. debug.print(INFLUXDB_PORT);
  1299. debug.print("/");
  1300. debug.print(INFLUXDB_DATABASE);
  1301. debug.println("/fertilizer");
  1302. }
  1303. #endif // PLATFORM_ESP
  1304. } else if (s == menu_valves) {
  1305. String a = String("(Input 1 to ") + String(plants.countPlants() + 1) + String(")");
  1306. String b = String(F("Valve: ")) + menu_entered_digits;
  1307. print("------ Valves ------",
  1308. "Please select valve",
  1309. a.c_str(),
  1310. b.c_str(),
  1311. 3);
  1312. } else if (s == menu_valves_time) {
  1313. String header = String("----- Valve ") + String(selected_id) + String(" -----");
  1314. String b = String(F("Runtime: ")) + menu_entered_digits;
  1315. print(header.c_str(),
  1316. "Please set runtime",
  1317. "(Input in seconds)",
  1318. b.c_str(),
  1319. 3);
  1320. } else if (s == menu_valves_go) {
  1321. String a = String("Valve No. ") + String(selected_id);
  1322. String b = String("Runtime ") + String(selected_time) + String('s');
  1323. print("----- Confirm? -----",
  1324. a.c_str(),
  1325. b.c_str(),
  1326. " # Confirm",
  1327. -1);
  1328. } else if (s == menu_valves_run) {
  1329. unsigned long runtime = millis() - start_time;
  1330. String a = String("Time: ") + String(runtime / 1000UL) + String("s / ") + String(selected_time) + String('s');
  1331. unsigned long anim = runtime * 20UL / (selected_time * 1000UL);
  1332. String b;
  1333. for (unsigned long i = 0; i <= anim; i++) {
  1334. b += '#';
  1335. }
  1336. print("---- Dispensing ----",
  1337. a.c_str(),
  1338. b.c_str(),
  1339. "Hit any key to stop!",
  1340. -1);
  1341. } else if (s == menu_valves_done) {
  1342. String a = String("after ") + String((stop_time - start_time) / 1000UL) + String("s.");
  1343. print("------- Done -------",
  1344. "Dispensing finished",
  1345. a.c_str(),
  1346. "Hit any key for menu",
  1347. -1);
  1348. #if defined(PLATFORM_ESP)
  1349. unsigned long runtime = stop_time - start_time;
  1350. if (selected_id <= plants.countPlants()) {
  1351. bool success = wifi_write_database(runtime / 1000, "plant", selected_id);
  1352. if (!success) {
  1353. debug.print("Error writing to InfluxDB ");
  1354. debug.print(INFLUXDB_HOST);
  1355. debug.print(":");
  1356. debug.print(INFLUXDB_PORT);
  1357. debug.print("/");
  1358. debug.print(INFLUXDB_DATABASE);
  1359. debug.println("/plant");
  1360. }
  1361. }
  1362. #endif // PLATFORM_ESP
  1363. } else if (s == menu_aux) {
  1364. String a = String("(Input 1 to ") + String(plants.countAux()) + String(")");
  1365. String b = String(F("Aux.: ")) + menu_entered_digits;
  1366. print("------- Aux. -------",
  1367. "Please select aux.",
  1368. a.c_str(),
  1369. b.c_str(),
  1370. 3);
  1371. } else if (s == menu_aux_time) {
  1372. String header = String("------ Aux ") + String(selected_id) + String(" ------");
  1373. String b = String(F("Runtime: ")) + menu_entered_digits;
  1374. print(header.c_str(),
  1375. "Please set runtime",
  1376. "(Input in seconds)",
  1377. b.c_str(),
  1378. 3);
  1379. } else if (s == menu_aux_go) {
  1380. String a = String("Aux No. ") + String(selected_id);
  1381. String b = String("Runtime ") + String(selected_time) + String('s');
  1382. print("----- Confirm? -----",
  1383. a.c_str(),
  1384. b.c_str(),
  1385. " # Confirm",
  1386. -1);
  1387. } else if (s == menu_aux_run) {
  1388. unsigned long runtime = millis() - start_time;
  1389. String a = String("Time: ") + String(runtime / 1000UL) + String("s / ") + String(selected_time) + String('s');
  1390. unsigned long anim = runtime * 20UL / (selected_time * 1000UL);
  1391. String b;
  1392. for (unsigned long i = 0; i <= anim; i++) {
  1393. b += '#';
  1394. }
  1395. print("----- Stirring -----",
  1396. a.c_str(),
  1397. b.c_str(),
  1398. "Hit any key to stop!",
  1399. -1);
  1400. } else if (s == menu_aux_done) {
  1401. String a = String("after ") + String((stop_time - start_time) / 1000UL) + String("s.");
  1402. print("------- Done -------",
  1403. "Aux. run finished",
  1404. a.c_str(),
  1405. "Hit any key for menu",
  1406. -1);
  1407. } else if (s == error) {
  1408. print("------ Error! ------",
  1409. "There is a problem:",
  1410. error_condition.c_str(),
  1411. " Press any key...",
  1412. -1);
  1413. } else {
  1414. debug.print("Invalid state ");
  1415. debug.println(s);
  1416. }
  1417. }
  1418. #endif