I did a mistake in posting a new issue in an older thread so I am starting over with a new thread here:
I have struck a problem that might be causing my WiFi problems:
It seems like some routers, for instance ASUS RT-AC68U(which I use) will drop the connection after it has been connected!!!! And it seems to be apparent on Espressif platform 8266 after version 2.5.0
See:
So In one of the googled threads (cannot find it anymore) was suggested that the espressif esp8266 platform which is now at version 4.0.1 does not work correctly towards ASUS routers since version 2.5.0.
So I have tried backing this platform to version 2.5.0 by using this in platformio.ini (I am using VSCode + PlatformIO):
But that did not seem to solve the problem.
So someone suggested that I should restart the ASUS router, which I did even though that disrupts a lot of my running services.
After the restart of the router things seemed to stabilize and the connection came back up, but then after a while it was all back again!!!!
Is anyone here aware of any way to handle the connection issues correctly?
NOTE:
I switched off the ESP8266 device last night when it would not ever connect properly and went to bed.
Now in the morning 8 hours later when I power it back on it connects instantly and continues to work!
What can be done about this problematic behavior????
This is what I do in my sketch to connect to WiFi (called from setup():
what you have posted is not a complete sketch. It is just one function.
If I shall answer based on this small piece of code the answer is:
You are doing just one single attempt to connect to the WiFi and if it takes a little bit longer to connect your code is executing the
Without seeing the rest of your code your potential helpers are unable to give advice how to modify your code to make it work more reliably. Except for the general advice
of coding something that does re-try to connect giving some time for the ESP8266 trying to do so.
I have decided to write the "tip" in such a short way to put you into the situation you are putting your potential helpers in.
With too few information and too less context a short hint is not as helpful as a detailed description would be.
In your case this is to post your complete sketch.
If you don't want to post your complete sketch reduce your code to a minimum example that re-produces the bug.
I have adapted it to PlatformIO by importing the Arduino sketch using the built-in import of PlatformIO.
Then I modified it by disabling WiFiManager in setup() by not calling its start code and instead used my code as shown.
I also used fixed values for ssid and password in the connect part to get away from the extra WiFiManager parameter entries, which one cannot reach once WiFiManager has been set up the first time...
So what I want to discuss here is simply the little code snippet SetupWiFiManual() above.
Meanwhile I have discussed the problem with another developer who also had problems with the device not wanting to reliably connect to an ASUS router. This is also described on the net, but few solutions.
Anyway I have now done the following because some Internet discussions suggest that the last platform code that works well with ASUS routers is 2.5.2.:
Platformio.ini changes:
[env:esp07s_OTA]
platform = espressif8266@2.5.2
;everything else is the same here
This by itself did not solve the issue, the device would not reliably connect anyway.
Then I also modified the connection function to add WiFi.setPhyMode():
What this does is restrict the WiFi connection protocol to use g rather than freely negotiating it.
So far this seems to have solved the problem and it performs like before (apart from the connection issues) when using platform 2.5.2.
Next I tested by returning to using the latest platform version from Expressif by removing @2.5.2 from the platform line in platformio.ini.
This also makes the device connect instantly and start working, however instead it has destroyed the Serial operations so there are no longer any debug prints being sent at all (or maybe sent but at the wrong baud rate)...
So the main issue seems to have been solved but now I have a Serial comm issue instead.
Notice that when the device is running I can still send commands to it via serial at 115200 baud and the device reads that and acts properly by sending the MQTT responses etc. BUT the data sent in the other direction do not show up..
So I had to switch back to 2.5.2 to be able to debug my code using Serial.println() etc.
Happens with every router. There is no such thing as a reliable WiFi connection. You have to write code that reconnects when the connection is dropped.
doesn't work each time.
if you look up almost any other connect-to-wifi-code you will find a loop that
uses a different method that includes a time-out that is coded on the user-code-level not inside the WiFi-library level
void ConnectToWiFi() {
int myCount = 0;
#if defined(ESP32)
WiFi.setHostname(AP_hostname); // xxy
#else
WiFi.hostname(AP_hostname);
#endif
// try to connect to existing network
WiFi.begin(home_ssid, home_password);
Serial.print("\n\nTry to connect to existing network");
Serial.print(" named #");
Serial.print(home_ssid);
Serial.println("#");
// Wait for connection
while (WiFi.status() != WL_CONNECTED && myCount < 31) {
yield(); // very important to execute yield to make it work
BlinkHeartBeatLED(OnBoard_LED, 50); // blink LED fast during attempt to connect
if ( TimePeriodIsOver(MyTestTimer, 500) ) { // once every 500 miliseconds
Serial.print("."); // print a dot
myCount++;
if (myCount > 30) { // after 30 dots = 15 seconds
Serial.println();
Serial.print("not connected ");
}
}
}
if (WiFi.status() == WL_CONNECTED ) {
Serial.println("");
Serial.print("Connected to #");
Serial.print(home_ssid);
Serial.print("# IP address: ");
Serial.println(WiFi.localIP());
}
}