Advertisement
hms11

ESP32ZoneCommandEdgent

Feb 23rd, 2022
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 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.2"
  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. // Set Pin Assignments For Output Pins
  20.  
  21. // MOSFETS:
  22. const int output[] = {4,16,17,18};
  23. //Motor Drivers:
  24. //U4 Motor Driver:
  25. const int mtrOutA1 = 22;
  26. const int mtrOutA2 = 23;
  27. //U7 Motor Driver:
  28. const int mtrOutB1 = 19;
  29. const int mtrOutB2 = 21;
  30.  
  31. //Set Pin Assignments for Input Pins
  32.  
  33. //Analog Inputs:
  34.  
  35. //Moisture Sensors:
  36. const int sensPin[] = {35,34,39,36};
  37.  
  38. //Water Level Sensors:
  39. const int wtrLvlTop = 26;
  40. const int wtrLvlBtm = 27;
  41.  
  42.  
  43. //Zone Loop Controls
  44.  
  45. int zoneNumber = 0;
  46. int triggerHigh[] = {550,550,550,550};
  47. int triggerLow[] = {670,670,670,670};
  48. bool zoneActive[] = {true, true, true, true};
  49. bool zoneManual[] = {false,false,false,false};
  50. bool zoneAuto[] = {true,true,true,true};
  51. bool zoneAutoTimer[] = {false,false,false,false};
  52. bool zonePump[] = {false,false,false,false};
  53. bool zoneManualPump[] = {false,false,false,false};
  54.  
  55. // Sensor Data:
  56. int sensor[] = {0, 0, 0, 0}; // Sensor reading values array
  57. int topWtrLvl = 0;
  58. int btmWtrLvl = 0;
  59. int lowWater = 700;
  60. int highWater = 500;
  61.  
  62. // Timer Variables
  63.  
  64. const int manualDayTimer[] = {86400000, 86400000, 86400000, 86400000}; // delay for once a day manual watering mode
  65. unsigned long lastManualDayTimer[] = {0, 0, 0, 0,};
  66. unsigned long pumpTimer[] = {60000, 60000, 60000, 60000}; //Pump timers in array
  67. unsigned long lastPumpTimer[] = {0, 0, 0, 0}; // last value of Pump timers in array
  68.  
  69. BlynkTimer timer;
  70.  
  71. void setup()
  72. {
  73. // Set OUT Pins to Output
  74. pinMode(output[0], OUTPUT);
  75. pinMode(output[1], OUTPUT);
  76. pinMode(output[2], OUTPUT);
  77. pinMode(output[3], OUTPUT);
  78. // Set Sensor Pins to Input
  79. pinMode(sensPin[0], INPUT);
  80. pinMode(sensPin[1], INPUT);
  81. pinMode(sensPin[2], INPUT);
  82. pinMode(sensPin[3], INPUT);
  83.  
  84. // set Water Level Pins to Input
  85. pinMode(wtrLvlTop, INPUT);
  86. pinMode(wtrLvlBtm, INPUT);
  87.  
  88. //Blynk Timers
  89. BlynkEdgent.begin();
  90. timer.setInterval(1000,sensorRead);
  91. timer.setInterval(1000,blynkData);
  92. timer.setInterval(1000,waterLevel);
  93. //timer.setInterval(500, zonePump);
  94.  
  95. Serial.begin(115200);
  96. delay(100);
  97.  
  98. }
  99.  
  100. void sensorRead()
  101. {
  102. for (zoneNumber = 0; zoneNumber < 3; zoneNumber++) {
  103. if (zoneActive[zoneNumber]) {
  104. sensor[zoneNumber] = analogRead(sensPin[zoneNumber]);
  105. Serial.println(sensor[zoneNumber];
  106. }
  107. }
  108. for (zoneNumber = 3; zoneNumber > 0; zoneNumber = 0) {
  109. if (zoneActive[zoneNumber]) {
  110. sensor[zoneNumber] = analogRead(sensPin[zoneNumber]);
  111. }
  112. }
  113. }
  114.  
  115. void blynkData()
  116. {
  117. Blynk.virtualWrite(V0, sensor[0]);
  118. Blynk.virtualWrite(V1, sensor[1]);
  119. Blynk.virtualWrite(V2, sensor[2]);
  120. Blynk.virtualWrite(V3, sensor[3]);
  121.  
  122.  
  123. }
  124.  
  125. void waterLevel()
  126. {
  127. int topWtrLvl = analogRead(wtrLvlTop);
  128. int btmWtrLvl = analogRead(wtrLvlBtm);
  129. if (btmWtrLvl > lowWater) {
  130. digitalWrite(mtrOutA1, HIGH);
  131. digitalWrite(mtrOutA2, LOW);
  132. Blynk.virtualWrite(V4, HIGH);
  133. }
  134. if (topWtrLvl < highWater) {
  135. digitalWrite(mtrOutA1, LOW);
  136. digitalWrite(mtrOutA2, LOW);
  137. Blynk.virtualWrite(V4, LOW);
  138. }
  139. }
  140.  
  141. void loop() {
  142. BlynkEdgent.run();
  143. }
  144.  
  145.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement