Hello. I know there must be a great answer to my question, but I’m stumped - this had been working very well and consistently previously. So … it goes to you the experts!
I have a network of a few ESP8266-12E modules that connect perfectly via SoftAP commands. I’ve never had any trouble connecting, disconnecting and then reconnecting over the SoftAP network. One (and only one) of the modules connects to my router after disconnecting from the SoftAP network and then back to the router and then back again etc on a schedule (a few seconds here then a few seconds there). The first time it connects to the router it also connects to Blynk successfully as you’ll see in my console dump below. But every time after when connected to the router it does not connect to Blynk.
I’ve updated everything but the ESP8266s. New Arduino IDE and Blynk libraries. Could the issue be with the ESP8266 instead of the router?
I’m including my code here as well as the results at bottom. Again, every connect attempt to SoftAP and router is always successful, but only the first attempt to connect to Blynk works as you can see. Am I having a router conflict issue? Is the IP address of my ESP8266 not really changing as the results shows it to be?
Thanks guys!!
#define BLYNK_PRINT Serial
#include <BlynkSimpleEsp8266.h>
#include <ESP8266WiFi.h>
IPAddress ipAddressR(192,168,0,29);
IPAddress ipAddressS(192,168,4,27);
IPAddress gatewayR(192,168,0,1);
IPAddress gatewayS(192,168,4,9);
IPAddress softAPMultiIP(192,168,4,255);
IPAddress routerMultiIP(192,168,0,255);
IPAddress subnet(255,255,255,0);
IPAddress myMultiIP;
const char auth[] = "RECENTLY REFRESHED";
const char ssid2[] = "CenturyLink0395";
const char sspwd2[] = "18 CHARS LONG PWD"; //pwd same for SoftAP
boolean connectToRouter = false;
boolean isBlynkConnected = false;
boolean isSoftAPConnected = false;
void setup() {
Serial.begin(115200);
delay(100L);
establishConnection();
}
void establishConnection(){
if (!isSoftAPConnected && Blynk.connected() == 1) {
Blynk.disconnect();
Serial.printf("Blynk disconnecting ...");
while (Blynk.connected() == 1) {
delay(100L);
Serial.print(".");
}
Serial.printf("\nBLYNK DISCONNECTED\n");
}
WiFi.disconnect(true);
WiFi.softAPdisconnect(true);
Serial.printf("WiFi disconnecting \n");
while (WiFi.status() == WL_CONNECTED) {
delay(100L);
Serial.print(".");
}
WiFi.mode(WIFI_STA);
connectToAP();
}
void connectToAP() {
isBlynkConnected = false;
isSoftAPConnected = false;
if (connectToRouter) {
myMultiIP = routerMultiIP;
Serial.print("ROUTER ");
WiFi.config(ipAddressR, gatewayR, subnet);
WiFi.begin(ssid2, sspwd2);
} else {
myMultiIP = softAPMultiIP;
Serial.print("softAPSSID1 ");
WiFi.config(ipAddressS, gatewayS, subnet);
WiFi.begin("SoftAPSSID1", sspwd2);
}
IPAddress espIP = WiFi.localIP(), espGateway = WiFi.gatewayIP();
Serial.printf("local values espIP=%s espGateway=%s\n", espIP.toString().c_str(), espGateway.toString().c_str() );
boolean tooLong = false;
unsigned long wifiConnectionTestMillis = millis();
while (WiFi.status() != WL_CONNECTED) {
delay(200);
Serial.print(".");
if (millis() - wifiConnectionTestMillis > 20000L) { // try for less than 20 secs then switch to another AP
tooLong = true;
yield();
break;
}
}
if (tooLong) {
//try again...
connectToAP();
} else {
if (connectToRouter){
isBlynkConnected = manageBlynkConnection();
if (isBlynkConnected) {
Serial.println("Blynk connected!");
} else {
Serial.println("Blynk NOT connected!");
}
}else{
isSoftAPConnected = true;
Serial.println("SoftAP connection successful!");
}
}
}
void manageConnections() {
static unsigned long wifiSwitchTimerStart = millis();
unsigned long WIFI_SWITCH_TIMEOUT = connectToRouter ? 4000L : 3000L;
if (millis() - wifiSwitchTimerStart > WIFI_SWITCH_TIMEOUT) {
connectToRouter = !connectToRouter;
establishConnection();
wifiSwitchTimerStart = millis();
}
}
boolean manageBlynkConnection(){
Blynk.config(auth);
Blynk.connect();
unsigned long blynkConnectTestMillis = millis();
while (Blynk.connected() == 0) {
delay(35);//use time to do background stuff - prevent resets!
if (millis() - blynkConnectTestMillis > 7000L) { // try for 7 seconds to connect to Blynk
break;
}
}
return Blynk.connected() == 1;
}
void loop() {
manageConnections();
if (isBlynkConnected){
Blynk.run();
delay(500);
}else{
delay(10);
}
}
WiFi disconnecting
.softAPSSID1 local values espIP=192.168.4.27 espGateway=192.168.4.9
.......SoftAP connection successful!
WiFi disconnecting
ROUTER local values espIP=192.168.0.29 espGateway=192.168.0.1
............[10952]
___ __ __
/ _ )/ /_ _____ / /__
/ _ / / // / _ \/ '_/
/____/_/\_, /_//_/_/\_\
/___/ v0.5.1 on NodeMCU
[10953] Connecting to blynk-cloud.com:80
[11170] Ready (ping: 86ms).
Blynk connected!
[15239] Disconnected
Blynk disconnecting ...
BLYNK DISCONNECTED
WiFi disconnecting
softAPSSID1 local values espIP=192.168.4.27 espGateway=192.168.4.9
............SoftAP connection successful!
WiFi disconnecting
ROUTER local values espIP=192.168.0.29 espGateway=192.168.0.1
............[26013]
___ __ __
/ _ )/ /_ _____ / /__
/ _ / / // / _ \/ '_/
/____/_/\_, /_//_/_/\_\
/___/ v0.5.1 on NodeMCU
[26015] Connecting to blynk-cloud.com:80
[31016] Connecting to blynk-cloud.com:80
Blynk NOT connected!
WiFi disconnecting
.softAPSSID1 local values espIP=192.168.4.27 espGateway=192.168.4.9
............SoftAP connection successful!
WiFi disconnecting
ROUTER local values espIP=192.168.0.29 espGateway=192.168.0.1
............[54863]
___ __ __
/ _ )/ /_ _____ / /__
/ _ / / // / _ \/ '_/
/____/_/\_, /_//_/_/\_\
/___/ v0.5.1 on NodeMCU
[54864] Connecting to blynk-cloud.com:80
[59865] Connecting to blynk-cloud.com:80
Blynk NOT connected!