Pete
Is the timer your speaking of the #20 - Variable timer? if so i moved my function into this timer but i cant seem to get it to work. i can put a button on my app press the button and get it to work but there is no delay. I don’t really want to use a button to initiate the timer because i may forget to turn it on and the door not close. It works as it did before with no delay see if you can tell me what im doing wrong. Im still a amateur at programming
/*************************************************************
Download latest Blynk library here:
https://github.com/blynkkk/blynk-library/releases/latest
Blynk is a platform with iOS and Android apps to control
Arduino, Raspberry Pi and the likes over the Internet.
You can easily build graphic interfaces for all your
projects by simply dragging and dropping widgets.
Downloads, docs, tutorials: http://www.blynk.cc
Sketch generator: http://examples.blynk.cc
Blynk community: http://community.blynk.cc
Blynk library is licensed under MIT license
This example code is in public domain.
*************************************************************
You’ll need:
- Blynk App (download from AppStore or Google Play)
- WeMos D1 board
- Decide how to connect to Blynk
(USB, Ethernet, Wi-Fi, Bluetooth, ...)
There is a bunch of great example sketches included to show you how to get
started. Think of them as LEGO bricks and combine them as you wish.
For example, take the Ethernet Shield sketch and combine it with the
Servo example, or choose a USB sketch and add a code from SendData
example.
*************************************************************/
// Assign Arduino Friendly Names to GPIO pins
#define D0 16
#define D1 5 // I2C Bus SCL (clock)
#define D2 4 // I2C Bus SDA (data)
#define D3 0
#define D4 2 // Same as "LED_BUILTIN", but inverted logic
#define D5 14 // SPI Bus SCK (clock)
#define D6 12 // SPI Bus MISO
#define D7 13 // SPI Bus MOSI
#define D8 15 // SPI Bus SS (CS)
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#define ONE_WIRE_BUS D3 // Your ESP8266 pin (ESP8266 GPIO 2 = WeMos D1 Mini pin D4)
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxxxxxxxx";
// Your WiFi credentials.
// Set password to "" for open networks.
const char* ssid = "xxxxxxx";
const char* pass = "xxxxxxx";
const char* IP = "192.168.10.10";
float timeout = (millis() / 1000);
int tries = 0;
//Sensors
#define sensor1 D1
#define sensor2 D2
// Relays
#define Relay_1 D5
#define Relay_2 D6
#define Relay_3 D7
//#define Relay_4 D3
#define Relay_ON LOW
#define Relay_OFF HIGH
// Photocell
const int sensorPin = A0;
const int NightValue = 1022;
const int DayValue = 400;
const int lowThresholdTimer = 1009;
int sensorValue;
// Coop Door
bool doorFlag = true;
bool doorNotify = true;
int open_state; // 0 close - 1 open switch
int closed_state; // 0 close - 1 open switch
// Temperature
int CoopTemperature; // Room temperature in F
bool heatFlag = true;
bool heatNotify = true;
// App Buttons
bool buttonFlag;
// the timer object
BlynkTimer timer;
// Blynk App Virtual Pins
BLYNK_WRITE(V10)
{
int UpBttn = param[1].asInt();; // assigning incoming value from pin V10 to a variable
//Motor Direction Up
open_state = digitalRead(sensor1);
Serial.println(open_state);
if (UpBttn == 0)
{ digitalWrite (Relay_2, Relay_OFF);
digitalWrite (Relay_1, Relay_ON);
Blynk.virtualWrite(V11, Relay_ON);
}
// Top door sensor
else if (open_state == LOW)
{
digitalWrite (Relay_1, Relay_OFF);
}
}
BLYNK_WRITE(V11)
{
int DownBttn = param[2].asInt(); // assigning incoming value from pin V11 to a variable
//Motor Direction Down
closed_state = digitalRead(sensor2);
Serial.println(closed_state);
if (DownBttn == 0)
{
digitalWrite (Relay_1, Relay_OFF);
Blynk.virtualWrite(V10, Relay_ON);
digitalWrite (Relay_2, Relay_ON);
}
// Bottom door sensor
else if (closed_state == LOW)
{
digitalWrite (Relay_2, Relay_OFF);
}
}
BLYNK_WRITE(V14)
{
if (param.asInt() == 0)
{
digitalWrite (Relay_3, Relay_ON);
}
// Bottom door sensor
if (param.asInt() == 1)
{
digitalWrite (Relay_3, Relay_OFF);
}
}
long variableTime = 10000; // default interval time of 1 second
int varTimer; // Setup Timeout Timer ID
BLYNK_WRITE(V0) { // Widget for interval timing
variableTime = param.asInt();
if (variableTime <= 0) { // prevents a timer from becoming 0 or less
variableTime = 1;
}
timerLoop(); // Call your timed loop
}
void timerLoop() {
timer.deleteTimer(varTimer); // Cancel previous Timeout Timer
// Do your stuff here every (variableTime) seconds
// If light level is low is detected, Close Door
closed_state = digitalRead(sensor2);
Serial.println(closed_state);
if ((sensorValue >= NightValue) && (doorFlag == false))
{
doorFlag = true;
Blynk.virtualWrite(V11, Relay_OFF);
Blynk.virtualWrite(V10, Relay_ON);
digitalWrite (Relay_1, Relay_OFF); //DOOR CLOSED
digitalWrite (Relay_2, Relay_ON);
}
// Bottom door sensor
else if (closed_state == LOW)
{
digitalWrite (Relay_2, Relay_OFF);
}
varTimer = timer.setTimeout(variableTime, timerLoop); // Set new Timeout timer
}
void setup()
{
// Debug console
Serial.begin(9600);
//Blynk.begin(auth, ssid, pass);
// You can also specify server:
//Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 80);
//Blynk.config(auth, IPAddress(192, 168, 10, 10), 80);
//Blynk.connectWiFi(ssid, pass);
//if (WiFi.status() == WL_CONNECTED){
//Serial.print("Wifi Connected"); }
timer.setInterval(60000L, connectWiFi);
//timer.setInterval(30000L,connectBlynk);
timer.setInterval(65000L, CheckConnection);
//Blynk.config(auth,IP, 80);
//
//
// if (WiFi.status() == WL_CONNECTED) {
// Blynk.connect(3333);}
//
// while (Blynk.connect() == false) {
// Serial.println("in while: Blynk.connect() == false");
// if (((millis() / 1000) - timeout) > 10)
// break;
// }
//Blynk.notify("Chicken Coop Connected");
pinMode(sensor1, INPUT_PULLUP);
pinMode(sensor2, INPUT_PULLUP);
pinMode (Relay_1, OUTPUT);
pinMode (Relay_2, OUTPUT);
pinMode (Relay_3, OUTPUT);
//pinMode (Relay_4, OUTPUT);
//-------( Initialize Pins so relays are inactive at reset)----
digitalWrite(Relay_1, Relay_OFF);
digitalWrite(Relay_2, Relay_OFF);
digitalWrite(Relay_3, Relay_OFF);
//digitalWrite(Relay_4, Relay_OFF);
//sensors.begin(); // Starts the DS18B20 sensor(s).
//sensors.setResolution(10); // More on resolution: http://www.homautomation.org/2015/11/17/ds18b20-how-to-change-resolution-9101112-bits/
//Scheduled Timer Functions
//timer.setInterval(1000L, MillisReset);
timer.setInterval(5000L, Day );
//timer.setInterval(2000L, Night );
timer.setInterval(1000L, SendLightGraph );
timer.setInterval(10000L, sendTemps); // Temperature sensor read interval. 10000 (ms) = 10 seconds.
timer.setInterval(30000L, CoopTemp) ;
timer.setInterval(2000L, CoopTempNotify);
timer.setInterval(3000L, DoorNotify);
}
void loop()
{
if (Blynk.connected()) {
Blynk.run();
}
timer.run();
}
void connectBlynk()
{
if ((WiFi.status() == WL_CONNECTED) && (((millis() / 1000) - timeout) > 30)) {
Blynk.config(auth, IP, 80);
Blynk.connect();
}
}
void connectWiFi()
{
BLYNK_LOG("Connecting to %s", ssid);
if (pass && strlen(pass))
{
WiFi.begin(ssid, pass);
}
else {
WiFi.begin(ssid);
}
//if (WiFi.status() == WL_CONNECTED) {
//}
if (WiFi.status() == WL_CONNECTED) {
Serial.print("Wifi Connected");
}
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
tries++;
if (tries > 15)
//offline
break;
}
}
void CheckConnection()
{ // check every 11s if connected to Blynk server
if (!Blynk.connected()) {
Serial.println("Not connected to Blynk server");
Blynk.config(auth, IP, 80);
Blynk.connect(); // try to connect to server with default timeout
}
else
{
Serial.println("Connected to Blynk server");
}
}
void SendLightGraph ()
{
sensorValue = analogRead(sensorPin);
Blynk.virtualWrite(V5, sensorValue );
}
void Day()
{
open_state = digitalRead(sensor1);
Serial.println(open_state);
// Read the sensor pin
sensorValue = analogRead(sensorPin);
Serial.println(sensorValue);
// If light level is high is detected, Open Door
if ((sensorValue < DayValue) && (doorFlag == true))
{
doorFlag = false;
Blynk.virtualWrite(V10, Relay_OFF);
Blynk.virtualWrite(V11, Relay_ON);
digitalWrite (Relay_2, Relay_OFF); //DOOR OPEN
digitalWrite (Relay_1, Relay_ON);
}
// Top door sensor
else if (open_state == LOW)
{
digitalWrite (Relay_1, Relay_OFF);
}
}
void Night () {
// If light level is low is detected, Close Door
closed_state = digitalRead(sensor2);
Serial.println(closed_state);
if ((sensorValue > NightValue) && (doorFlag == false))
{
doorFlag = true;
Blynk.virtualWrite(V11, Relay_OFF);
Blynk.virtualWrite(V10, Relay_ON);
digitalWrite (Relay_1, Relay_OFF); //DOOR CLOSED
digitalWrite (Relay_2, Relay_ON);
}
// Bottom door sensor
else if (closed_state == LOW)
{
digitalWrite (Relay_2, Relay_OFF);
}
}
void DoorNotify()
{
open_state = digitalRead(sensor1);
closed_state = digitalRead(sensor2);
if ((open_state == LOW) && (doorNotify == true))
{
doorNotify = false;
Blynk.notify("Coop Door Open");
}
if ((closed_state == LOW) && (doorNotify == false))
{
doorNotify = true;
Blynk.notify("Coop Door Closed");
}
}
void sendTemps()
{
sensors.requestTemperatures(); // Polls the sensors.
CoopTemperature = sensors.getTempFByIndex(0);// Stores temperature. Change to getTempCByIndex(0) for celcius.
Serial.println(CoopTemperature);
Blynk.virtualWrite(V13, CoopTemperature); // Send Temperature to Blynk app Virtual pin 13.
}
void CoopTemp()
{
if ((CoopTemperature <= 40 ) && (heatFlag == true))
{
heatFlag = false;
digitalWrite (Relay_3, Relay_ON);//Coop Temperature Control Heat Light ON Below 40 Degrees
Blynk.virtualWrite(V14, Relay_ON);
}
if ((CoopTemperature >= 45 ) && (CoopTemperature <= 46 ) && (heatFlag = false))
{
heatFlag = true;
digitalWrite (Relay_3, Relay_OFF); //Coop Temperature Control Heat Light OFF Above 45 Degrees
Blynk.virtualWrite(V14, Relay_OFF);
}
}
void CoopTempNotify()
{
if ((CoopTemperature <= 40 ) && (heatNotify == true))
{
heatNotify = false;
Blynk.notify("Coop Heat Lamp ON");
}
else if ((CoopTemperature >= 45 ) && (heatNotify == false))
{
heatNotify = true;
Blynk.notify("Coop Heat Lamp OFF");
}
}```