repeat83

esp8266+temper

May 1st, 2016
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.21 KB | None | 0 0
  1. -- start a web server.  Return the web server object
  2. function startWeb(cfg)
  3.   -- define member variables
  4.   local config = cfg
  5.   local state = "inactive"
  6.   local server = net.createServer(net.TCP)
  7.  
  8.   -- define functions
  9.   local close = function()
  10.     server:close()
  11.     state = "inactive"
  12.   end
  13.  
  14.   local getStatus = function()
  15.     return state
  16.   end
  17.  
  18.   local parseRequest = function(request)
  19.     -- the first line of the request is in the form METHOD URL PROTOCOL
  20.     _, _, method, url = string.find(request, "(%a+)%s([^%s]+)")
  21.     _, _, path, queryString = string.find(url, "([^%s]+)%?([^%s]+)")
  22.     if queryString then
  23.       query = {}
  24.       for name, value in string.gfind(queryString, "([^&=]+)=([^&=]+)") do
  25.         query[name] = value
  26.       end
  27.     else
  28.       path = url
  29.       query = nil
  30.     end
  31.     return { method = method, url = url, path = path, query = query, queryString = queryString}
  32.   end
  33.  
  34.   -- start listening for requests
  35.   server:listen(80,function(s)
  36.     s:on("receive", function(s, rawRequest)
  37.    local isopen = false
  38.       request = parseRequest(rawRequest)
  39.       print("Request received: ", request.method, request.url,request.path)
  40.       if config.pages[request.path] then
  41.         response = config.pages[request.path](request)
  42.         status = "200 OK"
  43.       else
  44.         response = "<html><body><p>" .. request.url .. " doesn't exist.</p></body></html>"
  45.         status = "404 Not Found"
  46.       end
  47.       headers = "HTTP/1.1 " .. status .. "\r\nConnection: keep-alive\r\nCache-Control: private, no-store\r\nContent-Length: " .. string.len(response) .. "\r\n\r\n"
  48.       s:send(headers .. response)
  49.     end)
  50.    s:on("sent", function(s)
  51.                 if not isopen then
  52.                    isopen = true
  53.                    file.open("led_gif.html", "r")
  54.                 end
  55.                 local data = file.read()
  56.                 if data then
  57.                    s:send(data)
  58.                 else
  59.                    file.close()
  60.                    s:close()
  61.                    s = nil
  62.                 end
  63.              end)
  64.   end)
  65.  
  66.   state = "listening"
  67.  
  68.   return { getStatus = getStatus, close = close }
  69.  
  70.  
  71. end
  72. pages = {}
  73.  
  74. pages["/led.gif"] = function(request)
  75.     file.open("led.gif")
  76.      X = file.read()
  77.      file.close()
  78.      tmr.wdclr()
  79.      return X , "image/gif"
  80.     end
  81.  
  82. pages["/"] = function(request)
  83.      file.open("led_gif.html")
  84.      X=file.read()
  85.      file.close()
  86.      tmr.wdclr()
  87.      X = X ..F()
  88.      return X
  89.     end - See more at: http://www.esp8266.com/viewtopic.php?f=24&t=1334#sthash.w0UC7RQR.dpuf
  90.  
  91. pages["/test.svg"] = function(request)
  92. local out = "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"400\" height=\"150\">\n";
  93. out = out.."<rect width=\"400\" height=\"150\" fill=\"rgb(250, 230, 210)\" stroke-width=\"1\" stroke=\"rgb(0, 0, 0)\" />\n";
  94. out = out.."<g stroke=\"black\">\n";
  95. y = math.random() % 130;
  96. for x=1, 390, 10 do
  97.     y2 = math.random() % 130;
  98.     print(x);
  99.     print(" ");
  100.     print(y2);
  101.     out= out..string.format("<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" stroke-width=\"1\" />\n", x, 140 - y, x + 10, 140 - y2)
  102.     y = y2;
  103. end
  104.     out = out.."</g>\n</svg>\n";
  105.     return out , "image/svg+xml"
  106. end
Advertisement
Add Comment
Please, Sign In to add comment