IP Doesn't Update After Reconnect

(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.

Assuming you are using fixed IP it would not change. If you are using DHCP it is the DHCP server that determines the address. In your system it is probably the router.

Okay, how do I get the new IP?

I am not familiar with all of the libraries but somebody can correct this. This is taken from a sketch I did a while back. What I learned is in order to get a new IP address on an Arduino using DHCP, you first have to release the current IP then request a new one. The I am familiar libraries like the Ethernet library do not have a direct function to release the IP address. Instead, you need to reset the network connection to trigger a DHCP request for a new IP address. Try this and see if it works for you.

#include <SPI.h>
#include <Ethernet.h>

// MAC address and buffer for IP address change to your MAC
byte mac[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
IPAddress ip;

void setup() {
  Serial.begin(9600);
  // Start the Ethernet connection
  Serial.println("Starting Ethernet...");
  Ethernet.begin(mac);
  delay(1000);
  
  // Print the current IP address
  printIPAddress();
  
  // Attempt to get a new IP address
  getNewIPAddress();
}

void loop() {
  // put your main code here, to run repeatedly:
}

void getNewIPAddress() {
  // Stop the Ethernet connection to release the current IP
  Ethernet.end();
  Serial.println("Releasing IP...");
  delay(2000);   // Delay to simulate IP release (optional, may need to be adjusted)

  // Now restart the Ethernet connection so you can get a new IP
  Serial.println("Requesting new IP...");
  Ethernet.begin(mac);
  delay(1000);

  printIPAddress();    // Show off print the new IP address
}

void printIPAddress() {
  ip = Ethernet.localIP();
  Serial.print("Current IP Address: ");
  Serial.println(ip);
}

I do not remember testing this but it should work. Try and let us know how you did.

The end() function in WiFiNINA works! Thanks!

You are welcome, mark it solved.