ESP32 DeepSleep problem + BME280

Hello! I am newbie. I am trying to make a weather station, powered by solar panel and battery. I was successful at running it without deepsleep. Now I want to implement deepsleep, but it just doesn’t work. My code worked before trying the deepsleep function. What am I doing wrong?

ESP32 S2 mini, BME280


#define BLYNK_TEMPLATE_ID "******"
#define BLYNK_DEVICE_NAME "********"
#define BLYNK_AUTH_TOKEN "******************"
#define BLYNK_PRINT Serial

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

#define I2C_SDA 35
#define I2C_SCL 36

TwoWire I2CBME = TwoWire(0);
Adafruit_BME280 bme;

char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "*******************";
char pass[] = "**********************";

#define uS_TO_S_FACTOR 1000000ULL  /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP  60        /* Time ESP32 will go to sleep (in seconds) */

BlynkTimer timer;

void setup() {
  Serial.begin(115200);
  I2CBME.begin(I2C_SDA, I2C_SCL, 100000);
  Blynk.begin(auth, ssid, pass);
  delay(2000);
  timer.run();
  esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);

  float humidity = bme.readHumidity();
  float pressure = bme.readPressure() / 100.0F;
  float temperature = bme.readTemperature();

  Blynk.run();
  Blynk.virtualWrite(V0, temperature );
  Blynk.run();
  Blynk.virtualWrite(V1, humidity);
  Blynk.run();
  Blynk.virtualWrite(V2, pressure);
  Blynk.run();
  delay(2000);
  esp_deep_sleep_start();
  delay(2000);

}

void loop(){

}

What exactly does that mean?
Is the device connecting to Blynk?

Adding some serial print statements will help you diagnose exactly what is happening.

Pete.

Hello Pete,

Most of the time it just connects and disconnects to my PC. I tried adding some serial print statements, but it doesn’t print anything.

Sometimes i get:

[3884] Connected to WiFi
[3884] IP: 192.168.0.106
[3886] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v1.1.0 on ESP32

 #StandWithUkraine    https://bit.ly/swua


[3887] Connecting to blynk.cloud:80 ```

But the ESP32 reboots after this.

Should be in the void loop.

You shouldn’t use delay in a Blynk sketch because it is a blocking function. The blocking function prevents the program from executing any additional actions until the current task has been completed.

Okay, I removed the delays and moved timer.run() and Blynk.run() in the void loop. In the documentation I read that void loop will not be run, so are you sure about that?

The ideal blynk void loop should look like this

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

and like this

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

When you use a blynk timer. Read the documentation carefully.

“The loop() function is empty because it never reaches this part of the program code. The ESP32 goes into deep sleep.”

This is from a tutorial from another website. Anyway… I am getting this in the serial:

[3852] Connecting to blynk.cloud:80

Here’s a very basic deep sleep sketch that you can try.

#define BLYNK_TEMPLATE_ID           "TMPLxxxxxx"
#define BLYNK_DEVICE_NAME           "Device"
#define BLYNK_AUTH_TOKEN            "YourAuthToken"


// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial


#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

#define uS_TO_S_FACTOR 1000000  /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP  60      /* Time ESP32 will go to sleep (in seconds) */


char auth[] = BLYNK_AUTH_TOKEN;

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "YourNetworkName";
char pass[] = "YourPassword";

BlynkTimer timer;

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

  esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
  Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP) +
  " Seconds");
 
  Blynk.begin(auth, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
  //Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
  
  timer.setInterval(60000L, deep_sleep);
}

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

void deep_sleep()
{
  Serial.println("Going to sleep now");
  esp_deep_sleep_start();
  
}

There are some potential problems when you use Blynk.begin with deepsleep.
Because Blynk.begin is a blocking function, if your device can’t connect to WiFi or the Blynk server, code processing will stop at Blynk.begin and your battery will be drained.

A better approach is to use Blunk.config and Blynk.connect.

Also, it doesn’t really matter where you put your code when you are using deepsleep, but this approach works well.
I don’t like using timers with deepsleep because you don’t want to loop until a timer kicks-in, you just want to run through one set of commands and then go to sleep, and you want this to happen as quickly as possible so that your wake time is as low as possible.
Take a look at this example…

Pete.

2 Likes

I tried using Blynk.config and Blynk.connect, but I think it just goes to sleep before it is connected to blynk and before it sends the data.

In that case, put your code in BLYNK_CONNECTED()

Pete.

I tried with 3 different ESPs and it is all the same. When I connect it to the PC i reboots every 3 seconds. This is the code:

#define BLYNK_TEMPLATE_ID "***"
#define BLYNK_DEVICE_NAME "ESP32 Test"
#define BLYNK_AUTH_TOKEN "***"
//#define BLYNK_PRINT Serial

char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "***";
char pass[] = "***";


#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

#define I2C_SDA 35
#define I2C_SCL 36

TwoWire I2CBME = TwoWire(0);
Adafruit_BME280 bme;

#define uS_TO_S_FACTOR 1000000ULL  /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP  60        /* Time ESP32 will go to sleep (in seconds) */

BlynkTimer timer;

void setup() {
  Serial.begin(115200);
  delay(500);
  Serial.println("Serial Start");
  I2CBME.begin(I2C_SDA, I2C_SCL, 100000);
  timer.run();
  Serial.println("Connect WIFI");
  Blynk.connectWiFi(ssid, pass);
  Serial.println("Blynk config");

  Blynk.config(auth);
  Serial.println("Blynk connecting");
  Blynk.connect();
  if (Blynk.connected())                          // If we manages to connect to Blynk then carry-on as normal, else go to sleep
  {
    Serial.println ("Connected to Blynk");
  float humidity = bme.readHumidity();
  float pressure = bme.readPressure() / 100.0F;
  float temperature = bme.readTemperature();

  Serial.println("Writing V0");
  Blynk.virtualWrite(V0, temperature );

  Serial.println("Writing V1");
  Blynk.virtualWrite(V1, humidity);

  Serial.println("Writing V2");
  Blynk.virtualWrite(V2, pressure);

  }
  goToSleep();

}

void goToSleep(){
  Serial.println("Going to sleep");
  esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);

  esp_deep_sleep_start();
}

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

And this is the serial output:

Blynk config
Blynk connecting
Connected to Blynk
Blynk config
Blynk connecting
Connected to Blynk
Blynk config
Blynk connecting
Connected to Blynk
Blynk config
Blynk connecting
Connected to Blynk

I’m not really surprised.
You’ve chopped and pasted chunks of various sketches into one badly structured sketch, and haven’t really taken my advice about the sketch structure.

Pete.

1 Like

Okay… I give up :sweat_smile: … I will not use deep sleep.

Why? It’s a perfectly sensible thing to do, you just need to structure your code differently from a normal Blynk sketch.

I’ve given you an example to follow, and suggested putting some code in the BLYNK_CONNECTED() function (although this requires care because if the sketch never connects to Blynk then you need a fallback).

In my opinion, timers shouldn’t be used in a deep sleep sketch, unless you are using them to trigger a deep sleep if no connection is made to Blynk.

Pete.