Get data from the server on start

Hi,
Could you please help with the Blynk.sync?
I’d like to restore my parameters from the server on the ESP start.

I try to use Blynk.syncVirtual(V1) and even Blynk.syncAll() but nothing causes BLINK_WRITE to trigger.

For tests I have:
V1 - slider (0…1023) to change my LED light. It works when I slide this widget. But it does not restore LED light on start.
V2 - Numeric input (just for test)
V3 - Button to trigger sync manully

Code:

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial

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

#include <EasyBlynk8266.h>
  
#define ledPin 13
#define RelayPin 12
#define PIN_BUTTON 0

// This function will be called every time Slider Widget
// in Blynk app writes values to the Virtual Pin 1
BLYNK_WRITE(V1)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
  Serial.print("V1 Slider value is: ");
  Serial.println(pinValue);
  analogWrite(ledPin,pinValue);
}

BLYNK_WRITE(V2)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V2 to a variable
  BLYNK_LOG2("V2 Slider value is: ", pinValue);
}

BLYNK_WRITE(V3)
{
  Serial.println("SyncV2");
  Blynk.syncVirtual(V2);
  delay(1000);
  Serial.println("SyncAll");
  Blynk.syncAll();
}

void setup()
{
  // Debug console
  Serial.begin(115200);
  Serial.println("");
  Serial.println("Starting..");
  delay(2000);

  Serial.println("set Pins..");
  pinMode(ledPin, OUTPUT);      // установка порта на выход для дублирования V1 на Pin 13 (green led)
  analogWrite(ledPin, 1022);  
  pinMode(PIN_BUTTON, INPUT); // кнопка GPIO0

  Serial.println("Starting EasyBlynk8266..");
  EasyBlynk8266.begin();
  Serial.println("EasyBlynk8266 completed.");
  
//  Serial.println("Set V0..");
//  Blynk.setProperty(V0, "offLabel", "Сохранить");  
//  Blynk.setProperty(V0, "onLabel", "Сохранено");  

//  while (Blynk.connect() == false) {  }
  Blynk.syncAll();
  Serial.println("Delay 5 sec..");
  delay(5000);
  Blynk.syncAll();
  Serial.println("Still no sync?");
}

BLYNK_CONNECTED() {
  //get data stored in virtual pins from server
  Serial.println("Sync..");
  Blynk.syncAll();
}

void loop()
{
  EasyBlynk8266.run();
}

I have many debug Serial.print and expect that inside BLYNK_CONNECT a sync will call BLYNK_WRITE(V1) and I’ll see the debug output "V1 Slider value is: "
But:

[6946] Connecting to blynk-cloud.com:80`
[8077] Ready (ping: 53ms).
Sync..
EasyBlynk8266 completed.
Delay 5 sec..
Still no sync?
-= I press the button V3 =-
SyncV2
SyncAll
-= I change V2 =-
[32725] V2 Slider value is: 6
SyncV2
SyncAll

As you can see when I press the button V3 my sync is called explicitly. But the debug output “V3 Slider value is: 6” appears only when I change V2 in the app.
Looks like sync works neither on start (BLYNK_CONNECT) nor by the button.

Thanks in advance

Maybe try using the regular Blynk library rather than this unsupported (by Blynk) wrapper library.

Pete.

Thank you Pete!
It works with the standard library indeed.

Could you suggest any other way to use a kind of WiFi manager instead of hard coded credentials for WiFi and Auth Token? (To ask them if the connection was unsuccessful)

WifiManager works well.

Also, I’d avoid syncAll if I were you.

Pete.

Sure. I’ve added it for test purposes only, when syncVirtual(V1) didn’t work.

1 Like