Advertisement
hms11

CoopCommandEsp32-0.1.2

Mar 8th, 2022
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.72 KB | None | 0 0
  1.  
  2. // Fill-in information from your Blynk Template here
  3. #define BLYNK_TEMPLATE_ID "TMPL-flCABCq"
  4. #define BLYNK_DEVICE_NAME "CoopCommand"
  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. #include <DallasTemperature.h>
  19. #include <OneWire.h>
  20. #include <DHT.h>
  21.  
  22. BlynkTimer timer;
  23.  
  24. // DS18B20 Sensor Setup
  25.  
  26. #define ONE_WIRE_BUS 25
  27. OneWire oneWire(ONE_WIRE_BUS);
  28. DallasTemperature sensors(&oneWire);
  29.  
  30. //DHT Setup
  31. #define DHTPIN 34 // Pin for DHT sensor
  32. #define DHTTYPE DHT22 // DHT 22 (AM2302)
  33. DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor
  34.  
  35. // pin assignments
  36. const int photocellPin = 35; // analog pin for photocell
  37. const int bottomSwitchPin = 26; // bottom switch is connected to pin 26
  38. const int topSwitchPin = 27; // top switch is connected to pin 27
  39. const int directionCloseCoopDoorMotorB = 19; // direction close motor b - pin 19
  40. const int directionOpenCoopDoorMotorB = 21; // direction open motor b - pin 21
  41. const int layLightRelay = 4; // output pin controlling lay light relay
  42. const int fanRelay = 16; // output pin controlling ventilation fan relay
  43. const int heatRelay = 17; // output pin controlling water heater relay
  44.  
  45. // Sensor Variables
  46. bool doorOpen = false; // is the coop door open
  47. bool doorClosed = false; // is the door closed
  48. bool doorOpenMove = false; // is the door opening?
  49. bool doorCloseMove = false; // is the door closing?
  50. int topSwitchState; // Current state (open/closed) of the top limit switch
  51. int bottomSwitchState; // Current state (open/closed) of the bottom limit switch
  52. bool doorSensor = true; // is the door in automatic or manual mode
  53. bool ventOn = false; // is the ventilation fan relay on or off
  54. bool heaterOn = true; // is the water heater function running
  55. bool nightTimer = false; // is it night time
  56. bool layLightOn = true; // is the Lay Light time monitoring system on
  57. bool nightLightOn = false; // is the Night Light on
  58. int coopTemp = 0; // Interior Coop Temperature Reading
  59. int closeDoor = 20; // Light level to close coop door (user editable, EEPROM saved)
  60. int openDoor = closeDoor + 10; // Light level to open coop door
  61. int hotTemp = 30; // Temperature to turn on Ventilation Fan Relay (user editable, EEPROM saved)
  62. int coldTemp = 3; // Temperature to turn on Water Heat Relay (user editable, EEPROM saved)
  63. float waterTemp = 0; // Water Tempterature Reading
  64. float hum; // Stores humidity value from DHT22
  65. float temp; // Stores temperature value from DHT22
  66. int photocellReading; // analog reading of the photocell
  67. int photocellReadingLevel = '2'; // photocell reading levels (night, light, twilight)
  68.  
  69. // Timer Variables
  70. unsigned long layLightTimer = 36000000; // Timer to make sure at least 14 hours or "daylight"
  71. unsigned long lastDayLightReadingTime = 0; // timer to keep track of how long it has been night
  72. unsigned long nightLightDelay = 300000; // 5 minute timer to turn on the coop light if "enter" is pushed and it is night.
  73. unsigned long lastNightLightTime = 0; // the last time the night light button was pushed
  74.  
  75. void setup()
  76. {
  77. Serial.begin(115200);
  78. delay(100);
  79. dht.begin();
  80. sensors.begin();
  81. BlynkEdgent.begin();
  82. pinMode(photocellPin, INPUT);
  83. pinMode(topSwitchPin, INPUT);
  84. pinMode(bottomSwitchPin, INPUT);
  85. pinMode(layLightRelay, OUTPUT);
  86. pinMode(fanRelay, OUTPUT);
  87. pinMode(heatRelay, OUTPUT);
  88. pinMode(directionCloseCoopDoorMotorB, OUTPUT);
  89. pinMode(directionOpenCoopDoorMotorB, OUTPUT);
  90. photocellReading = analogRead(photocellPin);
  91. photocellReading = map(photocellReading, 0, 1023, 0, 100);
  92. timer.setInterval(2000,readSensor);
  93. timer.setInterval(600000,doorControl);
  94. timer.setInterval(1000,ventFan);
  95. timer.setInterval(1000,waterHeat);
  96. timer.setInterval(2000,layLight);
  97. }
  98.  
  99. void layLight() {
  100. if (layLightOn) {
  101. if (!nightTimer) { // if it is not dark
  102. lastDayLightReadingTime = millis();
  103. digitalWrite(layLightRelay, LOW); // turn off the lay light
  104. } else { // if it is dark
  105. if ((unsigned long)(millis() - lastDayLightReadingTime) >= layLightTimer) { //if it has been dark more than 10 hours (or whatever the timer is
  106. digitalWrite(layLightRelay, HIGH); // turn on the lay light
  107. Blynk.virtualWrite(V11, HIGH);
  108. } else {
  109. digitalWrite(layLightRelay, LOW); // turn off the lay light
  110. Blynk.virtualWrite(V11, LOW);
  111. }
  112. }
  113. }
  114. if (nightLightOn) { // if someone wants the light on
  115. digitalWrite(layLightRelay, HIGH);
  116. Blynk.virtualWrite(V11, HIGH);
  117. }
  118. else if ((unsigned long)(millis() - lastNightLightTime) >= nightLightDelay) {
  119. digitalWrite (layLightRelay, LOW);
  120. Blynk.virtualWrite(V11, LOW);
  121. nightLightOn = false;
  122. }
  123. }
  124.  
  125. void readSensor()
  126. {
  127. //Read Photocell
  128. photocellReading = analogRead(photocellPin);
  129. photocellReading = map(photocellReading, 0, 1023, 0, 100);
  130. //Read DS18B20 Water Temp Sensor
  131. sensors.requestTemperatures();
  132. waterTemp = sensors.getTempCByIndex(0);
  133. Blynk.virtualWrite(V7, waterTemp);
  134. //Read DHT22
  135. hum = dht.readHumidity();
  136. temp = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
  137. Blynk.virtualWrite(V13, hum);
  138. Blynk.virtualWrite(V0, temp);
  139. }
  140.  
  141. void doorControl()
  142. {
  143. if (photocellReading >= 0 && photocellReading <= closeDoor) { // Night Setting based on user or default selected low light trigger
  144. if (doorSensor) {
  145. photocellReadingLevel = '1';
  146. }
  147. nightTimer = true;
  148. }
  149. else if (photocellReading >= closeDoor && photocellReading <= openDoor) { // Twighlight setting
  150. if (doorSensor) {
  151. photocellReadingLevel = '2';
  152. }
  153. }
  154. else if (photocellReading >= (openDoor + 1) ) { //Daylight Setting
  155. if (doorSensor) {
  156. photocellReadingLevel = '3';
  157. }
  158. nightTimer = false;
  159. }
  160. }
  161.  
  162. void waterHeat()
  163. {
  164. if (heaterOn) {
  165. if (waterTemp == -127) {
  166. digitalWrite(heatRelay, LOW); //turn off the water heater
  167. waterTemp = 0;
  168. heaterOn = false;
  169. }
  170. else if (waterTemp >= (coldTemp + 3)) { // if the temperature is 3 degrees above the trigger temp
  171. digitalWrite(heatRelay, LOW); //turn off the water heater
  172. }
  173. else if (waterTemp < coldTemp) { //if the temperature is below the cold temperature
  174. digitalWrite(heatRelay, HIGH); //turn on the water heater
  175. }
  176. }
  177. }
  178.  
  179. void ventFan()
  180. {
  181. if (coopTemp >= hotTemp) { // if the temperature is above the Hot temperature
  182. digitalWrite(fanRelay, HIGH);
  183. ventOn = true;
  184. }
  185. else if (coopTemp < (hotTemp - 2)) { // if the temperature has been lowered two degrees
  186. digitalWrite(fanRelay, LOW);
  187. ventOn = false;
  188. }
  189. }
  190.  
  191. void readSwitches() {
  192. topSwitchState = (digitalRead(topSwitchPin));
  193. bottomSwitchState = (digitalRead(bottomSwitchPin));
  194. }
  195.  
  196. // stop the coop door motor and put the motor driver IC to sleep (power saving)
  197. void stopCoopDoorMotorB() {
  198. digitalWrite (directionCloseCoopDoorMotorB, LOW); // turn off motor close direction
  199. digitalWrite (directionOpenCoopDoorMotorB, LOW); // turn off motor open direction
  200. }
  201.  
  202. // close the coop door motor
  203. void closeCoopDoorMotorB() {
  204. if (bottomSwitchState == 1) { //if the bottom reed switch is open
  205. digitalWrite (directionCloseCoopDoorMotorB, HIGH); // turn on motor close direction
  206. digitalWrite (directionOpenCoopDoorMotorB, LOW); // turn off motor open direction
  207. }
  208. if (bottomSwitchState == 1 && topSwitchState == 1) { // if both reed switches are open (door is moving)
  209.  
  210. }
  211. if (bottomSwitchState == 0) { // if bottom reed switch circuit is closed
  212. stopCoopDoorMotorB();
  213. }
  214. }
  215.  
  216. // open the coop door
  217. void openCoopDoorMotorB() {
  218. if (topSwitchState == 1) { //if the top reed switch is open
  219. digitalWrite(directionCloseCoopDoorMotorB, LOW); // turn off motor close direction
  220. digitalWrite(directionOpenCoopDoorMotorB, HIGH); // turn on motor open direction
  221. }
  222. if ((bottomSwitchState == 1) && (topSwitchState == 1)) { // if both reed switches are open
  223. }
  224. if (topSwitchState == 0) { // if top reed switch circuit is closed
  225. stopCoopDoorMotorB();
  226. }
  227. }
  228.  
  229. // do the coop door
  230. void doCoopDoor() {
  231. if (photocellReadingLevel == '1') { // if it's dark
  232. readSwitches();
  233. closeCoopDoorMotorB(); // close the door
  234. }
  235. else if (photocellReadingLevel == '3') { // if it's light
  236. readSwitches();
  237. openCoopDoorMotorB(); // Open the door
  238. }
  239. else if (photocellReadingLevel == '2') { // if it's twilight
  240. readSwitches();
  241. stopCoopDoorMotorB();
  242. }
  243. }
  244.  
  245. BLYNK_WRITE (V7)
  246. {
  247. photocellReadingLevel = '3';
  248. }
  249.  
  250. BLYNK_WRITE (V6)
  251. {
  252. photocellReadingLevel = '1';
  253. }
  254.  
  255. BLYNK_WRITE (V10)
  256. {
  257. if (doorSensor) {
  258. doorSensor = false;
  259. Blynk.virtualWrite(V9, HIGH);
  260. }
  261. else {
  262. doorSensor = true;
  263. Blynk.virtualWrite(V9, LOW);
  264. }
  265. }
  266.  
  267.  
  268.  
  269.  
  270. void loop() {
  271. BlynkEdgent.run();
  272. doCoopDoor();
  273. }
  274.  
  275.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement