Advertisement
hms11

CoopCommandEsp32-0.1.4

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