Hello guys.
I really need your help with this since I have been trying to solve the disconnection issue by checking my code and reading others topics but I can’t find the reason of my problem.
-
I’m connecting 2 wemos ESP8266 with the same auth code
-
I activate the alarm of my house using virtual pin 110 and both devices sent a notification if they detect motion
-
I am attaching the entire code, perhaps someone can see where the error is, I deed to note that Ive been careful of no sending multiple requests to the web server.
Thanks guys
char auth[] = “XXXXX”; // You should get Auth Token in the Blynk App.
const char* ssid = “YYYY”;
const char* password = “ZZZZZ”;
//Variables
char temp[10];
String Message = “”;
int Execution1=0;
int Execution2=0;
int Switch=0;
int Alarm=0;
int Timer=0;
int TimerStatus=0;
int MotionState=0;
int MotionStateP=0;
int SWState=0;
int SWStateP=0;
int SWFlag=0;
int NightTime=0;
//Modes
int Mode=1;
//Mode=1=Normal;
//Mode=2=Movie1;
//Mode=3=BedTime;
//Mode=4=Movie2;
//Mode=5=GoodBye;
// I/O initialization
//-----------------------------------------------------------
int Relay= D8;
int Motion= D5;
int SW=D3;
//----------------------------------------------------------
//Lux variables
int BH1750_address = 0x23; // i2c Addresse
byte buff[2];
//----------------------------------------------------------
WidgetLED led1(V103);
//-----------------------------------------------------------
void setup()
{
Wire.begin();
delay(200);
Serial.begin(9600); // See the connection status in Serial Monitor
//BLYNK*******//
Blynk.begin(auth, ssid, password);
//MQTT************//
// client.setServer(mqtt_server, 11835);
//client.setCallback(callback);
// reconnect();
//Lux sensor initiation//
Wire.begin();
BH1750_Init(BH1750_address);
//OTA//
ArduinoOTA.setPort(8266); // Port defaults to 8266
ArduinoOTA.setHostname(“Bedroom V10”);// Hostname defaults to esp8266-[ChipID]
ArduinoOTA.setPassword((const char )“sonyk790”); //OTA authentication
ArduinoOTA.onStart( {
Serial.println(“Start”);
});
ArduinoOTA.onEnd( {
Serial.println("\nEnd");
});
ArduinoOTA.begin();
//*************************************************************//
// I/O SetUp
pinMode(Relay, OUTPUT); //D3
pinMode(Motion, INPUT); //D5
pinMode(SW, INPUT); //D8
}
//****************************SYNC BLYNK ***************************************//
// Keep this flag not to re-sync on every reconnection
bool isFirstConnect = true;
// This function will run every time Blynk connection is established
BLYNK_CONNECTED() {
if (isFirstConnect) {
// Request Blynk server to re-send latest values for all pins
Blynk.syncAll();
// You can also update an individual Virtual pin like this:
//Blynk.syncVirtual(V0);
isFirstConnect = false;
}
}
//*********************************COMANDS FROM BLYNK *****************************************//
// Light Switch
BLYNK_WRITE(V10)
{
BLYNK_LOG(“Got a value: %s”, param.asStr());
// You can also use: double d = param.asDouble()
int i = param.asInt();
if (i==0){
Switch=0;
SWFlag=0;
}
if (i==1){
Switch=1;
SWFlag=1;
}
Execution1=1;
}
// Alarm
BLYNK_WRITE(V101)
{
BLYNK_LOG(“Got a value: %s”, param.asStr());
// You can also use: double d = param.asDouble()
int i = param.asInt();
if (i==0){
Alarm=0;
}
if (i==1){
Alarm=1;
}
Execution2=1;
}
//NightTime
BLYNK_WRITE(V110)
{
BLYNK_LOG(“Got a value: %s”, param.asStr());
// You can also use: double d = param.asDouble()
int i = param.asInt();
if (i==0){
NightTime=0;
}
if (i==1){
NightTime=1;
}
}
//*****************************************************************************************/
void activation1() {
if (Switch==1){
Serial.println(“Bedroom ON”);
//Blynk.notify(“Bedroom ON”);
//Blynk.virtualWrite(1,“Alarm armed”);
digitalWrite(Relay, HIGH);
}
if (Switch==0){
Serial.println(“Bedroom OFF”);
// Blynk.notify(“Bedroom OFF”);
//Blynk.virtualWrite(1,“Alarm armed”);
digitalWrite(Relay, LOW);
}
Execution1=0;
delay (1500);
Blynk.run();
Blynk.syncAll();
}
//*****************************************************************************************/
void activation2() {
if (Alarm==1){
Serial.println(“Alarm armed”);
// Blynk.notify(“Alarm armed”);
}
if (Alarm==0){
Serial.println(“Alarm disarmed”);
// Blynk.notify(“Alarm disarmed”);
MotionStateP=0;
//turn on siren
//digitalWrite(Siren, LOW);
led1.off(); //siren led off app V103
}
Execution2=0;
}
//*****************************************************************************************//
void loop()
{
// client.loop(); // Careful its necessary to get mqtt message
Blynk.run(); // All the Blynk Magic happens here…
ArduinoOTA.handle(); //OTA code
// Read Sensors
MotionState=digitalRead(Motion); // read the input motion:
SWState=digitalRead(SW); // read the input SW:
Serial.println(“SWState=”);
Serial.println(SWState);
//-------------------------------Light Logic------------------------------------------//
// By motion
if (MotionState==1 && NightTime==1 && Mode==1 && SWFlag==0 && Switch==0) { //Mode=1=Normal
Blynk.run();
Switch=1;
Execution1=1;
Timer=12000;
Blynk.virtualWrite(10,1);
Serial.println(“Light on by motion”);
}
if (MotionState==0 && NightTime==1 && Mode==1 && SWFlag==0 && Switch==1){
Timer–;
Serial.println(Timer);
}
if (Timer==0){
Blynk.run();
Switch=0;
Execution1=1;
Timer=12000;
Blynk.virtualWrite(10,0);
Serial.println(“Light off by motion”);
}
//Alarm Logic
if (MotionState==1 && MotionState!=MotionStateP && Alarm==1) // && Sensor1==1)
{// digitalWrite(Siren, HIGH);
delay (4000);
Blynk.run();
if (MotionState==1 && MotionState!=MotionStateP && Alarm==1) // && Sensor1==1)
{
Serial.println(“Intrution detected in Bedroom”);
Blynk.notify(“Intrution detected in Bedroom”);
led1.on(); //Led siren ON
MotionStateP=MotionState; //in alarm both =1
}
}
if(Execution1==1){ //Run loop for Light
activation1();
}
if(Execution2==1){ //Run loop for alarm
activation2();
}
}