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" {
uint8_t temprature_sens_read();
#ifdef __cplusplus
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);, 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");
 esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);

void setup()
  // Debug console
  while(!Serial) {}
    Serial.println("Could not find BME280 sensor!");
     case BME280::ChipModel_BME280:
       Serial.println("Found BME280 sensor! Success.");
     case BME280::ChipModel_BMP280:
       Serial.println("Found BMP280 sensor! No Humidity available.");
       Serial.println("Found UNKNOWN sensor! Error!");

  Blynk.begin(auth, ssid, pass);

  timer.setInterval(1000L, sendSensor);

void loop()


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


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.



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){;


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



Yes I did. The problem is still present.