Advertisement
hms11

CoopCommandEsp32-0.1.6

Mar 8th, 2022
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.82 KB | None | 0 0
  1. // Fill-in information from your Blynk Template here
  2. #define BLYNK_TEMPLATE_ID "TMPL-flCABCq"
  3. #define BLYNK_DEVICE_NAME "CoopCommand"
  4.  
  5. #define BLYNK_FIRMWARE_VERSION "0.1.6"
  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. #include <DallasTemperature.h>
  18. #include <OneWire.h>
  19. #include <DHT.h>
  20.  
  21. // Widget Setups
  22.  
  23. WidgetLED led1(V1);
  24. WidgetLED led2(V2);
  25. WidgetLED led3(V3);
  26. WidgetLED led4(V4);
  27. WidgetLED led5(V14);
  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(1000,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.  
  122. if (nightLightOn) { // if someone wants the light on
  123. digitalWrite(layLightRelay, HIGH);
  124. Blynk.virtualWrite(V11, HIGH);
  125. }
  126. else if ((unsigned long)(millis() - lastNightLightTime) >= nightLightDelay) {
  127. digitalWrite (layLightRelay, LOW);
  128. Blynk.virtualWrite(V11, LOW);
  129. nightLightOn = false;
  130. }
  131. }
  132.  
  133. void readSensor()
  134. {
  135. //Read Photocell
  136. photocellReading = analogRead(photocellPin);
  137. photocellReading = map(photocellReading, 0, 1023, 0, 100);
  138. Blynk.virtualWrite(V8, photocellReading);
  139. //Read DS18B20 Water Temp Sensor
  140. sensors.requestTemperatures();
  141. waterTemp = sensors.getTempCByIndex(0);
  142. if (waterTemp == -127) {
  143. waterTemp = 0;
  144. heaterOn = false;
  145. }
  146. else {
  147. heaterOn = true;
  148. }
  149. Blynk.virtualWrite(V7, waterTemp);
  150. //Read DHT22
  151. hum = dht.readHumidity();
  152. temp = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
  153. Blynk.virtualWrite(V13, hum);
  154. Blynk.virtualWrite(V0, temp);
  155.  
  156. if (nightLightOn) {
  157. Blynk.virtualWrite(V14, HIGH);
  158. }
  159. else {
  160. Blynk.virtualWrite(V14, LOW);
  161. }
  162.  
  163. if (doorOpen) {
  164. Blynk.virtualWrite(V1, HIGH);
  165. }
  166. else {
  167. Blynk.virtualWrite(V1, LOW);
  168. }
  169. if (doorClosed) {
  170. Blynk.virtualWrite(V2, HIGH);
  171. }
  172. else {
  173. Blynk.virtualWrite(V2, LOW);
  174. }
  175. if (doorOpenMove) {
  176. Blynk.virtualWrite(V3, HIGH);
  177. }
  178. else {
  179. Blynk.virtualWrite(V3, LOW);
  180. }
  181. if (doorCloseMove) {
  182. Blynk.virtualWrite(V4, HIGH);
  183. }
  184. else {
  185. Blynk.virtualWrite(V4, LOW);
  186. }
  187. }
  188.  
  189. void doorControl()
  190. {
  191. if (photocellReading >= 0 && photocellReading <= closeDoor) { // Night Setting based on user or default selected low light trigger
  192. if (doorSensor) {
  193. photocellReadingLevel = '1';
  194. }
  195. nightTimer = true;
  196. }
  197. else if (photocellReading >= closeDoor && photocellReading <= openDoor) { // Twighlight setting
  198. if (doorSensor) {
  199. photocellReadingLevel = '2';
  200. }
  201. }
  202. else if (photocellReading >= (openDoor + 1) ) { //Daylight Setting
  203. if (doorSensor) {
  204. photocellReadingLevel = '3';
  205. }
  206. nightTimer = false;
  207. }
  208. }
  209.  
  210. void waterHeat()
  211. {
  212. if (heaterOn) {
  213. if (waterTemp == -127) {
  214. digitalWrite(heatRelay, LOW); //turn off the water heater
  215. waterTemp = 0;
  216. }
  217. else if (waterTemp >= (coldTemp + 3)) { // if the temperature is 3 degrees above the trigger temp
  218. digitalWrite(heatRelay, LOW); //turn off the water heater
  219. }
  220. else if (waterTemp < coldTemp) { //if the temperature is below the cold temperature
  221. digitalWrite(heatRelay, HIGH); //turn on the water heater
  222. }
  223. }
  224. else {
  225. digitalWrite(heatRelay, LOW); //turn off the water heater
  226. }
  227. }
  228.  
  229. void ventFan()
  230. {
  231. if (coopTemp >= hotTemp) { // if the temperature is above the Hot temperature
  232. digitalWrite(fanRelay, HIGH);
  233. ventOn = true;
  234. }
  235. else if (coopTemp < (hotTemp - 2)) { // if the temperature has been lowered two degrees
  236. digitalWrite(fanRelay, LOW);
  237. ventOn = false;
  238. }
  239. }
  240.  
  241. void readSwitches() {
  242. topSwitchState = (digitalRead(topSwitchPin));
  243. bottomSwitchState = (digitalRead(bottomSwitchPin));
  244. }
  245.  
  246. // stop the coop door motor and put the motor driver IC to sleep (power saving)
  247. void stopCoopDoorMotorB() {
  248. digitalWrite (directionCloseCoopDoorMotorB, LOW); // turn off motor close direction
  249. digitalWrite (directionOpenCoopDoorMotorB, LOW); // turn off motor open direction
  250. }
  251.  
  252. // close the coop door motor
  253. void closeCoopDoorMotorB() {
  254. if (bottomSwitchState == 1) { //if the bottom reed switch is open
  255. digitalWrite (directionCloseCoopDoorMotorB, HIGH); // turn on motor close direction
  256. digitalWrite (directionOpenCoopDoorMotorB, LOW); // turn off motor open direction
  257. doorOpen = false;
  258. doorClosed = false;
  259. doorCloseMove = true;
  260. doorOpenMove = false;
  261. }
  262. else { // if bottom reed switch circuit is closed
  263. stopCoopDoorMotorB();
  264. doorClosed = true;
  265. doorCloseMove = false;
  266. }
  267. }
  268.  
  269. // open the coop door
  270. void openCoopDoorMotorB() {
  271. if (topSwitchState == 1) { //if the top reed switch is open
  272. digitalWrite(directionCloseCoopDoorMotorB, LOW); // turn off motor close direction
  273. digitalWrite(directionOpenCoopDoorMotorB, HIGH); // turn on motor open direction
  274. doorOpen = false;
  275. doorClosed = false;
  276. doorOpenMove = true;
  277. doorCloseMove = false;
  278. }
  279. else { // if top reed switch circuit is closed
  280. stopCoopDoorMotorB();
  281. doorOpen = true;
  282. doorOpenMove = false;
  283. }
  284. }
  285.  
  286. // do the coop door
  287. void doCoopDoor() {
  288. if (photocellReadingLevel == '1') { // if it's dark
  289. readSwitches();
  290. closeCoopDoorMotorB(); // close the door
  291. }
  292. else if (photocellReadingLevel == '3') { // if it's light
  293. readSwitches();
  294. openCoopDoorMotorB(); // Open the door
  295. }
  296. else if (photocellReadingLevel == '2') { // if it's twilight
  297. readSwitches();
  298. stopCoopDoorMotorB();
  299. }
  300. }
  301.  
  302. BLYNK_WRITE (V7)
  303. {
  304. photocellReadingLevel = '3';
  305. }
  306.  
  307. BLYNK_WRITE (V6)
  308. {
  309. photocellReadingLevel = '1';
  310. }
  311.  
  312. BLYNK_WRITE (V10)
  313. {
  314. if (param.asInt() == 0) {
  315. doorSensor = false;
  316. }
  317. else {
  318. doorSensor = true;
  319. }
  320. }
  321.  
  322. BLYNK_WRITE (V11)
  323. {
  324. if (param.asInt() == 0) {
  325. layLightOn = false;
  326. }
  327. else {
  328. layLightOn = true;
  329. }
  330. }
  331.  
  332. BLYNK_WRITE (V12)
  333. {
  334. nightLightOn = true;
  335. }
  336.  
  337.  
  338.  
  339. void loop() {
  340. BlynkEdgent.run();
  341. timer.run();
  342. doCoopDoor();
  343. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement