No Description

Marcos Kirsch 316af66d3c Improve demo files 9 years ago
http 316af66d3c Improve demo files 9 years ago
LICENSE 49d8dc8d43 Initial commit 9 years ago
README.md ac387a6c2c add open issue 9 years ago
b64.lua b0cbe2a77f Fix for Lua builds without floating point. 9 years ago
b64.py f52e8f47e6 Add all files to source control even though they are messy and don't do much yet 9 years ago
garage.lua f52e8f47e6 Add all files to source control even though they are messy and don't do much yet 9 years ago
httpserver.lua c0d6218397 Support for Lua scripts 9 years ago
init.lua 3f5caa7afb Clean up 9 years ago
makefile 44b40f00f3 Fixed upload_lua target in makefile 9 years ago
test.lua d137648706 More of a scratch pad, shouldn't be in source control at all but oh well... 9 years ago

README.md

nodemcu-httpserver

A (very) simple web server written in Lua for the ESP8266 firmware NodeMCU.

Features

  • GET
  • Multiple MIME types
  • Error pages (404 and others)
  • Remote execution of Lua scripts

How to use

  1. Upload httpserver.lua using luatool.py or equivalent. Add the following to your init.lua in order to start the server:

     require("httpserver")
     server = httpserver.start(80, 10)
    

80 is the port your server is listening to, and 10 is the timeout (in seconds) for clients.

  1. Upload the files you want to serve. Again, use luatool.py or similar and upload the HTML and other files.

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.

  1. Visit your server from a web browser.

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 using your web browser. The ESP8266 will serve you with the contents of the file «http/myPage.html» (if it exists). If you visit the root (/) then index.html is server. By the way, the URLs are case-sensitive.

How to create dynamic Lua scripts

Similar to static files, upload a Lua script called «http/[name].lua where you replace [name] with the script’s name. The script should return a function that takes two parameters:

  return function (connection, 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. 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».

Not supported

  • Other methods: HEAD, POST, PUT, DELETE, TRACE, OPTIONS, CONNECT, PATCH
  • Server side scripting.

Open issues

  • File system doesn’t like long names, need to protect:

    PANIC: unprotected error in call to Lua API (httpserver.lua:78: filename too long)
    
  • System is very, very memory constrained. Remote execution of Lua scripts only works with trivial scripts.