The tutorial below shows you how to set a static/fixed IP address for your ESP32 board. You may use this tutorial to assign a static/fixed IP address if your ESP32 is running a web server or a Wi-Fi client and the IP address changes every time you restart the board.
Static/Fixed IP Address Sketch
We'll use the ESP32 Web Server code as an example to show you how to fix your ESP32 IP address. You should be able to fix your IP address by the conclusion of our discussion, regardless of the web server or Wi-Fi project you're working on.
Copy the following code to your Arduino IDE, but do not upload it yet. To make it work for you, you'll need to make some changes.
Note: If you upload the next sketch to your ESP32 board, it should assign the fixed IP address 192.168.1.184 automatically.
/*********
LEDEdit PRO
Complete project details at https://lededitpro.com/esp32-static-fixed-ip-address/
*********/
// Load Wi-Fi library
#include <WiFi.h>
// Replace with your network credentials
const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";
// Set web server port number to 80
WiFiServer server(80);
// Variable to store the HTTP request
String header;
// Auxiliar variables to store the current output state
String output26State = "off";
String output27State = "off";
// Assign output variables to GPIO pins
const int output26 = 26;
const int output27 = 27;
// 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
void setup() {
Serial.begin(115200);
// Initialize the output variables as outputs
pinMode(output26, OUTPUT);
pinMode(output27, OUTPUT);
// Set outputs to LOW
digitalWrite(output26, LOW);
digitalWrite(output27, LOW);
// Configures static IP address
if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
Serial.println("STA Failed to configure");
}
// Connect to Wi-Fi network with SSID and password
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Print local IP address and start web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
}
void loop(){
WiFiClient client = server.available(); // Listen for incoming clients
if (client) { // If a new client connects,
Serial.println("New Client."); // print a message out in the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
header += c;
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
// turns the GPIOs on and off
if (header.indexOf("GET /26/on") >= 0) {
Serial.println("GPIO 26 on");
output26State = "on";
digitalWrite(output26, HIGH);
} else if (header.indexOf("GET /26/off") >= 0) {
Serial.println("GPIO 26 off");
output26State = "off";
digitalWrite(output26, LOW);
} else if (header.indexOf("GET /27/on") >= 0) {
Serial.println("GPIO 27 on");
output27State = "on";
digitalWrite(output27, HIGH);
} else if (header.indexOf("GET /27/off") >= 0) {
Serial.println("GPIO 27 off");
output27State = "off";
digitalWrite(output27, LOW);
}
// Display the HTML web page
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<link rel=\"icon\" href=\"data:,\">");
// CSS to style the on/off buttons
// Feel free to change the background-color and font-size attributes to fit your preferences
client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
client.println(".button { background-color: #4CAF50; border: none; color: white; padding: 16px 40px;");
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
client.println(".button2 {background-color: #555555;}</style></head>");
// Web Page Heading
client.println("<body><h1>ESP32 Web Server</h1>");
// Display current state, and ON/OFF buttons for GPIO 26
client.println("<p>GPIO 26 - State " + output26State + "</p>");
// If the output26State is off, it displays the ON button
if (output26State=="off") {
client.println("<p><a href=\"/26/on\"><button class=\"button\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/26/off\"><button class=\"button button2\">OFF</button></a></p>");
}
// Display current state, and ON/OFF buttons for GPIO 27
client.println("<p>GPIO 27 - State " + output27State + "</p>");
// If the output27State is off, it displays the ON button
if (output27State=="off") {
client.println("<p><a href=\"/27/on\"><button class=\"button\">ON</button></a></p>");
} else {
client.println("<p><a href=\"/27/off\"><button class=\"button button2\">OFF</button></a></p>");
}
client.println("</body></html>");
// The HTTP response ends with another blank line
client.println();
// Break out of the while loop
break;
} else { // if you got a newline, then clear currentLine
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
// Clear the header variable
header = "";
// Close the connection
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
}
Setting Your Network Credentials
You need to modify the following lines with your network's 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 of the setup()
and loop()
procedures declare the following variables with your own static IP address and gateway IP address:
The next code, by default, assigns the IP address 192.168.1.184, which works with the gateway 192.168.1.1.
// 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
Important: On your local network, you need to use the corresponding gateway and an available IP address.
setup()
You need to assign the configurations to your ESP32 in setup()
by using WiFi.config()
method.
// 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.
Testing
After uploading the code to your board, open the Arduino IDE Serial Monitor at 115200 baud rate, restart your ESP32 board, and the IP address you specified earlier should be assigned to your board.
As you can see, it prints the IP address 192.168.1.184.
To assign a fixed IP address to your ESP32, you may take this example and add it to all of your Wi-Fi sketches.
Assigning IP Address with MAC Address
If you tried using the previous example to assign a fixed IP address to the ESP8266 and it didn't work, we recommend assigning an IP address directly in your router settings using the ESP32 MAC Address.
Add your network credentials (SSID and password). Then, upload the next code to your ESP32:
/*********
LEDEdit PRO
Complete Instructions to Get and Change ESP MAC Address: https://lededitpro.com/get-mac-address-of-esp32-esp8266-arduino-ide/
*********/
// Load Wi-Fi library
#include <WiFi.h>
// Replace with your network credentials
const char* ssid = "REPLACE_WITH_YOUR_SSID";
const char* password = "REPLACE_WITH_YOUR_PASSWORD";
// Set web server port number to 80
WiFiServer server(80);
void setup() {
Serial.begin(115200);
// Connect to Wi-Fi network with SSID and password
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Print local IP address and start web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
// Print ESP MAC Address
Serial.println("MAC address: ");
Serial.println(WiFi.macAddress());
}
void loop() {
// put your main code here, to run repeatedly:
}
It prints the ESP32 MAC Address on the Serial Monitor in setup()
:
// Print ESP MAC Address
Serial.println("MAC address: ");
Serial.println(WiFi.macAddress());
In our example, the MAC Address of the ESP32 is “B4:E6:2D:97:EE:F1“. You'll need the MAC Address in just a bit, so copy it.
Router Settings
When you login to your router's admin page, you should see a page or menu where you can assign an IP address to a network device. Each router has its own set of menus and configurations. As a result, we are unable to give instructions for all available routers.
We recommend searching “assign IP address to MAC address” followed by the name of your router. You should be able to find some instructions that show you how to assign an IP address to a MAC address for your specific router.
In summary, you should be able to assign your desired IP address to your ESP32 MAC address (for example, B4:E6:2D:97:EE:F1) by going to your router's configuration menu.
Conclusion
You should be able to assign a fixed/static IP address to your ESP32 after following this tutorial. If you own an ESP8266, you should read this guide: Set ESP8266 Static/Fixed IP Address.
If you like ESP32, you may also like:
- [Resolve] Reconnect ESP32 to Wi-Fi Network After Lost Connection
- Useful Wi-Fi Library Functions for ESP32 (Arduino IDE)
- Set a Custom Hostname for ESP32 Using Arduino IDE
- Door Status Monitor using ESP32 with Email Notifications
We hope you find this tutorial useful. Thanks for reading.