ESP8266 NodeMCU Static/Fixed IP Address (Arduino IDE) | Random Nerd Tutorials (2024)

This tutorial shows how to set a static/fixed IP address for your ESP8266 NodeMCU board. If you’re running a web server or Wi-Fi client with your ESP8266 and every time you restart your board, it has a new IP address, you can follow this tutorial to assign a static/fixed IP address.

ESP8266 NodeMCU Static/Fixed IP Address (Arduino IDE) | Random Nerd Tutorials (1)

Static/Fixed IP Address Sketch

To show you how to fix your ESP8266 IP address, we’ll use the ESP8266 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 ESP8266 board, it should automatically assign the fixed IP address 192.168.1.184.

/********* Rui Santos Complete project details at https://RandomNerdTutorials.com/esp8266-nodemcu-static-fixed-ip-address-arduino/*********/// Load Wi-Fi library#include <ESP8266WiFi.h>// Replace with your network credentialsconst char* ssid = "REPLACE_WITH_YOUR_SSID";const char* password = "REPLACE_WITH_YOUR_PASSWORD";// Set web server port number to 80WiFiServer server(80);// Variable to store the HTTP requestString header;// Auxiliar variables to store the current output stateString output5State = "off";String output4State = "off";// Assign output variables to GPIO pinsconst int output5 = 5;const int output4 = 4;// Current timeunsigned long currentTime = millis();// Previous timeunsigned long previousTime = 0; // Define timeout time in milliseconds (example: 2000ms = 2s)const long timeoutTime = 2000;// Set your Static IP addressIPAddress local_IP(192, 168, 1, 184);// Set your Gateway IP addressIPAddress gateway(192, 168, 1, 1);IPAddress subnet(255, 255, 0, 0);IPAddress primaryDNS(8, 8, 8, 8); //optionalIPAddress secondaryDNS(8, 8, 4, 4); //optionalvoid setup() { Serial.begin(115200); // Initialize the output variables as outputs pinMode(output5, OUTPUT); pinMode(output4, OUTPUT); // Set outputs to LOW digitalWrite(output5, LOW); digitalWrite(output4, 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 currentTime = millis(); previousTime = currentTime; while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected currentTime = millis(); 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 /5/on") >= 0) { Serial.println("GPIO 5 on"); output5State = "on"; digitalWrite(output5, HIGH); } else if (header.indexOf("GET /5/off") >= 0) { Serial.println("GPIO 5 off"); output5State = "off"; digitalWrite(output5, LOW); } else if (header.indexOf("GET /4/on") >= 0) { Serial.println("GPIO 4 on"); output4State = "on"; digitalWrite(output4, HIGH); } else if (header.indexOf("GET /4/off") >= 0) { Serial.println("GPIO 4 off"); output4State = "off"; digitalWrite(output4, 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: #195B6A; border: none; color: white; padding: 16px 40px;"); client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}"); client.println(".button2 {background-color: #77878A;}</style></head>"); // Web Page Heading client.println("<body><h1>ESP8266 Web Server</h1>"); // Display current state, and ON/OFF buttons for GPIO 5 client.println("<p>GPIO 5 - State " + output5State + "</p>"); // If the output5State is off, it displays the ON button if (output5State=="off") { client.println("<p><a href=\"/5/on\"><button class=\"button\">ON</button></a></p>"); } else { client.println("<p><a href=\"/5/off\"><button class=\"button button2\">OFF</button></a></p>"); } // Display current state, and ON/OFF buttons for GPIO 4 client.println("<p>GPIO 4 - State " + output4State + "</p>"); // If the output4State is off, it displays the ON button if (output4State=="off") { client.println("<p><a href=\"/4/on\"><button class=\"button\">ON</button></a></p>"); } else { client.println("<p><a href=\"/4/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(""); }}

View raw code

Setting Your Network Credentials

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

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

Setting ESP8266 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 192.168.1.184 that works in the gateway 192.168.1.1.

// Set your Static IP addressIPAddress local_IP(192, 168, 1, 184);// Set your Gateway IP addressIPAddress gateway(192, 168, 1, 1);IPAddress subnet(255, 255, 0, 0);IPAddress primaryDNS(8, 8, 8, 8); // optionalIPAddress secondaryDNS(8, 8, 4, 4); // optional

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

setup()

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

// Configures static IP addressif (!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 the baud rate 115200, restart your ESP8266 board and the IP address defined earlier should be assigned to your board.

As you can see, it prints the IP address 192.168.1.184.

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

Assigning IP Address with MAC Address

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

Upload the following code to the ESP8266 board:

// Complete Instructions to Get and Change ESP MAC Address: https://RandomNerdTutorials.com/get-change-esp32-esp8266-mac-address-arduino/#ifdef ESP32 #include <WiFi.h>#else #include <ESP8266WiFi.h>#endifvoid setup(){ Serial.begin(115200); Serial.println(); Serial.print("ESP Board MAC Address: "); Serial.println(WiFi.macAddress());} void loop(){}

View raw code

In the setup(), it prints the ESP8266 MAC Address in the Serial Monitor:

// Print ESP MAC AddressSerial.println("MAC address: ");Serial.println(WiFi.macAddress());
ESP8266 NodeMCU Static/Fixed IP Address (Arduino IDE) | Random Nerd Tutorials (3)

In our case, the ESP8266 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 available routers.

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 ESP8266 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 ESP8266. If you have an ESP32, you can read this guide Set ESP32 Static/Fixed IP Address.

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

  • Home Automation Using ESP8266
  • MicroPython Programming with ESP32 and ESP8266
  • More ESP8266 NodeMCU Projects and Tutorials …

Thanks for reading.

ESP8266 NodeMCU Static/Fixed IP Address (Arduino IDE) | Random Nerd Tutorials (2024)
Top Articles
Latest Posts
Article information

Author: Carlyn Walter

Last Updated:

Views: 5582

Rating: 5 / 5 (50 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Carlyn Walter

Birthday: 1996-01-03

Address: Suite 452 40815 Denyse Extensions, Sengermouth, OR 42374

Phone: +8501809515404

Job: Manufacturing Technician

Hobby: Table tennis, Archery, Vacation, Metal detecting, Yo-yoing, Crocheting, Creative writing

Introduction: My name is Carlyn Walter, I am a lively, glamorous, healthy, clean, powerful, calm, combative person who loves writing and wants to share my knowledge and understanding with you.