diff --git a/makefile b/Makefile similarity index 89% rename from makefile rename to Makefile index a8ddd25..4ff9c0e 100644 --- a/makefile +++ b/Makefile @@ -1,34 +1,33 @@ -###################################################################### -# User configuration -###################################################################### -# Path to nodemcu-uploader (https://github.com/kmpm/nodemcu-uploader) -NODEMCU-UPLOADER=../nodemcu-uploader/nodemcu-uploader.py -# Serial port -PORT=/dev/cu.usbserial-A602HRAZ -# Bauds for the serial connection -SPEED=115200 - -###################################################################### -# End of user config -###################################################################### -HTTP_FILES := $(wildcard http/*) -LUA_FILES := init.lua httpserver.lua httpserver-request.lua httpserver-static.lua httpserver-error.lua - -# Print usage -usage: - @echo "make upload_http to upload files to be served" - @echo "make upload_server to upload the server code and init.lua" - @echo "make upload to upload all" - -# Upload HTTP files only -upload_http: $(HTTP_FILES) - @$(NODEMCU-UPLOADER) -b $(SPEED) -p $(PORT) upload $(foreach f, $^, $(f)) - -# Upload httpserver lua files (init and server module) -upload_server: $(LUA_FILES) - @$(NODEMCU-UPLOADER) -b $(SPEED) -p $(PORT) upload $(foreach f, $^, $(f)) - -# Upload all -upload: $(LUA_FILES) $(HTTP_FILES) - @$(NODEMCU-UPLOADER) -b $(SPEED) -p $(PORT) upload $(foreach f, $^, $(f)) - +###################################################################### +# User configuration +###################################################################### +# Path to nodemcu-uploader (https://github.com/kmpm/nodemcu-uploader) +NODEMCU-UPLOADER=nodemcu-uploader.py +# Serial port +PORT=/dev/ttyUSB0 +SPEED=460800 + +###################################################################### +# End of user config +###################################################################### +HTTP_FILES := $(wildcard http/*) +LUA_FILES := init.lua httpserver.lua httpserver-request.lua httpserver-static.lua httpserver-error.lua + +# Print usage +usage: + @echo "make upload_http to upload files to be served" + @echo "make upload_server to upload the server code and init.lua" + @echo "make upload to upload all" + +# Upload HTTP files only +upload_http: $(HTTP_FILES) + @$(NODEMCU-UPLOADER) -b $(SPEED) -p $(PORT) upload $(foreach f, $^, $(f)) + +# Upload httpserver lua files (init and server module) +upload_server: $(LUA_FILES) + @$(NODEMCU-UPLOADER) -b $(SPEED) -p $(PORT) upload $(foreach f, $^, $(f)) + +# Upload all +upload: $(LUA_FILES) $(HTTP_FILES) + @$(NODEMCU-UPLOADER) -b $(SPEED) -p $(PORT) upload $(foreach f, $^, $(f)) + diff --git a/http/jquery.min.js.gz b/http/jquery.min.js.gz new file mode 100644 index 0000000..17ea876 Binary files /dev/null and b/http/jquery.min.js.gz differ diff --git a/http/jquerytest.html b/http/jquerytest.html new file mode 100644 index 0000000..7c27b45 --- /dev/null +++ b/http/jquerytest.html @@ -0,0 +1,16 @@ + + + + + + +

Jquery test page

+ + \ No newline at end of file diff --git a/http/node_info.lua b/http/node_info.lua index 0cd02d1..0d24156 100644 --- a/http/node_info.lua +++ b/http/node_info.lua @@ -1,5 +1,6 @@ local function sendHeader(connection) - connection:send("HTTP/1.0 200 OK\r\nContent-Type: text/html\rCache-Control: private, no-store\r\n\r\n") + connection:send("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nCache-Control: private, no-store\r\n\r\n") + end local function sendAttr(connection, attr, val) diff --git a/httpserver-request.lua b/httpserver-request.lua index 9562068..638dc7a 100644 --- a/httpserver-request.lua +++ b/httpserver-request.lua @@ -24,6 +24,10 @@ end local function parseUri(uri) local r = {} + local filename + local ext + local fullExt = {} + if uri == nil then return r end if uri == "/" then uri = "/index.html" end questionMarkPos, b, c, d, e, f = uri:find("?") @@ -34,7 +38,12 @@ local function parseUri(uri) r.file = uri:sub(1, questionMarkPos - 1) r.args = parseArgs(uri:sub(questionMarkPos+1, #uri)) end - _, r.ext = r.file:match("(.+)%.(.+)") + filename = r.file + while filename:match("%.") do + filename,ext = filename:match("(.+)%.(.+)") + table.insert(fullExt,1,ext) + end + r.ext = table.concat(fullExt,".") r.isScript = r.ext == "lua" or r.ext == "lc" r.file = uriToFilename(r.file) return r diff --git a/httpserver-static.lua b/httpserver-static.lua index 7448fe8..e7ca343 100644 --- a/httpserver-static.lua +++ b/httpserver-static.lua @@ -3,13 +3,25 @@ -- Author: Marcos Kirsch local function getMimeType(ext) + local gzip = false -- A few MIME types. Keep list short. If you need something that is missing, let's add it. - local mt = {css = "text/css", gif = "image/gif", html = "text/html", ico = "image/x-icon", jpeg = "image/jpeg", jpg = "image/jpeg", js = "application/javascript", json="application/json", png = "image/png"} - if mt[ext] then return mt[ext] else return "text/plain" end + local mt = {css = "text/css", gif = "image/gif", html = "text/html", ico = "image/x-icon", jpeg = "image/jpeg", jpg = "image/jpeg", js = "application/javascript", json = "application/json", png = "image/png"} + -- add comressed flag if file ends with gz + if ext:find("%.gz$") then + ext = ext:sub(1, -4) + gzip = true + end + if mt[ext] then contentType = mt[ext] else contentType = "text/plain" end + return {contentType = contentType, gzip = gzip } + end local function sendHeader(connection, code, codeString, mimeType) - connection:send("HTTP/1.0 " .. code .. " " .. codeString .. "\r\nServer: nodemcu-httpserver\r\nContent-Type: " .. mimeType .. "\r\nConnection: close\r\n\r\n") + connection:send("HTTP/1.0 " .. code .. " " .. codeString .. "\r\nServer: nodemcu-httpserver\r\nContent-Type: " .. mimeType["contentType"] .. "\r\n") + if mimeType["gzip"] then + connection:send("Content-Encoding: gzip\r\n") + end + connection:send("Connection: close\r\n\r\n") end return function (connection, args) diff --git a/httpserver.lua b/httpserver.lua index 2e16a6f..1b61549 100644 --- a/httpserver.lua +++ b/httpserver.lua @@ -73,7 +73,9 @@ return function (port) end ) - print("nodemcu-httpserver running at http://" .. wifi.sta.getip() .. ":" .. port) + if wifi.sta.getip() then print("nodemcu-httpserver running at http://" .. wifi.sta.getip() .. ":" .. port) + else print("nodemcu-httpserver running at http://" .. wifi.ap.getip() .. ":" .. port) + end return s end diff --git a/init.lua b/init.lua index 1a872f9..971936b 100644 --- a/init.lua +++ b/init.lua @@ -1,10 +1,17 @@ --- Tell the chip to connect to the access point +-- Tel--l the chip to connect to the access point +--wifi.setmode(wifi.STATIONAP) wifi.setmode(wifi.STATION) -print('set mode=STATION (mode='..wifi.getmode()..')') +print('set (mode='..wifi.getmode()..')') print('MAC: ',wifi.sta.getmac()) print('chip: ',node.chipid()) print('heap: ',node.heap()) -wifi.sta.config("Internet","") + + +local cfg={} +cfg.ssid="ESP-"..node.chipid() +cfg.pwd="ESP-"..node.chipid() +wifi.ap.config(cfg) +cfg = nil -- Compile server code and remove original .lua files. -- This only happens the first time afer the .lua files are uploaded. @@ -12,6 +19,7 @@ wifi.sta.config("Internet","") local compileAndRemoveIfNeeded = function(f) if file.open(f) then file.close() + print(f) node.compile(f) file.remove(f) end @@ -25,14 +33,22 @@ serverFiles = nil -- Connect to the WiFi access point. Once the device is connected, -- you may start the HTTP server. + +local joincounter = 0 + tmr.alarm(0, 3000, 1, function() - if wifi.sta.getip() == nil then + + if wifi.sta.getip() == nil and joincounter < 5 then print("Connecting to AP...") - else + joincounter = joincounter +1 + else tmr.stop(0) - print('IP: ',wifi.sta.getip()) + -- print('IP: ',wifi.sta.getip()) -- Uncomment to automatically start the server in port 80 - -- dofile("httpserver.lc")(80) + joincounter = nil + collectgarbage() + dofile("httpserver.lc")(80) end + end)