Hello, I have been stucked with this code for 2 weeks. The idea is how to go offline and online mode, if online = myevent(), also if offline = myevent(). Serial monitor does not print out Serial.println("GG")
because it does not connected to blynk (I intentionally turned of my hotspot). Therefore, any idea on how to execute function without internet ?
#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
char auth[] = "XXXXXXXXXXXXX";
char ssid[] = "XXX";
char pass[] = "XXX";
#define EspSerial Serial3
#define ESP8266_BAUD 115200
ESP8266 wifi(&EspSerial);
void setup() {
Serial.begin(115200);
Serial3.begin(115200);
EspSerial.begin(ESP8266_BAUD);
Serial.println("Starting");
Blynk.begin(auth, wifi, ssid, pass);
Serial.println("GG");
}
void loop(){
Blynk.run();
if ( Serial3.available() ) {
Serial.write( Serial3.read() );}
if ( Serial.available() ) {
Serial3.write( Serial.read() );}
}
Thanks !
Blynk.begin is a blocking function. If the device is unable to connect to either WiFi or the Blynk server then code execution will stop at that point.
The alternative is to manage your own WiFi connection within the sketch and define the Blynk parameters using Blynk.config and connect to Blynk using Blynk.connect.
This is all covered in the documentation…
Firmware API, supported hardware, connection management, Blynk Mobile apps, tutorials, and more
and there are many topics on this subject. If you search for Blynk.config then you’ll find lots of examples.
Pete.
1 Like
Allright… i will give it a try tomorrow and uodate here… Thank you so much Pete…
Thanks
Atikahamdan
Hello @PeteKnight . It works ! Thanks for your explanation. The “Non Blocking” function really opened my eyes.
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
char auth[] = "XXXXXXXXXXXX";
char ssid[] = "XXX";
char pass[] = "XXX";
#define EspSerial Serial3
#define ESP8266_BAUD 115200
ESP8266 wifi(&EspSerial);
BlynkTimer timer;
#define RESET_PIN 7
int connection_loss_count = 0;
void setup() {
Serial.begin(115200);
Serial3.begin(115200);
EspSerial.begin(ESP8266_BAUD);
digitalWrite(RESET_PIN, HIGH);
pinMode(RESET_PIN, OUTPUT);
Serial.println("Starting");
delay(1000);
connect_wifi();
timer.setInterval(10000L, connection_check);
}
void loop(){
timer.run();
if(Blynk.connected()) Blynk.run();
if (Serial3.available()) Serial.write(Serial3.read());
if (Serial.available()) Serial3.write(Serial.read());
}
void connect_wifi(){
Blynk.config(wifi, auth);
Serial.print("Connecting to "); Serial.println(ssid);
if(!Blynk.connectWiFi(ssid, pass)){
Serial.println("WiFi Connection Failed");}
else{Serial.println("WiFi Connection Successful");
delay(1000);
Serial.println("Connecting to BLYNK");
Blynk.connect();
if(Blynk.connected()){
Serial.println("Blynk Connection Successful");
delay(1000);}
else{Serial.println("Blynk Connection Failed");}}
}
void connection_check(){
if(Blynk.connected()){
connection_loss_count = 0;
Serial.println("Blynk Connected");}
else{connection_loss_count = connection_loss_count + 1 ;
Serial.print("Blynk Disconnected || Reconnecting Trial: ");
Serial.println(connection_loss_count);
connect_wifi();}
if(connection_loss_count >= 10){
Serial.println("Rebooting System in 3"); delay(1000);
Serial.println("Rebooting System in 2"); delay(1000);
Serial.println("Rebooting System in 1"); delay(1000);
digitalWrite(RESET_PIN, LOW);}
}
1 Like