NodeMCU ESP8266 is a low-cost, open-source WiFi connectivity capable SoC(System on Chip) microcontroller that is widely used by hobbyists, makers, and professionals in various IoT projects. It is based on the ESP8266 chip and comes with built-in WiFi connectivity, making it an ideal choice for projects that require remote wireless data transmission or control.
One of the most popular applications of NodeMCU ESP8266 is setting up a WiFi server. With its powerful hardware and easy-to-use programming interface, it is possible to create a web server that can be accessed from any device connected to the same network. This makes it ideal for building IoT projects that require remote control or monitoring.
Here ESP8266 based NodeMCU board is setup as a simple WiFi server. When client connects to this server the server replies with "Welcome to ESP8266 NodeMCU web server". So this is illustration of simplest way of setting up NodeMCU ESP8266 module as a WiFi server. This is to learn and test how to configure NodeMCU ESP8266 as a simple web server.
How it works?
A program code is written, compiled using Arduino IDE and uploaded into the NodeMCU via usb cable. To write program code for NodeMCU we have to necessary board package and configure Arduino IDE to use the ESP8266 board. Setting up a basic WiFi server using NodeMCU ESP8266 is quite simple. All you need is the NodeMCU ESP8266 board, a USB cable for power, and a computer running the Arduino IDE.
Step 1. Installation of Arduino core for ESP8266 WiFi chip
ESP8266 can be programmed using Arduino IDE using Arduino programming language. For this however we must install supporting board packages and libraries. There are two ways to install the libraries.
(a) Download and install libraries manually
(b) Use Arduino IDE library manager
(a) Download and install libraries manually
If you want to download and install libraries, you have to first download the library, which is in zip format, from the following link.
https://github.com/esp8266/Arduino
Then install the library zip file from the Arduino IDE > Sketch > Include Library > Add .ZIP Library.
(b) Use Arduino IDE library manager
You can also install ESP8266 library from the library Manager. From Arduino IDE, open library manager from Sketch > Include Library > Manage Libraries.
Search and find esp8266 by ESP8266 community and install it.
Step 2: Configure ESP8266 board package
Next we have to configure Arduino IDE so that it recognizes the ESP8266 board. To do this, open Arduino IDE > File > Preferences and copy paste the following link in the Additional Boards Manager URLs field.
https://arduino.esp8266.com/stable/package_esp8266com_index.json
Once you have these, follow these steps to set up NodeMCU ESP8266 as a Hello World WiFi server:
Step 3: Connect your NodeMCU ESP8266 board to your computer using a USB cable
Step 4: Open the Arduino IDE and navigate to the "Tools" menu. Under "Board", select "NodeMCU 1.0 (ESP-12E Module)"
Step 5: Under "Port", select the port that your NodeMCU ESP8266 board is connected to
Step 6: Copy the following code into the Arduino IDE:
#include <ESP8266WiFi.h>
// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);
const char* ssid = "your-ssid";
const char* password = "your-password";
void setup() {
Serial.begin(115200);
delay(10);
// Connect to WiFi network
Serial.print("Connecting to WiFi hotspot");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.println(WiFi.localIP());
}
void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
while(!client.available()){
delay(1);
}
// Prepare the response
String webpage;
webpage += "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
webpage += "<html>\r\n";
webpage += "<head>\r\n";
webpage += "<meta charset=\"UTF-8\"/>\r\n";
webpage += "<title>ESP NodeMCU Webserver</title>\r\n";
webpage += "</head>\r\n";
webpage += "<body>\r\n";
webpage += "<h1>Welcome to ESP8266 NodeMCU Web server</h1>\r\n";
webpage += "</body>\r\n";
webpage += "</html>\r\n";
// Send the response to the client
client.print(webpage);
delay(1);
Serial.println("Client disonnected");
}
This is a NodeMCU ESP8266 code written in Arduino IDE that sets up a web server using ESP8266 WiFi module. Here is a line-by-line explanation of the code:
#include <ESP8266WiFi.h>
This line includes the library for using ESP8266 WiFi module.
WiFiServer server(80);
This line creates an instance of the server and specifies the port to listen on as an argument. Here, the port number is 80 which is the default HTTP port number.
const char* ssid = "your-ssid";
const char* password = "your-password";
These two lines declare the SSID (name) and password of the WiFi network to connect to.
void setup() {
Serial.begin(115200);
delay(10);
// Connect to WiFi network
Serial.print("Connecting to WiFi hotspot");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.println(WiFi.localIP());
}
The setup()
function is executed once when the program
starts. In this function, the serial communication is started at a baud
rate of 115200. The ESP8266 module connects to the WiFi network using
the SSID and password declared earlier. The function waits until the
module is connected to the network. Then, the server is started on the
port number 80. Finally, the IP address of the module is printed to the
serial monitor.
void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
while(!client.available()){
delay(1);
}
// Prepare the response
String webpage;
webpage += "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
webpage += "<html>\r\n";
webpage += "<head>\r\n";
webpage += "<meta charset=\"UTF-8\"/>\r\n";
webpage += "<title>ESP NodeMCU Webserver</title>\r\n";
webpage += "</head>\r\n";
webpage += "<body>\r\n";
webpage += "<h1>Welcome to ESP8266 NodeMCU Web server</h1>\r\n";
webpage += "</body>\r\n";
webpage += "</html>\r\n";
// Send the response to the client
client.print(webpage);
delay(1);
Serial.println("Client disonnected");
}
The loop()
function is executed repeatedly after the setup()
function. In this function, the ESP8266 module checks if a client has
connected to the server. If a client is connected, the function waits
until the client sends some data. Then, it prepares a response which is a
simple HTML page saying "Welcome to ESP8266 NodeMCU Web server". The response is sent to the
client using the client.print()
function. Finally, the
function prints a message to the serial monitor indicating that the
client has disconnected. This loop runs indefinitely until the program
is stopped.
Step 7: Replace "your-ssid" and "your-password" with your WiFi network name and password
Step 8: Upload the code to your NodeMCU ESP8266 board by clicking on the "Upload" button
Step 9: Open serial monitor and note down the IP address as illustrated below.
This is the IP address of your NodeMCU web server. Note that you have to select the correct baud rate, which is 115200.
Step 10: Open Web Browser and type in the IP address of the NodeMCU ESP8266 IP address. You should see the message "Hello from ESP8266 NodeMCU" as shown below.
This was a simple getting started example of configuring ESP8266 NodeMCU as a web server. This web server is a synchronous web server but a better method is to implement asynchronous web server. By this it means this example uses the traditional ESP8266WiFi.h library and a synchronous
server while the other option is to use ESPAsyncTCP.h and
ESPAsyncWebServer.h libraries to implement asynchronous ESP8266 NodeMCU server. The
asynchronous server allows handling multiple requests simultaneously and
provides better performance and scalability for IoT(Internet of Things) projects. After basic understanding of building synchronous or asynchronous web server with NodeMCU, then the next step is to learn how to control a LED from NodeMCU WiFi.
References
[2] PWM with ESP8266