Raenil
September 12, 2019, 9:10am
1
I want to read V1, V2 state from Blynk server when Arduino is on
So I expected the following code work like…
if Blynk connected => printed “BLYNK_CONNECTED()” => Call BLYNK_WRITE(V1) and BLYNK_WRITE(V2) with some print lines => First = false
However, these codes actually call BLYNK_WRITE after first = false.
Thus the serial monitor shows:
[12154] Ready (ping: 209ms).
BLYNK_CONNECTED()
Frist = true
Call V1
Call V2
Frist = false
BLYNK_WRITE(V1)
BLYNK_WRITE(V2)
What’s wrong with my code?
#define BLYNK_PRINT Serial
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
BlynkTimer timer;
//Blynk setting-----------------------------------------------------------------
byte arduino_mac[] = {};
IPAddress arduino_ip ();
IPAddress dns_ip ();
IPAddress gateway_ip ();
IPAddress subnet_mask();
char auth[] = "";
//Blynk setting-----------------------------------------------------------------
int First = true;
int A;
int B;
void setup()
{
Serial.begin(9600);
Blynk.begin(auth, "blynk-cloud.com", 80, arduino_ip, dns_ip, gateway_ip, subnet_mask, arduino_mac);
}
BLYNK_CONNECTED()
{
Serial.println("BLYNK_CONNECTED()");
Serial.println("Frist = true");
Serial.println("Call V1");
Blynk.syncVirtual(V1); //Call V1
Serial.println("Call V2");
Blynk.syncVirtual(V2); //Call V2
First = false;
Serial.println("Frist = false");
}
BLYNK_WRITE(V1)
{
Serial.println("BLYNK_WRITE(V1)");
A = param.asInt();
if (First)
{
Serial.println("FirstA");
}
else
{
//Some Code
}
}
//Wind
BLYNK_WRITE(V2)
{
Serial.println("BLYNK_WRITE(V2)");
B = param.asInt();
if (First)
{
Serial.println("FirsB");
}
else
{
//Some Code
}
}
void loop()
{
Blynk.run();
}
I don’t think there is anything wrong with your code.
I am pretty sure that ** Blynk.syncVirtual(xx);** will only get called at the next Blynk.run() call which won’t happen until the BLYNK_CONNECTED() function has finished.
Raenil
September 12, 2019, 10:08am
4
That’s mean Blynk.syncVirtual(xx); will work when next Blynk.run()? not the location of that code?
If I got it right, I’d have to approach it the other way.
1 Like
Yes - I’m pretty sure that how it works.
Maybe a dev can confim/deny this when they see this post?
If @JustBertC is correct, then simply add a Blynk.run(); immediately after your Blynk.syncVirtual(xx) commands and it should produce the result you expected.
Pete.
Raenil
September 12, 2019, 11:34am
7
I liked this method and tried it but it works like the code above.
BLYNK_CONNECTED()
{
Serial.println("BLYNK_CONNECTED()");
Serial.println("Frist = true");
Serial.println("Call V1");
Blynk.syncVirtual(V1); //Call V1
Blynk.run();
Serial.println("Call V2");
Blynk.syncVirtual(V2); //Call V2
Blynk.run();
First = false;
Serial.println("Frist = false");
}
Raenil
September 12, 2019, 11:47am
8
I liked this method and tried it but it works like the code above.
BLYNK_CONNECTED()
{
Serial.println("BLYNK_CONNECTED()");
Serial.println("Frist = true");
Serial.println("Call V1");
Blynk.syncVirtual(V1); //Call V1
Blynk.run();
Serial.println("Call V2");
Blynk.syncVirtual(V2); //Call V2
Blynk.run();
First = false;
Serial.println("Frist = false");
}
What about this?..
BLYNK_CONNECTED()
{
Blynk.syncVirtual(V1); //Call V1
Blynk.syncVirtual(V2); //Call V2
Serial.println("BLYNK_CONNECTED()");
Serial.println("Frist = true");
Serial.println("Call V1");
Serial.println("Call V2");
First = false;
Serial.println("Frist = false");
}
Raenil
September 12, 2019, 11:53am
10
This code works just like the first one. Perhaps Blynk.syncVirtual signals Blynk.run to call BLYNK_WRITE instead of calling BLYNK_WRITE.