Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- LUA code for ESP8266 with a Temp sensor DS18B20 connected to GPIO2
- -- John Ridley / DragonflyDIY.com
- -- October 18 2015
- -- CC0 public domain dedication license - no attribution necessary
- -- NOTE this is not stable and not recommended for use
- -- the server crashes after only an hour or so. Seems to be the fault of LUA
- pin = 4
- ow.setup(pin)
- --- Get temperature from DS18B20
- function initTemp()
- count = 0
- repeat
- count = count + 1
- addr = ow.reset_search(pin)
- addr = ow.search(pin)
- tmr.wdclr()
- until((addr ~= nil) or (count > 100))
- end
- function getTemp()
- 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) * 625
- t1 = t / 10000
- t2 = t % 10000
- buf = ""
- buf = buf..t1.."."..t2.." Centigrade"
- return buf
- end
- tmr.wdclr()
- end
- end
- end
- -- set up server
- wifi.setmode(wifi.STATION)
- wifi.sta.config("moria","notmyrealpassword")
- cfg =
- {
- ip="192.168.1.99",
- netmask="255.255.255.0",
- gateway="192.168.1.1"
- }
- wifi.sta.setip(cfg)
- initTemp()
- srv=net.createServer(net.TCP)
- srv:listen(80,function(conn)
- conn:on("receive", function(client,request)
- local buf = "";
- local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP");
- if(method == nil)then
- _, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP");
- end
- local _GET = {}
- if (vars ~= nil)then
- for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do
- _GET[k] = v
- end
- end
- -- buf = buf.."Hello, world";
- buf = buf..getTemp();
- client:send(buf);
- client:close();
- collectgarbage();
- end)
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement