Lab 07_NodeMCU as Web Server
Lab 07_NodeMCU as Web Server
HTTP Request
HTTP Response
Client Server
Web Server
▪ NodeMCU Wi-Fi has Access Point (AP) mode through which it can create
wireless LAN to which any Wi-Fi enabled device can connect.
NodeMCU as HTTP Server using Wi-Fi STA Mode
▪ NodeMCU has Station (STA) mode using which it can connect to the
existing Wi-Fi network and can act as a server with an IP address assigned
by that network.
NodeMCU as Web Server
▪ We want to build a simple webpage to turn on/off an LED and display the
temperature value.
NodeMCU as Web Server: Connecting to Wi-Fi
192.168.137.88/led/on 192.168.137.88/led/off
NodeMCU as Web Server: Web Page
<!DOCTYPE html>
<html>
<head>
<title>NodeMCU Web Server</title>
</head>
<body>
<div>
<h1>NodeMCU Web Server</h1>
<a href="/led/on">Turn On LED</a>
<a href="/led/off">Turn Off LED</a>
<div>Temperature: #temp#</div>
</div>
</body>
</html>
NodeMCU as Web Server: Web Page
NodeMCU as Web Server: Better Look
<!DOCTYPE html>
<html>
<head>
<title>NodeMCU Web Server</title>
<style>
body {
font-family: Arial;
background-color: #f2f2f2;
margin: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.container {
text-align: center;
padding: 20px;
border-radius: 8px;
background-color: #fff;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.btn {
display: inline-block;
padding: 10px 15px;
font-size: 18px;
border: none;
border-radius: 4px;
cursor: pointer;
margin: 10px;
text-decoration: none;
}
NodeMCU as Web Server: Better Look
.btn-on {
background-color: lightseagreen;
color: white;
}
.btn-off {
background-color: tomato;
color: white;
}
.temp {
font-size: 24px;
margin-top: 20px;
}
</style>
</head>
<body>
<div class="container">
<h1>NodeMCU Web Server</h1>
<a href="/led/on" class="btn btn-on">Turn On LED</a>
<a href="/led/off" class="btn btn-off">Turn Off LED</a>
<div class="temp">Temperature: #temp#</div>
</div>
</body>
</html>
NodeMCU as Web Server: Better Look
NodeMCU Pinout
PIN GPIO Why Not Safe?
HIGH at boot
D0 GPIO16
Used to wake up from deep sleep
D1 GPIO5 -
D2 GPIO4 -
Connected to FLASH button
D3 GPIO0
Boot fails if pulled LOW
HIGH at boot
D4 GPIO2
Boot fails if pulled LOW
D5 GPIO14 -
D6 GPIO12 -
D7 GPIO13 -
Required for boot
D8 GPIO15
Boot fails if pulled HIGH
Controlling an LED: Circuit
Controlling an LED: Code Notes
Port Protocol
20, 21 FTP
22 SSH
23 Telnet
25 SMTP
53 DNS
80 HTTP
443 HTTPS
Controlling an LED: Code Notes
▪ HTTP request handlers are defined to specify how the server should
respond to different requests, such as turning the LED on or off.
server.on("/", handleRoot);
server.on("/led/on", handleLedOn);
server.on("/led/off", handleLedOff);
server.onNotFound(handleNotFound);
▪ Finally, the HTTP server is started, allowing the NodeMCU to handle
incoming HTTP requests.
server.begin();
Controlling an LED: Code Notes
void loop() {
// Handle incoming client requests
server.handleClient();
}
Controlling an LED: Code Notes
▪ The handleRoot() function serves as the HTTP request handler for the
root URL ("/").
▪ The digitalWrite() function is used to control the state of the LED
pin, setting it either HIGH or LOW to turn the LED on or off.
digitalWrite(LED_PIN, ledStatus);
▪ After updating the LED status, an HTML response is sent back to the
client using the server.send() function.
server.send(200, "text/html", getHtml());
▪ The response has a status code of 200, indicating success.
▪ The HTML content to be sent is obtained by calling the getHtml()
function, which generates the appropriate HTML.
Controlling an LED: Code Notes
void handleNotFound(){
// Send a 404 Not Found response
server.send(404, "text/plain", "Not Found");
}
Controlling an LED: Code Notes
▪ The getHtml() function generates and returns the HTML content for
the web page served by the NodeMCU web server.
▪ Clicking <a> links triggers the /led/on or /led/off routes.
<a href="/led/on" class="btn btn-on">Turn On LED</a>
<a href="/led/off" class="btn btn-off">Turn Off LED</a>
▪ The temperature value is represented by #temp#, which suggests that it’s
needs to be replaced with the actual temperature value dynamically before
sending the HTML response to the client.
<div class="temp">Temperature: #temp#</div>
Controlling an LED: Accessing Website
▪ In Wi-Fi Station (STA) mode, NodeMCU gets IP address from the router
(access point).
▪ If we are also in the same network, then we can directly connect to
NodeMCU HTTP server using the IP address only.
Controlling an LED and Reading Temperature: Circuit
Controlling an LED and Reading Temperature: Code Notes
▪ The directive includes the DHT sensor library, which provides functions
for interfacing with DHT sensors.
#include "DHT.h"
▪ This line defines the pin connected to the DHT sensor, which is D5.
#define DHT_PIN D5
▪ This initializes a DHT11 sensor object named dht.
DHT dht(DHT_PIN, DHT11);
▪ This declares a variable to store the temperature read from DHT sensor.
float temp;
▪ This line starts the DHT sensor, allowing it to begin reading data.
dht.begin();
Controlling an LED and Reading Temperature: Code Notes