BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Connection lost after wifi access point reboot


#1

Hi friends. when I reboot my wifi router the nodemcu connection lost and cannot be reconnecting to server but it connected to the wifi router. the code is blow:

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <SI7021.h>
#include <Wire.h>
#include <Adafruit_BMP085.h>

#include <BH1750.h>  
BH1750 lightMeter;

char auth[] = "fe5078fcb5f04f5384ac70cb9ab91697";                 //Put your blink token
char ssid[] = "MikroTik Tesla";                            //Put your SSID of your connection
char pass[] = "12345678";                            //Put your Password of your connection
char server[] = "10.5.50.18";  

SI7021 sensor;

SimpleTimer timer;

#define I2C_SCL 12      //D6
#define I2C_SDA 13     //D7
Adafruit_BMP085 bmp;

#define ledPin 15 
#define pirPin 14

float dst, bt, bp, ba, q;
char dstmp[20], btmp[20], bprs[20], balt[20];
bool bmp085_present = true;

void setup()
{
  WiFi.mode(WIFI_STA);
  Serial.begin(115200); // See the connection status in Serial Monitor
  Blynk.begin(auth, ssid, pass, server, 8080); //insert here your SSID and password
  Wire.begin(I2C_SDA, I2C_SCL);
  delay(10);
if (!bmp.begin()) {
  Serial.println("Could not find a valid BMP085 sensor, check wiring!");
  //while (1) {}
  }
  timer.setInterval(1000L, sendUptime);
  timer.setInterval(1000L, PIRval);
  timer.setInterval(1000L, pir);
  timer.setInterval(1000L, dozd);
  timer.setInterval(1000L, sendWifi);
  lightMeter.begin();
  //sensor.begin(SDA,SCL);
  pinMode(ledPin, OUTPUT);
  pinMode(pirPin, INPUT);
}

int pirState;
int val;
int x;

BLYNK_CONNECTED() {
      Blynk.syncVirtual(V0);
  }

BLYNK_WRITE(V0){
 x = param.asInt();
 }

void PIRval()
{
val = digitalRead(pirPin);
    if (val == HIGH) {
      digitalWrite(ledPin, HIGH);  
      }
      else {
        digitalWrite(ledPin, LOW); 
      }
   }

  void pir()
  {
  if (x == 1){
    if (digitalRead(pirPin) == HIGH){
 Blynk.notify("??? ??? ???? ??");
 }
    }
  }

void sendUptime()
{
  float temperature = sensor.getCelsiusHundredths();
  Blynk.virtualWrite(5, (float)temperature/100);

  float humidity = sensor.getHumidityPercent();
  Blynk.virtualWrite(6, humidity); // virtual pin

  uint16_t lux = lightMeter.readLightLevel();
  Blynk.virtualWrite(4, lux);

  float bp =  bmp.readPressure();
  Blynk.virtualWrite(9,(float)bp/100); 

  float ba =  bmp.readAltitude();
  Blynk.virtualWrite(7, ba);

  float bt =  bmp.readTemperature();
  Blynk.virtualWrite(12, bt);

  float dst =  bmp.readSealevelPressure(520)/100;
  Blynk.virtualWrite(13, dst);

  float q = analogRead(A0);
  Blynk.virtualWrite(2, q);
}

void dozd()
{
  float val = digitalRead(pirPin);
  Blynk.virtualWrite(3, val);
}

void sendWifi() 
{
  Blynk.virtualWrite(15, map(WiFi.RSSI(), -105, -40, 0, 100) );
}

void loop()
{
  Blynk.run();
  timer.run();
}

#2

sorry. the problem solved by editing void setup line


#3

All this time in the Blynk forum and you are still using Blynk.begin() when all other reconnection topics refer to Blynk.config() and proper watchdog type timers, reconnection routines and so on :thinking:

Perhaps more time spent searching and reading all the reconnection topics :stuck_out_tongue_winking_eye:


#4

thanks gunner. I realized this now so I’ll use Blynk.config()


#5

This is fairly rough code… and not the only way to do it, but a good starting point.


#6

thanks gunner. the Blynk.config() is working perfectly. :heart_eyes:

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <SI7021.h>
#include <Wire.h>
#include <Adafruit_BMP085.h>

#include <BH1750.h>  
BH1750 lightMeter;

char auth[] = "fe5078fcb5f04f5384ac70cb9ab91697";                 //Put your blink token
char ssid[] = "MikroTik Tesla";                            //Put your SSID of your connection
char pass[] = "12345678";                            //Put your Password of your connection
char server[] = "10.5.50.18";  
int port = 8080;

int pirState;
int val;
int x;

SI7021 sensor;

SimpleTimer timer;

#define I2C_SCL 12      //D6
#define I2C_SDA 13     //D7
Adafruit_BMP085 bmp;

#define ledPin 15 
#define pirPin 14

float dst, bt, bp, ba, q;
char dstmp[20], btmp[20], bprs[20], balt[20];
bool bmp085_present = true;

void setup()
{
  WiFi.mode(WIFI_STA);
  Blynk.connectWiFi(ssid, pass);
  Blynk.config(auth, "10.5.50.18", 8080);
  Blynk.connect();
  Wire.begin(I2C_SDA, I2C_SCL);
  delay(10);
  pinMode(ledPin, OUTPUT);
  pinMode(pirPin, INPUT);
if (!bmp.begin()) {
  Serial.println("Could not find a valid BMP085 sensor, check wiring!");
  //while (1) {}
  }
  timer.setInterval(1000L, sendUptime);
  timer.setInterval(1000L, PIRval);
  timer.setInterval(1000L, pir);
  timer.setInterval(1000L, dozd);
  timer.setInterval(1000L, sendWifi);
  lightMeter.begin();
  //sensor.begin(SDA,SCL);
}

void sendUptime()
{
  float temperature = sensor.getCelsiusHundredths();
  Blynk.virtualWrite(5, (float)temperature/100);

  float humidity = sensor.getHumidityPercent();
  Blynk.virtualWrite(6, humidity); // virtual pin

  uint16_t lux = lightMeter.readLightLevel();
  Blynk.virtualWrite(4, lux);

  float bp =  bmp.readPressure();
  Blynk.virtualWrite(9,(float)bp/100); 

  float ba =  bmp.readAltitude();
  Blynk.virtualWrite(7, ba);

  float bt =  bmp.readTemperature();
  Blynk.virtualWrite(12, bt);

  float dst =  bmp.readSealevelPressure(520)/100;
  Blynk.virtualWrite(13, dst);

  float q = analogRead(A0);
  Blynk.virtualWrite(2, q);
}

BLYNK_CONNECTED() {
      Blynk.syncVirtual(V0);
  }

BLYNK_WRITE(V0){
 x = param.asInt();
 }

void PIRval()
{
val = digitalRead(pirPin);
    if (val == HIGH) {
      digitalWrite(ledPin, HIGH);  
      }
      else {
        digitalWrite(ledPin, LOW); 
      }
   }

  void pir()
  {
  if (x == 1){
    if (digitalRead(pirPin) == HIGH){
 Blynk.notify("ALARM");
 }
    }
  }

void dozd()
{
  float val = digitalRead(pirPin);
  Blynk.virtualWrite(3, val);
}

void sendWifi() 
{
  Blynk.virtualWrite(15, map(WiFi.RSSI(), -105, -40, 0, 100) );
}

void loop()
{
  Blynk.run();
  timer.run();
}

#7

@Gunner the problem is backed. can you tell me where is the problem ? thanks

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <SI7021.h>
#include <Wire.h>
#include <Adafruit_BMP085.h>

#include <BH1750.h>  
BH1750 lightMeter;

char auth[] = "fe5078fcb5f04f5384ac70cb9ab91697";                 //Put your blink token
char ssid[] = "MikroTik Tesla";                            //Put your SSID of your connection
char pass[] = "12345678";                            //Put your Password of your connection
char server[] = "10.5.50.18";
int port = 8080;

SI7021 sensor;

SimpleTimer timer;

#define I2C_SCL 12      //D6
#define I2C_SDA 13     //D7
Adafruit_BMP085 bmp;

#define ledPin 15 
#define pirPin 14

float dst, bt, bp, ba, q;
char dstmp[20], btmp[20], bprs[20], balt[20];
bool bmp085_present = true;

int pirState;
int val;
int x;

void setup()
{
  WiFi.begin(ssid, pass);
  Blynk.config(auth, "10.5.50.18", 8080);
  Blynk.connect();

  Wire.begin(I2C_SDA, I2C_SCL);
  lightMeter.begin();
  delay(10);
  pinMode(ledPin, OUTPUT);
  pinMode(pirPin, INPUT);
if (!bmp.begin()) {
  Serial.println("Could not find a valid BMP085 sensor, check wiring!");
  //while (1) {}
  WiFi.mode(WIFI_STA);
  }
  timer.setInterval(1000L, sendUptime);
  timer.setInterval(1000L, PIRval);
  timer.setInterval(1000L, pir);
  timer.setInterval(1000L, dozd);
  timer.setInterval(1000L, sendWifi);
}

void sendUptime(){
  float temperature = sensor.getCelsiusHundredths();
  Blynk.virtualWrite(5, (float)temperature/100);

  float humidity = sensor.getHumidityPercent();
  Blynk.virtualWrite(6, humidity); // virtual pin

  uint16_t lux = lightMeter.readLightLevel();
  Blynk.virtualWrite(4, lux);

  float bp =  bmp.readPressure();
  Blynk.virtualWrite(9,(float)bp/100); 

  float ba =  bmp.readAltitude();
  Blynk.virtualWrite(7, ba);

  float bt =  bmp.readTemperature();
  Blynk.virtualWrite(12, bt);

  float dst =  bmp.readSealevelPressure(520)/100;
  Blynk.virtualWrite(13, dst);

  float q = analogRead(A0);
  Blynk.virtualWrite(2, q);
}

BLYNK_WRITE(V0){
 x = param.asInt();
 }

void PIRval(){
val = digitalRead(pirPin);
    if (val == HIGH) {
      digitalWrite(ledPin, HIGH);  
      }
      else {
        digitalWrite(ledPin, LOW); 
      }
   }

void pir(){
  if (x == 1){
    if (digitalRead(pirPin) == HIGH){
 Blynk.notify("ALARM");
 }
    }
  }

void dozd(){
  float val = digitalRead(pirPin);
  Blynk.virtualWrite(3, val);
}

void sendWifi() {
  Blynk.virtualWrite(15, map(WiFi.RSSI(), -105, -40, 0, 100) );
}

void loop(){
  Blynk.run();
  timer.run();
}

#8

Not without taking more time than I want to… sorry. Whatever your issue is, it is resolvable with a bit of debugging on your part.

But I would start by staggering your timers… they are all trying to run at the exact same time… another common topic/issue that I am surprised you haven’t read about :thinking:


#9

yeah, the problem with other timer.setInterval except the timer: timer.setInterval(1000L, sendUptime);

do you know why deleting the other timers fixed the problem ?


#10

that avoid to flood .
:wink:


#11

Hi Alexis. and whats the solution for this problem ?


#12

set timers like that
timer.setInterval(1000L, sendUptime);
timer.setInterval(1100L, PIRval);
timer.setInterval(1200L, pir);
timer.setInterval(1300L, dozd);
timer.setInterval(1400L, sendWifi);


#13

thanks. I tried but the problem is still there :disappointed_relieved:


#14

so, it’s another issue
I will try your code


#15

Try timing your sendUpTime… maybe it takes longer than 400ms to finish so that could be the problem…
Put unsigned long currentMillis = millis(); at the beginning of the function and another unsigned long currentMillis2 = millis(); at the end and print them out and you’ll now if the problem is there…


#16

put something like this:


void StartOfTestingFunction()
{
unsigned long temp;
temp = millis();
// your function is here
functionDuration = millis() - temp;
 // print with you convenient way the functionDuration to serial or to terminal widget etc
|


#18

you have to install NoxPlayer, it works fine with blynk.


#19

@ErfanDL

please try that first

timer.setInterval(1000L, sendUptime);
//timer.setInterval(1100L, PIRval);
//timer.setInterval(1200L, pir);
//timer.setInterval(1300L, dozd);
//timer.setInterval(1400L, sendWifi);

and remove slash one by one
I think you have a sensor that needs more time …
I have had the same issue with DHT11 sensor when I tried my first blynk project :wink:


#20

…or, you could use setTimeout -> check this post: [SOLVED] Timer.setinterval - best use?
If you set it this way you will not need to take in consideration how long does a function run so there will be no collisions…
You will need to change your functions a little bit though…


#21

Thanks. I will try and report

My sensors is SI7021, BMP180, BH1750, MQ135 GAS at analog pin and a PIR sensor.