Hello,
I’d like to understand the order of execution of the blynk functions at startup .
for example this simplified sketch:
void setup()
{
Serial.begin(9600);
Blynk.begin(auth, server_ip, 8080, arduino_ip, dns_ip, gateway_ip, subnet_mask, arduino_mac);
//other setup: GPIO, Functions...
}
BLYNK_CONNECTED() { //update the values }
void loop(){
Blynk.run();
}
What is the correct order?
1-blynk begin function,
2-BLYNK_CONNECTED() function
3- //other setup: GPIO, Functions…
or
1-blynk begin function,
2-//other setup: GPIO, Functions…
3-BLYNK_CONNECTED() function
its a good question, and it can be answered by placing Serial.println(); commands
in setup() and BLYNK_CONNECTED(); and see what order they occur.
My guess is the latter… i.e. during Blynk.run()
1 Like
Setup runs first and only once.
loop runs all the time but not untill after setup has completed.
blynk_connected only runs once when loop is running and the app gets connected.
I just tried this bit of code:
void setup()
{
Serial.begin(57600);
Serial.println("about to do Blynk.begin");
Blynk.begin(auth, ssid, pass);
// <------------------------------------------------BLYNK_CONNECTED executes here
Serial.println("Blynk.begin completed");
delay(100);
Serial.println("Void setup 1");
delay(100);
Serial.println("Void setup 2");
}
BLYNK_CONNECTED()
{
Serial.println("Blynk Connected executed");
}
void loop()
{
Serial.println("Void Loop, about to do Blynk.run");
Blynk.run();
Serial.println("Void Loop, Blynk.run completed");
}
which produces this output in the serial monitor:
about to do Blynk.begin
[5398] Connecting to UltraNet
[15901] Connected to WiFi
[15901] IP: 192.168.1.27
[15901]
___ __ __
/ _ )/ /_ _____ / /__
/ _ / / // / _ \/ '_/
/____/_/\_, /_//_/_/\_\
/___/ v0.6.1 on ESP8266
[15914] Connecting to blynk-cloud.com:80
[16084] Ready (ping: 56ms).
Blynk Connected executed
Blynk.begin completed
Void setup 1
Void setup 2
Void Loop, about to do Blynk.run
Void Loop, Blynk.run completed
Void Loop, about to do Blynk.run
Void Loop, Blynk.run completed
This shows that BLYNK_CONNECTED
is executed IMMEDIATELY after the device has connected to Blynk, before the any further lines of code in void setup.
So this means that:
is the correct answer.
Pete.
3 Likes