[SOLVED] Problems with sketch`s work in rainy weather

Hello guys. First the wall, many thanks for this awesome app!
I have successful project of smart home with Blynk.
But now I have a problem. One of my rooms have autolight sketch based on D1 mini with two IR sensors and one photoresistor. The light turns on when in room detects some motion and if its too dark for this and goes off in 30 seconds without motion. But now when i turn on autolight function in app, the light immediately turn on too without the motion and not goes off according to timer in sketch. When i begin to search the problem i cut off router and without the internet connection with blynk the sketch works normally. When i gave back the internet connection the problem repeats. Noticed that now is rainy on the streets and net connection becomes more weak than in normal weather.
Earlier the sketch works fine more than week. Please, help me to solve the problem. Best regards guys.

We cannot do anything without any code. I think the hardware setup is clear. Can you post the code (without token code and properly formatted using three backticks ` in front and after the code).

here it is… Maybe the problem lies in Bridge Widget? When I remove bridge link from code the sketch works faster.

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <DHT.h>

#define DHTPIN 4
#define DHTTYPE DHT11 


long unsigned int lowIn; 
long unsigned int pause = 30000;
bool lockLow = true;
bool takeLowTime;




long unsigned int lowIn2; 
long unsigned int pause2 = 30000;
bool lockLow2 = true;
bool takeLowTime2;

bool needlightstate= false;
bool needlightwas=false;






const int doorprihoj = 14; 
const int pirprihoj = 12;    
const int pir2prihoj = 16;
const int releprihoj = 3;   

const int pir1kuh = 2;
const int pir2kuh = 0;
const int lightkuh = 5;
const int relekuh = 1;


WidgetBridge bridge1(V8);

int timer1;
int timer2;


int autoprihoj =0;
int autokuh=0;
int ohrana=0;


char auth[] = ""; 

char ssid[] = "";
char pass[] = "";


SimpleTimer timer;
DHT dht(DHTPIN, DHTTYPE);

bool isFirstConnect = true;

void sendUptime()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  t = ((int) (t * 10) / 10.0);
  h = ((int) (h * 10) / 10.0);
  
  Blynk.virtualWrite(8, t); // virtual pin
  Blynk.virtualWrite(9, h); // virtual pin
 delay(10);
  bridge1.virtualWrite (V26, t);
  delay(10);
  

}


 
 




BLYNK_CONNECTED() {
  
 bridge1.setAuthToken("");

 
  if (isFirstConnect) {
    
    Blynk.syncAll();

    

    isFirstConnect = false;
  }}



 


void sendnotify ()
{if (ohrana==1){
  Blynk.virtualWrite (V11, 255);
   if(digitalRead(pir1kuh)==HIGH||digitalRead(pir2kuh)==HIGH)
  {Blynk.notify ("Движение на кухне");
  Blynk.virtualWrite (V10, 255);}

 if(digitalRead(pir1kuh)==LOW&&digitalRead(pir2kuh)==LOW)
  {Blynk.virtualWrite (V10, 0);}
  
  
  if (digitalRead(doorprihoj)==0)
 {Blynk.notify ("Входная Дверь открыта"); 
   Blynk.virtualWrite (V4, 255);}

if (digitalRead(doorprihoj)==1)
 {
   Blynk.virtualWrite (V4, 0);}
  
   if(digitalRead(pirprihoj)==HIGH||digitalRead(pir2prihoj)==HIGH)
  { 
  Blynk.notify ("Движение в прихожей"); 
  Blynk.virtualWrite (V7, 255);}
  
  if(digitalRead(pirprihoj)==LOW&&digitalRead(pir2prihoj)==LOW)
  {Blynk.virtualWrite (V7, 0); 
      }}
      
 if (ohrana==0){
  Blynk.virtualWrite (V11, 0);
   if(digitalRead(pir1kuh)==HIGH||digitalRead(pir2kuh)==HIGH)
  {
  Blynk.virtualWrite (V10, 255);}

 if(digitalRead(pir1kuh)==LOW&&digitalRead(pir2kuh)==LOW)
  {Blynk.virtualWrite (V10, 0);}
  
  
  if (digitalRead(doorprihoj)==0)
 {
   Blynk.virtualWrite (V4, 255);}

if (digitalRead(doorprihoj)==1)
 { 
   Blynk.virtualWrite (V4, 0);}
  
   if(digitalRead(pirprihoj)==HIGH||digitalRead(pir2prihoj)==HIGH)
  { 
   
  Blynk.virtualWrite (V7, 255);}
  
  if(digitalRead(pirprihoj)==LOW&&digitalRead(pir2prihoj)==LOW)
  {Blynk.virtualWrite (V7, 0); 
      }}     
      
      
      
      }

  
void setup()
{
  
  timer1= timer.setInterval(10000, sendUptime);//1000=1s
  timer2= timer.setInterval(5000, sendnotify);
  
  Serial.begin(9600); 
  pinMode (pirprihoj,INPUT);    
  pinMode (pir2prihoj,INPUT); 
  pinMode (releprihoj,OUTPUT);   
  pinMode (doorprihoj,INPUT);
  pinMode (pir1kuh,INPUT);
  pinMode (pir2kuh,INPUT);
  pinMode (lightkuh,INPUT);
  pinMode (relekuh,OUTPUT);
  dht.begin();
  digitalWrite (releprihoj, HIGH);
  digitalWrite (relekuh, HIGH);
  Blynk.begin(auth, ssid, pass);
}


 BLYNK_WRITE(V0) 
  {if(param.asInt()==0)
{ if (autoprihoj==1) {digitalWrite(releprihoj, LOW);}
if (autoprihoj==0) {
 if (digitalRead (releprihoj)==LOW)
 {Blynk.virtualWrite (V0, LOW);}
 if (digitalRead (releprihoj)==HIGH)
 {Blynk.virtualWrite (V0, HIGH);}}}
  
else { if (autoprihoj==1) {digitalWrite(releprihoj, HIGH);}
if (autoprihoj==0) {
 if (digitalRead (releprihoj)==LOW)
 {Blynk.virtualWrite (V0, LOW);}
 if (digitalRead (releprihoj)==HIGH)
 {Blynk.virtualWrite (V0, HIGH);}}}}

 
 
 





 BLYNK_WRITE(V3) 
  {if(param.asInt()==0)
{ if (autokuh==1) {digitalWrite(relekuh, LOW);}
if (autokuh==0) {
 if (digitalRead (relekuh)==LOW)
 {Blynk.virtualWrite (V3, LOW);}
 if (digitalRead (relekuh)==HIGH)
 {Blynk.virtualWrite (V3, HIGH);}}}
  
else { if (autokuh==1) {digitalWrite(relekuh, HIGH);}
if (autokuh==0) {
 if (digitalRead (relekuh)==LOW)
 {Blynk.virtualWrite (V3, LOW);}
 if (digitalRead (relekuh)==HIGH)
 {Blynk.virtualWrite (V3, HIGH);}}}}

  BLYNK_WRITE(V1) 
 {if(param.asInt()==0)
{ autoprihoj=0;
  }
else {autoprihoj=1;}}

 BLYNK_WRITE(V2) 
 {if(param.asInt()==0)
{ autokuh=0;
  }
else {autokuh=1;}}


BLYNK_WRITE(V17)
 {if(param.asInt()==0)
 {ohrana=1;

 Blynk.notify ("ОХРАНА АКТИВИРОВАНА"); 
 Blynk.virtualWrite (V11, 255);}

 
 else {ohrana=0;

 Blynk.notify ("ОХРАНА ОТКЛЮЧЕНА");
 Blynk.virtualWrite (V11, 0);}}


void loop()
{

   Blynk.run();
  timer.run();
    
  if (autoprihoj==0)
  { 




   
 int door=digitalRead(doorprihoj);
  
  
  if(digitalRead(pirprihoj)==HIGH||door==0||digitalRead(pir2prihoj)==HIGH)
  {
    
    if(lockLow)
    {
      lockLow = false;     
      delay(50);
      digitalWrite(3, LOW);
      Blynk.virtualWrite (V0, LOW);
      
    }        
    takeLowTime = true;
  }

  
  if(digitalRead(pirprihoj)==LOW&&door==1&&digitalRead(pir2prihoj)==LOW)
  {      
    
    if(takeLowTime)
    {
      lowIn = millis();          
      takeLowTime = false;       
    }
   
    if(!lockLow && millis() - lowIn > pause)
    { 
     
      lockLow = true;               
      delay(50);
      digitalWrite (3, HIGH);
      Blynk.virtualWrite (V0, HIGH);
      
    }

  }
    
       
  }

 if (autokuh==0) \\\PROBLEM STARTS HERE
  { 

    
    
    
    bool needlight= (digitalRead(lightkuh)==HIGH&&digitalRead(pir1kuh)==HIGH)||(digitalRead(lightkuh)==HIGH&&digitalRead(pir2kuh)==HIGH);
    if (needlight)
     {digitalWrite(1, LOW);
      needlightstate = true;
      delay (10);
      Blynk.virtualWrite (V3, LOW);
     }
  

      if (needlightstate==true){
    
    if (digitalRead(pir1kuh)==HIGH||digitalRead(pir2kuh)==HIGH)
  {
    
    if(lockLow2)
    {
      lockLow2 = false;     
      delay(10);
      digitalWrite(1, LOW);
      Blynk.virtualWrite (V3, LOW);
    }        
    takeLowTime2 = true;
  }

  
  if(digitalRead(pir1kuh)==LOW&&digitalRead(pir2kuh)==LOW)
  {      
    
    if(takeLowTime2)
    {
      lowIn2 = millis();         
      takeLowTime2 = false;       
    }
    
    if(!lockLow2 &&( millis() - lowIn2 > pause2))
    { 
      
      lockLow2 = true;               
      delay(10);
     
      digitalWrite (1, HIGH);
      Blynk.virtualWrite (V3, HIGH);
      delay (10); 
      needlightstate= false;
      delay (10);
     }
   
    }}
   
    
    }

}

No, you have loads of stuff in your loop(), you cannot do that with Blynk. Everything, except Blynk.run() and timer.run() should go somewhere else. Like the notify loop. All logic needs to be scheduled with a timer.

1 Like

So I must take all loop() (exept timerrun, blynkrun) and insert in void () with timer.set interval for example in 1 second, so as not overload the Blynk. Am I Right?

2 Likes

Very right! That’s the way it will work most stable.

thanks for immediate help) I`ll fix it)

Also noticed a few delay() calls that you’ll need to get rid of.

1 Like

ok) I’ll continue to learn further.

1 Like

Just redone the sketch. Works fine. Thanks again) Problem solved.

oh no… Again does not turn off… Couple rounds in normal state after sketch`s redone , then the light turn on himself and not turned off.

Can you try commenting out the Blynk.syncAll() part in the BLYNK_CONNECTED? It could be overloading stuff. I’ve never found it to work reliable, but that could be just me :slight_smile:

okay. I`ll write the result)

It also sounds like you are running your device at the edge of it’s solid WiFi range… and humidity will attenuate the signal, so even after code cleanup (which will help with data transmission stability) you might also want to move the router to a more central location, or add repeaters for “rainy day” use :wink:

1 Like

No, router stands exactly in range. About 2 meters of straight contact without walls.
Everything was worked perfect even with void loop() was overloaded about over one week.
Yesterday when the weather was wet, internet connection shows weak stability for some reason.
And showtime begins… hangs, suspensions, and unpleasant arbitrary switching-on.

You’re right about this. I have an ESP D1 mini at the far end of my property to turn on garden lights. But during rainy nights, I lose connection to it and thus cannot control it. Most of the time I can’t turn it on.

So you are right about the weather part. I fixed it by upgrading to the D1 Mini Pro and adding a +3db antenna… twice the signal strength now and havnt lost connection for over a month.

Good decision. But in my event I must add antenna to my network provider`s equipment on a roof of my house)) So I’ll chop the provider to improve the quality of connection and just in case make sketch more lighter.

That would be the first place to start, yup :slight_smile:

There can be a done a lot in the software (coding skills :slight_smile: ). I think you can even boost the signal from the Wemos with some parameters.

What you have to remember when dealing with Wifi, it’s still a two-way medium. So if you have a VERY good sender (router or something) but your receiving equipment is lousy, e.g. bad or small antenna’s and such, you still get a crappy connection. Lots of people think buying a very expensive, big router is the solution, but it’s not. In most cases (laptops, phones etc.) you need to ensure good quality hardware too in the form of decent network cards, good software (drivers mostly) and good antenna’s.

1 Like

@Pacer, Take a look at a few of my projects, in particular one that relies heavily on carefully timed timers (as to not crash the little ESP).

1 Like