How to get current gateway IP address on ESP8266/ESP32

In order to get the current gateway IP address on the ESP8266 or ESP32, use:

get_gateway.cpp
WiFi.gatewayIP();

In order to print the gateway IP address on the serial port, use

get_gateway.cpp
Serial.println("Gateway IP address: ");
Serial.println(WiFi.gatewayIP());

In order to get the gateway IP address as string, use

get_gateway.cpp
String gatewayIP = WiFi.gatewayIP().toString();

Check out similar posts by category: C/C++, Embedded, ESP8266/ESP32, PlatformIO