EPS8266 sleep when not connected / WiFi n/a

Hi all,

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(); 
  }
}
}

Maybe someone has a tip :slight_smile:

for comparator you need == (double equal sign). And we would need the full code in order to help you.

1 Like

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
**/

}

Which action?

look at code

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.

Pete.

Yes i know - it was just an example.

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 :stuck_out_tongue_winking_eye:

2 Likes

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

1 Like

so, I have found the solution myself. thanks for the great support!

#include <ESP8266WebServer.h>
#include <ESP8266WebServerSecure.h>

#include <ESP8266WiFi.h>
#include <ESP8266WiFiAP.h>
#include <ESP8266WiFiGeneric.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266WiFiScan.h>
#include <ESP8266WiFiSTA.h>
#include <ESP8266WiFiType.h>
#include <WiFiClient.h>
#include <WiFiClientSecure.h>
#include <WiFiServer.h>
#include <WiFiServerSecure.h>
#include <WiFiUdp.h>
#include <ESP8266WiFi.h>
#define BLYNK_TIMEOUT_MS  3000  // must be BEFORE BlynkSimpleEsp8266.h doesn't work !!!
#define BLYNK_HEARTBEAT   17   // must be BEFORE BlynkSimpleEsp8266.h works OK as 17s
#include <BlynkSimpleEsp8266.h>
#include <Blynk.h>

#define DEBUG_Serial Serial


BlynkTimer timer;
int noConnect = 0;
const int PIN_LED = 2;

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

char auth[] = "xxx";
char Bserver[] = "blynk-cloud.com";
unsigned int port = 8442;



void setup() {
 Serial.begin(115200);
  pinMode(PIN_LED, OUTPUT);
  digitalWrite(PIN_LED, HIGH);

 WiFi.forceSleepWake();
 WiFi.begin(ssid, pass); // Connect to the network

timer.setInterval(18000, CheckConnection); 
timer.setInterval(30000, online);
}



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{
    noConnect = 0;
    Serial.println("Connected to Blynk server");
    Blynk.syncAll();   
 }
}


void online()
{
if (WiFi.status()!=WL_CONNECTED)
 {
  noConnect++;
  Serial.println(noConnect);
 }
}


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


if (noConnect == 5)
  {
  noConnect = 0;
  Serial.println("SLEEP");
  digitalWrite(PIN_LED, LOW);
  delay(5000);
  ESP.deepSleep(300000000, WAKE_RF_DEFAULT); // 15 Min sleep
  delay(1000);
  }
else
{
  digitalWrite(PIN_LED, HIGH);
  }
}