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

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