-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththermometer.lua
More file actions
43 lines (35 loc) · 916 Bytes
/
thermometer.lua
File metadata and controls
43 lines (35 loc) · 916 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
function get_current_temp (on_temp)
if get_temp_running then
on_temp(nil)
return
end
get_temp_running = true
local pinOw = 4 -- DS18B20 is on pin 4
local power = 1
ow.setup(pinOw)
-- set maximum resolution
if ow.reset(pinOw) == 0 then
on_temp(nil)
return
end
ow.skip(pinOw)
ow.write(pinOw, 0x4e, power)
ow.write(pinOw, 0, power)
ow.write(pinOw, 0, power)
ow.write(pinOw, 0xff, power)
-- start conversion
ow.reset(pinOw)
ow.skip(pinOw)
ow.write(pinOw, 0x44, power)
-- wait for temp
tmr.alarm(6, 750, tmr.ALARM_SINGLE, function()
-- extract temp
ow.reset(pinOw)
ow.skip(pinOw)
ow.write(pinOw, 0xbe, power)
local temp = (ow.read(pinOw) + ow.read(pinOw) * 256) / 16.0
ow.reset(pinOw)
get_temp_running = false
on_temp(temp)
end)
end