Advertisement
Izya12

ds1820.lua

Jun 10th, 2017
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.98 KB | None | 0 0
  1. gpio.mode(0, gpio.OUTPUT);
  2. gpio.write(0,gpio.LOW);
  3. gpio.mode(1, gpio.OUTPUT);
  4. gpio.write(1,gpio.LOW);
  5.  
  6. wifi.setmode(wifi.STATION);
  7. wifi.sta.config("Izya12","121788121788");
  8. wifi.sta.autoconnect(1);
  9.  
  10. tmr.alarm(0, 1000, 1, function()
  11.     ip = wifi.sta.getip();
  12.     if ip~=nil then
  13.         print(ip);
  14.         tmr.stop(0);
  15.         httpserver();
  16.         dofile("ds1820.lua")
  17.     else
  18.         print("no connect");
  19.     end
  20. end)
  21.  
  22. --tmr.alarm(0, 60000, 1, function()
  23. --      dofile("ds1820.lua")
  24. --end)
  25.  
  26. sendFileContents = function(conn, filename)
  27.     if file.open(filename, "r") then
  28.         --conn:send(responseHeader("200 OK","text/html"));
  29.         repeat
  30.         local line=file.readline();
  31.         if line then
  32.             conn:send(line);
  33.         end
  34.         until not line
  35.         file.close();
  36.     else
  37.         conn:send(responseHeader("404 Not Found","text/html"));
  38.         conn:send("Page not found");
  39.     end
  40. end
  41. responseHeader = function(code, type)
  42.   return "HTTP/1.1 " .. code .. "\r\nConnection: close\r\nServer: nunu-Luaweb\r\nContent-Type: " .. type .. "\r\n\r\n";
  43. end
  44. httpserver = function ()
  45.     srv=net.createServer(net.TCP);
  46.     srv:listen(80, function(conn)
  47.         conn:on("receive", function(conn,request)
  48.             print(request);
  49.             conn:send(responseHeader("200 OK", "text/html"));
  50. --          conn:send("Temp:"..t1 .. "."..string.format("%04d", t2).." C\n")
  51.             if string.find(request,"load=0") then
  52.                 gpio.write(0,gpio.HIGH);
  53.             elseif string.find(request,"load=1") then
  54.                 gpio.write(0,gpio.LOW);
  55.             elseif string.find(request,"load=3") then
  56.                 gpio.write(1,gpio.HIGH);
  57.             elseif string.find(request,"load=2") then
  58.                 gpio.write(1,gpio.LOW);
  59.             else
  60.                 sendFileContents(conn, "page.htm");
  61.             end    
  62.    
  63.         end)
  64.         conn:on("sent", function(conn)
  65.             conn:close();
  66.             conn = nil;
  67.         end)
  68.     end)
  69. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement