Marcos Kirsch 2167d7290f Changed wifi config to STATION, which I think would be most common. Reworked starting the server until after IP address is acquired. Added mdns conditonal mdns registration (thank you firmware people\!) | 7 年之前 | |
---|---|---|
http | 8 年之前 | |
.gitignore | 9 年之前 | |
LICENSE | 9 年之前 | |
Makefile | 7 年之前 | |
README.md | 8 年之前 | |
TODO.md | 8 年之前 | |
httpserver-b64decode.lua | 9 年之前 | |
httpserver-basicauth.lua | 8 年之前 | |
httpserver-conf.lua | 9 年之前 | |
httpserver-connection.lua | 7 年之前 | |
httpserver-error.lua | 8 年之前 | |
httpserver-header.lua | 7 年之前 | |
httpserver-request.lua | 8 年之前 | |
httpserver-static.lua | 8 年之前 | |
httpserver.lua | 7 年之前 | |
init.lua | 7 年之前 |
A (very) simple web server written in Lua for the ESP8266 running the NodeMCU firmware.
If you are trying to implement a user-interface or HTTP webserver in your ESP8266 then you are really abusing its intended purpose. When it comes to scoping your ESP8266 applications, the adage Keep It Simple Stupid truly applies.
– Terry Ellison, nodemcu-firmware maintainer,
Let the abuse begin.
Upload server files using nodemcu-uploader. The easiest is to use GNU Make with the bundled Makefile. Open the Makefile and modify the user configuration to point to your nodemcu-uploader script and your serial port. Type the following to upload the server code, init.lua (which you may want to modify), and some example files:
make upload_all
If you only want to upload the server code, then type:
make upload_server
And if you only want to upload the server files:
make upload_http
Restart the server. This will execute init.lua which will compile the server code. Then, assuming init.lua doesn’t have it, start the server yourself by typing:
dofile("httpserver.lc")(80)
In this example, 80 is the port your server is listening at, but you can change it.
All the files you upload must be prefixed with «http/». Wait, what?
Yes: NodeMCU’s filesystem does not support folders, but filenames can contain slashes. Only files that begin with «http/» will be accessible through the server.
Example: Say the IP for your ESP8266 is 2.2.2.2 and the server is running in the default port 80. Go to (http://2.2.2.2/index.html)[http://2.2.2.2/index.html] using your web browser. The ESP8266 will serve you with the contents of the file «http/index.html» (if it exists). If you visit the root (/) then index.html is served. By the way, unlike most HTTP servers, nodemcu_httpserver treats the URLs in a case-sensitive manner.
Enable and configure HTTP Basic Authentication by editing «httpserver-conf.lua» file.
When enabled, HTTP Basic Authentication is global to every file served by the server.
Remember that HTTP Basic Authentication is a very basic authentication protocol, and should not be considered secure if the server is not using encryption, as your username and password travel in plain text.
Similar to static files, upload a Lua script called «http/[name].lua where you replace [name] with your script’s name. The script should return a function that takes three parameters:
return function (connection, req, args)
-- code goes here
end
Use the connection parameter to send the response back to the client. Note that you are in charge of sending the HTTP header, but you can use the bundled httpserver-header.lua script for that. See how other examples do it. The req parameter contains information about the request. The args parameter is a Lua table that contains any arguments sent by the client in the GET request.
For example, if the client requests http://2.2.2.2/foo.lua?color=red then the server will execute the function in your Lua script foo.lua and pass in connection and args, where args.color = «red».
This is a bundled example that shows how to use nodemcu-httpserver together with server-side scripting to control something with the ESP8266. In this example, we will pretend to open one of two garage doors.
Your typical garage door opener has a wired remote with a single button. The button simply connects to two terminals on the electric motor and when pushed, the terminals are shorted. This causes the motor to open or close.
This example assumes that GPIO1 and GPIO2 on the ESP8266 are connected each to a relay that can be controlled. How to wire such thing is outside of the scope of this document but information is easily found online. The relays are controlled by the microcontroller and act as the push button, and can actually be connected in parallel with the existing mechanical button.
This example consists of the following files:
Be careful permanently installing something like this in your home. The server provides no encryption. Your only layers of security are the WiFi network’s password and simple HTTP authentication which sends your password unencrypted.
This script is provided simply as an educational example. You’ve been warned.
Since this is a project maintained in my free time, I am pretty lenient on contributions. I trust you to make sure you didn’t break existing functionality nor the shipping examples and that you add examples for new features. I won’t test all your changes myself but I am very grateful of improvements and fixes. Open issues in GitHub too, that’s useful.
Please follow the coding style as close as possible:
The chip is very, very memory constrained.