Hi There, I recently started using blink. I am building a small project with arduino mega and ESP8266 and couple of sensors. Basically, I am looking for a way to run the Blynk.run() in loop ONLY when the WiFi connection is establish. If due to some reason, if ESP8266 unable to establish a connection with WiFi, I don’t want Blynk.run() to be executed in loop. Is there a simple way to achieve this?
There is a function called blynk.connected() to check if you are connected. I think it returns either 1 or 0, so you could use that to execute code outside blynk if things are not connected.
// CheckServer.ino by Costas 17/7/2016
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
SimpleTimer timer;
char auth[] = "xxxxxxxxxxxx";
char ssid[] = "xxxxxxxxxxxx";
char pass[] = "xxxxxxxxxxxx";
bool Connected2Blynk = false;
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Blynk.config(auth); // in place of Blynk.begin(auth, ssid, pass);
Blynk.connect(3333); // timeout set to 10 seconds and then continue without Blynk
while (Blynk.connect() == false) {
// Wait until connected
}
Serial.println("Connected to Blynk server");
timer.setInterval(11000L, CheckConnection); // check if still connected every 11 seconds
}
void CheckConnection(){
Connected2Blynk = Blynk.connected();
if(!Connected2Blynk){
Serial.println("Not connected to Blynk server");
Blynk.connect(3333); // timeout set to 10 seconds and then continue without Blynk
}
else{
Serial.println("Connected to Blynk server");
}
}
void loop() {
if(Connected2Blynk){
Blynk.run();
}
timer.run();
}
Serial Monitor:
WiFi connected
IP address:
192.168.10.171
[18547] Blynk v0.3.8 on Arduino
[18547] Connecting to blynk-cloud.com:8442
[18683] Ready (ping: 2ms).
[23684] Connecting to blynk-cloud.com:8442
[23812] Ready (ping: 0ms).
Connected to Blynk server
Connected to Blynk server **[ROUTER TURNED OFF]**
Not connected to Blynk server
[49943] Connecting to blynk-cloud.com:8442
Not connected to Blynk server
[56876] Connecting to blynk-cloud.com:8442 **[ROUTER TURNED ON]**
Not connected to Blynk server
[67876] Connecting to blynk-cloud.com:8442
Not connected to Blynk server
[78876] Connecting to blynk-cloud.com:8442
Not connected to Blynk server
[89876] Connecting to blynk-cloud.com:8442
Not connected to Blynk server
[100876] Connecting to blynk-cloud.com:8442
[102828] Ready (ping: 0ms).
Connected to Blynk server
Connected to Blynk server
@vshymanskyy maybe it’s a bug. It was pointed out to me around 9 months ago and I’m pretty sure when I checked the “3” was in the library. Maybe I misunderstood the library.
/* CheckServerV2.ino by Costas 17/7/2016, revised 16/7/2017
Notes:
Blynk.connectWiFi() doesn't cover WiFi failures i.e. router problems as it's blocking code
Default Blynk.connect() tries for 5 seconds not 30s as per http://docs.blynk.cc/#blynk-firmware-connection-management-blynkconnect
Not 6s timeout either (2000UL * 3) as indicated by:
https://github.com/blynkkk/blynk-library/blob/master/src/Blynk/BlynkProtocol.h#L50
bool connect(uint32_t timeout = BLYNK_TIMEOUT_MS*3) {
https://github.com/blynkkk/blynk-library/blob/master/src/Blynk/BlynkConfig.h#L37
#define BLYNK_TIMEOUT_MS 2000UL
See the 5000L in https://github.com/blynkkk/blynk-library/blob/master/src/Blynk/BlynkProtocol.h#L343
lastLogin = lastActivityIn - 5000L; // Reconnect immediately
*/
//#define BLYNK_DEBUG
#define BLYNK_TIMEOUT_MS 750 // must be BEFORE BlynkSimpleEsp8266.h doesn't work !!!
#define BLYNK_HEARTBEAT 17 // must be BEFORE BlynkSimpleEsp8266.h works OK as 17s
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
BlynkTimer timer;
char ssid[] = "xxxxxxxxxxxxxxxxxx";
char pass[] = "xxxxxxxxxxxxxxxxxx";
char auth[] = "xxxxxxxxxxxxxxxxxx";
char server[] = "blynk-cloud.com";
unsigned int port = 8442;
void setup() {
Serial.begin(115200);
Serial.println();
// line below is blocking code, consider regular WiFi connection with timeout if you have router problems
Blynk.connectWiFi(ssid, pass); // used with Blynk.connect() in place of Blynk.begin(auth, ssid, pass, server, port);
// line below needs to be BEFORE Blynk.connect()
timer.setInterval(11000L, CheckConnection); // check if still connected every 11s
Blynk.config(auth, server, port);
Blynk.connect();
}
void CheckConnection(){ // check every 11s if connected to Blynk server
if(!Blynk.connected()){
Serial.println("Not connected to Blynk server");
Blynk.connect(); // try to connect to server with default timeout
}
else{
Serial.println("Connected to Blynk server");
}
}
void loop() {
if(Blynk.connected()){
Blynk.run();
}
timer.run();
}