Izya12

inir.lua

Jun 10th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.63 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. pin = 4
  7. ow.setup(pin)
  8.  
  9. counter=0
  10. lasttemp=-999
  11.  
  12. wifi.setmode(wifi.STATION);
  13. wifi.sta.config("Izya12","121788121788");
  14. wifi.sta.autoconnect(1);
  15.  
  16. tmr.alarm(0, 1000, 1, function()
  17.     ip = wifi.sta.getip();
  18.     if ip~=nil then
  19.         print(ip);
  20.         tmr.stop(0);
  21.         httpserver();
  22.     else
  23.         print("no connect");
  24.     end
  25. end)
  26.  
  27. function bxor(a,b)
  28.    local r = 0
  29.    for i = 0, 31 do
  30.       if ( a % 2 + b % 2 == 1 ) then
  31.          r = r + 2^i
  32.       end
  33.       a = a / 2
  34.       b = b / 2
  35.    end
  36.    return r
  37. end
  38.  
  39. --- Get temperature from DS18B20
  40. function getTemp()
  41.       addr = ow.reset_search(pin)
  42.       repeat
  43.         tmr.wdclr()
  44.      
  45.       if (addr ~= nil) then
  46.         crc = ow.crc8(string.sub(addr,1,7))
  47.         if (crc == addr:byte(8)) then
  48.           if ((addr:byte(1) == 0x10) or (addr:byte(1) == 0x28)) then
  49.                 ow.reset(pin)
  50.                 ow.select(pin, addr)
  51.                 ow.write(pin, 0x44, 1)
  52.                 tmr.delay(1000000)
  53.                 present = ow.reset(pin)
  54.                 ow.select(pin, addr)
  55.                 ow.write(pin,0xBE, 1)
  56.                 data = nil
  57.                 data = string.char(ow.read(pin))
  58.                 for i = 1, 8 do
  59.                   data = data .. string.char(ow.read(pin))
  60.                 end
  61.                 crc = ow.crc8(string.sub(data,1,8))
  62.                 if (crc == data:byte(9)) then
  63.                    t = (data:byte(1) + data:byte(2) * 256)
  64.          if (t > 32768) then
  65.                     t = (bxor(t, 0xffff)) + 1
  66.                     t = (-1) * t
  67.                    end
  68.          t = t * 625
  69.                    lasttemp = t
  70.          print("Last temp: " .. lasttemp)
  71.                 end                  
  72.                 tmr.wdclr()
  73.           end
  74.         end
  75.       end
  76.       addr = ow.search(pin)
  77.       until(addr == nil)
  78. end
  79.  
  80. sendFileContents = function(conn, filename)
  81.     if file.open(filename, "r") then
  82.         --conn:send(responseHeader("200 OK","text/html"));
  83.         repeat
  84.         local line=file.readline();
  85.         if line then
  86.             conn:send(line);
  87.         end
  88.         until not line
  89.         file.close();
  90.     else
  91.         conn:send(responseHeader("404 Not Found","text/html"));
  92.         conn:send("Page not found");
  93.     end
  94. end
  95.  
  96. responseHeader = function(code, type)
  97.   return "HTTP/1.1 " .. code .. "\r\nConnection: close\r\nServer: nunu-Luaweb\r\nContent-Type: " .. type .. "\r\n\r\n";
  98. end
  99.  
  100. httpserver = function ()
  101.     getTemp()
  102.     t1 = lasttemp / 10000
  103.     t2 = (lasttemp >= 0 and lasttemp % 10000) or (10000 - lasttemp % 10000)
  104.     srv=net.createServer(net.TCP);
  105.     srv:listen(80, function(conn)
  106.         conn:on("receive", function(conn,request)
  107.             print(request);
  108.             conn:send(responseHeader("200 OK", "text/html"));
  109.             if string.find(request,"load=0") then
  110.                 gpio.write(0,gpio.HIGH);
  111.             elseif string.find(request,"load=1") then
  112.                 gpio.write(0,gpio.LOW);
  113.             elseif string.find(request,"load=3") then
  114.                 gpio.write(1,gpio.HIGH);
  115.             elseif string.find(request,"load=2") then
  116.                 gpio.write(1,gpio.LOW);
  117.             else
  118.                 print("Temp:"..t1.." C\n")
  119.                 conn:send("<p style=\"text-align: center;\">Температура</p>")
  120.                 conn:send("<p style=\"text-align: center;\">"..t1.." </p>")
  121.                 sendFileContents(conn, "page.htm");
  122.             end    
  123.    
  124.         end)
  125.         conn:on("sent", function(conn)
  126.             conn:close();
  127.             conn = nil;
  128.         end)
  129.     end)
  130. end
Add Comment
Please, Sign In to add comment