Mysterious behaviour with "Blynk.syncAll()"

Hi all,
Before describing problem let me describe what I exactly did, what is the expected result and what is that actually happening.

  1. There is a button widget connected to virtual pin V1 that acts in a push mode

  2. There is a slider widget connected to virtual pin V2. This slider will send data on release ranging from ‘10 to 100’

Now data is being read from slider widget from the hardware side and stored in a global variable. But Regarding button widget it has nothing to do except one very important thing, if it is pressed 1 will be send and it will run Blynk.syncAll().

Now let’s get into the coding part:
I’m using ‘Amica NodeMCU running at 160MHz 4M(3M SPIFFS)’

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

SimpleTimer manageSlider;
volatile int sliderval;

const char* ssid     = "LOL";
const char* password = "ROFL";

char auth[] = "12345";

void setup()
{
    Serial.begin(115200);
    Blynk.begin(auth, "LOL", "ROFL");
    while (Blynk.connect() == false) {  }
    
    Blynk.syncAll(); //No need to write bunch of other stuffs as it runs for once
    manageSlider.setInterval(1000L, getSliderVal);
 }

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

BLYNK_WRITE(V2)//Slider widget is attached to V2
{
    sliderval = param.asInt();
}

BLYNK_WRITE(V1)//Sync hardware <Button widget attached to V1>
{
  if(param.asInt() == 1)
  {
    Blynk.syncAll();
  }
}

void getSliderVal()
{
    Serial.print("Value from slider is: ");
    Serial.println(sliderval );
}
//That's it

Here comes the mysterious problem:
As per as the coding, after connecting to WiFi it connects to blynk server, there is no problem in that, now simple timer gets value of slider widget and prints it every 1 second, for the first time let me assume I changed value to 30 and it is fine, 30 is being displayed. But once device is rebooted or disconnected from server due to some reason, void setup() runs once again, it is successfully connected to blynk server but Blynk.syncAll(); too runs for once in the setup AND 30 SHOULD MUST BE DISPLAYED WHEREAS I AM GETTING 0.

Case 1:
Now,if I press and release the button widget 30 is being displayed

Case 2:
If I change value of the slider, new value is being displayed

But to my surprise, in the app if I don’t change the value 30 is displayed IN APP whereas I’m getting 0 in the hardware side.

So, can anybody explain what exactly is going on with “Blynk.syncAll()” function??
Please one request, don’t instruct me to run above mentioned function in the way it has been already posted, as I got similar result.

Conclusion:
Blynk.syncAll() function don’t does its job till few seconds hardware establishes its connection with server.
It’s a humble request to the developers to fix this bug.
Yes I can overcome this using EEPROM, and numerous other ways.

Now allow me to take it a little bit further with “Eventor” widget
Now I created an event such that button widget is set to 1 (i.e turned on or pushed) when slider widget value is 0. To my surprise it don’t, as it is currently reading value as 30 which is being shown in app and not 0 which I’m getting in the hardware. So, I did a cross check by putting slider value to 0, now it does it’s job and button widget is high.
This was to confirm that whatever data is read by other widgets on app from server is absolutely correct before and after any disconnection but from hardware it is a mess simply because Blynk.syncAll() fails to do its job. Similar is the same case with “Blynk.syncVirtual(Vx)” “Blynk.syncVirtual(V0, V1, V2, …)”, if they are triggered in the setup or for the first time hardware is connected to the server they simply don’t work.

Really I appreciate if you just have read each words.
Well, that’s it for now. Hundred of bugs will be posted which I’m facing currently. I will do it whenever I will get time.

You are wrong here. When hardware resets your variable sliderval is 0. So seems your function getSliderVal is triggered before value for slider comes from server. That’s it.

We have correct and nice example of how you may handle sync - https://github.com/blynkkk/blynk-library/blob/master/examples/More/Sync/HardwareSyncStateFromApp/HardwareSyncStateFromApp.ino

Please do not judge so quickly in future :slight_smile:.

1 Like

Thank you so much Dmitriy, I thought calling sync function will do the job in setup but I never thought that what needs to be sync should come before other in loop()

Below is the corrected code.

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

SimpleTimer manageSlider;
volatile int sliderval;

const char* ssid     = "LOL";
const char* password = "ROFL";

char auth[] = "1234";

void setup()
{
    Serial.begin(115200);
    Blynk.begin(auth, "LOL", "ROFL");
    while (Blynk.connect() == false) {  }
    
    Blynk.syncAll(); //No need to write bunch of other stuffs as it runs for once
    manageSlider.setInterval(1000L, getSliderVal);
 }



BLYNK_WRITE(V1)//Slider widget is attached to V1
{
    sliderval = param.asInt();
}

BLYNK_WRITE(V4)//Sync hardware <Button widget attached to V4>
{
  if(param.asInt() == 1)
  {
    Blynk.syncAll();
  }
}

void getSliderVal()
{
    Serial.print("Value from slider is: ");
    Serial.println(sliderval );
}

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

Hi, Im having issues when I tried to add BLYNK_CONNECTED and Blynk.synkALL().

My code was working good but then, if I add the BLYNK_CONNECTED gets a compilation error, what I am doing wrong??

Sigh we are not here to be your eyes you need to check your code :stuck_out_tongue:

You are missing a end bracket ‘}’
For your BLYNK_CONNECTED function.

4 Likes

Thanks now compiles!!