Deepsleep is preventing Blynk from updating (weird)

Hi guys,

I’m creating a simple battery operated DHT22 sensor with a wemos d1, updating a few VPIN on Blynk.
As soon as I enable deepsleep then nothing is updating anymore on Blynk.
This code have been simplified as I also update EMONCMS (even on deepsleep) with no prob.
Jumper is on D0 and RST, i’m able to deepsleep some other code without going on Blynk no prob.
Maybe someone had same issue, can someone look at the code ? I must be missing something.

Any help is greatly appreciated, as always.



#include <ESP8266WiFi.h>

int SensorNumber = 21;

IPAddress arduino_ip ( 192,   168,   31,  221);
IPAddress dns_ip     (  8,   8,   8,   8);
IPAddress gateway_ip ( 192,  168,  31,   1);
IPAddress subnet_mask( 255, 255, 255,   0);

#include "DHT.h"
#define BLYNK_PRINT Serial                       
#include <SPI.h>
#include <BlynkSimpleEsp8266.h>
#define DHTPIN D4 

ADC_MODE(ADC_VCC);

#define DHTTYPE DHT22 

DHT dht(DHTPIN, DHTTYPE);
char auth[] = "hidden";
char ssid[] = "hidden";
char pass[] = "hidden";

void setup() {
  Serial.begin(9600);

  dht.begin();

  WiFi.config(arduino_ip, gateway_ip, subnet_mask);  //FIXED IP
  WiFi.begin(ssid, pass);
  Blynk.config(auth);
 }

void loop() {
  sendSensor();
  Blynk.run(); 
  ESP.deepSleep(1 * 60 * 1000000, WAKE_RF_DEFAULT);
}

void sendSensor() {

  float t = dht.readTemperature();
  float h = dht.readHumidity();
  int power = (ESP.getVcc());

  if (isnan(t)) {
    Serial.println("NAN returned");
    // cant read sensor
  }
  else {
    int VPIN1 = SensorNumber * 3;
    int VPIN2 = SensorNumber * 3 + 1;
    int VPIN3 = SensorNumber * 3 + 2;
    Serial.print(VPIN1);
    Serial.print(VPIN2);
    Serial.print(VPIN3);
    Blynk.virtualWrite(VPIN1, t);
    Blynk.virtualWrite(VPIN2, h);
    Serial.print("Humidity: ");
    Serial.print(h);
    Serial.print(" %\t");
    Serial.print("Temperature: ");
    Serial.print(t);
    Serial.print(" *C ");
    Serial.print("Batt Voltage: ");
    Serial.println(power);
    Blynk.virtualWrite(VPIN3, power);
  }
}

I don’t use deep sleep on my Arduinos (but personally, that is another story, ha, what is this awake thing people talk about? :wink: )… however two things that jump out to me…

First, since Blynk functionality requires constant communication to and from the server, then it makes sense that deepsleep would interrupt that. Making data transmission via Blynk only functional when the hardware awakens.

Secondly, even in that wakeful state your current code setup (sendSensor() in the main loop) would be constantly sending out data so fast that you should be getting flood errors when the hardware is awake (unless it is literally awake only a second or less, before sleeping again)

This is correct behavior for ESP.deepsleep()… as it kills WIFI connection.

@Jamin Thanks for the info.
I guess this is normal behaviour to cycle power off and obviously cut the wifi radio.
I should have a normal startup, get a DHT value, send that to blynk and then sleep.
So I should have a new value every X minutes, right ?

Regards,

@Gunner Thanks for following up. I understand that you do not use deepsleep not because its not working properly, probably because you are not working on battery operated projects. DeepSleep is not a option when working on battery. The code is really simple, it should sync 3 Vpin once per X minutes so flooding the server is not even thinkable.

I’m still working on this today, I’ll keep you posted.

Regards,

I suggest using the simpletimer (blynktimer) to run your sendSensor() function, and removing it from the loop() function. Then move the deepSleep command from the loop() function to the very end of the sendSensor() function, so that it only sleeps after it is done with that function.

Don’t forget to add the timer to the loop(), and the setup function. Review the documentation on this if you are not familiar on how to do this.

Hope this helps.

1 Like

@Gunner @Toro_Blanco @Jamin Hi again, thanks for following up with me. I’m really confused about all this deepsleep issue.

I simplified my script to pinpoint the problem and i’m not able to understand.
This script is running fine, i mean I see my temperature on the serial, then I see my “COUCOU” on the serial but Blynk is not updating on my iphone.
If I put the delay and deepsleep line away, then everything is fine, Blynk is updating normally.
There must be something wrong on the Blynk app update ? Seems it need to run continuously to update.

void loop() {
  sendSensor();
  Blynk.run();
  Serial.print("COUCOU");
  delay(50000000000);
  ESP.deepSleep(1 * 6 * 1000000, WAKE_RF_DEFAULT);

there’s an interesting project implementing deepsleep and blynk discussed here:

perhaps you could strip that code out and see how the components that @Toro_Blanco mentioned fit together…

you need to run deep sleep & your sensor read function OUTSIDE the loop

(just like the white bull has advised)

@Dave1829 Hi Dave,

I’m fooling around since the last few hours trying to understand so I placed everything everywhere but nothing is updating on blynk side. Even if i only have Blynk.run(); in the loop, everything is in the function.
Do you agree with me the deepsleep will never be run with a such long delay ? At that point the Blynk should have been updated already no ?

Regards,

what is the delay for?

Just to make sure its not the “deepsleep” that make the Blynk not updating.
It never goes to deepsleep that way, but its not updating blynk neither :frowning:

I suggest you start with some BLYNK basics and get your temperature sensor set up and working properly with blynk.

A proper BLYNK formatted code with have a very simple loop () with only the blynk and timer calls in most cases (there are a few cases where there are some exceptions).

The timer should call your sensor function every so often, but with the deepsleep at the end of it will put the esp to sleep after once through. I suggest taking some time looking at the docs to start.

yes,

much easier to do it in that order - get Blynk updating as you would like, then add deepsleep…

Thanks @Toro_Blanco

I did a lot of projet involving Blynk and never had a problem be4 using deepsleep.
In my original code (before stripping) I also update EmonCMS (kind of thinkspeak) without any problem even with deepsleep activated. I then noticed that Blynk stop updating when I activate deepsleep.

That is really weird.

i’ll repeat it again supposing my english is so bad I could not be understood.

I have the same code running extremely fine, I add a deepsleep single line (anywhere) and its not updating anymore. Even if the deepsleep is way after Blynk.run()

@Toro_Blanco @Dave1829 @Gunner

Thanks everyone to have contribued to my researches.
I found that if I use a simple connexion line like :
Blynk.begin(auth, "SSID", "WPAKEY");
Instead of my original codeWiFi.config(arduino_ip, gateway_ip, subnet_mask); WiFi.begin(ssid, pass); Blynk.config(auth);

EVERYTHING IS WORKING FINE
Even with deepsleep !!!

I wanted to save uptime by using a Fixed IP (someone in the forum suggested)

Like in my subject line, this is weird…

Merci, thank you, gratias !

1 Like

When it is sleeping it will not be updating BLYNK, or anything for that matter. It is sleeping.

Additionally, I am suprised to hear that your projects work with so much junk in the loop (). As I have mentioned, you should read through the docs and get familiar with the proper way to implement BLYNK into your projects. Just cause it works, doesn’t mean it is correct. Look at the sketch builder to see what it should look like. Simpletimer is a must.

1 Like