From 92a44592d50e1e33210ef3038461d34bf22b8ec5 Mon Sep 17 00:00:00 2001 From: Artem Pastukhov Date: Wed, 1 Apr 2015 08:57:18 +0300 Subject: [PATCH] Fix end of line --- Makefile | 72 ++++++++++---------- http/node_info.lua | 58 ++++++++-------- http/ual.lua | 51 +++++++------- httpserver.lua | 162 ++++++++++++++++++++++----------------------- init.lua | 102 ++++++++++++++-------------- 5 files changed, 222 insertions(+), 223 deletions(-) diff --git a/Makefile b/Makefile index dd7b030..1d1d900 100644 --- a/Makefile +++ b/Makefile @@ -1,36 +1,36 @@ -###################################################################### -# 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)) - -#test -test: $(LUA_FILES) $(HTTP_FILES) - echo -b $(SPEED) -p $(PORT) upload $(foreach f, $^, -f $(f) -d $(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)) + +#test +test: $(LUA_FILES) $(HTTP_FILES) + echo -b $(SPEED) -p $(PORT) upload $(foreach f, $^, -f $(f) -d $(f)) diff --git a/http/node_info.lua b/http/node_info.lua index ccb5bad..7ca4e4e 100644 --- a/http/node_info.lua +++ b/http/node_info.lua @@ -1,29 +1,29 @@ -local function sendHeader(connection) - 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) - connection:send("
  • ".. attr .. ": " .. val .. "
  • \n") -end - -return function (connection, args) - collectgarbage() - sendHeader(connection) - connection:send('A Lua script sample') - connection:send('') - connection:send('

    Node info

    ') - majorVer, minorVer, devVer, chipid, flashid, flashsize, flashmode, flashspeed = node.info(); - sendAttr(connection, "majorVer" , majorVer) - sendAttr(connection, "devVer" , devVer) - sendAttr(connection, "chipid" , chipid) - sendAttr(connection, "flashid" , flashid) - sendAttr(connection, "flashsize" , flashsize) - sendAttr(connection, "flashmode" , flashmode) - sendAttr(connection, "flashspeed" , flashspeed) - sendAttr(connection, "node.heap()" , node.heap()) - sendAttr(connection, 'Memory in use (KB)' , collectgarbage("count")) - sendAttr(connection, 'IP address' , wifi.sta.getip()) - sendAttr(connection, 'MAC address' , wifi.sta.getmac()) - connection:send('') - connection:send('') -end +local function sendHeader(connection) + 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) + connection:send("
  • ".. attr .. ": " .. val .. "
  • \n") +end + +return function (connection, args) + collectgarbage() + sendHeader(connection) + connection:send('A Lua script sample') + connection:send('') + connection:send('

    Node info

    ') + majorVer, minorVer, devVer, chipid, flashid, flashsize, flashmode, flashspeed = node.info(); + sendAttr(connection, "majorVer" , majorVer) + sendAttr(connection, "devVer" , devVer) + sendAttr(connection, "chipid" , chipid) + sendAttr(connection, "flashid" , flashid) + sendAttr(connection, "flashsize" , flashsize) + sendAttr(connection, "flashmode" , flashmode) + sendAttr(connection, "flashspeed" , flashspeed) + sendAttr(connection, "node.heap()" , node.heap()) + sendAttr(connection, 'Memory in use (KB)' , collectgarbage("count")) + sendAttr(connection, 'IP address' , wifi.sta.getip()) + sendAttr(connection, 'MAC address' , wifi.sta.getmac()) + connection:send('') + connection:send('') +end diff --git a/http/ual.lua b/http/ual.lua index 301ff2a..f86a799 100644 --- a/http/ual.lua +++ b/http/ual.lua @@ -1,26 +1,25 @@ -local function storelist(table) - file.remove("http/aplist.json") - file.open("http/aplist.json","w") - file.write(cjson.encode(table)) - file.close() - print(cjson.encode(table)) - coroutine.yield() - table = nil -end - - - - - - -local function sendHeader(connection) - connection:send("HTTP/1.0 200 OK\r\nContent-Type: application/json\r\nCache-Control: private, no-store\r\n\r\n") - connection:send('{"error":0, "message":"OK"}') -end - - - -return function(connection,args) - sendHeader(connection) - wifi.sta.getap(storelist) -end +local function storelist(table) + file.remove("http/aplist.json") + file.open("http/aplist.json","w") + coroutine.yield() + file.write(cjson.encode(table)) + file.close() + print(cjson.encode(table)) +end + + + + + + +local function sendHeader(connection) + connection:send("HTTP/1.0 200 OK\r\nContent-Type: application/json\r\nCache-Control: private, no-store\r\n\r\n") + connection:send('{"error":0, "message":"OK"}') +end + + + +return function(connection,args) + sendHeader(connection) + wifi.sta.getap(storelist) +end diff --git a/httpserver.lua b/httpserver.lua index 9279678..1b61549 100644 --- a/httpserver.lua +++ b/httpserver.lua @@ -1,81 +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 - ) - 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 +-- 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 0080b80..bc5490f 100644 --- a/init.lua +++ b/init.lua @@ -1,51 +1,51 @@ --- 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','http/ual.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) - +-- 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','http/ual.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) +