|Fractal147 9f4d7a9988 Fixed unnecessary globals as in issue #113. (#122)||1 year ago|
|http||1 year ago|
|.gitignore||4 years ago|
|LICENSE||4 years ago|
|Makefile||2 years ago|
|README.md||2 years ago|
|TODO.md||3 years ago|
|httpserver-b64decode.lua||1 year ago|
|httpserver-basicauth.lua||1 year ago|
|httpserver-compile.lua||2 years ago|
|httpserver-conf.lua||2 years ago|
|httpserver-connection.lua||1 year ago|
|httpserver-error.lua||2 years ago|
|httpserver-header.lua||2 years ago|
|httpserver-init.lua||2 years ago|
|httpserver-request.lua||1 year ago|
|httpserver-static.lua||2 years ago|
|httpserver-wifi.lua||2 years ago|
|httpserver.lua||2 years ago|
|init.lua||2 years ago|
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.
Modify your local copy of the configuration file httpserver-conf.lua.
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:
If you only want to upload just the server code, then type:
And if you only want to upload just the files that can be served:
Restart the server. This will execute included init.lua which will compile the server code, configure WiFi, and start the server.
Want to serve your own files? Put them under the http/ folder and upload to the chip. For example, assuming you want to serve myfile.html, upload by typing:
make upload FILE:=http/myfile.html
Notice that while NodeMCU’s filesystem does not support folders, filenames can contain slashes. We take advantage of that and only files that begin with «http/» will be accessible through the server.
Example: Say the IP for your ESP8266 is 18.104.22.168 and the server is running in the default port 80. Go to (http://22.214.171.124/index.html)[http://126.96.36.199/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.
It’s supported. Turn it on in httpserver-conf.lua.
Use it with care and don’t fall into a false sense of security: HTTP Basic Authentication should not be considered secure since the server is not using encryption. Username and passwords travel in the clear.
Yes, you can upload your own Lua scripts! This is pretty powerful. Just put it under http/ and upload it. Make sure it has a .lua extension. Your 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://188.8.131.52/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 you are using a Wemos D1 Pro with two relay shields and two reed switches. 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. The switches are wired so that the ESP8266 can tell whether the doors are open or closed at any given time.
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 (if you enable it) which sends your password unencrypted.
This script is provided for educational purposes. 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 keep your PRs focused on one thing. I don’t mind lots of PRs. I mind PRs that fix multiple unrelated things.
Follow the coding style as closely as possible:
The chip is very, very memory constrained.