From 0120924403ce2cc49e4ee9492d93743e600967aa Mon Sep 17 00:00:00 2001 From: Artem Pastukhov Date: Tue, 31 Mar 2015 10:02:33 +0300 Subject: [PATCH] Fix some typos --- httpserver-static.lua | 2 +- httpserver.lua | 160 +++++++++++++++++++++--------------------- init.lua | 89 +++++++++++++---------- makefile | 32 --------- 4 files changed, 133 insertions(+), 150 deletions(-) delete mode 100644 makefile diff --git a/httpserver-static.lua b/httpserver-static.lua index e48e5ce..7758548 100644 --- a/httpserver-static.lua +++ b/httpserver-static.lua @@ -4,7 +4,7 @@ local function getMimeType(ext) -- 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", josn="application/json", png = "image/png"} + 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 end diff --git a/httpserver.lua b/httpserver.lua index 2e16a6f..9279678 100644 --- a/httpserver.lua +++ b/httpserver.lua @@ -1,79 +1,81 @@ --- httpserver --- Author: Marcos Kirsch - --- Starts web server in the specified port. -return function (port) - - local s = net.createServer(net.TCP, 10) -- 10 seconds client timeout - s:listen( - port, - function (connection) - - -- This variable holds the thread used for sending data back to the user. - -- We do it in a separate thread because we need to yield when sending lots - -- of data in order to avoid overflowing the mcu's buffer. - local connectionThread - - local function onGet(connection, uri) - local fileServeFunction = nil - if #(uri.file) > 32 then - -- nodemcu-firmware cannot handle long filenames. - uri.args['code'] = 400 - fileServeFunction = dofile("httpserver-error.lc") - else - local fileExists = file.open(uri.file, "r") - file.close() - if not fileExists then - uri.args['code'] = 404 - fileServeFunction = dofile("httpserver-error.lc") - elseif uri.isScript then - collectgarbage() - fileServeFunction = dofile(uri.file) - else - uri.args['file'] = uri.file - uri.args['ext'] = uri.ext - fileServeFunction = dofile("httpserver-static.lc") - end - end - connectionThread = coroutine.create(fileServeFunction) - --print("Thread created", connectionThread) - coroutine.resume(connectionThread, connection, uri.args) - end - - local function onReceive(connection, payload) - -- print(payload) -- for debugging - -- parse payload and decide what to serve. - local req = dofile("httpserver-request.lc")(payload) - print("Requested URI: " .. req.request) - if req.methodIsValid then - if req.method == "GET" then onGet(connection, req.uri) - else dofile("httpserver-static.lc")(conection, {code=501}) end - else - dofile("httpserver-static.lc")(conection, {code=400}) - end - end - - local function onSent(connection, payload) - local connectionThreadStatus = coroutine.status(connectionThread) - -- print (connectionThread, "status is", connectionThreadStatus) - if connectionThreadStatus == "suspended" then - -- Not finished sending file, resume. - -- print("Resume thread", connectionThread) - coroutine.resume(connectionThread) - elseif connectionThreadStatus == "dead" then - -- We're done sending file. - -- print("Done thread", connectionThread) - connection:close() - connectionThread = nil - end - end - - connection:on("receive", onReceive) - connection:on("sent", onSent) - - end - ) - print("nodemcu-httpserver running at http://" .. wifi.sta.getip() .. ":" .. port) - return s - -end +-- httpserver +-- Author: Marcos Kirsch + +-- Starts web server in the specified port. +return function (port) + + local s = net.createServer(net.TCP, 10) -- 10 seconds client timeout + s:listen( + port, + function (connection) + + -- This variable holds the thread used for sending data back to the user. + -- We do it in a separate thread because we need to yield when sending lots + -- of data in order to avoid overflowing the mcu's buffer. + local connectionThread + + local function onGet(connection, uri) + local fileServeFunction = nil + if #(uri.file) > 32 then + -- nodemcu-firmware cannot handle long filenames. + uri.args['code'] = 400 + fileServeFunction = dofile("httpserver-error.lc") + else + local fileExists = file.open(uri.file, "r") + file.close() + if not fileExists then + uri.args['code'] = 404 + fileServeFunction = dofile("httpserver-error.lc") + elseif uri.isScript then + collectgarbage() + fileServeFunction = dofile(uri.file) + else + uri.args['file'] = uri.file + uri.args['ext'] = uri.ext + fileServeFunction = dofile("httpserver-static.lc") + end + end + connectionThread = coroutine.create(fileServeFunction) + --print("Thread created", connectionThread) + coroutine.resume(connectionThread, connection, uri.args) + end + + local function onReceive(connection, payload) + -- print(payload) -- for debugging + -- parse payload and decide what to serve. + local req = dofile("httpserver-request.lc")(payload) + print("Requested URI: " .. req.request) + if req.methodIsValid then + if req.method == "GET" then onGet(connection, req.uri) + else dofile("httpserver-static.lc")(conection, {code=501}) end + else + dofile("httpserver-static.lc")(conection, {code=400}) + end + end + + local function onSent(connection, payload) + local connectionThreadStatus = coroutine.status(connectionThread) + -- print (connectionThread, "status is", connectionThreadStatus) + if connectionThreadStatus == "suspended" then + -- Not finished sending file, resume. + -- print("Resume thread", connectionThread) + coroutine.resume(connectionThread) + elseif connectionThreadStatus == "dead" then + -- We're done sending file. + -- print("Done thread", connectionThread) + connection:close() + connectionThread = nil + end + end + + connection:on("receive", onReceive) + connection:on("sent", onSent) + + end + ) + 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..ce73c1f 100644 --- a/init.lua +++ b/init.lua @@ -1,38 +1,51 @@ --- Tell the chip to connect to the access point -wifi.setmode(wifi.STATION) -print('set mode=STATION (mode='..wifi.getmode()..')') -print('MAC: ',wifi.sta.getmac()) -print('chip: ',node.chipid()) -print('heap: ',node.heap()) -wifi.sta.config("Internet","") - --- Compile server code and remove original .lua files. --- This only happens the first time afer the .lua files are uploaded. - -local compileAndRemoveIfNeeded = function(f) - if file.open(f) then - file.close() - node.compile(f) - file.remove(f) - end -end - -local serverFiles = {'httpserver.lua', 'httpserver-request.lua', 'httpserver-static.lua', 'httpserver-error.lua'} -for i, f in ipairs(serverFiles) do compileAndRemoveIfNeeded(f) end - -compileAndRemoveIfNeeded = nil -serverFiles = nil - --- Connect to the WiFi access point. Once the device is connected, --- you may start the HTTP server. -tmr.alarm(0, 3000, 1, function() - if wifi.sta.getip() == nil then - print("Connecting to AP...") - else - tmr.stop(0) - print('IP: ',wifi.sta.getip()) - -- Uncomment to automatically start the server in port 80 - -- dofile("httpserver.lc")(80) - end -end) - +-- Tel--l the chip to connect to the access point +--wifi.setmode(wifi.STATIONAP) +wifi.setmode(wifi.STATION) +print('set (mode='..wifi.getmode()..')') +print('MAC: ',wifi.sta.getmac()) +print('chip: ',node.chipid()) +print('heap: ',node.heap()) + +local joincounter = 0 + +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. + +local compileAndRemoveIfNeeded = function(f) + if file.open(f) then + file.close() + node.compile(f) + file.remove(f) + end +end + +local serverFiles = {'httpserver.lua', 'httpserver-request.lua', 'httpserver-static.lua', 'httpserver-error.lua'} +for i, f in ipairs(serverFiles) do compileAndRemoveIfNeeded(f) end + +compileAndRemoveIfNeeded = nil +serverFiles = nil + +-- Connect to the WiFi access point. Once the device is connected, +-- you may start the HTTP server. +tmr.alarm(0, 3000, 1, function() + + if wifi.sta.getip() == nil and joincounter < 5 then + print("Connecting to AP...") + joincounter = joincounter +1 + else + tmr.stop(0) + -- print('IP: ',wifi.sta.getip()) + -- Uncomment to automatically start the server in port 80 + joincounter = nil + collectgarbage() + dofile("httpserver.lc")(80) + end + +end) + diff --git a/makefile b/makefile deleted file mode 100644 index 011962b..0000000 --- a/makefile +++ /dev/null @@ -1,32 +0,0 @@ -###################################################################### -# 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 - -###################################################################### -# 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) -p $(PORT) upload $(foreach f, $^, -f $(f) -d $(f)) - -# Upload httpserver lua files (init and server module) -upload_server: $(LUA_FILES) - @$(NODEMCU-UPLOADER) -p $(PORT) upload $(foreach f, $^, -f $(f) -d $(f)) - -# Upload all -upload: $(LUA_FILES) $(HTTP_FILES) - @$(NODEMCU-UPLOADER) -p $(PORT) upload $(foreach f, $^, -f $(f) -d $(f)) -