BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

BlynkSyncVirtual is not working after Virtual Write

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

StaticJsonDocument<200> doc;
#define R1 D1

BLYNK_CONNECTED(){
   Blynk.virtualWrite(V1, doc["V1"].as<int>());
   Blynk.syncVirtual(V1);
   
}
BLYNK_WRITE(V1){
   digitalWrite(R1, param.asInt());
}
char ssid[] = "";
char pass[]="";
char auth[]="";

void setup(){
   pinMode(R1, OUTPUT);

    doc["V1"]=1;
    Blynk.begin(ssid, pass, auth);
}
void loop(){
   Blynk.run();
}

BlynkSyncVirtual is not working after Virtual Write

What exactly do you mean by this?
Do you understand the purpose of the Blynk.syncVirtual( Pin) command?

Also, you appear to be missing some libraries.

Pete.

I want to put the data to the server and sync with that data.

Blynk.syncVirtual(vPin) is used to force the server to tell you the current value of the widget attached to vPin when you don’t currently know it. This is usually when your device first boots up, or when connection with the Blynk server is lost then re-established.

Calling Blynk.syncVirtual(vPin) causes the BLYNK_WRITE(vPin)` callback to be triggered.

The BLYNK_CONNECTED callback is triggered whenever the connection to the Blynk server is first established or re-established. This is why Blynk.syncVirtual(vPin) is placed in the BLYNK_CONNECTED callback.

There is no reason for you to write data to the server then immediately get the server to tell you what that data was. You already know that information and this approach would potentially create an endless loop.

Pete.

I want to get the feedback to the server.
In this case, doc[“R1”] Value is 1 which means off
But the relay doesn’t sync with this state

Well, your code isn’t populating any values into your StaticJsonDocument called doc so either your missing some code or doc["V1"] is always null.

Pete.

This is not my actual code it similar to this I forgot to add

 .as<int>()

The issue is not solved

As I said in my first post, you are missing some libraries.
I do t see the point in posting incomplete code and expecting assistance with it.

Pete.

I don’t want to share code because of some reasons. The code is same like this as you said there are some libraries missing, in my actual I have include all necessary libraries

Animesh

Well, unless you’re prepared to share the code which makes the sketch useable, and which populates the StaticJsonDocument with values then I don’t think you’ll get much assistance from Blynk community members.

Pete.

The only problem is that i virtualwrite the data to the server then sync it with the relays I have checked that the values are not null

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <ArduinoJson.h>
#include <SD.h>
#include <SPI.h>

#define R1 D2

int st1=0;

char ssid[]="";
char pass[]="";
char auth[]="";

BLYNK_CONNECTED(){
  File file = SD.open("./config.json","r");
  StaticJsonDocument<512> doc;
  deserializeJson(doc, file);
  st1=doc["R1"];
  Blynk.virtualWrite(V1, st1);
  Blynk.syncVirtual(V1);

}

BLYNK_WRITE(V1){
  digitalWrite(D2, param.asInt());
}

void setup(){
  pinMode(D2, R1);
  Blynk.begin(auth, ssid, pass);

  
}

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

So, This is my actual Code. I am reading the state from the sd card.
The json is like this {“R1”:1}