Hey everyone - I am working on a project that requires a very stable (total count) variable. The device must maintain the count on power loss or disconnection and retain
the total number of button presses. I have read a few similar topics and cant seem to wrap my head around how virtual sync works, or how to implement it in my situation. The theory is simple, but implementation hasnt given me much to work with.
Tried using a BLYNK_CONNECTED() with syncAll() and syncVirtual() but each time it is power cycled the variable is lost. Maybe there is some nuance I am missing but the docs and examples are limited so I am not finding much useful info.
If anyone has some code implementation they could suggest that would be great.
Preferably with an explanation of what I am missing as well. Ideally, I would like to be able to virtualRead the current value and set cycle_count equal to that to reset the volatile (MCU) value to the known last server value, but that doesnt seem to be how it works.
ESP32 | edgent | Arduino 2.3.2
In the below code, Serial monitor states “SYNC RAN” then “0” each time. Then cycle_count starts from 0 on button press. I have tried a myriad of solutions but nothing has done anything notable so this is a starting point.
TEST CODE - simplified for clarity and testing output. not final implementation
#define BLYNK_TEMPLATE_ID "***"
#define BLYNK_TEMPLATE_NAME "***"
#define BLYNK_FIRMWARE_VERSION "0.1.0"
#define BLYNK_PRINT Serial
#define APP_DEBUG
#define USE_ESP32_DEV_MODULE
#include "BlynkEdgent.h"
int button_pin = 27;
int total_count;
void setup() {
Serial.begin(115200);
pinMode(button_pin, INPUT_PULLUP);
BlynkEdgent.begin();
}
BLYNK_CONNECTED() {
Blynk.syncVirtual(V0);
Serial.println("SYNC RAN");
Serial.println(V0);
delay(10000);
}
void loop() {
BlynkEdgent.run();
if (!digitalRead(button_pin)) {
total_count++;
Blynk.virtualWrite(V0, total_count);
delay(100);
}
Serial.println(total_count);
}