Fixed IP Webserver using ESP8266 NodeMcu

Fixed IP Webserver using ESP8266

To set up a fixed IP webserver using an ESP8266, you’ll need to configure both the ESP8266 to use a static IP address and set up a basic web server. Here’s a step-by-step guide to help you through the process:

Hardware Requirements:
ESP8266 module (e.g., NodeMCU, Wemos D1 Mini)
USB-to-serial adapter (if your ESP8266 module doesn’t have one built-in)
Jumper wires

Software Requirements:

Arduino IDE (or PlatformIO)
ESP8266 board support package installed in the Arduino IDE
3. Setup the Arduino IDE:
Install the ESP8266 Board Package:

Open the Arduino IDE.

Go to File > Preferences (or Arduino > Preferences on macOS).
In the “Additional Boards Manager URLs” field, add http://arduino.esp8266.com/stable/package_esp8266com_index.json and click OK.
Go to Tools > Board > Boards Manager, search for “ESP8266” and install the package.
Select Your ESP8266 Board:
Go to Tools > Board and select the appropriate ESP8266 board (e.g., NodeMCU 1.0).

Upload the Code:
Here’s a basic example to set up a static IP address and a simple web server:

#include <ESP8266WiFi.h>

const char* ssid = "ANDROIDERODE"; // Replace with your Wi-Fi SSID
const char* password = "87654321"; // Replace with your Wi-Fi password

// Static IP configuration
IPAddress local_IP(192, 168, 1, 180); // Replace with your desired static IP address
IPAddress gateway(192, 168, 1, 1); // Replace with your network gateway IP address
IPAddress subnet(255, 255, 255, 0); // Replace with your network subnet mask

WiFiServer server(80); // Web server on port 80

void setup()

{
Serial.begin(115200);
delay(10);

// Set up the static IP address
WiFi.config(local_IP, gateway, subnet);
WiFi.begin(ssid, password);

// Wait for connection
while (WiFi.status() != WL_CONNECTED)

{
delay(500);
Serial.print(".");
}

Serial.println("Connected to Wi-Fi");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());

// Start the server
server.begin();
}

void loop()
{
WiFiClient client = server.available();
if (client)
{
Serial.println("New Client Connected");
String request = "";

// Read the HTTP request
while (client.available())

{
char c = client.read();
request += c;
delay(1);
}

Serial.println("Request: ");
Serial.println(request);

// Prepare the response
String response = "HTTP/1.1 200 OK\r\n";
response += "Content-Type: text/html\r\n";
response += "Connection: close\r\n";
response += "\r\n";
response += "<!DOCTYPE HTML>\r\n";
response += "<html>\r\n";
response += "<h1>Hello from ESP8266</h1>\r\n";
response += "</html>\r\n";

client.print(response);

delay(1);
Serial.println("Client Disconnected");
}
}

Upload the Code:
Connect your ESP8266 to your computer via USB.
Select the correct port in the Arduino IDE under Tools > Port.
Click the upload button (right arrow) to compile and upload the code to your ESP8266.

Testing:
Once the code is uploaded and the ESP8266 reboots, open the Serial Monitor (set to 115200 baud rate).
You should see the ESP8266 connecting to your Wi-Fi and obtaining the static IP address.
Open a web browser and navigate to the static IP address you assigned (e.g., http://192.168.1.180).
You should see a web page displaying “Hello from ESP8266”.

Troubleshooting:
Ensure that the static IP address you assigned does not conflict with other devices on your network.
Verify that the gateway and subnet values are correct for your network configuration.
If you encounter issues, check the Serial Monitor for any error messages or connection problems.
This setup should give you a basic, static IP web server running on your ESP8266. You can expand upon this by adding more complex web pages or functionalities as needed.

Subramanian
Subramanian

Subramanian MK, currently serving as a workshop instructor at Sakthi Polytechnic College, Erode Tamil Nadu. With a career spanning 25 + years, Subramanian MK has dedicated himself to advancing knowledge in Electronics and Communication Engineering (ECE). His passion for exploring new technologies has led to the development of numerous projects, showcasing expertise in IoT and PCB design.

Articles: 514

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.

×

Hi, How can I help you?

×