Fix end of line
This commit is contained in:
parent
de121b6dfa
commit
92a44592d5
72
Makefile
72
Makefile
@ -1,36 +1,36 @@
|
|||||||
######################################################################
|
######################################################################
|
||||||
# User configuration
|
# User configuration
|
||||||
######################################################################
|
######################################################################
|
||||||
# Path to nodemcu-uploader (https://github.com/kmpm/nodemcu-uploader)
|
# Path to nodemcu-uploader (https://github.com/kmpm/nodemcu-uploader)
|
||||||
NODEMCU-UPLOADER=nodemcu-uploader.py
|
NODEMCU-UPLOADER=nodemcu-uploader.py
|
||||||
# Serial port
|
# Serial port
|
||||||
PORT=/dev/ttyUSB0
|
PORT=/dev/ttyUSB0
|
||||||
SPEED=460800
|
SPEED=460800
|
||||||
|
|
||||||
######################################################################
|
######################################################################
|
||||||
# End of user config
|
# End of user config
|
||||||
######################################################################
|
######################################################################
|
||||||
HTTP_FILES := $(wildcard http/*)
|
HTTP_FILES := $(wildcard http/*)
|
||||||
LUA_FILES := init.lua httpserver.lua httpserver-request.lua httpserver-static.lua httpserver-error.lua
|
LUA_FILES := init.lua httpserver.lua httpserver-request.lua httpserver-static.lua httpserver-error.lua
|
||||||
|
|
||||||
# Print usage
|
# Print usage
|
||||||
usage:
|
usage:
|
||||||
@echo "make upload_http to upload files to be served"
|
@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_server to upload the server code and init.lua"
|
||||||
@echo "make upload to upload all"
|
@echo "make upload to upload all"
|
||||||
|
|
||||||
# Upload HTTP files only
|
# Upload HTTP files only
|
||||||
upload_http: $(HTTP_FILES)
|
upload_http: $(HTTP_FILES)
|
||||||
@$(NODEMCU-UPLOADER) -b $(SPEED) -p $(PORT) upload $(foreach f, $^, $(f))
|
@$(NODEMCU-UPLOADER) -b $(SPEED) -p $(PORT) upload $(foreach f, $^, $(f))
|
||||||
|
|
||||||
# Upload httpserver lua files (init and server module)
|
# Upload httpserver lua files (init and server module)
|
||||||
upload_server: $(LUA_FILES)
|
upload_server: $(LUA_FILES)
|
||||||
@$(NODEMCU-UPLOADER) -b $(SPEED) -p $(PORT) upload $(foreach f, $^, $(f))
|
@$(NODEMCU-UPLOADER) -b $(SPEED) -p $(PORT) upload $(foreach f, $^, $(f))
|
||||||
|
|
||||||
# Upload all
|
# Upload all
|
||||||
upload: $(LUA_FILES) $(HTTP_FILES)
|
upload: $(LUA_FILES) $(HTTP_FILES)
|
||||||
@$(NODEMCU-UPLOADER) -b $(SPEED) -p $(PORT) upload $(foreach f, $^, $(f))
|
@$(NODEMCU-UPLOADER) -b $(SPEED) -p $(PORT) upload $(foreach f, $^, $(f))
|
||||||
|
|
||||||
#test
|
#test
|
||||||
test: $(LUA_FILES) $(HTTP_FILES)
|
test: $(LUA_FILES) $(HTTP_FILES)
|
||||||
echo -b $(SPEED) -p $(PORT) upload $(foreach f, $^, -f $(f) -d $(f))
|
echo -b $(SPEED) -p $(PORT) upload $(foreach f, $^, -f $(f) -d $(f))
|
||||||
|
|||||||
@ -1,29 +1,29 @@
|
|||||||
local function sendHeader(connection)
|
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")
|
connection:send("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nCache-Control: private, no-store\r\n\r\n")
|
||||||
end
|
end
|
||||||
|
|
||||||
local function sendAttr(connection, attr, val)
|
local function sendAttr(connection, attr, val)
|
||||||
connection:send("<li><b>".. attr .. ":</b> " .. val .. "<br></li>\n")
|
connection:send("<li><b>".. attr .. ":</b> " .. val .. "<br></li>\n")
|
||||||
end
|
end
|
||||||
|
|
||||||
return function (connection, args)
|
return function (connection, args)
|
||||||
collectgarbage()
|
collectgarbage()
|
||||||
sendHeader(connection)
|
sendHeader(connection)
|
||||||
connection:send('<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>A Lua script sample</title></head>')
|
connection:send('<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>A Lua script sample</title></head>')
|
||||||
connection:send('<body>')
|
connection:send('<body>')
|
||||||
connection:send('<h1>Node info</h1>')
|
connection:send('<h1>Node info</h1>')
|
||||||
majorVer, minorVer, devVer, chipid, flashid, flashsize, flashmode, flashspeed = node.info();
|
majorVer, minorVer, devVer, chipid, flashid, flashsize, flashmode, flashspeed = node.info();
|
||||||
sendAttr(connection, "majorVer" , majorVer)
|
sendAttr(connection, "majorVer" , majorVer)
|
||||||
sendAttr(connection, "devVer" , devVer)
|
sendAttr(connection, "devVer" , devVer)
|
||||||
sendAttr(connection, "chipid" , chipid)
|
sendAttr(connection, "chipid" , chipid)
|
||||||
sendAttr(connection, "flashid" , flashid)
|
sendAttr(connection, "flashid" , flashid)
|
||||||
sendAttr(connection, "flashsize" , flashsize)
|
sendAttr(connection, "flashsize" , flashsize)
|
||||||
sendAttr(connection, "flashmode" , flashmode)
|
sendAttr(connection, "flashmode" , flashmode)
|
||||||
sendAttr(connection, "flashspeed" , flashspeed)
|
sendAttr(connection, "flashspeed" , flashspeed)
|
||||||
sendAttr(connection, "node.heap()" , node.heap())
|
sendAttr(connection, "node.heap()" , node.heap())
|
||||||
sendAttr(connection, 'Memory in use (KB)' , collectgarbage("count"))
|
sendAttr(connection, 'Memory in use (KB)' , collectgarbage("count"))
|
||||||
sendAttr(connection, 'IP address' , wifi.sta.getip())
|
sendAttr(connection, 'IP address' , wifi.sta.getip())
|
||||||
sendAttr(connection, 'MAC address' , wifi.sta.getmac())
|
sendAttr(connection, 'MAC address' , wifi.sta.getmac())
|
||||||
connection:send('</ul>')
|
connection:send('</ul>')
|
||||||
connection:send('</body></html>')
|
connection:send('</body></html>')
|
||||||
end
|
end
|
||||||
|
|||||||
51
http/ual.lua
51
http/ual.lua
@ -1,26 +1,25 @@
|
|||||||
local function storelist(table)
|
local function storelist(table)
|
||||||
file.remove("http/aplist.json")
|
file.remove("http/aplist.json")
|
||||||
file.open("http/aplist.json","w")
|
file.open("http/aplist.json","w")
|
||||||
file.write(cjson.encode(table))
|
coroutine.yield()
|
||||||
file.close()
|
file.write(cjson.encode(table))
|
||||||
print(cjson.encode(table))
|
file.close()
|
||||||
coroutine.yield()
|
print(cjson.encode(table))
|
||||||
table = nil
|
end
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
local function sendHeader(connection)
|
||||||
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("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"}')
|
||||||
connection:send('{"error":0, "message":"OK"}')
|
end
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
return function(connection,args)
|
||||||
return function(connection,args)
|
sendHeader(connection)
|
||||||
sendHeader(connection)
|
wifi.sta.getap(storelist)
|
||||||
wifi.sta.getap(storelist)
|
end
|
||||||
end
|
|
||||||
|
|||||||
162
httpserver.lua
162
httpserver.lua
@ -1,81 +1,81 @@
|
|||||||
-- httpserver
|
-- httpserver
|
||||||
-- Author: Marcos Kirsch
|
-- Author: Marcos Kirsch
|
||||||
|
|
||||||
-- Starts web server in the specified port.
|
-- Starts web server in the specified port.
|
||||||
return function (port)
|
return function (port)
|
||||||
|
|
||||||
local s = net.createServer(net.TCP, 10) -- 10 seconds client timeout
|
local s = net.createServer(net.TCP, 10) -- 10 seconds client timeout
|
||||||
s:listen(
|
s:listen(
|
||||||
port,
|
port,
|
||||||
function (connection)
|
function (connection)
|
||||||
|
|
||||||
-- This variable holds the thread used for sending data back to the user.
|
-- 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
|
-- 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.
|
-- of data in order to avoid overflowing the mcu's buffer.
|
||||||
local connectionThread
|
local connectionThread
|
||||||
|
|
||||||
local function onGet(connection, uri)
|
local function onGet(connection, uri)
|
||||||
local fileServeFunction = nil
|
local fileServeFunction = nil
|
||||||
if #(uri.file) > 32 then
|
if #(uri.file) > 32 then
|
||||||
-- nodemcu-firmware cannot handle long filenames.
|
-- nodemcu-firmware cannot handle long filenames.
|
||||||
uri.args['code'] = 400
|
uri.args['code'] = 400
|
||||||
fileServeFunction = dofile("httpserver-error.lc")
|
fileServeFunction = dofile("httpserver-error.lc")
|
||||||
else
|
else
|
||||||
local fileExists = file.open(uri.file, "r")
|
local fileExists = file.open(uri.file, "r")
|
||||||
file.close()
|
file.close()
|
||||||
if not fileExists then
|
if not fileExists then
|
||||||
uri.args['code'] = 404
|
uri.args['code'] = 404
|
||||||
fileServeFunction = dofile("httpserver-error.lc")
|
fileServeFunction = dofile("httpserver-error.lc")
|
||||||
elseif uri.isScript then
|
elseif uri.isScript then
|
||||||
collectgarbage()
|
collectgarbage()
|
||||||
fileServeFunction = dofile(uri.file)
|
fileServeFunction = dofile(uri.file)
|
||||||
else
|
else
|
||||||
uri.args['file'] = uri.file
|
uri.args['file'] = uri.file
|
||||||
uri.args['ext'] = uri.ext
|
uri.args['ext'] = uri.ext
|
||||||
fileServeFunction = dofile("httpserver-static.lc")
|
fileServeFunction = dofile("httpserver-static.lc")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
connectionThread = coroutine.create(fileServeFunction)
|
connectionThread = coroutine.create(fileServeFunction)
|
||||||
--print("Thread created", connectionThread)
|
--print("Thread created", connectionThread)
|
||||||
coroutine.resume(connectionThread, connection, uri.args)
|
coroutine.resume(connectionThread, connection, uri.args)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function onReceive(connection, payload)
|
local function onReceive(connection, payload)
|
||||||
-- print(payload) -- for debugging
|
-- print(payload) -- for debugging
|
||||||
-- parse payload and decide what to serve.
|
-- parse payload and decide what to serve.
|
||||||
local req = dofile("httpserver-request.lc")(payload)
|
local req = dofile("httpserver-request.lc")(payload)
|
||||||
print("Requested URI: " .. req.request)
|
print("Requested URI: " .. req.request)
|
||||||
if req.methodIsValid then
|
if req.methodIsValid then
|
||||||
if req.method == "GET" then onGet(connection, req.uri)
|
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=501}) end
|
||||||
else
|
else
|
||||||
dofile("httpserver-static.lc")(conection, {code=400})
|
dofile("httpserver-static.lc")(conection, {code=400})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function onSent(connection, payload)
|
local function onSent(connection, payload)
|
||||||
local connectionThreadStatus = coroutine.status(connectionThread)
|
local connectionThreadStatus = coroutine.status(connectionThread)
|
||||||
-- print (connectionThread, "status is", connectionThreadStatus)
|
-- print (connectionThread, "status is", connectionThreadStatus)
|
||||||
if connectionThreadStatus == "suspended" then
|
if connectionThreadStatus == "suspended" then
|
||||||
-- Not finished sending file, resume.
|
-- Not finished sending file, resume.
|
||||||
-- print("Resume thread", connectionThread)
|
-- print("Resume thread", connectionThread)
|
||||||
coroutine.resume(connectionThread)
|
coroutine.resume(connectionThread)
|
||||||
elseif connectionThreadStatus == "dead" then
|
elseif connectionThreadStatus == "dead" then
|
||||||
-- We're done sending file.
|
-- We're done sending file.
|
||||||
-- print("Done thread", connectionThread)
|
-- print("Done thread", connectionThread)
|
||||||
connection:close()
|
connection:close()
|
||||||
connectionThread = nil
|
connectionThread = nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
connection:on("receive", onReceive)
|
connection:on("receive", onReceive)
|
||||||
connection:on("sent", onSent)
|
connection:on("sent", onSent)
|
||||||
|
|
||||||
end
|
end
|
||||||
)
|
)
|
||||||
if wifi.sta.getip() then 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)
|
else print("nodemcu-httpserver running at http://" .. wifi.ap.getip() .. ":" .. port)
|
||||||
end
|
end
|
||||||
return s
|
return s
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
102
init.lua
102
init.lua
@ -1,51 +1,51 @@
|
|||||||
-- Tel--l 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.STATIONAP)
|
||||||
wifi.setmode(wifi.STATION)
|
wifi.setmode(wifi.STATION)
|
||||||
print('set (mode='..wifi.getmode()..')')
|
print('set (mode='..wifi.getmode()..')')
|
||||||
print('MAC: ',wifi.sta.getmac())
|
print('MAC: ',wifi.sta.getmac())
|
||||||
print('chip: ',node.chipid())
|
print('chip: ',node.chipid())
|
||||||
print('heap: ',node.heap())
|
print('heap: ',node.heap())
|
||||||
|
|
||||||
local joincounter = 0
|
local joincounter = 0
|
||||||
|
|
||||||
cfg={}
|
cfg={}
|
||||||
cfg.ssid="ESP-"..node.chipid()
|
cfg.ssid="ESP-"..node.chipid()
|
||||||
cfg.pwd="ESP-"..node.chipid()
|
cfg.pwd="ESP-"..node.chipid()
|
||||||
wifi.ap.config(cfg)
|
wifi.ap.config(cfg)
|
||||||
cfg = nil
|
cfg = nil
|
||||||
|
|
||||||
-- Compile server code and remove original .lua files.
|
-- Compile server code and remove original .lua files.
|
||||||
-- This only happens the first time afer the .lua files are uploaded.
|
-- This only happens the first time afer the .lua files are uploaded.
|
||||||
|
|
||||||
local compileAndRemoveIfNeeded = function(f)
|
local compileAndRemoveIfNeeded = function(f)
|
||||||
if file.open(f) then
|
if file.open(f) then
|
||||||
file.close()
|
file.close()
|
||||||
node.compile(f)
|
node.compile(f)
|
||||||
file.remove(f)
|
file.remove(f)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local serverFiles = {'httpserver.lua', 'httpserver-request.lua', 'httpserver-static.lua', 'httpserver-error.lua','http/ual.lua'}
|
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
|
for i, f in ipairs(serverFiles) do compileAndRemoveIfNeeded(f) end
|
||||||
|
|
||||||
compileAndRemoveIfNeeded = nil
|
compileAndRemoveIfNeeded = nil
|
||||||
serverFiles = nil
|
serverFiles = nil
|
||||||
|
|
||||||
-- Connect to the WiFi access point. Once the device is connected,
|
-- Connect to the WiFi access point. Once the device is connected,
|
||||||
-- you may start the HTTP server.
|
-- you may start the HTTP server.
|
||||||
tmr.alarm(0, 3000, 1, function()
|
tmr.alarm(0, 3000, 1, function()
|
||||||
|
|
||||||
if wifi.sta.getip() == nil and joincounter < 5 then
|
if wifi.sta.getip() == nil and joincounter < 5 then
|
||||||
print("Connecting to AP...")
|
print("Connecting to AP...")
|
||||||
joincounter = joincounter +1
|
joincounter = joincounter +1
|
||||||
else
|
else
|
||||||
tmr.stop(0)
|
tmr.stop(0)
|
||||||
-- print('IP: ',wifi.sta.getip())
|
-- print('IP: ',wifi.sta.getip())
|
||||||
-- Uncomment to automatically start the server in port 80
|
-- Uncomment to automatically start the server in port 80
|
||||||
joincounter = nil
|
joincounter = nil
|
||||||
collectgarbage()
|
collectgarbage()
|
||||||
dofile("httpserver.lc")(80)
|
dofile("httpserver.lc")(80)
|
||||||
end
|
end
|
||||||
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user