Advertisement
hms11

ESP32ZoneCommandEdgent0.7.3

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