Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- gpio.mode(0, gpio.OUTPUT);
- gpio.write(0,gpio.LOW);
- gpio.mode(1, gpio.OUTPUT);
- gpio.write(1,gpio.LOW);
- pin = 4
- ow.setup(pin)
- counter=0
- lasttemp=-999
- wifi.setmode(wifi.STATION);
- wifi.sta.config("Izya12","121788121788");
- wifi.sta.autoconnect(1);
- tmr.alarm(0, 1000, 1, function()
- ip = wifi.sta.getip();
- if ip~=nil then
- print(ip);
- tmr.stop(0);
- httpserver();
- else
- print("no connect");
- end
- end)
- function bxor(a,b)
- local r = 0
- for i = 0, 31 do
- if ( a % 2 + b % 2 == 1 ) then
- r = r + 2^i
- end
- a = a / 2
- b = b / 2
- end
- return r
- end
- --- Get temperature from DS18B20
- function getTemp()
- addr = ow.reset_search(pin)
- repeat
- tmr.wdclr()
- if (addr ~= nil) then
- crc = ow.crc8(string.sub(addr,1,7))
- if (crc == addr:byte(8)) then
- if ((addr:byte(1) == 0x10) or (addr:byte(1) == 0x28)) then
- ow.reset(pin)
- ow.select(pin, addr)
- ow.write(pin, 0x44, 1)
- tmr.delay(1000000)
- present = ow.reset(pin)
- ow.select(pin, addr)
- ow.write(pin,0xBE, 1)
- data = nil
- data = string.char(ow.read(pin))
- for i = 1, 8 do
- data = data .. string.char(ow.read(pin))
- end
- crc = ow.crc8(string.sub(data,1,8))
- if (crc == data:byte(9)) then
- t = (data:byte(1) + data:byte(2) * 256)
- if (t > 32768) then
- t = (bxor(t, 0xffff)) + 1
- t = (-1) * t
- end
- t = t * 625
- lasttemp = t
- print("Last temp: " .. lasttemp)
- end
- tmr.wdclr()
- end
- end
- end
- addr = ow.search(pin)
- until(addr == nil)
- end
- sendFileContents = function(conn, filename)
- if file.open(filename, "r") then
- --conn:send(responseHeader("200 OK","text/html"));
- repeat
- local line=file.readline();
- if line then
- conn:send(line);
- end
- until not line
- file.close();
- else
- conn:send(responseHeader("404 Not Found","text/html"));
- conn:send("Page not found");
- end
- end
- responseHeader = function(code, type)
- return "HTTP/1.1 " .. code .. "\r\nConnection: close\r\nServer: nunu-Luaweb\r\nContent-Type: " .. type .. "\r\n\r\n";
- end
- httpserver = function ()
- getTemp()
- t1 = lasttemp / 10000
- t2 = (lasttemp >= 0 and lasttemp % 10000) or (10000 - lasttemp % 10000)
- srv=net.createServer(net.TCP);
- srv:listen(80, function(conn)
- conn:on("receive", function(conn,request)
- print(request);
- conn:send(responseHeader("200 OK", "text/html"));
- if string.find(request,"load=0") then
- gpio.write(0,gpio.HIGH);
- elseif string.find(request,"load=1") then
- gpio.write(0,gpio.LOW);
- elseif string.find(request,"load=3") then
- gpio.write(1,gpio.HIGH);
- elseif string.find(request,"load=2") then
- gpio.write(1,gpio.LOW);
- else
- print("Temp:"..t1.." C\n")
- conn:send("<p style=\"text-align: center;\">Температура</p>")
- conn:send("<p style=\"text-align: center;\">"..t1.." </p>")
- sendFileContents(conn, "page.htm");
- end
- end)
- conn:on("sent", function(conn)
- conn:close();
- conn = nil;
- end)
- end)
- end
Add Comment
Please, Sign In to add comment