Browse Source

Fix end of line

Artem Pastukhov 9 years ago
parent
commit
92a44592d5
5 changed files with 222 additions and 223 deletions
  1. 36 36
      Makefile
  2. 29 29
      http/node_info.lua
  3. 25 26
      http/ual.lua
  4. 81 81
      httpserver.lua
  5. 51 51
      init.lua

+ 36 - 36
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))

+ 29 - 29
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("<li><b>".. attr .. ":</b> " .. val .. "<br></li>\n")
-end
-
-return function (connection, args)
-   collectgarbage()
-   sendHeader(connection)
-   connection:send('<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>A Lua script sample</title></head>')
-   connection:send('<body>')
-   connection:send('<h1>Node info</h1>')
-   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('</ul>')
-   connection:send('</body></html>')
-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("<li><b>".. attr .. ":</b> " .. val .. "<br></li>\n")
+end
+
+return function (connection, args)
+   collectgarbage()
+   sendHeader(connection)
+   connection:send('<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>A Lua script sample</title></head>')
+   connection:send('<body>')
+   connection:send('<h1>Node info</h1>')
+   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('</ul>')
+   connection:send('</body></html>')
+end

+ 25 - 26
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

+ 81 - 81
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

+ 51 - 51
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)
+