Hello there!
I am working on a project where 2 ch relay board can be controlled by blynk app as well as physical switches with feedback.
I want to add timer circuit , when button 2 is switched on (blynk or physical switch) the appliance attached to relay 2 is ON for 2 minutes and then turned OFF for 5 minutes this cycle is continuously repeat until button 2 is switched off. please help
here is the code
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <ESP8266mDNS.h> // For OTA with ESP8266
#include <WiFiUdp.h> // For OTA
#include <ArduinoOTA.h> // For OTA
BlynkTimer timer;
void checkPhysicalButton();
int relay1State = LOW;
int pushButton1State = HIGH;
int relay2State = LOW;
int pushButton2State = HIGH;
#define AUTH " " // You should get Auth Token in the Blynk App.
#define WIFI_SSID " " //Enter Wifi Name
#define WIFI_PASS " " //Enter wifi Password
#define SERVER "blynk-cloud.com " // Comment-out if use Blynk hosted cloud service
#define PORT 8442
#define RELAY_PIN_1 12 //D6
#define RELAY_PIN_2 16 //D0
#define PUSH_BUTTON_1 2 //D4
#define PUSH_BUTTON_2 14 //D5
#define VPIN_BUTTON_1 V12
#define VPIN_BUTTON_2 V13
#define OTA_HOSTNAME "Home_Automation"
BLYNK_CONNECTED() {
// Request the latest state from the server
Blynk.syncVirtual(VPIN_BUTTON_1);
Blynk.syncVirtual(VPIN_BUTTON_2);
// Alternatively, you could override server state using:
// Blynk.virtualWrite(VPIN_BUTTON_1, relay1State);
// Blynk.virtualWrite(VPIN_BUTTON_2, relay2State);
}
// When App button is pushed - switch the state
BLYNK_WRITE(VPIN_BUTTON_1) {
relay1State = param.asInt();
digitalWrite(RELAY_PIN_1, relay1State);
}
BLYNK_WRITE(VPIN_BUTTON_2) {
relay2State = param.asInt();
digitalWrite(RELAY_PIN_2, relay2State);
}
void checkPhysicalButton()
{
if (digitalRead(PUSH_BUTTON_1) == LOW) {
// pushButton1State is used to avoid sequential toggles
if (pushButton1State != LOW) {
// Toggle Relay state
relay1State = !relay1State;
digitalWrite(RELAY_PIN_1, relay1State);
// Update Button Widget
Blynk.virtualWrite(VPIN_BUTTON_1, relay1State);
}
pushButton1State = LOW;
} else {
pushButton1State = HIGH;
}
if (digitalRead(PUSH_BUTTON_2) == LOW) {
// pushButton2State is used to avoid sequential toggles
if (pushButton2State != LOW) {
// Toggle Relay state
relay2State = !relay2State;
digitalWrite(RELAY_PIN_2, relay2State);
// Update Button Widget
Blynk.virtualWrite(VPIN_BUTTON_2, relay2State);
}
pushButton2State = LOW;
} else {
pushButton2State = HIGH;
}
}
void setup()
{
Serial.begin(115200);
Blynk.begin(AUTH, WIFI_SSID, WIFI_PASS,"blynk-cloud.com", 8442);
ArduinoOTA.setHostname(OTA_HOSTNAME); // For OTA - Use your own device identifying name
ArduinoOTA.begin(); // For OTA
pinMode(RELAY_PIN_1, OUTPUT);
pinMode(PUSH_BUTTON_1, INPUT_PULLUP);
digitalWrite(RELAY_PIN_1, relay1State);
pinMode(RELAY_PIN_2, OUTPUT);
pinMode(PUSH_BUTTON_2, INPUT_PULLUP);
digitalWrite(RELAY_PIN_2, relay2State);
// Setup a function to be called every 100 ms
timer.setInterval(500L, checkPhysicalButton);
}
void loop()
{
Blynk.run();
ArduinoOTA.handle(); // For OTA
timer.run();
}