Annoyed by changing IP addresses? The solution: Assign your ESP32 a Static IP address!
By default, the ESP32 receives a dynamic IP address from the router, which can change each time the device connects.
In this guide, you’ll learn how to assign a static IP address to your ESP32 using the Arduino IDE!
Why Assign a Static IP Address to Your ESP32?
When the ESP32 connects to a Wi-Fi network, the router typically assigns an IP address dynamically through DHCP (Dynamic Host Configuration Protocol). This means that if the ESP32 restarts, it will probably be assigned a different IP address.
However, in some cases, we need a fixed IP address that stays the same even after restarting the device or switching networks. For example, you would want your Web Server, to always be available under the same address.
That’s where a static IP address comes in handy. Luckily, the ESP32 allows us to specify a fixed IP address. When it connects to a Wi-Fi network, it requests this specific IP address.
Check the Current Network Settings
When setting a static IP, we also need to specify the standard gateway and subnet mask. Therefore, it can be useful to check the current network settings in order to find out what your gateway and subnet mask are.
Check out how IP addresses work here.
Use the following Arduino Sketch to check your current network settings:
#include <WiFi.h>
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
void setup() {
Serial.begin(115200);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
Serial.print("Connecting to Wi-Fi . . .");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(" .");
}
Serial.println(" Connected!");
Serial.print("Current ESP32 IP: ");
Serial.println(WiFi.localIP());
Serial.print("Gateway (router) IP: ");
Serial.println(WiFi.gatewayIP());
Serial.print("Subnet Mask: " );
Serial.println(WiFi.subnetMask());
}
void loop() {}
In case you haven’t set the Arduino IDE up to work with the ESP32, here’s how to install the ESP32 in Arduino IDE.
How to Assign a Static IP Address to ESP32 in Arduino IDE
To configure the ESP32 with a static IP, you need to use the WiFi.config() function from the WiFi library in the Arduino IDE.
Use the following code example to connect to your Wi-Fi network with a static IP:
#include <WiFi.h>
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
IPAddress staticIP(192, 168, 1, 100); // desired static IP
IPAddress gateway(192, 168, 1, 1); // gateway IP
IPAddress subnet(255, 255, 255, 0); // subnet mask
void setup() {
Serial.begin(115200);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
WiFi.config(staticIP, gateway, subnet);
Serial.print("Connecting to Wi-Fi . . .");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(" .");
}
Serial.println("\nConnected to Wi-Fi.");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
}
void loop() {}
Notice how we included the WiFi library of the Arduino IDE. That’s the only library we need for assigning a static IP.
#include <WiFi.h>
Specify the SSID and password of the Wi-Fi network you want to connect to.
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
Next, we set our gateway, subnet mask, and, most importantly, the static IP we want to assign to our microcontroller.
IPAddress staticIP(192, 168, 1, 100); // desired static IP
IPAddress gateway(192, 168, 1, 1); // gateway IP
IPAddress subnet(255, 255, 255, 0); // subnet mask
Now, we connect to the Wi-Fi network and apply our configuration with WiFi.begin() and WiFi.config().
WiFi.begin(ssid, password);
WiFi.config(staticIP, gateway, subnet);
When running the full code and requesting a static IP from your router was successful, you can see your specified IP in the serial monitor.
ESP32 Static IP: Troubleshooting Common Issues
My ESP has the correct IP, but I can’t access it
If you can’t access your ESP32 even though it is assigned the correct IP address, there might be an IP address conflict. This means, that there is another device in the same network with the same IP address. Hence, the traffic of the two devices can be mixed up.
Check whether there is a device in your network that already has the IP address you are trying to assign.
Can’t connect to Wi-Fi
When your microcontroller doesn’t connect to Wi-Fi or is stuck trying to connect, check whether your Wi-Fi credentials (SSID and password) and whether your standard gateway and subnet mask are correct.
Wrapping Up
Assigning a static IP address to your ESP32 helps maintain consistent network access for web servers and IoT applications. With the Arduino IDE and a few lines of code, you can easily configure your microcontroller to use a fixed IP address.
Also, check out how to read & change the MAC address of your ESP32.
What do you need a static IP for? Share your experiences in the comments below!
Thanks for reading!
Links marked with an asterisk (*) are affiliate links which means we may receive a commission for purchases made through these links at no extra cost to you. Read more on our Affiliate Disclosure Page.