ds18b20.lua 3.4 KB

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