How to create simple ESP8266 NodeMCU web server in Arduino IDE. You will learn to control GPIO pins of ESP8266 NodeMCU from a basic Web server. You also get to know about creating password protected web server with ESP8266 based NodeMCU board. Although, you can use this tutorial for any type of ESP8266 board like ESP01, ESP12.

Here we used Arduino development boards to new ESP8266 development boards like NodeMCU and Wemos D1 development boards. The reason behind this switching is to communicate and control the development board remotely from a internet by creating a simple web server. Yes, this is true that we can communicate and control Arduino boards through interfacing externals modules like Bluetooth RF and IR but there are many practical limitations and these techniques are not much efficient while comparing with modules having on board WiFi Like ESP8266 NodeMCU modules.

There are different ways to communicate ESP8266 development boards like Android application through WiFi, Web server through WiFi, and Serial communication with PC through the wire. In this tutorial, you will learn how to create Web server to control the GPIO’s of the development board through buttons on Web server and to protect your Web server with password from unauthorized user access. We can access our Web server from the smartphone, tablet, laptop, and desktop personal computer.
Controlling LEDs with ESP8266 Web Server NodeMCU
By controlling GPIO’s of the development boards through Web server, we can connect LEDs, Relays, Servo motors, and so on. But in this tutorial, we will make Web servers with four buttons to control LEDs and to update the status of the LEDs on the Web server.

Components Required

1. ESP8266 Development Board.
2. LEDs – 4 nos
3. Resistors (100 ohms)
4. Jumper Wires
5. Breadboard
6. Micro USB cable
7. Smartphone
8. Arduino IDE
ESP8266 NodeMCU Web Server Circuit
For practical demonstration purpose, we are controlling through LEDs through a web server. Connection diagram for this functionality is shown below.
We need four LEDs with suitable resistors, NodeMCU, breadboard and jumper wires. One terminal of resistors is connected to the cathode of each LED and other terminal connected to the ground pin. Anodes of the LEDs connected to D1 to D4 pins of NodeMCU respectively.
Writing Sketch for ESP8266 Web server
First of all, we have to include ESP8266WiFi library so that we can use built-in function to connect to WiFi. Then we have to write the network credential which includes the name and the password. Make sure write your network credentials correctly otherwise, you will not be able to connect with the network. After this we have to describe the server port number, in this case, I write 443 which is Secure Hyper Text Transfer Protocol (HTTPS). We can also use the different port number like 80 which is not that secure to use.
How to Open NodeMCU based Web Server
1. After uploading the code open the serial monitor and wait until NodeMCU connected with WiFi network, the Web server initiated and display IP address as shown below. If you facing a problem in getting IP address just simply press the reset button on the NodeMCU.
2. Note the IP address and open your web browser on PC and write the IP address on the URL search tab followed by a colon (:) server port number, in this case, 192.168.18.45:443. If you entered the correct IP address and server port you will see a window asking for user name and password as shown in the picture.


3. If you entered the right credentials so you will be given the access to the main web page. You can click the desired button to perform the operation.
4. To open the Web server using smartphone open your browser on the smartphone write the IP address shown up in the serial monitor and IP address followed by a colon (:) and server port number in my case 192.168.18.45:443
5. If you entered the right credentials so you will be given the access to the main web page. You can click the desired button to perform the operation.

MAIN PROGRAMME.

UPLOAD VIA ARDUINO IDE
#include //ESP8266 Arduino library with built in functions
#define ssid “eMKEYES” // Replace with your network name
#define password “987654321” // Replace with your network password
WiFiServer server(443); // Web Server on port 443
String header;
String state1 = “Off”;
String state2 = “Off”;
String state3 = “Off”;
String state4 = “Off”;
int LED1 = 5;
int LED2 = 4;
int LED3 = 0;
int LED4 = 2;
void setup()
{
Serial.begin(115200); // Initializing serial port
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
Serial.println();
Serial.print(“Connecting to “);
Serial.println(ssid);
WiFi.begin(ssid, password); // Connecting to WiFi network
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(“.”);
}
Serial.println(“”);
Serial.println(“WiFi connected”);
server.begin(); // Starting the web server
Serial.println(“Web server Initiated. Waiting for the ESP IP…”);
delay(10000);
Serial.println(WiFi.localIP()); // Printing the ESP IP address
}
// runs over and over again
void loop()
{
// Searching for new clients
WiFiClient client = server.available();
if (client)
{
Serial.println(“New client”);
boolean blank_line = true; // boolean to locate when the http request ends
while (client.connected())
{
if (client.available())
{
char c = client.read();
header += c;
if (c == ‘\n’ && blank_line)
{
Serial.print(header);
// Finding the right credential string
if (header.indexOf(“VXNlcjoxMjM0”) >= 0)
{ //
//successful login
client.println(“HTTP/1.1 200 OK”);
client.println(“Content-Type: text/html”);
client.println(“Connection: close”);
client.println();
// turns the LED on and off
if (header.indexOf(“GET / HTTP/1.1”) >= 0) {
Serial.println(“Web Page”);
}
else if (header.indexOf(“GET /LED1on HTTP/1.1”) >= 0) {
Serial.println(“GPIO 5 On”);
state1 = “On”;
digitalWrite(LED1, HIGH);
}
else if (header.indexOf(“GET /LED1off HTTP/1.1”) >= 0) {
Serial.println(“GPIO 5 Off”);
state1 = “Off”;
digitalWrite(LED1, LOW);
}
else if (header.indexOf(“GET /LED2on HTTP/1.1”) >= 0) {
Serial.println(“GPIO 4 On”);
state2 = “On”;
digitalWrite(LED2, HIGH);
}
else if (header.indexOf(“GET /LED2off HTTP/1.1”) >= 0) {
Serial.println(“GPIO 4 Off”);
state2 = “Off”;
digitalWrite(LED2, LOW);
}
else if (header.indexOf(“GET /LED3on HTTP/1.1”) >= 0) {
Serial.println(“GPIO 0 On”);
state3 = “On”;
digitalWrite(LED3, HIGH);
}
else if (header.indexOf(“GET /LED3off HTTP/1.1”) >= 0) {
Serial.println(“GPIO 0 Off”);
state3 = “Off”;
digitalWrite(LED3, LOW);
}
else if (header.indexOf(“GET /LED4on HTTP/1.1”) >= 0) {
Serial.println(“GPIO 2 On”);
state4 = “On”;
digitalWrite(LED4, HIGH);
}
else if (header.indexOf(“GET /LED4off HTTP/1.1”) >= 0) {
Serial.println(“GPIO 2 Off”);
state4 = “Off”;
digitalWrite(LED4, LOW);
}
// Web page
client.println(“”);
client.println(“”);client.println(“”);
client.println(“”);
client.println(“”);
client.println(”  “);
client.println(“MKS-WEB SERVER”);

client.println(“WebServer”);
client.println(“LED 1: State: ” + state1);
client.println(“”);
client.println(“ON”);
client.println(“OFF”);
client.println(“”);
client.println(“LED 2: State: ” + state2);
client.println(“”);
client.println(“ON”);
client.println(“OFF”);
client.println(“”);
client.println(“LED 3: State: ” + state3);
client.println(“”);
client.println(“ON”);
client.println(“OFF”);
client.println(“”);
client.println(“LED 4: State: ” + state4);
client.println(“”);
client.println(“ON”);
client.println(“OFF”);
client.println(“”);
}
else
{
client.println(“HTTP/1.1 401 Unauthorized”);
client.println(“WWW-Authenticate: Basic realm=\”Secure\””);
client.println(“Content-Type: text/html”);
client.println();
client.println(“Authentication failed”);
}
header = “”;
break;
}
if (c == ‘\n’)
{ // starts reading a new line
blank_line = true;
}
else if (c != ‘\r’)
{ // finds a character on the current line
blank_line = false;
}
}
}
delay(1);
client.stop(); // ending the client connection
Serial.println(“Client disconnected.”);
}
}

About the Author

Subramanian

Hello! My Dear Friends. I am Subramanian. I am writing posts on androiderode about Electronics testing and equipments.

View All Articles