ESP32 WiFi Library – WiFi.gatewayIP

Home / References / ESP32 Library / WiFi API / Station

Description

The gatewayIP method retrieves the IP address of the gateway (router) of the currently connected Wi-Fi network. This method is useful for network configuration and troubleshooting.


Syntax and Usage

This method returns the IP address of the gateway of the network to which the ESP32 is connected.

IPAddress gateway = WiFi.gatewayIP();

This method does not accept any arguments and simply returns the gateway IP address as an IPAddress object.


Argument(s)

This method does not accept any arguments.


Return Value

The gatewayIP method returns an IPAddress object that contains the IP address of the gateway of the connected Wi-Fi network. If the ESP32 is not connected to a Wi-Fi network, this method will return 0.0.0.0.


Example Codes

Retrieve and print the gateway IP address

This code demonstrates how to use the ESP32 WiFi Library’s gatewayIP() method to retrieve the IP address of the network gateway. After connecting to a WiFi network using WiFi.begin(), the code fetches the gateway IP address using WiFi.gatewayIP() and prints it to the Serial Monitor. This is useful for identifying the router or gateway that connects the ESP32 to the internet.

To use this code, replace “your_SSID” and “your_PASSWORD” with your WiFi network’s SSID and password. Upload the code to your ESP32 board and open the Serial Monitor at 115200 baud. The code will connect to the WiFi network and display the gateway IP address. Ensure the ESP32 is within range of the WiFi signal for a successful connection.

/*
 * Author: Avant Maker
 * Date: February 8, 2025
 * Version: 1.0
 * Description: This code demonstrates how to connect an ESP32 
 * to a WiFi network and retrieve the gateway IP address of the connected 
 * network using the ESP32's WiFi library's WiFi.gatewayIP().
 * License: MIT 
 * 
 * Code Source: This example code is sourced from the Comprehensive Guide
 * to the ESP32 Arduino Core Library, accessible on AvantMaker.com. 
 * For additional code examples and in-depth documentation related to 
 * the ESP32 Arduino Core Library, please visit:
 *
 * https://avantmaker.com/references/esp32-arduino-core-index/
 *
 * AvantMaker.com, your premier destination for all things DIY, AI, IoT, 
 * Smart Home, and STEM projects. We are dedicated to empowering makers,
 * learners, and enthusiasts with the resources they need to bring their 
 * innovative ideas to life.
 */

#include <WiFi.h>

const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";

void setup() {
    Serial.begin(115200);
    WiFi.begin(ssid, password);

    Serial.println("Connecting to WiFi...");
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    Serial.println("\nConnected to WiFi!");

    // Retrieve and print the gateway IP address
    IPAddress gateway = WiFi.gatewayIP();
    Serial.print("Gateway IP Address: ");
    Serial.println(gateway);
}

void loop() {
    // Your code here
}

AvantMaker Tutorial Suggestion

If you want to learn more about the ESP32’s WiFi capabilities, we’ve created a beginner-friendly guide that explains them in detail. Just click the link below, and you’ll be teleported to that page. It will clear up any confusion you may have while connecting your ESP32 to WiFi or setting it up as a WiFi access point (AP).

ESP32 Essential Guide – Network Section – WiFi Chapter

ESP32 Library Index

ESP32 Arduino Core Library


FAQ

Ready to experiment and explore more about ESP32? Visit our website’s All About ESP32 Resources Hub, packed with tutorials, guides, and tools to inspire your maker journey. Experiment, explore, and elevate your skills with everything you need to master this powerful microcontroller platform!

error: Content is protected !!