Blynk app does not update values when using with deepsleep

Hello. In my application, I need to connect to the blynk server, send data once to the blynk app and then go to sleep. After 10 seconds, wake up, second data and go to sleep again.

I have implemented the code but the only issue is that the blynk app simply does not update, I think it has to do something with the way Blynk.run() is being called. Normally the Blynk.run() must be called in the void.loop(). But since I am using esp32 Deep sleep, void loop is never executed hence I must called blynk.run() elsewhere.

My code:

#define BLYNK_PRINT Serial

#include <BlynkSimpleEsp32.h>

#include "adc.h"

#include "deepsleep.h"

#include "wifi_wrapper.h"

char auth[] = "LdYGyhQrFV28vBAl-CxNZi_-vO_Q2IAP";

BlynkTimer timer;

uint16_t adc1_value=0;

uint16_t adc2_value=0;

uint16_t adc3_value=0;

double current1=0;

double current2=0;

double current3=0;

RTC_DATA_ATTR int bootCount = 0;

void setup()

{

  //timer.setInterval(1000L, myTimerEvent);

  Serial.begin(115200);

  setupWifi();

  Blynk.config(auth);

  

  delay(1000); //Take some time to open up the Serial Monitor

  ++bootCount;

  Serial.println("Boot number: " + String(bootCount));

  print_wakeup_reason();

  Serial.println("sending data");

  adc1_value = ADC_read(pin_adc1);

  adc2_value = ADC_read(pin_adc2);

  adc3_value = ADC_read(pin_adc3);

  current1 = double(adc1_value / 1241.0f) *30.0f;

  current2 = double(adc2_value / 1241.0f) *30.0f;

  current3 = double(adc3_value / 1241.0f) *30.0f;

  Serial.println(current1);

  Blynk.virtualWrite(V4, current1); //sending to Blynk

  Blynk.virtualWrite(V5, current2); //sending to Blynk

  Blynk.virtualWrite(V6, current3); //sending to Blynk

  Blynk.run();

  WiFi.disconnect();

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

  deepSleep();

}

void loop()

{


}

rst:0x5 (DEEPSLEEP_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:1044
load:0x40078000,len:10124
load:0x40080400,len:5828
entry 0x400806a8
.Connected :192.168.43.135
[1077]
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v1.0.0 on ESP32

Boot number: 3
Wakeup caused by timer
sending data
98.99
[2083] Connecting to blynk-cloud.com:80
Setup ESP32 to sleep for every 10 Seconds
Going to sleep now
ets Jun  8 2016 00:22:57

rst:0x5 (DEEPSLEEP_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:1044
load:0x40078000,len:10124
load:0x40080400,len:5828
entry 0x400806a8
..Connected :192.168.43.135
[2077]
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v1.0.0 on ESP32

Boot number: 4
Wakeup caused by timer
sending data
98.99
[3083] Connecting to blynk-cloud.com:80
Setup ESP32 to sleep for every 10 Seconds
Going to sleep now

As you can see from the serial monitor, I am printing a value before sending to blynk and the value printed is 98,99, but on my blink app it just says 0.0000000. I have tested without the deep sleep just a normal blynk example from the arduino library example and it works fine.

Looking at your serial monitor output, it looks like it is attempting to send the data BEFORE being connected to Blynk, you might want to add a statement to make sure you are connected to BLYNK before sending the data, then go back to sleep.

It hardly seems worth it. I’d guess that your wake-up process takes roughly that amount of time, so you’re only getting a 1:1 wake sleep ratio.

The documentation for Blynk.config says…

NOTE: After Blynk.config(...) is called, your hardware is not yet connected to the server. It will try to connect while until it hits first instance of Blynk.run() or Blynk.connect() routine.
To skip connecting to the server or to disconnect manually, call Blynk.disconnect() after configuration.

As you don’t have a Blynk.connect() command, the connection only begins to be initialised when your one and only Blynk.run() is processed, which is after your Blynk.virtualWrites are called, so they don’t do anything.

Pete.

Hello guys. Sorry for the delayed response. I have did some further testing and there are the results:

  1. I am confused how to properly Initialise the blynk as the following does not work:
void setup()
{
  Serial.begin(115200);
  setupWifi();
  BlynkDelay(1000);
  Blynk.config(auth);
  Blynk.run();
...
...

...
...

As you can see, I have written a custom function setupWifi which simply connects to the wifi:

void setupWifi() {

  WiFi.mode(WIFI_STA);

  WiFi.begin(ssid, pass);

  uint8_t retry = 0;

  while (WiFi.status() != WL_CONNECTED) {

    Serial.print(".");

    delay(1000);

    if ((++retry % 16) == 0) {

      Serial.println("wifi connection failed");

      

      esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);

      deepSleep();

    }

  }

  Serial.print("Connected :");

  Serial.println(WiFi.localIP());

}

After the setup wifi, I call the blynk.config. If I understand correctly, when Blynk.begin(auth, ssid, pass); is called, it is doing exactly these 2 things : seting up wifi and calling blynk.config.
However, the blynk app is not updating at all when doing it this way.

When I replace the blynk setup with the following:

void setup()

{

  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  Blynk.run();
...
...
...

The values in my blynk app start changing, however it is extremely unreliable .
I have simplified the code even more:


#include <BlynkSimpleEsp32.h>

#include "adc.h"

#include "deepsleep.h"

#include "wifi_wrapper.h"

char auth[] = "LdYGyhQrFV28vBAl-CxNZi_-vO_Q2IAP";

extern char ssid[];

extern char pass[];

BlynkTimer timer;

RTC_DATA_ATTR int bootCount = 0;

void setup()

{

  Serial.begin(115200);

  Blynk.begin(auth, ssid, pass);

  Blynk.run();

  

  delay(1000); //Take some time to open up the Serial Monitor

  ++bootCount;

  Serial.println("Boot number: " + String(bootCount));

  print_wakeup_reason();

  Blynk.virtualWrite(V4, bootCount); //sending to Blynk

  Blynk.virtualWrite(V5, bootCount); //sending to Blynk

  Blynk.virtualWrite(V6, bootCount); //sending to Blynk

  Blynk.run();

  WiFi.disconnect();

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

  deepSleep();

}

void loop()

{

}

What happens now, the first value (V4) seems to updating almost every deep cycle ( but not everytime!) whereas the other 2 will only get updated at random times. V5 gets updated once every 3-4 deep cycle resets and V6 never gets updated - really weird. Any ideas how to debug this?

Hello. I have simplified the blynk program quite a bit:

#include <BlynkSimpleEsp32.h>

#include "adc.h"

#include "deepsleep.h"

#include "wifi_wrapper.h"

char auth[] = "LdYGyhQrFV28vBAl-CxNZi_-vO_Q2IAP";

extern char ssid[];

extern char pass[];

BlynkTimer timer;

RTC_DATA_ATTR int bootCount = 0;

void setup()

{

  Serial.begin(115200);

  Blynk.begin(auth, ssid, pass);

  Blynk.run();

  

  delay(1000); //Take some time to open up the Serial Monitor

  ++bootCount;

  Serial.println("Boot number: " + String(bootCount));

  print_wakeup_reason();

  Blynk.virtualWrite(V4, bootCount); //sending to Blynk

  Blynk.virtualWrite(V5, bootCount); //sending to Blynk

  Blynk.virtualWrite(V6, bootCount); //sending to Blynk

  Blynk.run();

  WiFi.disconnect();

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

  deepSleep();

}

void loop()

{

}

As I mentioned, it is working extremely unreliable.
Check the serial monitor:


rst:0x5 (DEEPSLEEP_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:1044
load:0x40078000,len:10124
load:0x40080400,len:5828
entry 0x400806a8
[30] Connecting to Evse_smart_current
[576] Connected to WiFi
[576] IP: 192.168.43.135
[576]
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v1.0.0 on ESP32

[582] Connecting to blynk-cloud.com:80
[1141] Ready (ping: 405ms).
Boot number: 2
Wakeup caused by timer
sending data
data sent
Setup ESP32 to sleep for every 10 Seconds
Going to sleep now
ets Jun  8 2016 00:22:57

rst:0x5 (DEEPSLEEP_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:1044
load:0x40078000,len:10124
load:0x40080400,len:5828
entry 0x400806a8
[31] Connecting to Evse_smart_current
[577] Connected to WiFi
[577] IP: 192.168.43.135
[577]
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v1.0.0 on ESP32

[583] Connecting to blynk-cloud.com:80
[818] Ready (ping: 186ms).
Boot number: 3
Wakeup caused by timer
sending data
data sent
Setup ESP32 to sleep for every 10 Seconds
Going to sleep now

Any other ideas what can be done to further debug/test it

UPDATE:

I have managed to get it to work somewhat reliably by adding a delay statement after virtualWrite before wifi.disconnect. I was under impression that the virtualwrite function is an asynchronous meaning that once it is called, it will not finish the function untill the data is transferred. However, somehow if If I enter the deep sleep too fast , the data transfer to the blynk app does not finish and cut off midway. the code:

#define BLYNK_PRINT Serial

#include <BlynkSimpleEsp32.h>

#include "adc.h"

#include "deepsleep.h"

#include "wifi_wrapper.h"

char auth[] = "LdYGyhQrFV28vBAl-CxNZi_-vO_Q2IAP";

extern char ssid[];

extern char pass[];

BlynkTimer timer;

RTC_DATA_ATTR int bootCount = 0;

void setup()

{

  Serial.begin(115200);

  Blynk.begin(auth, ssid, pass);

  while (Blynk.connect() == false) {

// Wait until connected

}

  Blynk.run();

  ++bootCount;

  Serial.println("Boot number: " + String(bootCount));

  print_wakeup_reason();

  Serial.println("sending data");

  Blynk.virtualWrite(V4, bootCount); //sending to Blynk

  Blynk.virtualWrite(V5, bootCount); //sending to Blynk

  Blynk.virtualWrite(V6, bootCount); //sending to Blynk

  delay(1000);

  Serial.println("data sent");

  WiFi.disconnect();

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

  deepSleep();

}

Using Blynk.begin in a deep sleep scenario is not a good idea.
Blynk.begin is a blocking function and it stops all further code execution it can’t establish a WiFi connection, or a connection to the Blynk server.
This will mean that your device will never enter deep sleep mode in either of these situations, and if this is a battery powered device then it will stay awake until the battery dies.

I’m still doubtful about the usefulness of a battery powered device that has such a poor sleep/wake ratio, but you don’t seem to want to explain the logic for that.

I think the problem you’re seeing is caused by the fact that there is a small delay between Blynk.connect completing, and the device being ready to perform Blynk.virtualWrite commands. Also, I believe that each Blynk.virtualWrite needs to be followed by a Blynk.run to force the operation to complete.

Using BLYNK_CONNECTED, and putting your code that reads your sensors and writes the results to Blynk is probably the best method.

I’s suggest that your sketch should do the following…

  • Initialise variables etc
  • Connect to WiFi. If successful then
  • Call Blynk.config and Blynk.connect

In BLYNK_CONNECTED do the following:

  • Take your sensor readings
  • Write to V4
  • Blynk.run
  • Write to V5
  • Blynk.run
  • Write to V6
  • Blynk.run
  • deep sleep

Once this is working then you could look at a timer that is initialised at startup and which caused deep sleep to be commenced if BLYNK_CONNECTED never executes.

Pete.

Hello. In the production, the device will only measure once per hour or something like that. I obviously dont want to wait 1 hour to wait for the next reading hence I have set it to 10 seconds for testing.

I think you are totally right about Blynk.begin().

I have updated my void.setup() with the following;


void setup()

{

  uint16_t adc1_value, adc2_value, adc3_value;

  double current1,current2,current3;

  Serial.begin(115200);

  setupWifi();

  Blynk.config(auth);

  while (Blynk.connect() == false) {

// Wait until connected

  }

  Blynk.run();

  ++bootCount;

  Serial.println("Boot number: " + String(bootCount));

  print_wakeup_reason();

  Serial.println("sending data");

  adc1_value = ADC_read(pin_adc1);

  adc2_value = ADC_read(pin_adc2);

  adc3_value = ADC_read(pin_adc3);

  current1 = double(adc1_value / 1241.0f) *30.0f;

  current2 = double(adc2_value / 1241.0f) *30.0f;

  current3 = double(adc3_value / 1241.0f) *30.0f;

  Blynk.virtualWrite(V4, bootCount); //sending to Blynk

  Blynk.run();

  Blynk.virtualWrite(V5, bootCount); //sending to Blynk

  Blynk.run();

  Blynk.virtualWrite(V6, bootCount); //sending to Blynk

  Blynk.run();

  delay(1000);

  Serial.println("data sent");

  WiFi.disconnect();

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

  deepSleep();

}

Seems to work decently now.

I’m just a tinkerer so I am no expert @ zazas321 but I got caught on an ESP32 trying to use pins on ADC2 for “analogRead()” while “WiFi” is up. You’ll either drop the WiFi connection or you won’t get a reading as ADC2 is used for WiFi.

I swapped my pins to ADC1 and everything burst into life
Here’s an extract from the API on it


Source: Analog to Digital Converter - ESP32 - — ESP-IDF Programming Guide latest documentation

Cheers