Static IP Connection in NodeMCU

In this Post, Need for Static IP Connection in NodeMCU is discussed. First, we shall know some basics about IP address.

IP ADDRESS:

Internet Protocol Address (IP Address) is assigned to every device that is connected to a TCP/IP network.  Internet Protocol Version 4 (IPv4) uses 32-bit numbers wherein Internet Protocol Version 6 (Ipv6) uses 128-bit numbers. Due to lack of IPv4 address concept of local and public address for a device is used. There are two types of addressing, they are

  • Physical Addressing
  • Logical Addressing 

In physical addressing, the MAC Address of the device is used and in Logical addressing, the IP address is used. 

Types of IP Addressing:

  1. Static IP [ IP Address assigned to a device does not change ]
  2. Dynamic IP [ IP Address assigned changes every time when we connect to the network ]

When a device is connected to a network (Access Point), the access point assigns an IP address to the device. When the same device connects to the same access point for the next time IP address assigned may vary. Generally, most of the users don’t require static IP addresses. The need for Static IP Addressing is given below. 

Need of Static IP Connection in NodeMCU:

When IP Address is to be remembered for whitelisting a user for security purposes. 

Suppose if a DHT sensor information is hosted on a Web Server using a NodeMCU, the IP of the server may change dynamically, so if an app or a program can’t be designed to access information from the same IP address as it changes. Thus Static IP addressing is used. In this post how to configure NodeMCU with Static IP is explained.

1
2
3
4
//Enter the static ip that you want to set
IPAddress ip(192, 168, 0, 110); 
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);

Provide the Static IP that is required for that device and also the gateway and subnet mask. Gateway and Subnet Mask can be the same as provided. 

PROGRAM:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Program from www.mybtechprojects.tech
#include <ESP8266WiFi.h>
 
//Enter the SSID and Password of the Access Point or Hotspot.
const char* ssid = ""; 
const char* password = "";
 
//Enter the static ip that you want to set
IPAddress ip(192, 168, 0, 110); 
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0); 
 
WiFiServer server(80);
 
void setup() {
  Serial.begin(115200);
  delay(10); 
  // Connection to wireless network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
 
  //Configuring the WI-FI with the specified static IP.
  WiFi.config(ip, gateway, subnet);
 
  //Start the WI-FI connection with specified ACCESS-POINT 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
 
  Serial.println("");
  Serial.println("WiFi connected");
 
  server.begin();
  Serial.println("Server started");
 
  // Print the IP address in the serial monitor.
  Serial.print("Type this address in URL to connect: ");
  Serial.print("http://");
  Serial.println(ip);
  Serial.println("/");
 
}
 
void loop() {
 
}

OUTPUT:

 

Thank You

 

SHARE THIS!!!

Gowtham S

Gowtham is a programming enthusiast. His field of interest includes Arduino, NodeMCU, Raspberry Pi, and Python. To know more about him visit https://mybtechprojects.tech/about-us/.

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.