if my ESP8266 is not conneced / lost connection to WiFi,
i want to put it into deep sleep for x time.
Here my basic connection check, this works perfect.
void setup() {
Serial.begin(115200);
// line below needs to be BEFORE Blynk.connect()
// check if still connected every 11s
timer.setInterval(11000, CheckConnection);
Blynk.config(auth, Bserver, port);
Blynk.connect();
}
void CheckConnection(){ // check every 11s if connected to Blynk server
if(!Blynk.connected()){
Serial.println("Not connected to Blynk server");
Blynk.config(auth, Bserver, port);
Blynk.connect(); // try to connect to server with default timeout
}
else{
Serial.println("Connected to Blynk server");
Blynk.syncAll();
rtc.begin();
}
}
void loop()
{
if(Blynk.connected())
{
Blynk.run();
}
timer.run();
}
I tried it this way but it didnt work because after few secs the ESP goes sleep.
void CheckConnection(){ // check every 11s if connected to Blynk server
if(!Blynk.connected()){
Serial.println("Not connected to Blynk server");
noConnect++;
Blynk.config(auth, Bserver, port);
Blynk.connect(); // try to connect to server with default timeout
if noConnect = 5
{
noConnect = 0;
delay(100);
ESP.deepSleep(900000000); // 15 Min sleep
}
else{
noConnect = 0;
Serial.println("Connected to Blynk server");
Blynk.syncAll();
rtc.begin();
}
}
}
sorry, you are right, just forgot to type here in the post.
Here the whole code for my example.
afer x connection the code should do a action⌠sleep, blink⌠what ever.
the problem is, counter ânoConnectionâ counts just till 1 than the action startsâŚ
int noConnect = 0;
void setup() {
Serial.begin(115200);
// line below needs to be BEFORE Blynk.connect()
// check if still connected every 11s
timer.setInterval(11000, CheckConnection);
Blynk.config(auth, Bserver, port);
Blynk.connect();
}
void CheckConnection(){ // check every 11s if connected to Blynk server
if(!Blynk.connected()){
Serial.println("Not connected to Blynk server");
noConnect++;
Serial.println(noConnect);
Blynk.config(auth, Bserver, port);
Blynk.connect(); // try to connect to server with default timeout
if (noConnect == 5)
{
noConnect = 0;
delay(100);
//do sometning if noConnect Counter = 5
//f.e. ESP.deepSleep(900000000); // 15 Min sleep
}
}
else{
Serial.println("Connected to Blynk server");
Blynk.syncAll();
rtc.begin();
}
}
void loop()
{
if(Blynk.connected())
{
Blynk.run();
}
timer.run();
/***
* do something
**/
}
The problem is that with the deepsleep command commented-out, you canât really tell if that branch of the if statement is being evaluated as true.
When you do enable the deepsleep command, it should be followed by a short delay (say 100ms) to give it time to âkick inâ, otherwise the code that follows can be executed and the deepsleep command ignored.
what happens on the spot, plays no role in my problem.
The point is that the meter does not count correctly, but directly triggers the if function, even if ânoConnect = 5â has not yet been reached.
Aside from the lack of direct Blynk issue in this here Blynk forum topic⌠Perhaps your actual, âThis causes the issueâ code would be more helpful then us troubleshooting your âJust an exampleâ code
I looked at the code, but how do you know itâs stuck there? You can perhaps include some Serial.print and print the variable status.
As Gunner said you need to share a more appropriate full code would increase your chances of getting suggestions.
What youâre trying to achieve is fairly simple and definitely has been done before, a lot of times what is preventing your code to work would be something simple. By copying and pasting here a âjust an exampleâ code you may miss the fundamental part of the problem