Hello @khoih
Thank you very much for your advice and assistance. Your code was helpful, I found that this code runs much quicker to connect to one of my 2 WiFi networks, however I am having a few problems. I added the code segment you suggested as shown here:
bool SetupBlynkConnection()
{
wifi.setDHCP(1, 1, 1); //Enable dhcp in station mode and save in flash of esp8266
Blynk.config(wifi, auth, server, 8080);
//Check if the first network is available and connect
if (Blynk.connectWiFi(ssid1, pass1))
{
Serial.println("Connected to primary Wifi network");
}
//Check if second network is available and connect
else
{
Serial.println("Primary Wifi network not available, connecting to backup Wifi network");
//wifi.setDHCP(1, 1, 1); //Enable dhcp in station mode and save in flash of esp8266
//Blynk.config(wifi, auth, server, port);
if (Blynk.connectWiFi(ssid0, pass0))
{
Serial.println("Connected to Blynk via backup Wifi network");
}
//if no networks are available then continue with rest of program
else
{
Serial.println("No Wifi networks are available for connection");
return false;
}
}
return (Blynk.connect(5000));
}
void CheckBlynkConnection()
{
if (!Blynk.connected())
{
Serial.println("Blynk connection status: Disconnected\nAttempting to re-establish connection to Blynk...");
if ( SetupBlynkConnection() ) //
Serial.println("Blynk connection status: OK...");
}
}
#define NOT_USE_BLYNK_BEGIN true
void setup()
{
// Debug console
Serial.begin(9600);
delay(1000);
Serial.println("\nStart Teensy4");
// Set ESP8266 baud rate
EspSerial.begin(ESP8266_BAUD);
delay(10);
Serial.println("Start Blynk");
#if USE_BLYNK_WM
Blynk.begin(wifi);
#else
#if NOT_USE_BLYNK_BEGIN
SetupBlynkConnection();
#else
//Blynk.begin(auth, wifi, ssid, pass);
Blynk.begin(auth, wifi, ssid0, pass0, server, 8080);
#endif
#endif
//timer.setInterval(30000L, CheckBlynkConnection); // check every 30s if still connected to server
FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
//Blynk.begin(auth, wifi, ssid0, pass0);
//SetupBlynkConnection();
Serial.println("WS2812 RGB LED strip test reconnecting to the wifi when disconnected: V3 (Running code provided by Blynk community member)");
timer1.setInterval(300, MainFunction);
}
void loop()
{
Blynk.run();
timer1.run();
}
void MainFunction(){
if(PresetLightSelection==1){
PresetLightSettings();
}
BlynkCounter++;
if(BlynkCounter==50){ //Check if Blynk is connected every 15 seconds approximately
Serial.println("Checking Blynk connection status...");
CheckBlynkConnection();
BlynkCounter=0;
}
}
Here is the serial monitor output when the blynk print command is included in the sketch:
Start Teensy4
Start Blynk
[1045]
___ __ __
/ _ )/ /_ _____ / /__
/ _ / / // / _ \/ '_/
/____/_/\_, /_//_/_/\_\
/___/ v0.6.1 on Arduino Mega
[19118] Connecting to NETWORK1
[20129] ESP is not responding
Primary Wifi network not available, connecting to backup Wifi network
[20667] Connecting to NETWORK2
[23844] AT version:1.1.0.0(May 11 2016 18:09:56)
SDK version:1.5.4(baaeaebb)
compile time:May 20 2016 15:08:19
OK
[29232] +CIFSR:STAIP,"192.168.43.102"
+CIFSR:STAMAC,"3c:71:bf:25:ef:e0"
[29243] Connected to WiFi
Connected to Blynk via backup Wifi network
WS2812 RGB LED strip test reconnecting to the wifi when disconnected: V3 (Running code provided by Blynk community member)
[40665] Ready (ping: 651ms).
Checking Blynk connection status...
Checking Blynk connection status...
Checking Blynk connection status...
Checking Blynk connection status...
As you can see, the system never realizes that the connection between Blynk and my Esp Hardware has been established when one of the 2 WiFi networks are available. So even when the system does connect to Blynk, it never runs the through the code segment
if ( SetupBlynkConnection() )
Serial.println("Blynk connection status: OK...");
Situation 1:
If both networks are unavailable on startup then the program stalls after it completes the void setup subroutine (it prints the WS2812 RGB LED strip test reconnecting to the wifi when disconnected: V3 (Running code provided by Blynk community member)
and stops running). If either of the 2 networks becomes available the program then unfreezes and connects to the available network.
Situation 2:
If both the network fails/unavailable after a connection was established then when either of the networks returns/available then the system still remains frozen. Here is the serial output for this situation:
Start Teensy4
Start Blynk
Primary Wifi network not available, connecting to backup Wifi network
No Wifi networks are available for connection
WS2812 RGB LED strip test reconnecting to the wifi when disconnected: V3 (Runing code provided by Blynk community member)
Checking Blynk connection status...
Blynk connection status: Disconnected //Here is where it stalls under situation 1
Attempting to re-establish connection to Blynk...
Connected to primary Wifi network
Checking Blynk connection status...
Checking Blynk connection status...
Checking Blynk connection status...
Checking Blynk connection status...
Checking Blynk connection status...
Checking Blynk connection status...
Checking Blynk connection status...
Blynk connection status: Disconnected
Attempting to re-establish connection to Blynk...
Primary Wifi network not available, connecting to backup Wifi network
No Wifi networks are available for connection //Here is where it stalls under situation 2
I apologize for not mentioning this earlier but I need my program to run even when there is no Wifi network available and reconnect when there is 1 available. I am playing around with the commands now to see if sprinkling some Blynk.connect()
and Blynk.run()
here and there might resolve the issues.