Advertisement
hms11

ESP32CoopCommand0.60

Apr 13th, 2023
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.89 KB | None | 0 0
  1. // COOP COMMAND ESP32 (Full Function Field Test Ready)
  2. //
  3. //
  4. // Component/Feature Notes:
  5. // -- Photo Resistor GL5539 W/10K Divider
  6. // -- Interior Temp/Humidity Sensor DHT22
  7. // -- Water Temp Sensor DS18B20
  8. // -- WIFI Monitored and Controlled via Blynk App
  9. // -- Designed to be used with TacoChicken Control Boards (V1.0)
  10.  
  11.  
  12.  
  13. // Fill-in information from your Blynk Template here
  14. #define BLYNK_TEMPLATE_ID "TMPL-flCABCq"
  15. #define BLYNK_DEVICE_NAME "CoopCommand"
  16.  
  17. #define BLYNK_FIRMWARE_VERSION "0.6.0"
  18.  
  19. #define BLYNK_PRINT Serial
  20. //#define BLYNK_DEBUG
  21.  
  22. #define APP_DEBUG
  23.  
  24. // Uncomment your board, or configure a custom board in Settings.h
  25. //#define USE_WROVER_BOARD
  26. //#define USE_TTGO_T7
  27.  
  28.  
  29. // Libraries
  30. #include "BlynkEdgent.h"
  31. #include <DallasTemperature.h>
  32. #include <OneWire.h>
  33. #include <DHT.h>
  34.  
  35. // Blynk Widget Setups
  36.  
  37. WidgetLED led1(V1);
  38. WidgetLED led2(V2);
  39. WidgetLED led3(V3);
  40. WidgetLED led4(V4);
  41. WidgetLED led5(V14);
  42.  
  43. // Blynk Timer
  44.  
  45. BlynkTimer timer;
  46.  
  47. // DS18B20 Sensor Setup
  48.  
  49. #define ONE_WIRE_BUS 25 // Pin for One-Wire Bus
  50. OneWire oneWire(ONE_WIRE_BUS); // Initialize One-Wire Bus
  51. DallasTemperature sensors(&oneWire); // Initialize DS18B20
  52.  
  53. //DHT Setup
  54.  
  55. #define DHTPIN 32 // Pin for DHT sensor
  56. #define DHTTYPE DHT22 // DHT 22 (AM2302)
  57. DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor
  58.  
  59. // Photocell Pin
  60.  
  61. const int photocellPin = 34; // analog pin for photocell
  62.  
  63. // Battery Voltage Monitor Pin
  64.  
  65. const int batteryPin = 36; // analog pin for battery voltage monitor
  66.  
  67. // Door Limit Switches
  68.  
  69. const int bottomSwitchPin = 26; // bottom switch is connected to pin 26
  70. const int topSwitchPin = 27; // top switch is connected to pin 27
  71.  
  72. //Door Motor Driver Pins
  73.  
  74. const int directionCloseCoopDoorMotorB = 19; // direction close motor b - pin 19
  75. const int directionOpenCoopDoorMotorB = 21; // direction open motor b - pin 21
  76.  
  77. // OutPuts
  78. const int layLightRelay = 4; // output pin controlling lay light MOSFET
  79. const int fanRelay = 16; // output pin controlling ventilation fan MOSFET
  80. const int heatRelay = 17; // output pin controlling water heater MOSFET
  81. const int output4 = 18; // Spare Output MOSFET
  82.  
  83.  
  84. // Sensor Variables
  85. bool doorOpen = false; // is the coop door open
  86. bool doorClosed = false; // is the door closed
  87. bool doorOpenMove = false; // is the door opening?
  88. bool doorCloseMove = false; // is the door closing?
  89. int topSwitchState; // Current state (open/closed) of the top limit switch
  90. int bottomSwitchState; // Current state (open/closed) of the bottom limit switch
  91. bool doorSensor = true; // is the door in automatic or manual mode
  92. bool heaterOn = true; // is the water heater function running
  93. bool nightTimer = false; // is it night time
  94. bool layLightOn = true; // is the Lay Light time monitoring system on
  95. bool nightLightOn = false; // is the Night Light on
  96. int coopTemp = 0; // Interior Coop Temperature Reading
  97. int closeDoor = 5; // Light level to close coop door (user editable)
  98. int openDoor = closeDoor + 20; // Light level to open coop door
  99. int hotTemp = 30; // Temperature to turn on Ventilation Fan Relay (user editable)
  100. int coldTemp = 3; // Temperature to turn on Water Heat MOSFET (user editable)
  101. float waterTemp = 0; // Water Tempterature Reading
  102. float hum; // Stores humidity value from DHT22
  103. float temp; // Stores temperature value from DHT22
  104. int photocellReading; // analog reading of the photocell
  105. int photocellReadingLevel = '2'; // photocell reading levels (night, light, twilight)
  106. float batteryVoltage; // battery voltage reading mapped 0-100%
  107. int batteryVoltageRaw; // raw battery voltage reading
  108. int batteryPercent;
  109.  
  110. // Timer Variables
  111. unsigned long layLightTimer = 36000000; // Timer to make sure at least 14 hours of "daylight"
  112. unsigned long lastDayLightReadingTime = 0; // timer to keep track of how long it has been night
  113. unsigned long nightLightDelay = 300000; // 5 minute timer to turn on the coop light if light switch button is pushed and it is night.
  114. unsigned long lastNightLightTime = 0; // the last time the night light button was pushed
  115.  
  116. void setup()
  117. {
  118. Serial.begin(115200); // Start Serial Comms
  119. delay(100);
  120. dht.begin(); // Start DHT sensor
  121. sensors.begin(); // Start DS18B20 Sensor
  122. BlynkEdgent.begin(); // Start Edgent
  123.  
  124. // Set Pin Modes
  125. pinMode(photocellPin, INPUT);
  126. pinMode(batteryPin, INPUT);
  127. pinMode(topSwitchPin, INPUT);
  128. pinMode(bottomSwitchPin, INPUT);
  129. pinMode(layLightRelay, OUTPUT);
  130. pinMode(fanRelay, OUTPUT);
  131. pinMode(heatRelay, OUTPUT);
  132. pinMode(directionCloseCoopDoorMotorB, OUTPUT);
  133. pinMode(directionOpenCoopDoorMotorB, OUTPUT);
  134.  
  135. // Initial Photocell reading
  136. photocellReading = analogRead(photocellPin);
  137. photocellReading = map(photocellReading, 0, 4000, 0, 100); // Map reading to 0-100
  138.  
  139. // Ensure Mosfets are pulled low
  140. digitalWrite(layLightRelay, LOW);
  141. digitalWrite(fanRelay, LOW);
  142. digitalWrite(heatRelay, LOW);
  143. digitalWrite(output4, LOW);
  144. readSwitches();
  145.  
  146. // Set Timers
  147. timer.setInterval(2000,readSensor);
  148. timer.setInterval(600000,doorControl);
  149. timer.setInterval(1000,ventFan);
  150. timer.setInterval(1000,waterHeat);
  151. timer.setInterval(1000,layLight);
  152. }
  153.  
  154. // Function to Control Coop Light
  155. void layLight() {
  156. if (layLightOn) {
  157. if (!nightTimer) { // if it is not dark
  158. lastDayLightReadingTime = millis();
  159. digitalWrite(layLightRelay, LOW); // turn off the lay light
  160. Blynk.virtualWrite(V14, LOW); // Turn off Blynk Widge LED
  161. }
  162. else { // if it is dark
  163. if ((unsigned long)(millis() - lastDayLightReadingTime) >= layLightTimer) { //if it has been dark more than 14 hours (or whatever the timer is
  164. digitalWrite(layLightRelay, HIGH); // turn on the lay light
  165. Blynk.virtualWrite(V14, HIGH); // turn on Blynk Widget LED
  166. }
  167. }
  168. }
  169.  
  170. if (nightLightOn) { // if someone wants the light on
  171. digitalWrite(layLightRelay, HIGH); // Turn on the light
  172. Blynk.virtualWrite(V14, HIGH); // Turn on Blynk Widge LED
  173. // Set Timeout to shut light off after 5 minutes
  174. timer.setTimeout(300000, []()
  175. {
  176. digitalWrite(layLightRelay, LOW);
  177. Blynk.virtualWrite(V14, LOW);
  178. nightLightOn = false;
  179. });
  180. }
  181. }
  182.  
  183. // Function to read sensors as well as send data to Blynk
  184. void readSensor()
  185. {
  186. // Send Light Value to Blynk
  187. Blynk.virtualWrite(V8, photocellReading);
  188.  
  189. // Read the battery voltage and send to Blynk
  190.  
  191. batteryVoltageRaw = analogRead(batteryPin);
  192. Blynk.virtualWrite(V23, batteryVoltageRaw);
  193. batteryVoltage = (batteryVoltageRaw * 0.003241); // Map reading to 12v reference
  194. batteryPercent = map(batteryVoltageRaw, 3708, 3920, 0, 100); // Map reading to 0-100
  195.  
  196. Blynk.virtualWrite(V9, batteryVoltage);
  197. Blynk.virtualWrite(V24, batteryPercent);
  198.  
  199. // Read DS18B20 Water Temp Sensor
  200. sensors.requestTemperatures();
  201. waterTemp = sensors.getTempCByIndex(0);
  202. // If Reading is -127 sensor is faulty or not connected, default to 0 value and turn off water heat ability
  203. if (waterTemp == -127) {
  204. waterTemp = 0;
  205. heaterOn = false;
  206. }
  207. // If the sensor reads literally any other value, sensor is working, heater is enabled
  208. else {
  209. heaterOn = true;
  210. }
  211. // Send Water Temp to Blynk
  212. Blynk.virtualWrite(V5, waterTemp);
  213. //Read DHT22
  214. hum = dht.readHumidity();
  215. temp = dht.readTemperature();
  216. // Send DHT22 Values to Blynk
  217. Blynk.virtualWrite(V13, hum);
  218. Blynk.virtualWrite(V0, temp);
  219. // Send control settings to Blynk
  220. Blynk.virtualWrite(V20, hotTemp);
  221. Blynk.virtualWrite(V21, coldTemp);
  222. Blynk.virtualWrite(V22, closeDoor);
  223.  
  224.  
  225. // Blynk Widget LED control based on Coop Door Status
  226. if (doorOpen) {
  227. Blynk.virtualWrite(V1, HIGH);
  228. }
  229. else {
  230. Blynk.virtualWrite(V1, LOW);
  231. }
  232. if (doorClosed) {
  233. Blynk.virtualWrite(V2, HIGH);
  234. }
  235. else {
  236. Blynk.virtualWrite(V2, LOW);
  237. }
  238. if (doorOpenMove) {
  239. Blynk.virtualWrite(V3, HIGH);
  240. }
  241. else {
  242. Blynk.virtualWrite(V3, LOW);
  243. }
  244. if (doorCloseMove) {
  245. Blynk.virtualWrite(V4, HIGH);
  246. }
  247. else {
  248. Blynk.virtualWrite(V4, LOW);
  249. }
  250. }
  251.  
  252. // Function to read photocell and send commands to door function at proper intervals
  253. void doorControl()
  254. {
  255. photocellReading = analogRead(photocellPin); // Read the photocell
  256. photocellReading = map(photocellReading, 0, 4000, 0, 100); // Map photocell readings to 0-100 value
  257. if (photocellReading >= 0 && photocellReading <= closeDoor) { // Night Setting based on user or default selected low light trigger
  258. // If it is "night" set 20 minute timer before door closes to make sure chickens are all inside
  259. if (doorSensor) {
  260. timer.setTimeout(1200000, []()
  261. {
  262. photocellReadingLevel = '1';
  263. });
  264. }
  265. nightTimer = true; // Tell system to enable LayLight timer monitoring
  266. }
  267. else if (photocellReading >= closeDoor && photocellReading <= openDoor) { // Twighlight setting
  268. if (doorSensor) {
  269. photocellReadingLevel = '2';
  270. nightTimer = false;
  271. }
  272. }
  273. else if (photocellReading >= openDoor) { //Daylight Setting
  274. // If "daytime", set coop door control to open
  275. if (doorSensor) {
  276. photocellReadingLevel = '3';
  277. nightTimer = false;
  278. }
  279. }
  280. }
  281.  
  282. // Function to control water heater
  283. void waterHeat()
  284. {
  285. if (heaterOn) {
  286. //If sensor fails or becomes disconnected, turn off the heater and zero the sensor
  287. if (waterTemp == -127) {
  288. digitalWrite(heatRelay, LOW); // Turn off the water heater
  289. Blynk.virtualWrite(V15, LOW);
  290. waterTemp = 0;
  291. heaterOn = false;
  292. }
  293. else if (waterTemp >= (coldTemp + 3)) { // If the temperature is 3 degrees above the trigger temp
  294. digitalWrite(heatRelay, LOW); // Turn off the water heater
  295. Blynk.virtualWrite(V15, LOW); // Turn off the Blynk LED
  296. }
  297. else if (waterTemp < coldTemp) { // If the temperature is below the cold temperature
  298. digitalWrite(heatRelay, HIGH); // Turn on the water heater
  299. Blynk.virtualWrite(V15, HIGH); // Turn on the Blynk LED
  300. }
  301. }
  302. else {
  303. digitalWrite(heatRelay, LOW); // Turn off the water heater
  304. Blynk.virtualWrite(V15, LOW); // Turn off the Blynk LED
  305. }
  306.  
  307. }
  308.  
  309. // Function to control ventilation fan
  310. void ventFan()
  311. {
  312. if (coopTemp >= hotTemp) { // If the temperature is above the Hot temperature
  313. digitalWrite(fanRelay, HIGH); // Turn on the ventilation fan
  314. Blynk.virtualWrite(V16, HIGH); // Turn on the Blynk LED
  315. }
  316. else if (coopTemp < (hotTemp - 2)) { // If the temperature has been lowered two degrees
  317. digitalWrite(fanRelay, LOW); // Turn off the ventilation fan
  318. Blynk.virtualWrite(V16, LOW); // Turn off the Blynk LED
  319. }
  320. }
  321.  
  322. // Function to check limit switches
  323. void readSwitches() {
  324. topSwitchState = (digitalRead(topSwitchPin)); // Check the state of the top limit switch
  325. if (topSwitchState == 0) {
  326. doorOpen = true;
  327. }
  328. bottomSwitchState = (digitalRead(bottomSwitchPin)); // Check the state of the bottom limit switch
  329. if (bottomSwitchState == 0) {
  330. doorClosed = true;
  331. }
  332. }
  333.  
  334. // stop the coop door motor and put the motor driver IC to sleep (power saving)
  335. void stopCoopDoorMotorB() {
  336. digitalWrite (directionCloseCoopDoorMotorB, LOW); // turn off motor close direction
  337. digitalWrite (directionOpenCoopDoorMotorB, LOW); // turn off motor open direction
  338. }
  339.  
  340. // close the coop door motor
  341. void closeCoopDoorMotorB() {
  342. if (bottomSwitchState == 1) { //if the bottom reed switch is open
  343. digitalWrite (directionCloseCoopDoorMotorB, HIGH); // turn on motor close direction
  344. digitalWrite (directionOpenCoopDoorMotorB, LOW); // turn off motor open direction
  345. doorOpen = false;
  346. doorClosed = false;
  347. doorCloseMove = true;
  348. doorOpenMove = false;
  349. }
  350. else { // if bottom reed switch circuit is closed
  351. stopCoopDoorMotorB();
  352. doorClosed = true;
  353. doorCloseMove = false;
  354. doorOpenMove = false;
  355. }
  356. }
  357.  
  358. // open the coop door
  359. void openCoopDoorMotorB() {
  360. if (topSwitchState == 1) { //if the top reed switch is open
  361. digitalWrite(directionCloseCoopDoorMotorB, LOW); // turn off motor close direction
  362. digitalWrite(directionOpenCoopDoorMotorB, HIGH); // turn on motor open direction
  363. doorOpen = false;
  364. doorClosed = false;
  365. doorOpenMove = true;
  366. doorCloseMove = false;
  367. }
  368. else { // if top reed switch circuit is closed
  369. stopCoopDoorMotorB();
  370. doorOpen = true;
  371. doorOpenMove = false;
  372. doorCloseMove = false;
  373. }
  374. }
  375.  
  376. // Function to run the coop door
  377. void doCoopDoor() {
  378. if (photocellReadingLevel == '1') { // if it's dark
  379. readSwitches();
  380. closeCoopDoorMotorB(); // close the door
  381. }
  382. else if (photocellReadingLevel == '3') { // if it's light
  383. readSwitches();
  384. openCoopDoorMotorB(); // Open the door
  385. }
  386. else if (photocellReadingLevel == '2') { // if it's twilight
  387. readSwitches();
  388. stopCoopDoorMotorB();
  389. }
  390. }
  391.  
  392. // Door Override Open
  393. BLYNK_WRITE (V7)
  394. {
  395. photocellReadingLevel = '3';
  396. }
  397.  
  398. // Door Override Closed
  399. BLYNK_WRITE (V6)
  400. {
  401. photocellReadingLevel = '1';
  402. }
  403.  
  404. // Door in Auto or Manual Mode
  405. BLYNK_WRITE (V10)
  406. {
  407. if (param.asInt() == 0) {
  408. doorSensor = false;
  409. }
  410. else {
  411. doorSensor = true;
  412. }
  413. }
  414.  
  415. // LayLight On or Off
  416. BLYNK_WRITE (V11)
  417. {
  418. if (param.asInt() == 0) {
  419. layLightOn = false;
  420. nightTimer = false;
  421. digitalWrite(layLightRelay, LOW); // turn off the lay light
  422. }
  423. else {
  424. layLightOn = true;
  425. }
  426. }
  427.  
  428. // Turn on the Night Light
  429. BLYNK_WRITE (V12)
  430. {
  431. nightLightOn = true;
  432. }
  433.  
  434. // Adjust Ventilation Fan Temperature
  435. BLYNK_WRITE (V17)
  436. {
  437. hotTemp = param.asInt();
  438. }
  439.  
  440. // Adjust Water Heater Temp
  441. BLYNK_WRITE (V18)
  442. {
  443. coldTemp = param.asInt();
  444. }
  445.  
  446. // Adjust Door Close Light Level
  447. BLYNK_WRITE (V19)
  448. {
  449. closeDoor = param.asInt();
  450. openDoor = closeDoor + 20;
  451. }
  452.  
  453.  
  454.  
  455.  
  456.  
  457. // Run the code
  458. void loop() {
  459. BlynkEdgent.run();
  460. timer.run();
  461. doCoopDoor();
  462. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement