Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Fill-in information from your Blynk Template here
- #define BLYNK_TEMPLATE_ID "TMPL-flCABCq"
- #define BLYNK_DEVICE_NAME "CoopCommand"
- #define BLYNK_FIRMWARE_VERSION "0.1.4"
- #define BLYNK_PRINT Serial
- //#define BLYNK_DEBUG
- #define APP_DEBUG
- // Uncomment your board, or configure a custom board in Settings.h
- //#define USE_WROVER_BOARD
- //#define USE_TTGO_T7
- #include "BlynkEdgent.h"
- #include <DallasTemperature.h>
- #include <OneWire.h>
- #include <DHT.h>
- // Widget Setups
- WidgetLED led1(V1);
- WidgetLED led2(V2);
- WidgetLED led3(V3);
- WidgetLED led4(V4);
- BlynkTimer timer;
- // DS18B20 Sensor Setup
- #define ONE_WIRE_BUS 25
- OneWire oneWire(ONE_WIRE_BUS);
- DallasTemperature sensors(&oneWire);
- //DHT Setup
- #define DHTPIN 34 // Pin for DHT sensor
- #define DHTTYPE DHT22 // DHT 22 (AM2302)
- DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor
- // pin assignments
- const int photocellPin = 35; // analog pin for photocell
- const int bottomSwitchPin = 26; // bottom switch is connected to pin 26
- const int topSwitchPin = 27; // top switch is connected to pin 27
- const int directionCloseCoopDoorMotorB = 19; // direction close motor b - pin 19
- const int directionOpenCoopDoorMotorB = 21; // direction open motor b - pin 21
- const int layLightRelay = 4; // output pin controlling lay light relay
- const int fanRelay = 16; // output pin controlling ventilation fan relay
- const int heatRelay = 17; // output pin controlling water heater relay
- // Sensor Variables
- bool doorOpen = false; // is the coop door open
- bool doorClosed = false; // is the door closed
- bool doorOpenMove = false; // is the door opening?
- bool doorCloseMove = false; // is the door closing?
- int topSwitchState; // Current state (open/closed) of the top limit switch
- int bottomSwitchState; // Current state (open/closed) of the bottom limit switch
- bool doorSensor = true; // is the door in automatic or manual mode
- bool ventOn = false; // is the ventilation fan relay on or off
- bool heaterOn = true; // is the water heater function running
- bool nightTimer = false; // is it night time
- bool layLightOn = true; // is the Lay Light time monitoring system on
- bool nightLightOn = false; // is the Night Light on
- int coopTemp = 0; // Interior Coop Temperature Reading
- int closeDoor = 20; // Light level to close coop door (user editable, EEPROM saved)
- int openDoor = closeDoor + 10; // Light level to open coop door
- int hotTemp = 30; // Temperature to turn on Ventilation Fan Relay (user editable, EEPROM saved)
- int coldTemp = 3; // Temperature to turn on Water Heat Relay (user editable, EEPROM saved)
- float waterTemp = 0; // Water Tempterature Reading
- float hum; // Stores humidity value from DHT22
- float temp; // Stores temperature value from DHT22
- int photocellReading; // analog reading of the photocell
- int photocellReadingLevel = '2'; // photocell reading levels (night, light, twilight)
- // Timer Variables
- unsigned long layLightTimer = 36000000; // Timer to make sure at least 14 hours or "daylight"
- unsigned long lastDayLightReadingTime = 0; // timer to keep track of how long it has been night
- unsigned long nightLightDelay = 300000; // 5 minute timer to turn on the coop light if "enter" is pushed and it is night.
- unsigned long lastNightLightTime = 0; // the last time the night light button was pushed
- void setup()
- {
- Serial.begin(115200);
- delay(100);
- dht.begin();
- sensors.begin();
- BlynkEdgent.begin();
- pinMode(photocellPin, INPUT);
- pinMode(topSwitchPin, INPUT);
- pinMode(bottomSwitchPin, INPUT);
- pinMode(layLightRelay, OUTPUT);
- pinMode(fanRelay, OUTPUT);
- pinMode(heatRelay, OUTPUT);
- pinMode(directionCloseCoopDoorMotorB, OUTPUT);
- pinMode(directionOpenCoopDoorMotorB, OUTPUT);
- photocellReading = analogRead(photocellPin);
- photocellReading = map(photocellReading, 0, 1023, 0, 100);
- timer.setInterval(2000,readSensor);
- timer.setInterval(600000,doorControl);
- timer.setInterval(1000,ventFan);
- timer.setInterval(1000,waterHeat);
- timer.setInterval(2000,layLight);
- }
- void layLight() {
- if (layLightOn) {
- if (!nightTimer) { // if it is not dark
- lastDayLightReadingTime = millis();
- digitalWrite(layLightRelay, LOW); // turn off the lay light
- } else { // if it is dark
- if ((unsigned long)(millis() - lastDayLightReadingTime) >= layLightTimer) { //if it has been dark more than 10 hours (or whatever the timer is
- digitalWrite(layLightRelay, HIGH); // turn on the lay light
- Blynk.virtualWrite(V11, HIGH);
- } else {
- digitalWrite(layLightRelay, LOW); // turn off the lay light
- Blynk.virtualWrite(V11, LOW);
- }
- }
- }
- if (nightLightOn) { // if someone wants the light on
- digitalWrite(layLightRelay, HIGH);
- Blynk.virtualWrite(V11, HIGH);
- }
- else if ((unsigned long)(millis() - lastNightLightTime) >= nightLightDelay) {
- digitalWrite (layLightRelay, LOW);
- Blynk.virtualWrite(V11, LOW);
- nightLightOn = false;
- }
- }
- void readSensor()
- {
- //Read Photocell
- photocellReading = analogRead(photocellPin);
- photocellReading = map(photocellReading, 0, 1023, 0, 100);
- Blynk.virtualWrite(V8, photocellReading);
- //Read DS18B20 Water Temp Sensor
- sensors.requestTemperatures();
- waterTemp = sensors.getTempCByIndex(0);
- if (waterTemp == -127) {
- waterTemp = 0;
- }
- Blynk.virtualWrite(V7, waterTemp);
- //Read DHT22
- hum = dht.readHumidity();
- temp = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
- Blynk.virtualWrite(V13, hum);
- Blynk.virtualWrite(V0, temp);
- if (doorOpen) {
- Blynk.virtualWrite(V1, HIGH);
- }
- else {
- Blynk.virtualWrite(V1, LOW);
- }
- if (doorClosed) {
- Blynk.virtualWrite(V2, HIGH);
- }
- else {
- Blynk.virtualWrite(V2, LOW);
- }
- if (doorOpenMove) {
- Blynk.virtualWrite(V3, HIGH);
- }
- else {
- Blynk.virtualWrite(V3, LOW);
- }
- if (doorCloseMove) {
- Blynk.virtualWrite(V4, HIGH);
- }
- else {
- Blynk.virtualWrite(V4, LOW);
- }
- }
- void doorControl()
- {
- if (photocellReading >= 0 && photocellReading <= closeDoor) { // Night Setting based on user or default selected low light trigger
- if (doorSensor) {
- photocellReadingLevel = '1';
- }
- nightTimer = true;
- }
- else if (photocellReading >= closeDoor && photocellReading <= openDoor) { // Twighlight setting
- if (doorSensor) {
- photocellReadingLevel = '2';
- }
- }
- else if (photocellReading >= (openDoor + 1) ) { //Daylight Setting
- if (doorSensor) {
- photocellReadingLevel = '3';
- }
- nightTimer = false;
- }
- }
- void waterHeat()
- {
- if (heaterOn) {
- if (waterTemp == -127) {
- digitalWrite(heatRelay, LOW); //turn off the water heater
- waterTemp = 0;
- heaterOn = false;
- }
- else if (waterTemp >= (coldTemp + 3)) { // if the temperature is 3 degrees above the trigger temp
- digitalWrite(heatRelay, LOW); //turn off the water heater
- }
- else if (waterTemp < coldTemp) { //if the temperature is below the cold temperature
- digitalWrite(heatRelay, HIGH); //turn on the water heater
- }
- }
- }
- void ventFan()
- {
- if (coopTemp >= hotTemp) { // if the temperature is above the Hot temperature
- digitalWrite(fanRelay, HIGH);
- ventOn = true;
- }
- else if (coopTemp < (hotTemp - 2)) { // if the temperature has been lowered two degrees
- digitalWrite(fanRelay, LOW);
- ventOn = false;
- }
- }
- void readSwitches() {
- topSwitchState = (digitalRead(topSwitchPin));
- bottomSwitchState = (digitalRead(bottomSwitchPin));
- }
- // stop the coop door motor and put the motor driver IC to sleep (power saving)
- void stopCoopDoorMotorB() {
- digitalWrite (directionCloseCoopDoorMotorB, LOW); // turn off motor close direction
- digitalWrite (directionOpenCoopDoorMotorB, LOW); // turn off motor open direction
- }
- // close the coop door motor
- void closeCoopDoorMotorB() {
- if (bottomSwitchState == 1) { //if the bottom reed switch is open
- digitalWrite (directionCloseCoopDoorMotorB, HIGH); // turn on motor close direction
- digitalWrite (directionOpenCoopDoorMotorB, LOW); // turn off motor open direction
- doorOpen = false;
- doorClosed = false;
- doorCloseMove = true;
- }
- else { // if bottom reed switch circuit is closed
- stopCoopDoorMotorB();
- doorClosed = true;
- doorCloseMove = false;
- }
- }
- // open the coop door
- void openCoopDoorMotorB() {
- if (topSwitchState == 1) { //if the top reed switch is open
- digitalWrite(directionCloseCoopDoorMotorB, LOW); // turn off motor close direction
- digitalWrite(directionOpenCoopDoorMotorB, HIGH); // turn on motor open direction
- doorOpen = false;
- doorClosed = false;
- doorOpenMove = true;
- }
- else { // if top reed switch circuit is closed
- stopCoopDoorMotorB();
- doorOpen = true;
- doorOpenMove = false;
- }
- }
- // do the coop door
- void doCoopDoor() {
- if (photocellReadingLevel == '1') { // if it's dark
- readSwitches();
- closeCoopDoorMotorB(); // close the door
- }
- else if (photocellReadingLevel == '3') { // if it's light
- readSwitches();
- openCoopDoorMotorB(); // Open the door
- }
- else if (photocellReadingLevel == '2') { // if it's twilight
- readSwitches();
- stopCoopDoorMotorB();
- }
- }
- BLYNK_WRITE (V7)
- {
- photocellReadingLevel = '3';
- }
- BLYNK_WRITE (V6)
- {
- photocellReadingLevel = '1';
- }
- BLYNK_WRITE (V10)
- {
- if (param.asInt() == 0) {
- doorSensor = false;
- }
- else {
- doorSensor = true;
- }
- }
- void loop() {
- BlynkEdgent.run();
- doCoopDoor();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement