syncAll() and the problem to get data after deep sleep

Hello,

I have a problem with my project. It runs on battery and so I decided to go with deep sleep procedure. This makes my Redbear Duo to run smoothly but one parameter still makes problem. I try to count for how many hours the device was active to check for the battery efficiency, but the counter keeps getting back to 0. You can see what happens with counter on the screenshot. The varaible in question is the one called licznik_godzin i code and called “ile godzin już?” on the app - this should be rising. Or did i make a mistake?

`    // This #include statement was automatically added by the Particle IDE.
#include "blynk/blynk.h"

// This #include statement was automatically added by the Particle IDE.
#include "Adafruit_DHT/Adafruit_DHT.h"

char auth[] = "my_auth_key";


#define DHTPIN 5     // what pin we're connected to
#define DHTTYPE DHT11		// DHT 11 

DHT dht(DHTPIN, DHTTYPE);

WidgetTerminal terminal(V3);
int godzina = 0;
int last_godzina = 0;
int licznik_godzin = 0;
int loopcounter = 0;

void setup() {

    Blynk.begin(auth);
    delay(50);
	pinMode(4, OUTPUT);
	dht.begin();
	
	while (Blynk.connect() == false) {
    // Wait until connected
    }
    Time.zone(+1);
	digitalWrite(4, HIGH);
    delay(100);
	float h = dht.getHumidity();
	float t = dht.getTempCelcius();
	digitalWrite(4, LOW);
	
	Blynk.virtualWrite(V1, h);	
	Blynk.virtualWrite(V2, t);
	
	delay(50);
	godzina = Time.hour();
}

BLYNK_WRITE(V4)
    {
        last_godzina = param.asInt(); 
  }
  BLYNK_WRITE(V5)
    {
        licznik_godzin = param.asInt(); 
  
  }


BLYNK_CONNECTED() {
  Blynk.syncAll();
}


void loop() {
Blynk.run();
loopcounter++;

if(loopcounter ==50){

   if ((godzina > last_godzina)||((godzina==0)&&(last_godzina==23))){
       licznik_godzin++;
       Blynk.virtualWrite(V5, licznik_godzin);
       Blynk.virtualWrite(V4, godzina);
       
   }
}
if(loopcounter ==150){
System.sleep(SLEEP_MODE_DEEP, 30);
}
delay(5);
}    `

Hello. Maybe the problem is that your board was restarted and your counter became zero again? Maybe there is some problem with sleep mode?

Your logic also seems to me a bit complicated. I didn’t understand it.

Hi there, after your reply i decided to go and rethink my approach. I did succeed with this task by using different method (Unix timestamp hardcoded into firmware, and than using RTC capabilities from Particle - similar to your RTC widget - to calculate how much time did pass since the time in code).

The problem with Blynk was , that syncAll() was not checked to be executed before other things happened. I fixed this by using this (just a snippet of code):

BLYNK_CONNECTED() {
    Blynk.syncAll();
}

BLYNK_WRITE(V5)                                     //firmware
{   
  firmware = param.asInt();
  syncTest = 1;
}

void loop() {
    
    Blynk.run();
    
    if (syncTest == 1 && firmware==0){
      System.sleep(SLEEP_MODE_DEEP, 60);
      }
  
}

This method makes sure, that the RedBear Duo will not go to sleep before, it checks Blynk for the status of V5 pin, and if this pin is High, it will not go into sleep until it is low. I use this, to upload new code to RedBear Duo, since it would be very hard to work with, since it is constantly sleeping - this method makes it wait for me to push the flash button, and than the new firmware gets sent.

I wrote it here in case, somebody else has got problem with battery powered, blynk operated project that needs to check for some conditions on the blynk app side.

1 Like

@ethelder nice fix. What we also do is check for a 1970 timestamp. We know if it shows 1970 then we don’t have the correct time from Blynk. We then go to sleep for a short while and check the timestamp again.

This is great method but I’m not taking my “time” from blynk RTC widget. I use RedBear Duo which is coded via build.particle.io and so I use this method of obtaining time:
https://docs.particle.io/reference/firmware/photon/#time

“The device synchronizes time with the Particle Cloud during the handshake. From then, the time is continually updated on the device. This reduces the need for external libraries to manage dates and times.”

Tak na przyszlosc przeczytaj dokumentacje, dlatego ze RAM jest czesto wymazywany jak urzadzenie wchodzi w deep sleep. Sa rozne stany uspienia i w niektorzych RAM trzyma zawartosc w innych nie.

Cześć - odpiszę Ci rodaku po angielsku, bo inni też czytają:)
Hello - I will answer you in English since there are others reading.

You have right, but not in this case. The code, was being written from the very begging with the idea in mind, that the device will be totally restarted the moment it awakens and as the documentation you quote states “it will reset and run all user code from the beginning with no values being maintained in memory from before the deep sleep”.

Usually when you see “deep sleep” it means that the device goes in to ultra low power mode, just to be restarted after some time. The only method to keep any value from one cycle to another is to write it to eeprom which at least in arduino has a specified life of 100,000 write/erase cycles, and so I tend not to do that and the second approach was the one, I started with, writing the value to virtual pin in Blynk, and than trying to read it back on wakeup.

But you are true, there are some sleep modes in some devices, which keep the RAM memory and so, you can keep the global variables. Never used them, but heard of that.