ESP8266 Static IP Address arduino Example

In this tutorial we will learn how to use static IP address for ESP8266 NodeMCU. We make LED on off control with simple web server with static IP to our ESP.

Step 1: Required Header files

There is no need of any special header file for having static IP address.

#include <ESP8266WiFi.h>
#include <WiFiClient.h>

//ESP Web Server Library to host a web page
#include <ESP8266WebServer.h>

Step 2: WiFi IP configuration variables

IPAddress variable is used in arduino IDE to define IP address. Note it uses comma separated four byte. and NO equal to sign.

Define Device IP address, Gateway (i.e. wifi router ip), subnet mask and dns

You can get this information from your laptops wifi connection details.

//Static IP address configuration
IPAddress staticIP(192, 168, 43, 90); //ESP static ip
IPAddress gateway(192, 168, 43, 1);   //IP Address of your WiFi Router (Gateway)
IPAddress subnet(255, 255, 255, 0);  //Subnet mask
IPAddress dns(8, 8, 8, 8);  //DNS


const char* deviceName = "circuits4you.com";

Step 3: Connecting to WiFi Router with Above Configuration

Static ip configuration can be applied to ESP using WiFi.config statement.

WiFi.config(staticIP, subnet, gateway, DNS)

Use this command before WiFi begin. WiFi.hostname is optional, it is used to give name to ESP to identify in WiFi router.

Note That: Sequence of WiFi.config is important, first IPaddress, then subnet then gateway and last is DNS

WiFi.disconnect();  //Prevent connecting to wifi based on previous configuration
  
  WiFi.hostname(deviceName);      // DHCP Hostname (useful for finding device for static lease)
  WiFi.config(staticIP, subnet, gateway, dns);
  WiFi.begin(ssid, password);

  WiFi.mode(WIFI_STA); //WiFi mode station (connect to wifi router only
  
  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

Complete Code of ESP8266 using Static IP address

This code makes a NodeMCU as webserver and controls onboard LED using web interface.

Change the IP settings, SSID and Password as per your WiFi router and upload.

/*
 * ESP8266 NodeMCU LED Control over WiFi Demo
 * Using Static IP Address for ESP8266
 * https://circuits4you.com
 */
#include <ESP8266WiFi.h>
#include <WiFiClient.h>

//ESP Web Server Library to host a web page
#include <ESP8266WebServer.h>

//---------------------------------------------------------------
//Our HTML webpage contents in program memory
const char MAIN_page[] PROGMEM = R"=====(
  <!DOCTYPE html>
  <html>
  <body>
  <center>
  <h1>WiFi LED on off demo: 1</h1><br>
  Ciclk to turn <a href="ledOn">LED ON</a><br>
  Ciclk to turn <a href="ledOff">LED OFF</a><br>
  <hr>
  <a href="https://circuits4you.com">circuits4you.com</a>
  </center>
  
  </body>
  </html>
)=====";
//---------------------------------------------------------------


//Static IP address configuration
IPAddress staticIP(192, 168, 43, 90); //ESP static ip
IPAddress gateway(192, 168, 43, 1);   //IP Address of your WiFi Router (Gateway)
IPAddress subnet(255, 255, 255, 0);  //Subnet mask
IPAddress dns(8, 8, 8, 8);  //DNS


const char* deviceName = "circuits4you.com";

//On board LED Connected to GPIO2
#define LED 2  

//SSID and Password of your WiFi router
const char* ssid = "yourWiFissid";
const char* password = "yourWiFipassword";

//Declare a global object variable from the ESP8266WebServer class.
ESP8266WebServer server(80); //Server on port 80

//===============================================================
// This routine is executed when you open its IP in browser
//===============================================================
void handleRoot() {
 Serial.println("You called root page");
 String s = MAIN_page; //Read HTML contents
 server.send(200, "text/html", s); //Send web page
}

void handleLEDon() { 
 Serial.println("LED on page");
 digitalWrite(LED,LOW); //LED is connected in reverse
 server.send(200, "text/html", "LED is ON"); //Send ADC value only to client ajax request
}

void handleLEDoff() { 
 Serial.println("LED off page");
 digitalWrite(LED,HIGH); //LED off
 server.send(200, "text/html", "LED is OFF"); //Send ADC value only to client ajax request
}
//==============================================================
//                  SETUP
//==============================================================
void setup(void){
  Serial.begin(115200);
  
  WiFi.begin(ssid, password);     //Connect to your WiFi router
  Serial.println("");

  //Onboard LED port Direction output
  pinMode(LED,OUTPUT); 
  //Power on LED state off
  digitalWrite(LED,HIGH);

  WiFi.disconnect();  //Prevent connecting to wifi based on previous configuration
  
  WiFi.hostname(deviceName);      // DHCP Hostname (useful for finding device for static lease)
  WiFi.config(staticIP, subnet, gateway, dns);
  WiFi.begin(ssid, password);

  WiFi.mode(WIFI_STA); //WiFi mode station (connect to wifi router only
  
  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  //If connection successful show IP address in serial monitor
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());  //IP address assigned to your ESP
 
  server.on("/", handleRoot);      //Which routine to handle at root location. This is display page
  server.on("/ledOn", handleLEDon); //as Per  <a href="ledOn">, Subroutine to be called
  server.on("/ledOff", handleLEDoff);

  server.begin();                  //Start server
  Serial.println("HTTP server started");
}
//==============================================================
//                     LOOP
//==============================================================
void loop(void){
  server.handleClient();          //Handle client requests
}

Upload this code and observer serial monitor.

Read more how to get MAC address and List of WiFi Hotspots

2 thoughts on “ESP8266 Static IP Address arduino Example

  1. Made the sketch like you suggested, but the esp8266 still comes with an ip-adress, that is not in my range. (192.168.4.2 instead of 192.168.178.xx ) as I programmed it to do.
    Any suggestion?

    1. In line 88, try using

      WiFi.config(staticIP, gateway, subnet, dns);

      instead of
      WiFi.config(staticIP, subnet, gateway, dns);

      Also, make sure your gateway address is correct. Mine was wrong and the ESP8266 was getting the previous address.

      In my case,

Leave a Reply