Advertisement
hms11

ESP32ZoneCommandEdgent0.7.7

May 9th, 2024 (edited)
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.65 KB | None | 0 0
  1. /*************************************************************
  2. Blynk is a platform with iOS and Android apps to control
  3. ESP32, Arduino, Raspberry Pi and the likes over the Internet.
  4. You can easily build mobile and web interfaces for any
  5. projects by simply dragging and dropping widgets.
  6.  
  7. Downloads, docs, tutorials: https://www.blynk.io
  8. Sketch generator: https://examples.blynk.cc
  9. Blynk community: https://community.blynk.cc
  10. Follow us: https://www.fb.com/blynkapp
  11. https://twitter.com/blynk_app
  12.  
  13. Blynk library is licensed under MIT license
  14. *************************************************************
  15. Blynk.Edgent implements:
  16. - Blynk.Inject - Dynamic WiFi credentials provisioning
  17. - Blynk.Air - Over The Air firmware updates
  18. - Device state indication using a physical LED
  19. - Credentials reset using a physical Button
  20. *************************************************************/
  21.  
  22.  
  23. // Fill-in information from your Blynk Template here
  24. #define BLYNK_TEMPLATE_ID "xxxxxxxxxx"
  25. #define BLYNK_TEMPLATE_NAME "ZoneCommand"
  26.  
  27. #define BLYNK_FIRMWARE_VERSION "0.7.7"
  28.  
  29. #define BLYNK_PRINT Serial
  30. //#define BLYNK_DEBUG
  31.  
  32. #define APP_DEBUG
  33.  
  34. // Uncomment your board, or configure a custom board in Settings.h
  35. //#define USE_WROVER_BOARD
  36. //#define USE_TTGO_T7
  37.  
  38. #include "BlynkEdgent.h"
  39.  
  40. //Widget Setups
  41.  
  42. //WidgetLED led1(V4);
  43. //WidgetLED led2(V5);
  44. //WidgetLED led3(V6);
  45. //WidgetLED led4(V7);
  46. //WidgetLED led5(V8);
  47. //WidgetLED led6(V9);
  48. //WidgetLED led7(V10);
  49. //WidgetLED led8(V55);
  50. //WidgetLED led9(V56);
  51. //WidgetLED led10(V57);
  52. //WidgetLED led11(V58);
  53. const int ledArray[] = {V7,V8,V9,V10};
  54. const int soakLedArray[] = {V55,V56,V57,V58};
  55. const int sensorData[] = {V0,V1,V2,V3};
  56. const int triggerDisplay[] = {V11,V12,V13,V14};
  57. const int soakTimeArray[] = {V27,V28,V29,V30};
  58. const int countdownArray[] = {V35,V36,V37,V38};
  59. const int pumpTimeArray[] = {V47,V48,V49,V50};
  60. int ledStatus[] = {0,0,0,0};
  61. int soakLedStatus[] = {0,0,0,0};
  62. int zoneState[4];
  63.  
  64. // Set Pin Assignments For Output Pins
  65.  
  66. // MOSFETS:
  67. const int output[] = {4,16,17,18};
  68. //Motor Drivers:
  69. //U4 Motor Driver:
  70. const int mtrOutA1 = 22;
  71. const int mtrOutA2 = 23;
  72. //U7 Motor Driver:
  73. const int mtrOutB1 = 19;
  74. const int mtrOutB2 = 21;
  75.  
  76. // PWM Settings for Motor Drivers
  77. const int freq = 30000;
  78. const int pwmChannel = 0;
  79. const int resolution = 8;
  80. int dutyCycle = 255;
  81.  
  82.  
  83.  
  84. //Set Pin Assignments for Input Pins
  85.  
  86. //Analog Inputs:
  87.  
  88. //Moisture Sensors:
  89. const int sensPin[] = {35,34,39,36};
  90.  
  91. //Water Level Sensors:
  92. const int wtrLvlTop = 26;
  93. const int wtrLvlBtm = 27;
  94.  
  95.  
  96. //Zone Loop Controls
  97.  
  98. int zoneNumber = 0;
  99. int blynkZone = 0;
  100. int triggerLow[] = {3600,3600,3600,3600};
  101. int triggerHigh[] = {2700,2700,2700,2700};
  102. int triggerSpread[] = {700,700,700,700};
  103. bool systemActive = true;
  104. bool zoneActive[] = {true, true, true, true};
  105. bool zoneManual[] = {false,false,false,false};
  106. bool zoneAuto[] = {true,true,true,true};
  107. bool zonePump[] = {false,false,false,false};
  108. bool zoneManualPump[] = {false,false,false,false};
  109. bool zoneSoak[] = {false,false,false,false};
  110. int waterStatus[] = {0,0,0,0};
  111.  
  112.  
  113. // Sensor Data:
  114. int sensor[] = {0, 0, 0, 0}; // Sensor reading values array
  115. int topWtrLvl = 0;
  116. int btmWtrLvl = 0;
  117. int lowWater = 3500;
  118. int highWater = 3500;
  119.  
  120. // Timer Variables
  121.  
  122. const int sensorReadDelay[] = {5000, 5000, 5000, 5000}; // delay between sensor readings in millis
  123. unsigned long lastSensorReadTime[] = {0, 0, 0, 0}; // the last time a sensor was read
  124. int manualDayTimer[] = {86400000, 86400000, 86400000, 86400000}; // delay for x times a day manual watering mode
  125. unsigned long lastManualDayTimer[] = {0,0,0,0,};
  126. unsigned long pumpCountDown[] = {0,0,0,0};
  127. unsigned long pumpTimer[] = {60000, 60000, 60000, 60000}; //Pump timers in array
  128. unsigned long soakTimer[] = {43200000, 43200000, 43200000, 43200000}; //soak timers in array
  129. unsigned long lastPumpTimer[] = {0, 0, 0, 0}; // last value of Pump timers in array
  130. unsigned long lastSoakTimer[] = {0, 0, 0, 0}; // last value of soak timers in array
  131.  
  132. BlynkTimer timer;
  133.  
  134. void setup()
  135. {
  136. Serial.begin(115200);
  137. delay(100);
  138.  
  139. // Set OUT Pins to Output
  140. pinMode(output[0], OUTPUT);
  141. pinMode(output[1], OUTPUT);
  142. pinMode(output[2], OUTPUT);
  143. pinMode(output[3], OUTPUT);
  144. pinMode(mtrOutA1, OUTPUT);
  145. pinMode(mtrOutA2, OUTPUT);
  146. pinMode(mtrOutB1, OUTPUT);
  147. pinMode(mtrOutB2, OUTPUT);
  148.  
  149. // Set PWM
  150.  
  151. ledcSetup(pwmChannel, freq, resolution);
  152. ledcAttachPin(mtrOutA2, pwmChannel);
  153. ledcAttachPin(mtrOutB2, pwmChannel);
  154.  
  155. // Set Sensor Pins to Input
  156. pinMode(sensPin[0], INPUT);
  157. pinMode(sensPin[1], INPUT);
  158. pinMode(sensPin[2], INPUT);
  159. pinMode(sensPin[3], INPUT);
  160.  
  161. // Ensure Mosfets are pulled low
  162. digitalWrite(output[0], LOW);
  163. digitalWrite(output[1], LOW);
  164. digitalWrite(output[2], LOW);
  165. digitalWrite(output[3], LOW);
  166.  
  167.  
  168. // set Water Level Pins to Input
  169. pinMode(wtrLvlTop, INPUT);
  170. pinMode(wtrLvlBtm, INPUT);
  171.  
  172. //Blynk Timers
  173. BlynkEdgent.begin();
  174. timer.setInterval(250, blynkLoop);
  175.  
  176.  
  177. }
  178.  
  179. BLYNK_CONNECTED() {
  180. Blynk.syncAll();
  181. }
  182.  
  183. void zoneLoop() {
  184. for (zoneNumber = 0; zoneNumber < 3; zoneNumber++) {
  185. if (systemActive) {
  186. zoneControl();
  187. zoneControlManual();
  188. zoneControlPump();
  189. zoneSoakCycle();
  190. }
  191. else {
  192. zonePump[zoneNumber] = false;
  193. sensor[zoneNumber] = 0;
  194. digitalWrite(output[zoneNumber], LOW);
  195. }
  196. waterLevel();
  197. }
  198. for (zoneNumber = 3; zoneNumber > 0; zoneNumber = 0) {
  199. if (systemActive) {
  200. zoneControl();
  201. zoneControlManual();
  202. zoneControlPump();
  203. zoneSoakCycle();
  204. }
  205. else {
  206. zonePump[zoneNumber] = false;
  207. sensor[zoneNumber] = 0;
  208. digitalWrite(output[zoneNumber], LOW);
  209. }
  210. waterLevel();
  211. }
  212. }
  213.  
  214. void blynkLoop(){
  215. for (blynkZone = 0; blynkZone < 3; blynkZone++) {
  216. blynkData();
  217. }
  218. for (blynkZone = 3; blynkZone > 0; blynkZone = 0) {
  219. blynkData();
  220. }
  221. }
  222.  
  223. void blynkData(){
  224. if (systemActive) {
  225. Blynk.virtualWrite(V5, LOW);
  226. }
  227. else {
  228. Blynk.virtualWrite(V5, HIGH);
  229. }
  230. Blynk.virtualWrite(sensorData[blynkZone], sensor[blynkZone]);
  231. Blynk.virtualWrite(triggerDisplay[blynkZone], triggerLow[blynkZone]);
  232. Blynk.virtualWrite(pumpTimeArray[blynkZone], (pumpTimer[blynkZone] / 60000));
  233. Blynk.virtualWrite(soakTimeArray[blynkZone], (soakTimer[blynkZone] / 3600000));
  234. if (zonePump[blynkZone]) {
  235. Blynk.virtualWrite(ledArray[blynkZone], HIGH);
  236. }
  237. else {
  238. Blynk.virtualWrite(ledArray[blynkZone], LOW);
  239. }
  240. if (zoneSoak[blynkZone]) {
  241. Blynk.virtualWrite(soakLedArray[blynkZone], HIGH);
  242. }
  243. else {
  244. Blynk.virtualWrite(soakLedArray[blynkZone], LOW);
  245. }
  246. if (zoneManual[blynkZone]) {
  247. pumpCountDown[blynkZone] = ((unsigned long)(millis() - lastManualDayTimer[blynkZone])) - manualDayTimer[blynkZone];
  248. pumpCountDown[blynkZone] = pumpCountDown[blynkZone] * -1;
  249. Blynk.virtualWrite(countdownArray[blynkZone], pumpCountDown[blynkZone] / 3600000);
  250. }
  251. else {
  252. Blynk.virtualWrite(countdownArray[blynkZone], 0);
  253. }
  254. }
  255.  
  256.  
  257. void waterLevel()
  258. {
  259. int btmWtrLvl = digitalRead(wtrLvlBtm);
  260.  
  261. if (btmWtrLvl == 1) {
  262. systemActive = false;
  263. }
  264. else if (btmWtrLvl == 0) {
  265. systemActive = true;
  266. }
  267. }
  268.  
  269.  
  270. void zoneSoakCycle()
  271. {
  272. if (zoneSoak[zoneNumber]) {
  273. if ((unsigned long)(millis() - lastSoakTimer[zoneNumber]) >= soakTimer[zoneNumber])
  274. {
  275. zoneSoak[zoneNumber] = false;
  276. zoneActive[zoneNumber] = true;
  277. lastManualDayTimer[zoneNumber] = millis();
  278. }
  279. }
  280. }
  281.  
  282.  
  283. void zoneControlManual() {
  284. if (zoneManual[zoneNumber]) {
  285. if (!zonePump[zoneNumber] && !zoneManualPump[zoneNumber]) {
  286. digitalWrite(output[zoneNumber], LOW);
  287. sensor[zoneNumber] = 0;
  288. if ((unsigned long)(millis() - lastManualDayTimer[zoneNumber]) > manualDayTimer[zoneNumber]) {
  289. zoneManualPump[zoneNumber] = true;
  290. digitalWrite(output[zoneNumber], HIGH);
  291. lastPumpTimer[zoneNumber] = millis();
  292. lastManualDayTimer[zoneNumber] = millis();
  293. zoneManual[zoneNumber] = false;
  294. }
  295. }
  296. }
  297. }
  298.  
  299.  
  300. void zoneControl() {
  301. if (zoneAuto[zoneNumber]) {
  302. if (zoneActive[zoneNumber] && (!zonePump[zoneNumber] && !zoneManualPump[zoneNumber])) {
  303. sensor[zoneNumber] = analogRead(sensPin[zoneNumber]);
  304. digitalWrite(output[zoneNumber], LOW);
  305. if ((unsigned long)(millis() - lastSensorReadTime[zoneNumber]) >= sensorReadDelay[zoneNumber]) {
  306. lastSensorReadTime[zoneNumber] = millis();
  307.  
  308. if (sensor[zoneNumber] >= triggerLow[zoneNumber]) {
  309. zonePump[zoneNumber] = true;
  310. digitalWrite(output[zoneNumber], HIGH);
  311. lastPumpTimer[zoneNumber] = millis();
  312. zoneActive[zoneNumber] = false;
  313. }
  314. }
  315. }
  316. }
  317. }
  318.  
  319.  
  320. void zoneControlPump() {
  321. if (zonePump[zoneNumber]) {
  322. if ((unsigned long)(millis() - lastPumpTimer[zoneNumber]) >= pumpTimer[zoneNumber]) {
  323. digitalWrite(output[zoneNumber], LOW);
  324. zoneSoak[zoneNumber] = true;
  325. lastSoakTimer[zoneNumber] = millis();
  326. lastSensorReadTime[zoneNumber] = millis();
  327. zonePump[zoneNumber] = false;
  328. zoneManualPump[zoneNumber] = false;
  329. lastManualDayTimer[zoneNumber] = millis();
  330. }
  331. }
  332. else if (zoneManualPump[zoneNumber]) {
  333. if ((unsigned long)(millis() - lastPumpTimer[zoneNumber]) >= pumpTimer[zoneNumber]) {
  334. digitalWrite(output[zoneNumber], LOW);
  335. zoneManual[zoneNumber] = true;
  336. zonePump[zoneNumber] = false;
  337. zoneManualPump[zoneNumber] = false;
  338. lastManualDayTimer[zoneNumber] = millis();
  339. }
  340. }
  341. else if (!zoneManualPump[zoneNumber] && !zonePump[zoneNumber]) {
  342. digitalWrite(output[zoneNumber], LOW);
  343. }
  344. }
  345.  
  346.  
  347.  
  348. BLYNK_WRITE(V15)
  349. {
  350. triggerLow[0] = param.asInt();
  351. triggerHigh[0] = triggerLow[0] - triggerSpread[0];
  352. }
  353.  
  354. BLYNK_WRITE(V16)
  355. {
  356. triggerLow[1] = param.asInt();
  357. triggerHigh[1] = triggerLow[1] - triggerSpread[1];
  358. }
  359.  
  360. BLYNK_WRITE(V17)
  361. {
  362. triggerLow[2] = param.asInt();
  363. triggerHigh[2] = triggerLow[2] - triggerSpread[2];
  364. }
  365.  
  366. BLYNK_WRITE(V18)
  367. {
  368. triggerLow[3] = param.asInt();
  369. triggerHigh[3] = triggerLow[3] - triggerSpread[3];
  370. }
  371.  
  372. BLYNK_WRITE(V19)
  373. {
  374. zoneState[0] = param.asInt();
  375. if (zoneState[0] == 0) {
  376. zoneActive[0] = false;
  377. zoneManual[0] = true;
  378. lastPumpTimer[0] = millis();
  379. lastManualDayTimer[0] = millis();
  380. digitalWrite(output[0], LOW);
  381. ledStatus[0] = 0;
  382. Blynk.virtualWrite(ledArray[0], LOW);
  383. Blynk.virtualWrite(soakLedArray[0], LOW);
  384. }
  385. else {
  386. zoneActive[0] = true;
  387. zoneSoak[0] = false;
  388. zoneManual[0] = false;
  389. lastPumpTimer[0] = millis();
  390. lastManualDayTimer[0] = millis();
  391. }
  392. }
  393.  
  394. BLYNK_WRITE(V20)
  395. {
  396. zoneState[1] = param.asInt();
  397. if (zoneState[1] == 0) {
  398. zoneActive[1] = false;
  399. zoneManual[1] = true;
  400. lastPumpTimer[1] = millis();
  401. lastManualDayTimer[1] = millis();
  402. digitalWrite(output[1], LOW);
  403. ledStatus[1] = 0;
  404. Blynk.virtualWrite(ledArray[1], LOW);
  405. Blynk.virtualWrite(soakLedArray[1], LOW);
  406. }
  407. else {
  408. zoneActive[1] = true;
  409. zoneSoak[1] = false;
  410. zoneManual[1] = false;
  411. lastPumpTimer[1] = millis();
  412. lastManualDayTimer[1] = millis();
  413. }
  414. }
  415.  
  416. BLYNK_WRITE(V21)
  417. {
  418. zoneState[2] = param.asInt();
  419. if (zoneState[2] == 0){
  420. zoneActive[2] = false;
  421. zoneManual[2] = true;
  422. lastPumpTimer[2] = millis();
  423. lastManualDayTimer[2] = millis();
  424. digitalWrite(output[2], LOW);
  425. ledStatus[2] = 0;
  426. Blynk.virtualWrite(ledArray[2], LOW);
  427. Blynk.virtualWrite(soakLedArray[2], LOW);
  428. }
  429. else {
  430. zoneActive[2] = true;
  431. zoneSoak[2] = false;
  432. zoneManual[2] = false;
  433. lastPumpTimer[2] = millis();
  434. lastManualDayTimer[2] = millis();
  435. }
  436. }
  437.  
  438. BLYNK_WRITE(V22)
  439. {
  440. zoneState[3] = param.asInt();
  441. if (zoneState[3] == 0){
  442. zoneActive[3] = false;
  443. zoneManual[3] = true;
  444. lastPumpTimer[3] = millis();
  445. lastManualDayTimer[3] = millis();
  446. digitalWrite(output[3], LOW);
  447. ledStatus[3] = 0;
  448. Blynk.virtualWrite(ledArray[3], LOW);
  449. Blynk.virtualWrite(soakLedArray[3], LOW);
  450. }
  451. else {
  452. zoneActive[3] = true;
  453. zoneSoak[3] = false;
  454. zoneManual[3] = false;
  455. lastPumpTimer[3] = millis();
  456. lastManualDayTimer[3] = millis();
  457. }
  458. }
  459.  
  460.  
  461. BLYNK_WRITE(V31)
  462. {
  463. if (zoneSoak[0]) {
  464. zoneSoak[0] = false;
  465. zoneActive[0] = true;
  466. }
  467. zonePump[0] = true;
  468. digitalWrite(output[0], HIGH);
  469. lastPumpTimer[0] = millis();
  470. zoneActive[0] = false;
  471. lastPumpTimer[0] = millis();
  472. }
  473.  
  474. BLYNK_WRITE(V32)
  475. {
  476. if (zoneSoak[1]) {
  477. zoneSoak[1] = false;
  478. zoneActive[1] = true;
  479. }
  480. zonePump[1] = true;
  481. digitalWrite(output[1], HIGH);
  482. lastPumpTimer[1] = millis();
  483. zoneActive[1] = false;
  484. lastPumpTimer[1] = millis();
  485. }
  486.  
  487. BLYNK_WRITE(V33)
  488. {
  489. if (zoneSoak[2]) {
  490. zoneSoak[2] = false;
  491. zoneActive[2] = true;
  492. }
  493. zonePump[2] = true;
  494. digitalWrite(output[2], HIGH);
  495. lastPumpTimer[2] = millis();
  496. zoneActive[2] = false;
  497. lastPumpTimer[2] = millis();
  498. }
  499.  
  500. BLYNK_WRITE(V34)
  501. {
  502. if (zoneSoak[3]) {
  503. zoneSoak[3] = false;
  504. zoneActive[3] = true;
  505. }
  506. zonePump[3] = true;
  507. digitalWrite(output[3], HIGH);
  508. lastPumpTimer[3] = millis();
  509. zoneActive[3] = false;
  510. lastPumpTimer[3] = millis();
  511. }
  512.  
  513. BLYNK_WRITE(V39)
  514. {
  515. manualDayTimer[0] = param.asInt();
  516. }
  517.  
  518. BLYNK_WRITE(V40)
  519. {
  520. manualDayTimer[1] = param.asInt();
  521. }
  522.  
  523. BLYNK_WRITE(V41)
  524. {
  525. manualDayTimer[2] = param.asInt();
  526. }
  527.  
  528. BLYNK_WRITE(V42)
  529. {
  530. manualDayTimer[3] = param.asInt();
  531. }
  532.  
  533. BLYNK_WRITE(V23)
  534. {
  535. soakTimer[0] = (param.asInt() * 3600000);
  536. }
  537.  
  538. BLYNK_WRITE(V24)
  539. {
  540. soakTimer[1] = (param.asInt() * 3600000);
  541. }
  542.  
  543. BLYNK_WRITE(V25)
  544. {
  545. soakTimer[2] = (param.asInt() * 3600000);
  546. }
  547.  
  548. BLYNK_WRITE(V26)
  549. {
  550. soakTimer[3] = (param.asInt() * 3600000);
  551. }
  552.  
  553. BLYNK_WRITE(V43)
  554. {
  555. pumpTimer[0] = (param.asInt() * 60000);
  556. }
  557.  
  558. BLYNK_WRITE(V44)
  559. {
  560. pumpTimer[1] = (param.asInt() * 60000);
  561. }
  562.  
  563. BLYNK_WRITE(V45)
  564. {
  565. pumpTimer[2] = (param.asInt() * 60000);
  566. }
  567.  
  568. BLYNK_WRITE(V46)
  569. {
  570. pumpTimer[3] = (param.asInt() * 60000);
  571. }
  572.  
  573. BLYNK_WRITE(V51)
  574. {
  575. if (param.asInt() == 0) {
  576. zoneActive[0] = false;
  577. zonePump[0] = false;
  578. zoneSoak[0] = false;
  579. digitalWrite(output[0], LOW);
  580. ledStatus[0] = 0;
  581. sensor[0] = 0;
  582. Blynk.virtualWrite(ledArray[0], LOW);
  583. Blynk.virtualWrite(soakLedArray[0], LOW);
  584. }
  585. else {
  586. zoneActive[0] = true;
  587. }
  588. }
  589.  
  590. BLYNK_WRITE(V52)
  591. {
  592. if (param.asInt() == 0) {
  593. zoneActive[1] = false;
  594. zonePump[1] = false;
  595. zoneSoak[1] = false;
  596. digitalWrite(output[1], LOW);
  597. ledStatus[1] = 0;
  598. sensor[1] = 0;
  599. Blynk.virtualWrite(ledArray[1], LOW);
  600. Blynk.virtualWrite(soakLedArray[1], LOW);
  601. }
  602. else {
  603. zoneActive[1] = true;
  604. }
  605. }
  606.  
  607. BLYNK_WRITE(V53)
  608. {
  609. if (param.asInt() == 0) {
  610. zoneActive[2] = false;
  611. zonePump[2] = false;
  612. zoneSoak[2] = false;
  613. digitalWrite(output[2], LOW);
  614. ledStatus[2] = 0;
  615. sensor[2] = 0;
  616. Blynk.virtualWrite(ledArray[2], LOW);
  617. Blynk.virtualWrite(soakLedArray[2], LOW);
  618. }
  619. else {
  620. zoneActive[2] = true;
  621. }
  622. }
  623.  
  624. BLYNK_WRITE(V54)
  625. {
  626. if (param.asInt() == 0) {
  627. zoneActive[3] = false;
  628. zonePump[3] = false;
  629. zoneSoak[3] = false;
  630. digitalWrite(output[3], LOW);
  631. ledStatus[3] = 0;
  632. sensor[3] = 0;
  633. Blynk.virtualWrite(ledArray[3], LOW);
  634. Blynk.virtualWrite(soakLedArray[3], LOW);
  635. }
  636. else {
  637. zoneActive[3] = true;
  638. }
  639. }
  640.  
  641. void loop() {
  642. BlynkEdgent.run();
  643. timer.run();
  644. zoneLoop();
  645. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement