ESP32 stuck on connecting to Wifi

I want to build my own weather station based around the ESP32, the BME280 and some Batteries. To save power, the ESP is in deepsleep for 7 minutes, wakes up to send data to the blynk cloud and goes back to deepsleep again. All works fine, but sometimes the ESP gets stuck on connecting to the Wifi.
It just says
[38] Connecting to xxx
and nothing more happens. The ESP is powered on and drains the battery, but does not do anything.
Is there a way to automatically reset the ESP after a given time if it is not connected to the Wifi so that it goes back to normal operation?


#define BLYNK_PRINT Serial
#define uS_TO_S_FACTOR 1000000
#define TIME_TO_SLEEP  420 


#include <BlynkSimpleEsp32.h>
#include <BME280I2C.h>
#include <Wire.h>
#include <SimpleTimer.h>

BME280I2C bme;

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

int measurement = 0;

#ifdef __cplusplus
extern "C" {
#endif
uint8_t temprature_sens_read();
#ifdef __cplusplus
}
#endif
uint8_t temprature_sens_read();

BlynkTimer timer;

void sendSensor()
{
  float temp(NAN), hum(NAN), pres(NAN);
  
  measurement = hallRead();
  BME280::TempUnit tempUnit(BME280::TempUnit_Celsius);
  BME280::PresUnit presUnit(BME280::PresUnit_Pa);
  bme.read(pres, temp, hum, tempUnit, presUnit);
  float voltage = (float)analogRead(36) / 4096 * 5 * 1941 / 2000;

  Blynk.virtualWrite(V1, temp);
  Blynk.virtualWrite(V2, temp);
  Blynk.virtualWrite(V3, hum);
  Blynk.virtualWrite(V4, hum);
  Blynk.virtualWrite(V5, pres / 100);
  Blynk.virtualWrite(V6, pres / 100);
  Blynk.virtualWrite(V7, measurement);
  Blynk.virtualWrite(V8, measurement);
  Blynk.virtualWrite(V9, (temprature_sens_read() - 32) / 1.8);
  Blynk.virtualWrite(V10, (temprature_sens_read() - 32) / 1.8);
  Blynk.virtualWrite(V11, voltage);
  Blynk.virtualWrite(V12, voltage);

 Serial.println("Going into deep sleep");
 delay(1000);
 esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
 esp_deep_sleep_start();
 delay(100);
}

void setup()
{
  // Debug console
  Serial.begin(9600);
  while(!Serial) {}
  Wire.begin();
  while(!bme.begin())
  {
    Serial.println("Could not find BME280 sensor!");
    delay(1000);
  }
  switch(bme.chipModel())
  {
     case BME280::ChipModel_BME280:
       Serial.println("Found BME280 sensor! Success.");
       break;
     case BME280::ChipModel_BMP280:
       Serial.println("Found BMP280 sensor! No Humidity available.");
       break;
     default:
       Serial.println("Found UNKNOWN sensor! Error!");
  }

  Blynk.begin(auth, ssid, pass);

  timer.setInterval(1000L, sendSensor);
}

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

Yes, you will need to handle the WiFi connection yourself by using Blynk.config(auth) instead of Blynk.begin()

http://docs.blynk.cc/#blynk-firmware-configuration-blynkconfig

You’ve written your code in the correct style for Blynk - an un-cluttered void loop and a timer to call your functions. However, this isn’t the most efficient way for deep sleep.

There will be a 1 second delay before your sendSensor code is called, which is unnecessary.
Forget the timer and put all of your sendSensor code in the void loop. It will execute immediately and go to sleep attge end of the first cycle through the void loop.

Also, if you haven’t already then you’d be better assigning a static IP address to your ESP, preferably via your router. This way the connection to your router will be quicker.
If you don’t do this, and rely on DHCP instead, the router’s routing table will have flushed in the time between ESP connections and the ESP will have to negotiate a new routing arrangement with your router. This takes time, and slows down the connection process.

You may also find that rather than connecting to Blynk using Blynk.begin or Blynk.config you’d be better-off doing a series of API calls to the Blynk server to upload the data. This approach has been discussed before In an old topic called something like “why doesn’t my battery last very long”.

Oh, just found the topic I was thinking of. It’s this one:

I can’t be bothered to read the whole topic, and the bit about API calls might be in one of the linked topics, but it’s worth you having a read.

Pete.

1 Like

Thanks. Assigning a static IP helped, but did not fix the Problem. If try using Blynk.config, the same issue occurs.

Is there a way to set a timeout to reset the esp after a given time?
e.g. Try to connect for 15 seconds, if connected run the code, if not use ESP.restart(); ?

i tried this, but it just results in a bootloop :

void loop()
{
  bool connect = Blynk.connect(15000);
  if (connect != true){
    Blynk.run();
  }else{
    ESP.restart();
  }
  timer.run();
}

Have you tried assigning a fixed IP for your device in your router?

Pete.

Yes I did. The problem is still present.

I Googled this in reference to ESP32… found issues and possible alternative…

Try this instead.

void hard_restart() {
  esp_task_wdt_init(1,true);
  esp_task_wdt_add(NULL);
  while(true);
}

It sets the wdt to 1 second, adds the current process and then starts an infinite loop.

1 Like

I modified my code a little bit. Now its running stable for more than a day.
The modifications I made:

  1. instead of blynk.begin I now use Blynk.config
  2. I added uint64_t in the esp_sleep_enable_timer_wakeup command
  3. I now use ESP32 version 1.0.0 instead of 1.0.1 in the Arduino IDE

If you are interested, here is my complete code:

#define BLYNK_PRINT Serial
#define uS_TO_S_FACTOR 1000000
#define TIME_TO_SLEEP  600        //set deep sleep time (in seconds)

#include <BlynkSimpleEsp32.h>
#include <BME280I2C.h>
#include <Wire.h>
#include <SimpleTimer.h>

BME280I2C bme;

char auth[] = "xxx";                          //define auth code
char ssid[] = "xxx";                          // define Network name
char pass[] = "xxx";                        // define wifi password
char server[] = "blynk-cloud.com";                //define blynk server (leave as is)
unsigned int port = 8442;                         // define port (leave as is)

int measurement = 0;

#ifdef __cplusplus
extern "C" {
#endif
uint8_t temprature_sens_read();
#ifdef __cplusplus
}
#endif
uint8_t temprature_sens_read();

BlynkTimer timer;

void sendSensor()
{
  float temp(NAN), hum(NAN), pres(NAN);
  
  measurement = hallRead();
  BME280::TempUnit tempUnit(BME280::TempUnit_Celsius);                // read BME 280
  BME280::PresUnit presUnit(BME280::PresUnit_Pa);                     // read BME 280
  bme.read(pres, temp, hum, tempUnit, presUnit);                      // read BME 280
  float voltage = (float)analogRead(36) / 4096 * 5 * 1941 / 2000;     // read voltage divider / battery voltage
  double gamma = log(hum / 100) + ((17.62 * temp) / (243.5 + temp));  // calculate dewpoint
  double dp = 243 * gamma / (17.62 - gamma);                          // calculate dewpoint
  long rssi = WiFi.RSSI();                                            // read signal strength

  Blynk.virtualWrite(V1, temp);                                 //temperature
  Blynk.virtualWrite(V2, temp);                                 //temperature
  Blynk.virtualWrite(V3, hum);                                  //humidity
  Blynk.virtualWrite(V4, hum);                                  //humidity
  Blynk.virtualWrite(V5, pres / 100);                           //pressure
  Blynk.virtualWrite(V6, pres / 100);                           //pressure
  Blynk.virtualWrite(V7, measurement);                          //hallsensor
  Blynk.virtualWrite(V8, measurement);                          //hallsensor
  Blynk.virtualWrite(V9, (temprature_sens_read() - 32) / 1.8);  //CPU Temp
  Blynk.virtualWrite(V10, (temprature_sens_read() - 32) / 1.8); //CPU Temp
  Blynk.virtualWrite(V11, voltage + 0.28);                      //Battery Voltage
  Blynk.virtualWrite(V12, voltage + 0.28);                      //Battery Voltage
  Blynk.virtualWrite(V13, dp);                                  //Dew point
  Blynk.virtualWrite(V14, dp);                                  //Dew point
  Blynk.virtualWrite(V15, rssi);                                //rssi
  Blynk.virtualWrite(V16, rssi);                                //rssi

 Serial.println("Going into deep sleep");                                   
 delay(500);
 esp_sleep_enable_timer_wakeup((uint64_t)TIME_TO_SLEEP * uS_TO_S_FACTOR);   //set deepsleep time
 esp_deep_sleep_start();                                                    //deepsleep start
 delay(100);
}

void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.connectWiFi(ssid, pass);        //conect to wifi
  Blynk.config(auth, server, port);
  Blynk.connect();                      //connect to blynk server
  
  while(!Serial) {}
  Wire.begin();
  while(!bme.begin())                   //start BME 280
  {
    Serial.println("Could not find BME280 sensor!");
    delay(1000);
  }
  switch(bme.chipModel())
  {
     case BME280::ChipModel_BME280:
       Serial.println("Found BME280 sensor! Success.");
       break;
     case BME280::ChipModel_BMP280:
       Serial.println("Found BMP280 sensor! No Humidity available.");
       break;
     default:
       Serial.println("Found UNKNOWN sensor! Error!");
  }

  timer.setInterval(500L, sendSensor);        //set timer to execute void sendSensor()
}

void loop()
{
  Blynk.run();      //start the Blynk sketch
  timer.run();      //start the timer
}

I calculated an estimate battery runtime of approximately 66 days, with a 4400 mAh LiIon 18650 battery.

4 Likes