(from WiFiNINA) WiFi.localIP() doesn't update after reconnnect. When it disconnect from one subnet (for example 192.168.137.xxx) and reconnect to another (192.168.138.xxx, of the same ssid and password), its localIP stays the same.
My wifi routine that's called repeatedly
void WiFi_ROUTINE()
{
if(micros() - routine_timestamp < routine_interval) return;
routine_timestamp = micros();
WiFi_status = WiFi.status();
switch(WiFi_state)
{
case 0: // begin (disconnected, reconnect)
{
WiFi.begin(ssid, pass); // connect to WPA/WPA2 network
WiFi_state = 1; // connecting
WiFi_heartbeat = micros();
WiFi_LED.strobe_soft();
#ifdef web_dbug
Serial.print("\nWiFi begin\nSSID: ");
Serial.print(ssid);
Serial.print("\nPassword: ");
Serial.print(pass);
#endif
break;
}
case 1: // connecting (begin to connected transition)
{
if( micros() - WiFi_heartbeat > WiFi_timeout_connecting )
{
if(WiFi_status == WL_CONNECTED) // success
{
WiFi_server_setup();
WiFi_state = 3;
WiFi_LED_sequence[1] = WiFi_IP[2]/100;
WiFi_LED_sequence[2] = (WiFi_IP[2]/10)%10;
WiFi_LED_sequence[3] = WiFi_IP[2]%10;
WiFi_LED.assign(WiFi_LED_sequence, 4); // assign sequence, length
#ifdef web_dbug
Serial.print("\nWiFi connected");
#endif
}
else // WL_IDLE_STATUS or WL_CONNECT_FAILED
{
WiFi_heartbeat = micros();
WiFi_state = 2;
#ifdef web_dbug
Serial.print("\nWiFi fails to connect");
#endif
}
}
break;
}
case 2: // fail to connect (connecting to begin transition)
{
// wait for a while before retry
if(micros() - WiFi_heartbeat > WiFi_timeout_fail_to_connect)
{
#ifdef web_dbug
Serial.print("\nWiFi retry");
#endif
WiFi_state = 0;
}
break;
}
case 3: // connected (runs SERVER and detect disconnection)
{
WiFi_heartbeat = micros();
if(WiFi_status == WL_CONNECTED)
{
//server
WiFi_SERVER();
}
else // disconnected
{
WiFi_state = 2;
#ifdef web_dbug
Serial.print("\nWiFi disconnected");
#endif
}
break;
}
case 4: // idle
{
if(WiFi_status == WL_CONNECTED) // disconnect
{
WiFi.disconnect();
//WiFi.end();
}
break;
}
}
}
Where the some variables and the IP setup function are:
WiFiServer webServer(80);
WiFiClient webClient;
IPAddress WiFi_IP;
int WiFi_status = WL_IDLE_STATUS;
int WiFi_state = 0;
unsigned long WiFi_heartbeat = 0; // beat when connected
unsigned long routine_timestamp = 0;
// server setup: set up IP, gateway, and starts webserver
void WiFi_server_setup()
{
WiFi_IP = WiFi.localIP();
WiFi_IP[3] = WiFi_IP_byte3;
WiFi.config(WiFi_IP, WiFi.gatewayIP(), WiFi.gatewayIP(), WiFi.subnetMask());
webServer.begin();
#ifdef web_dbug
WiFi_print_config();
#endif
}
Update: gatewayIP() also doesn't update.