Advertisement
hms11

CoopCommandEsp32-0.2.3

Mar 29th, 2022
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.01 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.2.3"
  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 32 // 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 = 10; // 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. //Blynk.sync(V12);
  90. //Blynk.sync(V11);
  91. //Blynk.sync(V10);
  92. pinMode(photocellPin, INPUT);
  93. pinMode(topSwitchPin, INPUT);
  94. pinMode(bottomSwitchPin, INPUT);
  95. pinMode(layLightRelay, OUTPUT);
  96. pinMode(fanRelay, OUTPUT);
  97. pinMode(heatRelay, OUTPUT);
  98. pinMode(directionCloseCoopDoorMotorB, OUTPUT);
  99. pinMode(directionOpenCoopDoorMotorB, OUTPUT);
  100. photocellReading = analogRead(photocellPin);
  101. photocellReading = map(photocellReading, 0, 4095, 0, 100);
  102. timer.setInterval(2000,readSensor);
  103. timer.setInterval(600000,doorControl);
  104. timer.setInterval(1000,ventFan);
  105. timer.setInterval(1000,waterHeat);
  106. timer.setInterval(1000,layLight);
  107. }
  108.  
  109. void layLight() {
  110. if (layLightOn) {
  111. if (!nightTimer) { // if it is not dark
  112. lastDayLightReadingTime = millis();
  113. digitalWrite(layLightRelay, LOW); // turn off the lay light
  114. }
  115. else { // if it is dark
  116. if ((unsigned long)(millis() - lastDayLightReadingTime) >= layLightTimer) { //if it has been dark more than 10 hours (or whatever the timer is
  117. digitalWrite(layLightRelay, HIGH); // turn on the lay light
  118. Blynk.virtualWrite(14, HIGH);
  119. } else {
  120. digitalWrite(layLightRelay, LOW); // turn off the lay light
  121. Blynk.virtualWrite(V14, LOW);
  122. }
  123. }
  124. }
  125.  
  126. if (nightLightOn) { // if someone wants the light on
  127. digitalWrite(layLightRelay, HIGH);
  128. Blynk.virtualWrite(V14, HIGH);
  129. timer.setTimeout(300000, []()
  130. {
  131. digitalWrite(layLightRelay, LOW);
  132. Blynk.virtualWrite(V14, LOW);
  133. nightLightOn = false;
  134. });
  135. }
  136. }
  137.  
  138. void readSensor()
  139. {
  140. // Send Light Value
  141. Blynk.virtualWrite(V8, photocellReading);
  142. // Read DS18B20 Water Temp Sensor
  143. sensors.requestTemperatures();
  144. waterTemp = sensors.getTempCByIndex(0);
  145. if (waterTemp == -127) {
  146. waterTemp = 0;
  147. heaterOn = false;
  148. }
  149. else {
  150. heaterOn = true;
  151. }
  152. Blynk.virtualWrite(V5, waterTemp);
  153. //Read DHT22
  154. hum = dht.readHumidity();
  155. temp = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
  156. Blynk.virtualWrite(V13, hum);
  157. Blynk.virtualWrite(V0, temp);
  158.  
  159. if (nightLightOn) {
  160. Blynk.virtualWrite(V14, HIGH);
  161. }
  162. else {
  163. Blynk.virtualWrite(V14, LOW);
  164. }
  165.  
  166. if (doorOpen) {
  167. Blynk.virtualWrite(V1, HIGH);
  168. }
  169. else {
  170. Blynk.virtualWrite(V1, LOW);
  171. }
  172. if (doorClosed) {
  173. Blynk.virtualWrite(V2, HIGH);
  174. }
  175. else {
  176. Blynk.virtualWrite(V2, LOW);
  177. }
  178. if (doorOpenMove) {
  179. Blynk.virtualWrite(V3, HIGH);
  180. }
  181. else {
  182. Blynk.virtualWrite(V3, LOW);
  183. }
  184. if (doorCloseMove) {
  185. Blynk.virtualWrite(V4, HIGH);
  186. }
  187. else {
  188. Blynk.virtualWrite(V4, LOW);
  189. }
  190. }
  191.  
  192. void doorControl()
  193. {
  194. photocellReading = analogRead(photocellPin);
  195. photocellReading = map(photocellReading, 0, 4095, 0, 100);
  196. if (photocellReading >= 0 && photocellReading <= closeDoor) { // Night Setting based on user or default selected low light trigger
  197. if (doorSensor) {
  198. photocellReadingLevel = '1';
  199. }
  200. nightTimer = true;
  201. }
  202. else if (photocellReading >= closeDoor && photocellReading <= openDoor) { // Twighlight setting
  203. if (doorSensor) {
  204. photocellReadingLevel = '2';
  205. }
  206. }
  207. else if (photocellReading >= (openDoor + 1) ) { //Daylight Setting
  208. if (doorSensor) {
  209. photocellReadingLevel = '3';
  210. }
  211. nightTimer = false;
  212. }
  213. }
  214.  
  215. void waterHeat()
  216. {
  217. if (heaterOn) {
  218. if (waterTemp == -127) {
  219. digitalWrite(heatRelay, LOW); //turn off the water heater
  220. waterTemp = 0;
  221. }
  222. else if (waterTemp >= (coldTemp + 3)) { // if the temperature is 3 degrees above the trigger temp
  223. digitalWrite(heatRelay, LOW); //turn off the water heater
  224. }
  225. else if (waterTemp < coldTemp) { //if the temperature is below the cold temperature
  226. digitalWrite(heatRelay, HIGH); //turn on the water heater
  227. }
  228. }
  229. else {
  230. digitalWrite(heatRelay, LOW); //turn off the water heater
  231. }
  232. }
  233.  
  234. void ventFan()
  235. {
  236. if (coopTemp >= hotTemp) { // if the temperature is above the Hot temperature
  237. digitalWrite(fanRelay, HIGH);
  238. ventOn = true;
  239. }
  240. else if (coopTemp < (hotTemp - 2)) { // if the temperature has been lowered two degrees
  241. digitalWrite(fanRelay, LOW);
  242. ventOn = false;
  243. }
  244. }
  245.  
  246. void readSwitches() {
  247. topSwitchState = (digitalRead(topSwitchPin));
  248. bottomSwitchState = (digitalRead(bottomSwitchPin));
  249. }
  250.  
  251. // stop the coop door motor and put the motor driver IC to sleep (power saving)
  252. void stopCoopDoorMotorB() {
  253. digitalWrite (directionCloseCoopDoorMotorB, LOW); // turn off motor close direction
  254. digitalWrite (directionOpenCoopDoorMotorB, LOW); // turn off motor open direction
  255. }
  256.  
  257. // close the coop door motor
  258. void closeCoopDoorMotorB() {
  259. if (bottomSwitchState == 1) { //if the bottom reed switch is open
  260. digitalWrite (directionCloseCoopDoorMotorB, HIGH); // turn on motor close direction
  261. digitalWrite (directionOpenCoopDoorMotorB, LOW); // turn off motor open direction
  262. doorOpen = false;
  263. doorClosed = false;
  264. doorCloseMove = true;
  265. doorOpenMove = false;
  266. }
  267. else { // if bottom reed switch circuit is closed
  268. stopCoopDoorMotorB();
  269. doorClosed = true;
  270. doorCloseMove = false;
  271. doorOpenMove = false;
  272. }
  273. }
  274.  
  275. // open the coop door
  276. void openCoopDoorMotorB() {
  277. if (topSwitchState == 1) { //if the top reed switch is open
  278. digitalWrite(directionCloseCoopDoorMotorB, LOW); // turn off motor close direction
  279. digitalWrite(directionOpenCoopDoorMotorB, HIGH); // turn on motor open direction
  280. doorOpen = false;
  281. doorClosed = false;
  282. doorOpenMove = true;
  283. doorCloseMove = false;
  284. }
  285. else { // if top reed switch circuit is closed
  286. stopCoopDoorMotorB();
  287. doorOpen = true;
  288. doorOpenMove = false;
  289. doorCloseMove = false;
  290. }
  291. }
  292.  
  293. // do the coop door
  294. void doCoopDoor() {
  295. if (photocellReadingLevel == '1') { // if it's dark
  296. readSwitches();
  297. closeCoopDoorMotorB(); // close the door
  298. }
  299. else if (photocellReadingLevel == '3') { // if it's light
  300. readSwitches();
  301. openCoopDoorMotorB(); // Open the door
  302. }
  303. else if (photocellReadingLevel == '2') { // if it's twilight
  304. readSwitches();
  305. stopCoopDoorMotorB();
  306. }
  307. }
  308.  
  309. BLYNK_WRITE (V7)
  310. {
  311. photocellReadingLevel = '3';
  312. }
  313.  
  314. BLYNK_WRITE (V6)
  315. {
  316. photocellReadingLevel = '1';
  317. }
  318.  
  319. BLYNK_WRITE (V10)
  320. {
  321. if (param.asInt() == 0) {
  322. doorSensor = false;
  323. }
  324. else {
  325. doorSensor = true;
  326. }
  327. }
  328.  
  329. BLYNK_WRITE (V11)
  330. {
  331. if (param.asInt() == 0) {
  332. layLightOn = false;
  333. nightTimer = false;
  334. digitalWrite(layLightRelay, LOW); // turn off the lay light
  335. }
  336. else {
  337. layLightOn = true;
  338. }
  339. }
  340.  
  341. BLYNK_WRITE (V12)
  342. {
  343. nightLightOn = true;
  344. }
  345.  
  346.  
  347.  
  348.  
  349. void loop() {
  350. BlynkEdgent.run();
  351. timer.run();
  352. doCoopDoor();
  353. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement