Advertisement
hms11

ESP32ZoneCommandEdgentRev0.1.9

Feb 24th, 2022
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.91 KB | None | 0 0
  1.  
  2. // Fill-in information from your Blynk Template here
  3. #define BLYNK_TEMPLATE_ID "TMPL3bTnPK_y"
  4. #define BLYNK_DEVICE_NAME "ZoneCommand"
  5.  
  6. #define BLYNK_FIRMWARE_VERSION "0.1.9"
  7.  
  8. #define BLYNK_PRINT Serial
  9. //#define BLYNK_DEBUG
  10.  
  11. #define APP_DEBUG
  12.  
  13. // Uncomment your board, or configure a custom board in Settings.h
  14. //#define USE_WROVER_BOARD
  15. //#define USE_TTGO_T7
  16.  
  17. #include "BlynkEdgent.h"
  18.  
  19. //Widget Setups
  20.  
  21. WidgetLED led1(V4);
  22. WidgetLED led2(V5);
  23. WidgetLED led3(V6);
  24. WidgetLED led4(V7);
  25. WidgetLED led5(V8);
  26. WidgetLED led6(V9);
  27. WidgetLED led7(V10);
  28. const int ledArray[] = {V7,V8,V9,V10};
  29. int ledStatus[] = {0,0,0,0};
  30.  
  31. // Set Pin Assignments For Output Pins
  32.  
  33. // MOSFETS:
  34. const int output[] = {4,16,17,18};
  35. //Motor Drivers:
  36. //U4 Motor Driver:
  37. const int mtrOutA1 = 22;
  38. const int mtrOutA2 = 23;
  39. //U7 Motor Driver:
  40. const int mtrOutB1 = 19;
  41. const int mtrOutB2 = 21;
  42.  
  43. //Set Pin Assignments for Input Pins
  44.  
  45. //Analog Inputs:
  46.  
  47. //Moisture Sensors:
  48. const int sensPin[] = {35,34,39,36};
  49.  
  50. //Water Level Sensors:
  51. const int wtrLvlTop = 26;
  52. const int wtrLvlBtm = 27;
  53.  
  54.  
  55. //Zone Loop Controls
  56.  
  57. int zoneNumber = 0;
  58. int blynkZone = 0;
  59. int triggerHigh[] = {30,30,30,30};
  60. int triggerLow[] = {70,70,70,70};
  61. bool zoneActive[] = {true, true, true, true};
  62. bool zoneManual[] = {false,false,false,false};
  63. bool zoneAuto[] = {true,true,true,true};
  64.  
  65. // Sensor Data:
  66. int sensor[] = {0, 0, 0, 0}; // Sensor reading values array
  67. int topWtrLvl = 0;
  68. int btmWtrLvl = 0;
  69. int lowWater = 700;
  70. int highWater = 500;
  71.  
  72. // Timer Variables
  73.  
  74. const int manualDayTimer[] = {86400000, 86400000, 86400000, 86400000}; // delay for once a day manual watering mode
  75. unsigned long lastManualDayTimer[] = {0, 0, 0, 0,};
  76. unsigned long pumpTimer[] = {60000, 60000, 60000, 60000}; //Pump timers in array
  77. unsigned long lastPumpTimer[] = {0, 0, 0, 0}; // last value of Pump timers in array
  78.  
  79. BlynkTimer timer;
  80.  
  81. void setup()
  82. {
  83. // Set OUT Pins to Output
  84. pinMode(output[0], OUTPUT);
  85. pinMode(output[1], OUTPUT);
  86. pinMode(output[2], OUTPUT);
  87. pinMode(output[3], OUTPUT);
  88. // Set Sensor Pins to Input
  89. pinMode(sensPin[0], INPUT);
  90. pinMode(sensPin[1], INPUT);
  91. pinMode(sensPin[2], INPUT);
  92. pinMode(sensPin[3], INPUT);
  93.  
  94. // set Water Level Pins to Input
  95. pinMode(wtrLvlTop, INPUT);
  96. pinMode(wtrLvlBtm, INPUT);
  97.  
  98. //Blynk Timers
  99. BlynkEdgent.begin();
  100. timer.setInterval(500L,blynkData);
  101. timer.setInterval(1000L,waterLevel);
  102. //timer.setInterval(500, zoneWater);
  103.  
  104. Serial.begin(115200);
  105. delay(100);
  106.  
  107. }
  108.  
  109.  
  110. void sensorRead()
  111. {
  112. for (zoneNumber = 0; zoneNumber < 3; zoneNumber++) {
  113. if (zoneActive[zoneNumber]) {
  114. sensor[zoneNumber] = analogRead(sensPin[zoneNumber]);
  115. sensor[zoneNumber] = map(sensor[zoneNumber], 0, 4095, 0, 100);
  116. if (sensor[zoneNumber] > triggerLow[zoneNumber]) {
  117. zoneActive[zoneNumber] = false;
  118. digitalWrite(zoneActive[zoneNumber], HIGH);
  119. timer.setTimeout(60000, []()
  120. {
  121. zoneActive[zoneNumber] = true;
  122. digitalWrite(zoneActive[zoneNumber], LOW);
  123. });
  124. }
  125. Serial.println(sensor[zoneNumber]);
  126. }
  127. }
  128. for (zoneNumber = 3; zoneNumber > 0; zoneNumber = 0) {
  129. if (zoneActive[zoneNumber]) {
  130. sensor[zoneNumber] = analogRead(sensPin[zoneNumber]);
  131. sensor[zoneNumber] = map(sensor[zoneNumber], 0, 4095, 0, 100);
  132. if (sensor[zoneNumber] > triggerLow[zoneNumber]) {
  133. zoneActive[zoneNumber] = false;
  134. digitalWrite(zoneActive[zoneNumber], HIGH);
  135. timer.setTimeout(60000, []()
  136. {
  137. zoneActive[zoneNumber] = true;
  138. digitalWrite(zoneActive[zoneNumber], LOW);
  139. });
  140. }
  141. Serial.println(sensor[zoneNumber]);
  142. }
  143. }
  144. }
  145.  
  146.  
  147. void blynkData()
  148. {
  149. Blynk.virtualWrite(V0, sensor[0]);
  150. Blynk.virtualWrite(V1, sensor[1]);
  151. Blynk.virtualWrite(V2, sensor[2]);
  152. Blynk.virtualWrite(V3, sensor[3]);
  153. for (blynkZone = 0; blynkZone < 3; blynkZone++) {
  154. ledStatus[blynkZone] = digitalRead(output[blynkZone]);
  155. if (ledStatus[blynkZone] == HIGH) {
  156. Blynk.virtualWrite(ledArray[blynkZone], HIGH);
  157. }
  158. else {
  159. Blynk.virtualWrite(ledArray[blynkZone], LOW);
  160. }
  161. }
  162. for (blynkZone = 3; blynkZone > 0; blynkZone = 0) {
  163. ledStatus[blynkZone] = digitalRead(output[blynkZone]);
  164. if (ledStatus[blynkZone] == HIGH) {
  165. Blynk.virtualWrite(ledArray[blynkZone], HIGH);
  166. }
  167. else {
  168. Blynk.virtualWrite(ledArray[blynkZone], LOW);
  169. }
  170. }
  171.  
  172. }
  173.  
  174. void waterLevel()
  175. {
  176. int topWtrLvl = analogRead(wtrLvlTop);
  177. int btmWtrLvl = analogRead(wtrLvlBtm);
  178. if (btmWtrLvl > lowWater) {
  179. digitalWrite(mtrOutA1, HIGH);
  180. digitalWrite(mtrOutA2, LOW);
  181. led1.on();
  182. led2.on();
  183. }
  184. if (topWtrLvl < highWater) {
  185. digitalWrite(mtrOutA1, LOW);
  186. digitalWrite(mtrOutA2, LOW);
  187. led1.off();
  188. led2.off();
  189. led3.on();
  190. }
  191. }
  192.  
  193. void loop() {
  194. BlynkEdgent.run();
  195. timer.run();
  196. sensorRead();
  197. }
  198.  
  199.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement