I simply need to sync the states of my Blynk “Buttons” to my ESP8266 code at power up so that the mobile app and the sketch or on the same page initially.
I’ve spent a ton of time today trying to figure out why I can’t get the initial states synced. I read that I need to use Blynk.syncVirtual(Vx) or Blynk.syncAll() to accomplish this but I’ve tried both in my Setup code and it doesn’t update the states of my virtual pins. It also doesn’t run the BLYNK_WRITE methods as I expected (from the documentation).
See serial output and code attached below. I start out with my mobile test app (with only a single button) initially set with the button ON. However, you can see in the serial output, the button appears to be “0” from the code.
Also notice that after Setup is complete the BLYNK_WRITE is never called as a result of the syncAll call.
Finally, Also notice that after 3 iterations of the timer, after the call to change the virtual pin to 1, the code still sees the value of the button as “0”.
Help! I’m pulling my hair out here. In short… I’m just trying to figure out how to sync up the states of Blynk “Buttons” between the mobile app and my Arduino code at power-up.
Serial output:
�Connecting to bullet3
.....
WiFi connected
IP address:
192.168.0.83
[26111]
___ __ __
/ _ )/ /_ _____ / /__
/ _ / / // / _ \/ '_/
/____/_/\_, /_//_/_/\_\
/___/ v0.4.7 on ESP8266
Setup complete.
[26118] Connecting to blynk-cloud.com:8442
[26989] Ready (ping: 16ms).
Button Val = 0
i=1
Button Val = 0
i=2
Button Val = 0
i=3
Button Val = 0
Changing button value to 1
Button Val = 0
Button Val = 0
Button Val = 0
Button Val = 0
Button Val = 0
Code follows…
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp8266.h>
BlynkTimer timer1;
BlynkTimer timer2;
char auth[] = "5a94b07f079e4f7a97d6516ccad02b21"; // key for Blynk
int button1Val;
boolean firstLoop = true;
//--------------------
*** wifi setup code removed for sake of brevity
BLYNK_WRITE(V1) {
button1Val = param.asInt();
Serial.println("Button pressed!");
}
void printButtons() {
static int i = 0;
++i;
Serial.print("Button Val = "); Serial.println(button1Val);
if (i <= 3) {
Serial.print("i="); Serial.println(i);
} else {
if (i == 4) {
Serial.println("Changing button value to 1");
Blynk.virtualWrite(V1, 1);
}
}
}
void setup() {
Serial.begin(115200);
WiFiSetup();
Blynk.config(auth);
Blynk.syncAll();
timer1.setInterval(2000L, printButtons);
Serial.println("Setup complete.");
}
void loop() {
Blynk.run();
timer1.run();
}
So when you sync at boot, you run this command which does nothing but output the current state of the widget.
What you see in the serial monitor is exactly as you coded it.
Take a few step back and simplify your code.
Sync only the pin in question.
Blynk.config(auth);
Serial.println("before sync button state =" + String(button1Val));
Blynk.syncVirtual(V1);
Serial.println("after sync button state =" + String(button1Val));
Serial.println("syncing done");
//timer1.setInterval(2000L, printButtons);
Remove the printButtons timer as its also messing with the testing.
So set the button state to 1/HIGH, and reboot you device.
You should see in your output,
[26118] Connecting to blynk-cloud.com:8442
[26989] Ready (ping: 16ms).
before sync button state = 0
Button pressed!
after sync button state = 1
syncing done
Jamin, thank you very much for the speedy reply.
Yes, I understand the syncAll and syncVirtual difference. I just happened to post the version of the code with syncAll. Both result in identical output in the serial monitor since I only have one pin.
The basic problem is that when syncAll/Virtual is called in Setup, the BLYNK_WRITE method is never called as the documentation suggests it would be. If it were called then “Button pressed!” would appear in the serial monitor, but as you can see in the output, it’s not. Also, you can see that ever after the virtual pin is explicitly changed to 1 in the code (inside of printButtons), the serial output shows that the change is not detected (still shows “Button Val = 0)”.
I’m probably overlooking something stupid, but I just don’t see it right now. Your help is appreciated.
Thanks Gunner. I tried putting the three ``` at the beginning and end of the code, but in the right hand preview window it doesn’t show the formatted code. Does it get reformatted only after the post is complete?
@Jamin I also want to emphasize that the major problem is syncing button states immediately after power up between the mobile app and the ESP8266 code. In other words, I need to prevent the Blynk app from showing a button state that is different than the code sees as soon after power up as possible. I have not found a way to accomplish that.
This is mostly a guess as I don’t use sync much, but If I understand the Blynk.syncALL() correctly, then when it recalls the last widget value, it should get a LOW/0 for the button, because unless you are actually pressing the button (to bring its state to HIGH/1) then LOW/0 is it’s normal value… unless it is in switch mode, then yes it should display a HIGH/1 if that is the button’s current state.
In other words, Blynk.syncALL() doesn’t activate all the widgets (i.e. a button widget does not magically act as if pushed) , it just grabs their last known state as provided to the server from the App (as in generating a BLYNK_WRITE event).
Not after the first three, but it should as soon as the last three are in place.
Thanks @Lichtsignaal ! I think that did the trick. I thought I was on the right track, putting the sync.* in Setup based on @Jamin first reply. Apparently that might not be the best place. I’m doing further testing but that seems to correct the behavior I was seeing.
It also seems that every time my sketch does a Blynk.virtualWrite, I must follow that immediately by a Blynk.syncVirtual if I want the sketch to see the value pf the virtualWrite. Can anyone confirm that this indeed is the correct and exprected behavior?
THANKS TO EVERYONE THAT REPLIED!
I’ll mark this as resolved as soon as I complete testing.
virtualWrite should be synced across server/app when you issue that command from the hardware. Sync shouldn’t be needed at that time.
The BLYNK_CONNECTED does only sync when Blynk connects, hence the name. So in the event of a disconnect of the hardware it should get the latest values from the server.