The ESP8266 NodeMCU Tutorial – Web Server provides a comprehensive guide on how to set up a web server using the NodeMCU development board powered by the ESP8266 microcontroller.
Read more: ESP8266 NodeMCU Tutorial – Web ServerSimple Web Server
- Simple web server display heap, ip, mac address some other details ..
- First need to connect ESP8266 with WiFi Router. then access the web server using ip address
print("****** Wifi data's Initializing *********") mode = wifi.STATION ssid = "ArunEworld" password = "Arun" print("****** Setup Wifi Settings *********") wifi.setmode(mode) wifi.sta.config(ssid, password) function Wif_Connect() WC = wifi.sta.status() --WC Wifi_Connect print("Wifi Status is : " .. WC) if WC == 0 then print("0.Satation is Idle ??") wifi.sta.autoconnect(auto) elseif WC == 1 then print("1.Satation Connecting....Wait a moment") elseif WC == 2 then print("2.password is wrong !") elseif WC == 3 then print("3.No Access Point Found !") elseif WC == 4 then print("4.Station connecting fail !") elseif WC == 5 then print("IP :" .. wifi.sta.getip()) tmr.stop(0) -- dofile("AEW_Webserver.lua") -- print("IP :"..wifi.sta.getip()) -- dofile("File_name.lua") -- please write your file name end end print("**** Webserver Running........ ****") srv = net.createServer(net.TCP) srv:listen(80, function(conn) conn:on("receive", function(client, request) buf = buf .. "<h1>Arun Eworld</h1>" buf = buf .. "Chip ID :<b>" .. node.chipid() .. "</b><BR>" --[[ buf = buf.."Heap :<b>".. node.heap() .. "</b><BR>"; buf = buf.."Station Mac Address :<b>".. wifi.sta.getmac() .. "</b><BR>"; buf = buf.."AP Mac Address :<b>".. wifi.ap.getmac() .. "</b><BR>"; buf = buf.."GetMode :<b>".. wifi.getmode() .. "</b><BR>"; buf = buf.."Status :<b>".. wifi.sta.status() .. "</b><BR>"; --buf = buf.."AP IP Address :<b>".. wifi.ap.getip() .. "</b><BR>"; buf = buf.."Station IP Address :<b>".. wifi.sta.getip() .. "</b><BR>"; buf = buf.."TMR.NOW :<b>" .. tmr.now() .. "</b><BR<BR><BR>"; ]]-- client:send(buf) client:close() collectgarbage() end) end) tmr.alarm(0, 5000, 1, Wif_Connect)
Code Explanation
Sure, here’s an explanation of the provided Lua code:
- Wifi Initialization:
- The code begins by printing a message indicating the initialization of WiFi data.
- It sets the WiFi mode to Station mode.
- Defines the SSID and password for the WiFi connection.
- WiFi Connection Function (
Wif_Connect
):- This function is responsible for handling the WiFi connection process.
- It retrieves the current WiFi connection status using
wifi.sta.status()
. - Depending on the status, it performs different actions:
- If the status is
0
, it indicates that the station is idle, and it attempts to autoconnect. - If the status is
1
, it indicates that the station is connecting. - If the status is
2
, it indicates that the password is incorrect. - If the status is
3
, it indicates that no Access Point (AP) is found. - If the status is
4
, it indicates that the connection to the AP failed. - If the status is
5
, it indicates a successful connection, and it prints the assigned IP address.- It also stops the timer (
tmr.stop(0)
), which triggers the connection attempt.
- It also stops the timer (
- If the status is
- Webserver Setup:
- The code prints a message indicating that the web server is running.
- It creates a TCP server (
srv
) that listens on port80
. - Upon receiving a connection, it defines a callback function to handle incoming requests.
- The callback function constructs an HTML response (
buf
) containing the header<h1>Arun Eworld</h1>
and the chip ID. - The response is sent back to the client, and the connection is closed.
- Timer Initialization:
- A timer (
tmr
) is set to call theWif_Connect
function every 5 seconds (tmr.alarm(0, 5000, 1, Wif_Connect)
).
- A timer (
This code initializes the WiFi connection, continuously attempts to connect to the configured network, and sets up a basic web server to handle incoming requests. (ESP8266 NodeMCU Tutorial Web Server)
TCP Server Listener
This code given below will configure the ESP to act as an Access Point and it has its own SSID=”ArunEworld” and Password=”Arun” and also act as the server which will continuously listen for a connection.
Code
This code initializes the ESP8266 module as an access point with the SSID “ArunEworld” and password “Arun”. It then creates a TCP server that listens on port 80. When a connection is established, it prints any received data and releases resources after sending a response.
print("ArunEworld - ESP8266 Server") wifi.setmode(wifi.STATIONAP) wifi.ap.config({ssid="ArunEworld", pwd="Arun"}) print("Server IP Address:", wifi.ap.getip()) sv = net.createServer(net.TCP) sv:listen(80, function(conn) conn:on("receive", function(conn, receivedData) print("Received Data: " .. receivedData) end) conn:on("sent", function(conn) collectgarbage() end) end)
Result
Host pot
Code Explanation
This code initializes the ESP8266 module as an access point with the SSID “ArunEworld” and password “Arun”, creates a TCP server listening on port 80, and defines callback functions to handle received and sent data.
Line | Code | Explanation |
---|---|---|
1 | print("ArunEworld - ESP8266 Server") | Prints a message indicating the initialization of the ESP8266 server. |
2 | wifi.setmode(wifi.STATIONAP) | Sets the mode of the WiFi module to STATIONAP, enabling it to function as both a station and an access point. |
3 | wifi.ap.config({ssid="ArunEworld", pwd="Arun"}) | Configures the access point with the SSID “ArunEworld” and password “Arun”. |
4 | print("Server IP Address:", wifi.ap.getip()) | Prints the IP address assigned to the ESP8266 access point. |
6 | sv = net.createServer(net.TCP) | Creates a TCP server object. |
7 | sv:listen(80, function(conn) | Starts the server and listens for connections on port 80. |
8 | conn:on("receive", function(conn, receivedData) | Sets up a callback function to handle received data. |
9 | print("Received Data: " .. receivedData) | Prints the received data. |
10 | end) | Ends the “receive” callback function definition. |
11 | conn:on("sent", function(conn) | Sets up a callback function to handle data sent. |
12 | collectgarbage() | Collects garbage to free up memory. |
13 | end) | Ends the “sent” callback function definition. |
14 | end) | Ends the server callback function definition. |