Skip to content

Code Request - reset needed for WiFi reconnect #627

@mickeypop

Description

@mickeypop

code request

A minor bug I noticed

My WiFi gets bumped occasionally for a few seconds and the LCD changes to the disconnect status and only a reset reconnects.

An "if then else" approach in loop() fixes this and makes for an auto-reconnect.

I have used this example structure for about 10 years with good luck.
Though written for Arduino with adaptation it should work well.

void loop()
{
  if ( WiFi.status() ==  WL_CONNECTED )      // Main connected loop
  { 
	// ANY MAIN LOOP CODE HERE

  }  	// END Main connected loop()
  else
  {  	// WiFi DOWN - write wifi down status... reconnecting to LCD

     WFstatus = getWifiStatus( WFstatus );   // get current state

     WiFi.begin( SSID , Password );		// reconnect
     int WLcount = 0;
     while (WiFi.status() != WL_CONNECTED && WLcount < 200 )  	// while, could be rewriten for LCD display
     {
      delay( 100 );
         Serial.printf(".");
         if (UpCount >= 60)  // keep from scrolling sideways forever
         {
            UpCount = 0;
               Serial.printf("\n");
         }
         ++UpCount;
         ++WLcount;
     }
    if( getWifiStatus( WFstatus ) == 3 )   //wifi returns
    { 
		// clear WiFi down status
    }
     delay( 500 );
  }  // END WiFi down 
} // END loop()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions