Advanced Users can post their questions and comments here for the not so Newbie crowd.

Moderator: eriksl

User avatar
By lucasromeiro
#87026 Hello, I have faced a problem with the stability of my internet connection if the wifi is weak.
I noticed that when ESP12F (node MCU) is connected to a network with a very weak signal it transmits information for a few minutes and sometimes hours. Then he can no longer connect to the wifi. The connection is lost and I cannot re-establish it.
The only way to reestablish the connection is to do a soft reset or hard reset. I've tried to reconfigure and reset everything regarding wifi. But it does not work.
In this way, I imagine that by doing an RF RECALIBRATION I can get the connection. I am guessing that when restarting the RF it recalibrates and is able to connect to wifi, for this reason it only works when I restart.

Does anyone suggest something to me?
Does anyone know how to do an RF recalibration in code time without restarting esp?


Thank you!
User avatar
By prairiedog
#87315 In my main loop, I look at WiFi connection status and if it goes bad, after 2 minutes I reconnect. This is working for me with loss of WiFi due to poor signal. You can check signal strength RSSI:
Code: Select all 
String ptr;
ptr ="Signal RSSI: ";
ptr+=String(WiFi.RSSI());
ptr+="dBm, ";
Serial.println (ptr);


I lose connection below -90dBm and worse now with rain or tree leaves.

Code: Select allunsigned char WiFi_Connect(void) {

   unsigned char timeout_count=0;
   unsigned char result=99;
 
   // Connecting to WiFi network
   DEBUG_PRINTLN();   DEBUG_PRINT("Attempting to connect to WPA network: ");
   DEBUG_PRINTLN(ssid);
   WiFi.mode(WIFI_STA); // station mode, not software access point
   WiFi.disconnect(); // shutdown any prev connection
   delay(200);

   WiFi.begin(ssid, password); // listen for incoming connections
   while ((result != WL_CONNECTED) && (timeout_count < 40)) {
      result = WiFi.status();
      DEBUG_PRINTDEC(result); DEBUG_PRINT(".");
      delay(1000);
      timeout_count++;
   }  // end while
   if (timeout_count >= 30) {
      DEBUG_PRINTLN(" WiFi Connect Timeout");
      // we did not connect, return and allow pgm to continue, not hang
      result= false;
   }
   else {
      DEBUG_PRINTLN(); DEBUG_PRINT("* WiFi connected *  ");
      DEBUG_PRINT("IP address: "); DEBUG_PRINT(WiFi.localIP());
      server.on("/", handle_Root); //Call the 'handleRoot' function when a client requests URI "/"
      server.on("/LED", HTTP_POST, handleButton);  // Call the 'handleButton' function when a POST request is made to URI "/LED"
      server.onNotFound(handle_NotFound); // 404 exception handler
      // Starting the web server
      server.begin(); DEBUG_PRINTLN(" HTTP server started");
      result= true;
   }
   return(result);



//-------------------------------------------------------------
// in MAIN
   if (WiFi.status() != WL_CONNECTED) {  // if we are not connected to the host
      wifi_loss++; // incr loss counter
   }
   else wifi_loss = 0; // clear loss counter
   
   if (wifi_loss >= 120) { // over 60 times is about 1 minute 60 sec., 5 min is 300
      DEBUG_PRINTLN("*** WiFi Off-line ***"); WiFi.printDiag(Serial); DEBUG_PRINTLN();
      if (WiFi_Connect() == false) { // try restore wifi connection
         DEBUG_PRINTLN("*** WiFi Recovery Fail! ***");
         WiFi.printDiag(Serial);
         DEBUG_PRINTLN();
      }
      wifi_loss = 0; // reset loss counter
 }
User avatar
By lucasromeiro
#87318
prairiedog wrote:In my main loop, I look at WiFi connection status and if it goes bad, after 2 minutes I reconnect. This is working for me with loss of WiFi due to poor signal. You can check signal strength RSSI:
Code: Select all 
String ptr;
ptr ="Signal RSSI: ";
ptr+=String(WiFi.RSSI());
ptr+="dBm, ";
Serial.println (ptr);


I lose connection below -90dBm and worse now with rain or tree leaves.

Code: Select allunsigned char WiFi_Connect(void) {

   unsigned char timeout_count=0;
   unsigned char result=99;
 
   // Connecting to WiFi network
   DEBUG_PRINTLN();   DEBUG_PRINT("Attempting to connect to WPA network: ");
   DEBUG_PRINTLN(ssid);
   WiFi.mode(WIFI_STA); // station mode, not software access point
   WiFi.disconnect(); // shutdown any prev connection
   delay(200);

   WiFi.begin(ssid, password); // listen for incoming connections
   while ((result != WL_CONNECTED) && (timeout_count < 40)) {
      result = WiFi.status();
      DEBUG_PRINTDEC(result); DEBUG_PRINT(".");
      delay(1000);
      timeout_count++;
   }  // end while
   if (timeout_count >= 30) {
      DEBUG_PRINTLN(" WiFi Connect Timeout");
      // we did not connect, return and allow pgm to continue, not hang
      result= false;
   }
   else {
      DEBUG_PRINTLN(); DEBUG_PRINT("* WiFi connected *  ");
      DEBUG_PRINT("IP address: "); DEBUG_PRINT(WiFi.localIP());
      server.on("/", handle_Root); //Call the 'handleRoot' function when a client requests URI "/"
      server.on("/LED", HTTP_POST, handleButton);  // Call the 'handleButton' function when a POST request is made to URI "/LED"
      server.onNotFound(handle_NotFound); // 404 exception handler
      // Starting the web server
      server.begin(); DEBUG_PRINTLN(" HTTP server started");
      result= true;
   }
   return(result);



//-------------------------------------------------------------
// in MAIN
   if (WiFi.status() != WL_CONNECTED) {  // if we are not connected to the host
      wifi_loss++; // incr loss counter
   }
   else wifi_loss = 0; // clear loss counter
   
   if (wifi_loss >= 120) { // over 60 times is about 1 minute 60 sec., 5 min is 300
      DEBUG_PRINTLN("*** WiFi Off-line ***"); WiFi.printDiag(Serial); DEBUG_PRINTLN();
      if (WiFi_Connect() == false) { // try restore wifi connection
         DEBUG_PRINTLN("*** WiFi Recovery Fail! ***");
         WiFi.printDiag(Serial);
         DEBUG_PRINTLN();
      }
      wifi_loss = 0; // reset loss counter
 }


Hello, I already do that.
I already have a check and reconnect routine.
But it doesn't always work.
I realized that ALWAYS that reset works!
I believe it is due to wifi calibration.
Procedures that only occur when ESP restarts.