Skip to content

Instantly share code, notes, and snippets.

@lbuchy
Created June 5, 2015 03:50
Show Gist options
  • Save lbuchy/4adca4dd707b1ced8448 to your computer and use it in GitHub Desktop.
Save lbuchy/4adca4dd707b1ced8448 to your computer and use it in GitHub Desktop.
pin=4
ow.setup(pin)
lastTemp=-999
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
tempSenseState=0
tmr.alarm(0, 1000, 1, function()
function ServiceTempSensor()
if (tempSenseState == 0) then
ow.reset(pin)
ow.skip(pin)
ow.write(pin, 0x44, 1)
tempSenseState = 1
else
ow.reset(pin)
ow.skip(pin)
ow.write(pin, 0xBE, 1)
data = nil
data = string.char(ow.read(pin))
data = data .. string.char(ow.read(pin))
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 / 10000
tempSenseState = 0
end
end
ServiceTempSensor()
end)
-- TCP server
server=net.createServer(net.TCP)
server:listen(80, function(conn)
conn:on("receive", function(conn, payload)
conn:send(tostring(lastTemp))
end)
conn:on("sent", function(conn) conn:close() end)
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment