ds18b20.lua 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. --------------------------------------------------------------------------------
  2. -- DS18B20 one wire module for NODEMCU
  3. -- NODEMCU TEAM
  4. -- LICENCE: http://opensource.org/licenses/MIT
  5. -- Vowstar <vowstar@nodemcu.com>
  6. --------------------------------------------------------------------------------
  7. -- Set module name as parameter of require
  8. local modname = ...
  9. local M = {}
  10. _G[modname] = M
  11. --------------------------------------------------------------------------------
  12. -- Local used variables
  13. --------------------------------------------------------------------------------
  14. -- DS18B20 dq pin
  15. local pin = nil
  16. -- DS18B20 default pin
  17. local defaultPin = 9
  18. --------------------------------------------------------------------------------
  19. -- Local used modules
  20. --------------------------------------------------------------------------------
  21. -- Table module
  22. local table = table
  23. -- String module
  24. local string = string
  25. -- One wire module
  26. local ow = ow
  27. -- Timer module
  28. local tmr = tmr
  29. -- Limited to local environment
  30. setfenv(1,M)
  31. --------------------------------------------------------------------------------
  32. -- Implementation
  33. --------------------------------------------------------------------------------
  34. C = 0
  35. F = 1
  36. K = 2
  37. function setup(dq)
  38. pin = dq
  39. if(pin == nil) then
  40. pin = defaultPin
  41. end
  42. ow.setup(pin)
  43. end
  44. function addrs()
  45. setup(pin)
  46. tbl = {}
  47. ow.reset_search(pin)
  48. repeat
  49. addr = ow.search(pin)
  50. if(addr ~= nil) then
  51. table.insert(tbl, addr)
  52. end
  53. tmr.wdclr()
  54. until (addr == nil)
  55. ow.reset_search(pin)
  56. return tbl
  57. end
  58. function readNumber(addr, unit)
  59. result = nil
  60. setup(pin)
  61. flag = false
  62. if(addr == nil) then
  63. ow.reset_search(pin)
  64. count = 0
  65. repeat
  66. count = count + 1
  67. addr = ow.search(pin)
  68. tmr.wdclr()
  69. until((addr ~= nil) or (count > 100))
  70. ow.reset_search(pin)
  71. end
  72. if(addr == nil) then
  73. return result
  74. end
  75. crc = ow.crc8(string.sub(addr,1,7))
  76. if (crc == addr:byte(8)) then
  77. if ((addr:byte(1) == 0x10) or (addr:byte(1) == 0x28)) then
  78. -- print("Device is a DS18S20 family device.")
  79. ow.reset(pin)
  80. ow.select(pin, addr)
  81. ow.write(pin, 0x44, 1)
  82. -- tmr.delay(1000000)
  83. present = ow.reset(pin)
  84. ow.select(pin, addr)
  85. ow.write(pin,0xBE,1)
  86. -- print("P="..present)
  87. data = nil
  88. data = string.char(ow.read(pin))
  89. for i = 1, 8 do
  90. data = data .. string.char(ow.read(pin))
  91. end
  92. -- print(data:byte(1,9))
  93. crc = ow.crc8(string.sub(data,1,8))
  94. -- print("CRC="..crc)
  95. if (crc == data:byte(9)) then
  96. if(unit == nil or unit == C) then
  97. t = (data:byte(1) + data:byte(2) * 256) * 625
  98. elseif(unit == F) then
  99. t = (data:byte(1) + data:byte(2) * 256) * 1125 + 320000
  100. elseif(unit == K) then
  101. t = (data:byte(1) + data:byte(2) * 256) * 625 + 2731500
  102. else
  103. return nil
  104. end
  105. t1 = t / 10000
  106. t2 = t % 10000
  107. -- print("Temperature="..t1.."."..t2.." Centigrade")
  108. -- result = t1.."."..t2
  109. return t1, t2
  110. end
  111. tmr.wdclr()
  112. else
  113. -- print("Device family is not recognized.")
  114. end
  115. else
  116. -- print("CRC is not valid!")
  117. end
  118. return result
  119. end
  120. function read(addr, unit)
  121. t1, t2 = readNumber(addr, unit)
  122. if((t1 == nil ) or (t2 ==nil)) then
  123. return nil
  124. else
  125. return t1.."."..t2
  126. end
  127. end
  128. -- Return module table
  129. return M