Random Nerd Tutorials

ESP32 Static/Fixed IP Address


If you’re running a web server or Wi-Fi client with your ESP32 and every time you restart your board the ESP32 has a new IP address, you can follow this tutorial to assign a static/fixed IP address to your ESP32 board.


Static/Fixed IP Address Sketch

To show you how to fix your ESP32 IP address, we’ll use the ESP32 Web Sever code as an example. By the end of our explanation you should be able to fix your IP address regardless of the web server or Wi-Fi project you’re building.

Copy the code below to your Arduino IDE, but don’t upload it yet. You need to make some changes to make it work for you.

Note: if you upload the next sketch to your ESP32 board, it should automatically assign the fixed IP address

Setting Your Network Credentials

You need to modify the following lines with your network credentials: SSID and password:

// Replace with your network credentials
const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";

Setting your Static IP Address

Then, outside the setup() and loop() functions, you define the following variables with your own static IP address and corresponding gateway IP address.

By default, the next code assigns the IP address that works in the gateway

// Set your Static IP address
IPAddress local_IP(192, 168, 1, 184);
// Set your Gateway IP address
IPAddress gateway(192, 168, 1, 1);

IPAddress subnet(255, 255, 0, 0);
IPAddress primaryDNS(8, 8, 8, 8); // optional
IPAddress secondaryDNS(8, 8, 4, 4); // optional

The parameters highlighted in red in the previous snippet are the ones you need to change if you want to assign your desired IP address to the ESP32.

Important: you need to use an available IP address in your local network and the corresponding gateway.


In the setup() you need to call the WiFi.config() method to assign the configurations to your ESP32.

// Configures static IP address
if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
  Serial.println("STA Failed to configure");

Note: the primaryDNS and secondaryDNS parameters are optional and you can remove them.


After uploading the code to your board, open the Arduino IDE Serial Monitor at the baud rate 115200, restart your ESP32 board and the IP address defined earlier should be assigned to your board.


As you can see, it prints the IP address

You can take this example and add it to all your Wi-Fi sketches to assign a fixed IP address to your ESP32.

Assigning IP Address with MAC Address

If you’ve tried to assign a fixed IP address to the ESP32 using the previous example and it doesn’t work, we recommend assigning an IP address directly in your router settings through the ESP32 MAC Address.

Add your network credentials (SSID and password). Then, upload the next code to your ESP32:

In the setup(), after connecting to your network, it prints the ESP32 MAC Address in the Serial Monitor:

// Print ESP MAC Address
Serial.println("MAC address: ");

In our case, the ESP32 MAC Address is B4:E6:2D:97:EE:F1. Copy the MAC Address, because you’ll need it in just a moment.

Router Settings

If you login into your router admin page, there should be a page/menu where you can assign an IP address to a network device. Each router has different menus and configurations. So, we can’t provide instructions on how do to it for all the routers available.

We recommend Googling “assign IP address to MAC address” followed by your router name. You should find some instructions that show how to assign the IP to a MAC address for your specific router.

In summary, if you go to your router configurations menu, you should be able to assign your desired IP address to your ESP32 MAC address (for example B4:E6:2D:97:EE:F1).

Wrapping Up

After following this tutorial you should be able to assign a fixed/static IP address to your ESP32.

We hope you’ve found this tutorial useful. If you like ESP32, you may also like:

Thanks for reading.

MicroPython Programming with ESP32/ESP8266 eBook

Learn how to program the ESP boards with MicroPython, including projects, tips, and tricks! The registrations are open for a limited time, so SIGN UP NOW »

Recommended Resources

Home Automation using ESP8266 »
Build IoT projects and home automation gadgets with the ESP8266 Wi-Fi module.

Build a Home Automation System »
Learn how to build a automation system using open-source hardware and software from scratch.

Arduino Step-by-Step Projects »
Build 25 cool Arduino projects with our course even with no prior experience!

Leave a Comment:

Add Your Reply

⏰ Registrations are open for "MicroPython Programming with ESP32 and ESP8266" eBook