Hi I make a project on shop security system. If anyone wants to try to enter in room blynk send a notification to owner and all appliances can be control through blynk apps. It perfectly work for couple of hours. But after few hours blynk notification does not working and if I control appliances by bluetooth or manual switch not reflected on blynk platform which previously working. I think there may be problem in Blynk.virtualWrite(); Please help me. I upload my program below.
Please note blynk show online and can be controlled through blynk.
#define BLYNK_TEMPLATE_ID "xxxxxxxxx"
#define BLYNK_TEMPLATE_NAME "xxxxxxxxxx"
#define BLYNK_AUTH_TOKEN "xxxxxxxxx"
char auth[] = BLYNK_AUTH_TOKEN;
#include <BlynkSimpleEsp32.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BluetoothSerial.h>
#include <Preferences.h>
#include <AceButton.h>
BluetoothSerial SerialBT;
using namespace ace_button;
Preferences pref;
// Replace with your network credentials
char ssid[] = "xxxx";
char pass[] = "12345678";
bool wifiConnected = false;
ButtonConfig config1;
AceButton button1(&config1);
ButtonConfig config2;
AceButton button2(&config2);
ButtonConfig config3;
AceButton button3(&config3);
ButtonConfig config4;
AceButton button4(&config4);
ButtonConfig config5;
AceButton button5(&config5);
ButtonConfig config6;
AceButton button6(&config6);
void handleEvent1(AceButton*, uint8_t, uint8_t);
void handleEvent2(AceButton*, uint8_t, uint8_t);
void handleEvent3(AceButton*, uint8_t, uint8_t);
void handleEvent4(AceButton*, uint8_t, uint8_t);
void handleEvent5(AceButton*, uint8_t, uint8_t);
void handleEvent6(AceButton*, uint8_t, uint8_t);
// Pin Definitions
#define pirPin 36
#define rcwlPin 4
#define irPin 35
#define sirenPin 23
#define emergencyLight 22
#define indoorLight1 18
#define indoorLight2 19
#define outdoorLight1 21
#define bannerLight 5
#define light2Pin 15
#define secureOnLight 17
#define wifiLED 2
#define buttonPin_1 13
#define buttonPin_2 12
#define buttonPin_3 14
#define buttonPin_4 27
#define buttonPin_5 26
#define buttonPin_6 25
#define buttonPin_7 33
//Define Blynk Virtual PIN
#define VPIN_Indoor1 V0
#define VPIN_Indoor2 V1
#define VPIN_Outdoor V2
#define VPIN_Emergency V3
#define VPIN_Banner V4
#define VPIN_OutdoorSensor V5
#define VPIN_DoorSensor V6
#define VPIN_IndoorSensor V7
#define VPIN_DoorMotion V8
#define VPIN_IndoorMotion V9
#define VPIN_SecureMode V10
#define VPIN_ModeName V11
#define VPIN_Light2 V12
BlynkTimer timer;
char btCommand;
// State Variables
// Timing Constants
const unsigned long sirenOffTime = 5000;
const unsigned long rcwlSirenDuration = 5000; // Siren stays on for 5 seconds
const unsigned long rcwlTimeout = 60000;
unsigned long lastReconnectAttempt = 0;
const unsigned long CONNECTION_TIMEOUT = 10000;
const unsigned long WIFI_RECONNECT_INTERVAL = 10000; // 10 seconds
unsigned long currentMillis =0;
unsigned long millis1 = 0;
unsigned long pirFirstDetected = 0;
unsigned long pirLastDetected = 0;
unsigned long rcwlSirenTime = 0;
bool secureModeOn = false;
bool pirActive = false;
bool rcwlActive = false;
bool rcwlSirenOn= false;
bool pirLightOn = false;
static bool sirenAlreadyOn = false; // Track siren state
bool toggleState_1 = LOW;
bool toggleState_2 = LOW;
bool toggleState_3 = LOW;
bool toggleState_4 = LOW;
bool toggleState_5 = LOW;
bool toggleState_6 = LOW;
void handleBT(char btCommand){
switch (btCommand) {
case 'A':
digitalWrite(indoorLight2, HIGH);
Blynk.virtualWrite(VPIN_Indoor2, HIGH);
toggleState_1 = HIGH;
Serial.println("Indoor on");
delay(50);
break;
case 'B':
digitalWrite(indoorLight2, LOW);
Blynk.virtualWrite(VPIN_Indoor2, LOW);
toggleState_1 = LOW;
Serial.println("Indoor off");
break;
case 'C':
digitalWrite(light2Pin, HIGH);
Blynk.virtualWrite(VPIN_Light2, HIGH);
toggleState_2 = HIGH;
break;
case 'D':
digitalWrite(light2Pin, LOW);
Blynk.virtualWrite(VPIN_Light2, LOW);
toggleState_2 = LOW;
break;
case 'E':
digitalWrite(indoorLight1, HIGH);
Blynk.virtualWrite(VPIN_Indoor1, HIGH);
toggleState_3 = HIGH;
delay(50);
break;
case 'F':
digitalWrite(indoorLight1, LOW);
Blynk.virtualWrite(VPIN_Indoor1, LOW);
toggleState_3 = LOW;
delay(50);
break;
case 'G':
digitalWrite(outdoorLight1, HIGH);
Blynk.virtualWrite(VPIN_Outdoor, HIGH);
Serial.println("Outdoor on");
toggleState_4 = HIGH;
delay(50);
break;
case 'H':
digitalWrite(outdoorLight1, LOW);
Blynk.virtualWrite(VPIN_Outdoor, LOW);
Serial.println("Outdoor off");
toggleState_4 = LOW;
delay(50);
break;
case 'I':
digitalWrite(emergencyLight, HIGH);
Blynk.virtualWrite(VPIN_Emergency, HIGH);
toggleState_5 = HIGH;
delay(50);
break;
case 'J':
digitalWrite(emergencyLight, LOW);
Blynk.virtualWrite(VPIN_Emergency, LOW);
toggleState_5 = LOW;
delay(50);
break;
case 'K':
digitalWrite(bannerLight, HIGH);
Blynk.virtualWrite(VPIN_Banner, HIGH);
toggleState_6 = HIGH;
delay(50);
break;
case 'L':
digitalWrite(bannerLight, LOW);
Blynk.virtualWrite(VPIN_Banner, LOW);
toggleState_6 = LOW;
break;
case 'X':
SerialBT.write('X');
Serial.println("Secure_ON");
secureModeOn = true;
Serial.println("Secure Mode On");
Blynk.virtualWrite(VPIN_SecureMode, secureModeOn);
Blynk.virtualWrite(VPIN_ModeName, "Security ON");
break;
case 'Y':
SerialBT.write('Y');
Serial.println("Secure_OFF");
secureModeOn = false;
Serial.println("Secure Mode Off");
Blynk.virtualWrite(VPIN_SecureMode, secureModeOn);
Blynk.virtualWrite(VPIN_ModeName, "Security OFF!");
pirLightOn = false;
rcwlSirenOn = false;
break;
default:
break;
}
}
void setup() {
Serial.begin(115200);
unsigned long startAttemptTime = millis();
connectToWiFi();
Blynk.config(BLYNK_AUTH_TOKEN);
config1.setEventHandler(handleEvent1);
config2.setEventHandler(handleEvent2);
config3.setEventHandler(handleEvent3);
config4.setEventHandler(handleEvent4);
config5.setEventHandler(handleEvent5);
config6.setEventHandler(handleEvent6);
button1.init(buttonPin_1);
button2.init(buttonPin_2);
button3.init(buttonPin_3);
button4.init(buttonPin_4);
button5.init(buttonPin_5);
button6.init(buttonPin_6);
SerialBT.begin("abcd");
Serial.println("Printing Ready");
pinMode(pirPin, INPUT);
pinMode(rcwlPin, INPUT);
pinMode(indoorLight1, OUTPUT);
pinMode(light2Pin, OUTPUT);
pinMode(indoorLight2, OUTPUT);
pinMode(outdoorLight1, OUTPUT);
pinMode(emergencyLight, OUTPUT);
pinMode(bannerLight, OUTPUT);
pinMode(sirenPin, OUTPUT);
pinMode(secureOnLight, OUTPUT);
pinMode(buttonPin_1, INPUT);
pinMode(buttonPin_2, INPUT);
pinMode(buttonPin_3, INPUT);
pinMode(buttonPin_4, INPUT);
pinMode(buttonPin_5, INPUT);
pinMode(buttonPin_6, INPUT);
pinMode(wifiLED, OUTPUT);
digitalWrite(secureOnLight, LOW);
digitalWrite(indoorLight1, LOW);
digitalWrite(light2Pin, LOW);
digitalWrite(sirenPin, LOW);
digitalWrite(wifiLED, LOW);
digitalWrite(outdoorLight1, LOW);
delay(250);
digitalWrite(indoorLight2, LOW);
delay(100);
Blynk.virtualWrite(VPIN_IndoorMotion, LOW);
Blynk.virtualWrite(VPIN_IndoorSensor, LOW);
Blynk.virtualWrite(VPIN_OutdoorSensor, LOW);
}
BLYNK_WRITE(VPIN_Indoor1) {
toggleState_3 = param.asInt();
digitalWrite(indoorLight1, toggleState_3);
}
// Blynk write handlers for relays
BLYNK_WRITE(VPIN_Indoor2) {
toggleState_1 = param.asInt();
digitalWrite(indoorLight2, toggleState_1);
}
BLYNK_WRITE(VPIN_Outdoor) {
toggleState_4 = param.asInt();
digitalWrite(outdoorLight1, toggleState_4);
}
BLYNK_WRITE(VPIN_Banner) {
toggleState_6 = param.asInt();
digitalWrite(bannerLight, toggleState_6);
}
BLYNK_WRITE(VPIN_Light2) {
toggleState_2 = param.asInt();
digitalWrite(light2Pin, toggleState_2);
}
BLYNK_WRITE(VPIN_Emergency) {
toggleState_5 = param.asInt();
digitalWrite(emergencyLight, toggleState_5);
}
BLYNK_WRITE(VPIN_SecureMode) {
secureModeOn = param.asInt();
if(secureModeOn){
Blynk.virtualWrite(VPIN_ModeName, "Security ON");
}
else{
Blynk.virtualWrite(VPIN_ModeName, "Security OFF");
Blynk.virtualWrite(VPIN_IndoorMotion, LOW);
Blynk.virtualWrite(VPIN_IndoorSensor, LOW);
Blynk.virtualWrite(VPIN_OutdoorSensor, LOW);
pirLightOn = false;
rcwlSirenOn = false;
}
}
void loop() {
// Only run Blynk tasks if connected
if (wifiConnected) {
digitalWrite(wifiLED, HIGH);
Blynk.run();
}
if (!wifiConnected && (millis() - lastReconnectAttempt >= WIFI_RECONNECT_INTERVAL)) {
digitalWrite(wifiLED, LOW);
lastReconnectAttempt = millis();
connectToWiFi();
}
// Read sensor states
pirActive = digitalRead(pirPin);
rcwlActive = digitalRead(rcwlPin);
button1.check();
button2.check();
button3.check();
button4.check();
button5.check();
button6.check();
// Offline mode control via Serial Bluetooth{ .... }
if (SerialBT.available()) {
btCommand = SerialBT.read();
handleBT(btCommand);
Serial.println(btCommand);
}
if(secureModeOn){
handlePIR();
handleRCWL();
digitalWrite(secureOnLight, HIGH);
}
else {
digitalWrite(secureOnLight, LOW);
digitalWrite(sirenPin, LOW);
}
}
void handlePIR() {
unsigned long currentMillis = millis();
if (pirActive) {
if (!pirLightOn) {
// PIR is detected for the first time
Serial.println("PIR detected");
pirFirstDetected = currentMillis;
millis1 = currentMillis;
digitalWrite(sirenPin, HIGH);
Blynk.virtualWrite(VPIN_DoorMotion, HIGH);
pirLightOn = true;
}
}
// If the PIR is no longer active
if (!pirActive){
if(!sirenAlreadyOn) {
if (pirLightOn && (currentMillis - pirLastDetected) > sirenOffTime) {
digitalWrite(sirenPin, LOW);
Blynk.virtualWrite(VPIN_DoorMotion, LOW);
Serial.println("Light turned off due to PIR inactivity");
pirLightOn = false;
//pirLastDetected = currentMillis;
}
}
}
// Update the last detected time when PIR is active
if (pirActive) {
pirLastDetected = currentMillis;
}
}
void handleRCWL() {
unsigned long currentMillis = millis();
// Condition 1: If RCWL active and PIR not active
if (rcwlActive && !pirActive && (currentMillis - pirLastDetected) > rcwlTimeout) {
if (!rcwlSirenOn){
Serial.println("RCWL detected without PIR");
digitalWrite(sirenPin, HIGH); // Turn on siren
Blynk.virtualWrite(VPIN_IndoorSensor, HIGH);
rcwlSirenOn = true;
rcwlSirenTime = millis();
}
}
if (rcwlSirenOn && !rcwlActive){
if(millis() - rcwlSirenTime >= rcwlSirenDuration){
if(!sirenAlreadyOn){
Serial.println("RCWL only siren off");
digitalWrite(sirenPin, LOW); // Turn off siren
Blynk.virtualWrite(VPIN_IndoorSensor, LOW);
rcwlSirenOn = false;
}
}
}
static unsigned long rcwlActiveStartTime = 0; // Track when RCWL became active
if (rcwlActive && pirActive || rcwlActive && (currentMillis - pirLastDetected) <= rcwlTimeout) { // add pir lastactive time
rcwlActiveStartTime = currentMillis; // Record when RCWL became active
if (!sirenAlreadyOn) {
Serial.println("RCWL detected with PIR active or recently active");
digitalWrite(sirenPin, HIGH); // Turn on siren
Blynk.virtualWrite(VPIN_IndoorMotion, HIGH);
sirenAlreadyOn = true; // Record siren state
}
}
if (sirenAlreadyOn && !rcwlActive) {
if (currentMillis - rcwlActiveStartTime >= 8000) {
digitalWrite(sirenPin, LOW); // Turn off siren
Blynk.virtualWrite(VPIN_IndoorMotion, LOW);
sirenAlreadyOn = false; // Reset siren state
Serial.println("Siren turned off after RCWL became inactive");
}
}
}
void handleEvent1(AceButton* button, uint8_t eventType, uint8_t buttonState) {
Serial.println("EVENT1");
switch (eventType) {
case AceButton::kEventPressed:
toggleState_5 = !toggleState_5;
digitalWrite(emergencyLight, toggleState_5);
Blynk.virtualWrite(VPIN_Emergency, toggleState_5);
delay(50);
break;
}
}
void handleEvent2(AceButton* button, uint8_t eventType, uint8_t buttonState) {
Serial.println("EVENT2");
switch (eventType) {
case AceButton::kEventPressed:
digitalWrite(indoorLight2, !toggleState_1);
toggleState_1 = !toggleState_1;
Blynk.virtualWrite(VPIN_Indoor2, toggleState_1);
delay(50);
break;
}
}
void handleEvent3(AceButton* button, uint8_t eventType, uint8_t buttonState) {
Serial.println("EVENT3");
switch (eventType) {
case AceButton::kEventPressed:
digitalWrite(light2Pin, !toggleState_2);
toggleState_2 = !toggleState_2;
Blynk.virtualWrite(VPIN_Light2, toggleState_2);
delay(50);
break;
}
}
void handleEvent4(AceButton* button, uint8_t eventType, uint8_t buttonState) {
Serial.println("EVENT4");
switch (eventType) {
case AceButton::kEventPressed:
digitalWrite(indoorLight1, !toggleState_3);
toggleState_3 = !toggleState_3;
Blynk.virtualWrite(VPIN_Indoor1, toggleState_3);
delay(50);
break;
}
}
void handleEvent5(AceButton* button, uint8_t eventType, uint8_t buttonState) {
Serial.println("EVENT5");
switch (eventType) {
case AceButton::kEventPressed:
digitalWrite(outdoorLight1, !toggleState_4);
toggleState_4 = !toggleState_4;
Blynk.virtualWrite(VPIN_Outdoor, toggleState_4);
delay(50);
break;
}
}
void handleEvent6(AceButton* button, uint8_t eventType, uint8_t buttonState) {
Serial.println("EVENT6");
switch (eventType) {
case AceButton::kEventPressed:
digitalWrite(bannerLight, !toggleState_6);
toggleState_6= !toggleState_6;
Blynk.virtualWrite(VPIN_Banner, toggleState_6);
delay(50);
break;
}
}
void connectToWiFi() {
WiFi.begin(ssid, pass);
Serial.print("Connecting to Wi-Fi");
for (int i = 0; i < 10; i++) { // Try for 10 seconds
if (WiFi.status() == WL_CONNECTED) {
wifiConnected = true;
Serial.println("\nWi-Fi Connected!");
Blynk.connect();
return;
}
Serial.print(".");
delay(1000);
}
Serial.println("\nWi-Fi Connection Failed!");
wifiConnected = false;
digitalWrite(wifiLED, LOW);
}