BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

LOLIN32 Deep Sleep mode - Not posting

I am acquiring temperature and posting to Blynk using LoLin32 board + Arduino code. Simple code but it does not seem to post as the Superchart does not update… Blynk is configured for Push Type. The builtIn LED lights up every 15 secs and so I know the code is firing after deep sleep. But why is it not posting the value ?

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

#define ADC_Ch3 39

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


int adcRawBits = 0;
int adcMeanBits = 0;
int raSampleSize = 50;
double DegC ;
// Use Virtual pin 0 for temperature value in Blynk App
#define ambientTemp V0


byte flashLED  = 5;
bool ledState;

RunningAverage ra_Ch3(raSampleSize);

char auth[] = "eb8ade88cefa468f9f6974600e9a0dfa";
char ssid[] = "RR_DLINK_2.4";
char pass[] = "@act2.4ragu1957";

//#################################

void setup()
{
  Serial.begin(9600);
  delay(1000);
  Serial.println("ESP32 LM35 Read and Post to Blynk.");

  Blynk.begin(auth, ssid, pass);

  pinMode( flashLED, OUTPUT);
  digitalWrite(flashLED, LOW);

  Blynk.run();

  readAdcPin();                   // Read the ADC pin and average it ...
  getDegCValue();
  Blynk.virtualWrite(ambientTemp, DegC);

  digitalWrite(flashLED, HIGH); 
  esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
  esp_deep_sleep_start();
}

//#################################

void loop()
{
  // Nothing here !
}

//#################################

void readAdcPin()
{
  for (byte count = 0; count < 100; count++)
  {
    adcRawBits = analogRead(ADC_Ch3);
    ra_Ch3.addValue(adcRawBits);
    adcMeanBits = ra_Ch3.getAverage();
    delay(5);
  }
}

//*********************************

void getDegCValue()
{
  DegC = getVoltage(adcMeanBits) * 100;
}

//*********************************

double getVoltage(int count)
{
  double reading = count;          // Reference voltage is 3v3 so maximum reading is 3v3 = 4095 in range 0 to 4095
  if (reading < 1 || reading > 4095) return 0;
  return -0.000000000000016 * pow(reading, 4) + 0.000000000118171 * pow(reading, 3) - 0.000000301211691 * pow(reading, 2) + 0.001109019271794 * reading + 0.034143524634089;
}

This won’t be processed until the code encounters the next Blynk.run(); command, but as there isn’t one before the deep sleep command it never happens.
I’d suggest adding a Blynk.run(); and a short delay before your sleep command.

Also, because you’re using Blynk.begin, which is a blocking command, if the device fails to connect to either Wi-Fi or the Blynk server it will never go back to sleep again.

Pete.

Thanks PeteKnight.

I did as you mentioned and with a delay of 500ms before going to sleep. OK its working.

But I am not clear about how to overcome the blocking Blynk.run()… what is the standard method to overcome that ?

It’s Blynk.begin that’s the blocking function.
Using Blynk.config and Blynk.connect, after establishing a WiFi connection is the best approach.

Take a look at this code:

Pete.

1 Like

Thanks Pete.

All well now… its been running since yesterday night and working well … just to check if it tries to recover switched off the router for some time. It recovered nicely .

:star_struck:

1 Like

Just one point to clarify …Refer the code below …

  if (Blynk.connected())                          // If we manages to connect to Blynk then carry-on as normal, else go to sleep
  {
    Serial.println ("Connected to Blynk");
  }
  else
  {
    sleep_time_minutes = sleep_time_minutes * 0.5; // If you enable this line of code the it will make the device go to sleep for twice as long before trying again. Changing to 0.5 would make it try again sooner than normal
    Serial.println("Blynk connection failed - going to sleep");
    deepSleepNow();
  }

And deepSleepNow() function …

void deepSleepNow() {
  digitalWrite(flashLED, HIGH);
  esp_sleep_enable_timer_wakeup((uint64_t)(TIME_TO_SLEEP) * uS_TO_S_FACTOR);
  esp_deep_sleep_start();
}

But I don’t see where the variable “sleep_time_minutes” used in the function deepSleepNow(). Is it part of the Blynk API ?

You’re right!
This was a bit of a collaboration project via email and it looks like some of my code didn’t make it into the final version.
My original version of deepSleepNow() looked like this…

void Deep_Sleep_Now()
{
  Serial.print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Going to sleep for ");
  Serial.print(sleep_time_minutes);  
  Serial.println(" minutes");   
  Serial.println();
  ESP.deepSleep(sleep_time_minutes * 60000000);  // Deep Sleep time is specified in micros
  delay(2000);
}

and of course sleep_time_minutes = sleep_time_minutes * 2; is needed to make it sleep for twice as long if no connection, sleep_time_minutes = sleep_time_minutes * 0.5; to make it re-try again sooner than normal.

This was intended to give some flexibility based on whether your readings were more important than saving battery life. If you think the scenario is that that Wi-Fi or internet connection being down is something that will fix itself quite quickly then repeating after 0.5x the normal sleep time is probably a good idea. If you think manual intervention is required to get the fault fixed and you just want to save battery life in the meantime then 2x (or more) is probably a better setting.

Pete.

Excellent idea.

Thanks for the clarifications. And I am a bit curious about the "delay(2000) " after the ESP.deepSleep() … does it get executed at all ?

It’s recommended to have a delay after deepSleep. Without it, it is possible that code execution continues and the device doesn’t actually sleep.

Pete.

:+1:

1 Like