-
-
Notifications
You must be signed in to change notification settings - Fork 320
/
Copy pathonewire-ds18b20.lua
80 lines (78 loc) · 2.23 KB
/
onewire-ds18b20.lua
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
--'
-- 18b20 one wire example for NODEMCU
-- NODEMCU TEAM
-- LICENCE: http://opensource.org/licenses/MIT
-- Vowstar <vowstar@nodemcu.com>
--'
pin = 8
ow.setup(pin)
count = 0
repeat
count = count + 1
addr = ow.reset_search(pin)
addr = ow.search(pin)
tmr.wdclr()
until((addr ~= nil) or (count > 100))
if (addr == nil) then
print("No more addresses.")
else
print(addr:byte(1,8))
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
print("Device is a DS18S20 family device.")
repeat
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)
print("P="..present)
data = nil
data = string.char(ow.read(pin))
for i = 1, 8 do
data = data .. string.char(ow.read(pin))
end
print(data:byte(1,9))
crc = ow.crc8(string.sub(data,1,8))
print("CRC="..crc)
if (crc == data:byte(9)) then
t = (data:byte(1) + data:byte(2) * 256) * 625
t1 = t / 10000
t2 = t % 10000
print("Temperature= "..t1.."."..t2.." Centigrade")
end
tmr.wdclr()
until false
else
print("Device family is not recognized.")
end
else
print("CRC is not valid!")
end
end
-- -- read temperature with DS18B20
-- -- node.compile("ds18b20.lua") -- run this only once to compile and save to "ds18b20.lc"
-- t=require("ds18b20")
-- t.setup(9)
-- addrs=t.addrs()
-- -- Total DS18B20 numbers, assume it is 2
-- print(table.getn(addrs))
-- -- The first DS18B20
-- print(t.read(addrs[1],t.C))
-- print(t.read(addrs[1],t.F))
-- print(t.read(addrs[1],t.K))
-- -- The second DS18B20
-- print(t.read(addrs[2],t.C))
-- print(t.read(addrs[2],t.F))
-- print(t.read(addrs[2],t.K))
-- -- Just read
-- print(t.read())
-- -- Just read as centigrade
-- print(t.read(nil,t.C))
-- -- Don't forget to release it after use
-- t = nil
-- ds18b20 = nil
-- package.loaded["ds18b20"]=nil