Rolling boot count

I am trying to retrieve my last number from the server. In this case it is for a boot count every deep sleep cycle. Why does > Blynk.sync(V9); not work? Or is the only way to do this write it to SPIFFS?

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>

//You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "====";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "=====";
char pass[] = "=====";

BlynkTimer timer;
WidgetLCD lcd (V0);

int espCycles;

void goToSleep(){
  Blynk.syncVirtual(V9);
  timer.setTimeout(5000L,[](){                                     //Lambda 
      espCycles ++;
      Blynk.run();
      lcd.print(0,0,"Sleeping..");
      Blynk.virtualWrite(V9, espCycles);
  //------------------------------------------------------------------------------
      ESP.deepSleep(3e+7); //sleep  NOTE THIS IS IN MICROSECONDS!!!!!!!!
  //------------------------------------------------------------------------------
    });
}

void setup()
{
// Debug console
Serial.begin(9600);
//Blynk.begin(auth, ssid, pass);
Blynk.begin(auth, ssid, pass, IPAddress(192,168,0,97), 8080);
//--------------------------------------------------------------------------------
timer.setInterval(10000L, goToSleep); //this one is millis
//--------------------------------------------------------------------------------
lcd.clear();
delay(500);
lcd.print(0,0, "Starting..");
Blynk.virtualWrite(V9, espCycles);
}

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

I have been trying to wade through your connected beehive @christophebl @PeteKnight . It looks like you are doing a boot count to V0 but I can’t see a virtual sync??

David

You have this in your void setup() Which runs at boot and may dump somthing new to that vPin before it gets a chance to read what was previously there.

If you want to get last value, then remove the above issue and try the sync command in a Blynk.connected() function

The Blynk.syncVirtual(V9) command won’t actually retrieve the value from pin V9, it just causes the BLYNK_WRITE(V9) callback function to be ‘fired’. As you don’t have a BLYNK_WRITE(V9) function and corresponding espCycles = param.asInt() line of code then the sync process will never work.

Also, I firmly believe that timers should never be used with deep sleep, unless you need to repeat something x times before sleeping.
The timer simply takes away your control over when the sleep process begins. Far better to execute the code you want, then as soon as it’s done go to sleep.
BTW, you have the SimpleTimer library included, which isn’t being used.

Pete.

Good catch, I wasn’t paying full attention.

It has been awhile since I used Blynk, so that’s my story and I am sticking to it :grin:

1 Like

Thanks for the pointers. The timer was for testing :rofl: no seriously, I thought maybe there wasn’t enough elapse time to complete the writes. I will try to remember that for the future.

So what you are saying is I need to make a function. Like below.

BLYNK_WRITE(V9)
{
  espCycles = param.asInt();
}

Then any time I call

Blynk.syncVirtual(V9);

It will execute that function?

1 Like

You’ve got it!

Pete.

Wow I sure did read that in the Docs… maybe there is more info on it elsewhere…

The description in the docs is somewhat cryptic…

Blynk.syncVirtual(vPin)

This command updates individual Virtual Pin to the latest stored value on the server. When it’s used, a corresponding BLYNK_WRITE handler is called.

Basically the BLYNK_WRITE(Vpin) callback is triggered every time the value of the corresponding Vpin changes. What the Blynk.syncVpin command does is to force the server to push out the current value for that Vpin as if it had changed - but of course it hasn’t.
It’s usually used in connection with BLYNK_CONNECTED, so that every time the device connects, or re-connects to Blynk, the latest value for the Vpin is pulled back from the server and saved to your variable using the param.as[variable type] command.

Pete.

2 Likes