ds18b20.lua 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. --------------------------------------------------------------------------------
  2. -- DS18B20 one wire module for NODEMCU
  3. -- NODEMCU TEAM
  4. -- LICENCE: http://opensource.org/licenses/MIT
  5. -- @voborsky, @devsaurus, TerryE 26 Mar 2017
  6. --------------------------------------------------------------------------------
  7. local modname = ...
  8. -- Used modules and functions
  9. local type, tostring, pcall, ipairs =
  10. type, tostring, pcall, ipairs
  11. -- Local functions
  12. local ow_setup, ow_search, ow_select, ow_read, ow_read_bytes, ow_write, ow_crc8,
  13. ow_reset, ow_reset_search, ow_skip, ow_depower =
  14. ow.setup, ow.search, ow.select, ow.read, ow.read_bytes, ow.write, ow.crc8,
  15. ow.reset, ow.reset_search, ow.skip, ow.depower
  16. local node_task_post, node_task_LOW_PRIORITY = node.task.post, node.task.LOW_PRIORITY
  17. local string_char, string_dump = string.char, string.dump
  18. local now, tmr_create, tmr_ALARM_SINGLE = tmr.now, tmr.create, tmr.ALARM_SINGLE
  19. local table_sort, table_concat = table.sort, table.concat
  20. local math_floor = math.floor
  21. local file_open = file.open
  22. local conversion
  23. local DS18B20FAMILY = 0x28
  24. local DS1920FAMILY = 0x10 -- and DS18S20 series
  25. local CONVERT_T = 0x44
  26. local READ_SCRATCHPAD = 0xBE
  27. local READ_POWERSUPPLY= 0xB4
  28. local MODE = 1
  29. local pin, cb, unit = 3
  30. local status = {}
  31. local debugPrint = function() return end
  32. --------------------------------------------------------------------------------
  33. -- Implementation
  34. --------------------------------------------------------------------------------
  35. local function enable_debug()
  36. debugPrint = function (...) print(now(),' ', ...) end
  37. end
  38. local function to_string(addr, esc)
  39. if type(addr) == 'string' and #addr == 8 then
  40. return ( esc == true and
  41. '"\\%u\\%u\\%u\\%u\\%u\\%u\\%u\\%u"' or
  42. '%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X '):format(addr:byte(1,8))
  43. else
  44. return tostring(addr)
  45. end
  46. end
  47. local function readout(self)
  48. local next = false
  49. local sens = self.sens
  50. local temp = self.temp
  51. for i, s in ipairs(sens) do
  52. if status[i] == 1 then
  53. ow_reset(pin)
  54. local addr = s:sub(1,8)
  55. ow_select(pin, addr) -- select the sensor
  56. ow_write(pin, READ_SCRATCHPAD, MODE)
  57. local data = ow_read_bytes(pin, 9)
  58. local t=(data:byte(1)+data:byte(2)*256)
  59. -- t is actually signed so process the sign bit and adjust for fractional bits
  60. -- the DS18B20 family has 4 fractional bits and the DS18S20s, 1 fractional bit
  61. t = ((t <= 32767) and t or t - 65536) *
  62. ((addr:byte(1) == DS18B20FAMILY) and 625 or 5000)
  63. local crc, b9 = ow_crc8(string.sub(data,1,8)), data:byte(9)
  64. t = t / 10000
  65. if math_floor(t)~=85 then
  66. if unit == 'F' then
  67. t = t * 18/10 + 32
  68. elseif unit == 'K' then
  69. t = t + 27315/100
  70. end
  71. debugPrint(to_string(addr), t, crc, b9)
  72. if crc==b9 then temp[addr]=t end
  73. status[i] = 2
  74. end
  75. end
  76. next = next or status[i] == 0
  77. end
  78. if next then
  79. node_task_post(node_task_LOW_PRIORITY, function() return conversion(self) end)
  80. else
  81. --sens = {}
  82. if cb then
  83. node_task_post(node_task_LOW_PRIORITY, function() return cb(temp) end)
  84. end
  85. end
  86. end
  87. conversion = (function (self)
  88. local sens = self.sens
  89. local powered_only = true
  90. for _, s in ipairs(sens) do powered_only = powered_only and s:byte(9) ~= 1 end
  91. if powered_only then
  92. debugPrint("starting conversion: all sensors")
  93. ow_reset(pin)
  94. ow_skip(pin) -- skip ROM selection, talk to all sensors
  95. ow_write(pin, CONVERT_T, MODE) -- and start conversion
  96. for i, _ in ipairs(sens) do status[i] = 1 end
  97. else
  98. local started = false
  99. for i, s in ipairs(sens) do
  100. if status[i] == 0 then
  101. local addr, parasite = s:sub(1,8), s:byte(9) == 1
  102. if parasite and started then break end -- do not start concurrent conversion of powered and parasite
  103. debugPrint("starting conversion:", to_string(addr), parasite and "parasite" or "")
  104. ow_reset(pin)
  105. ow_select(pin, addr) -- select the sensor
  106. ow_write(pin, CONVERT_T, MODE) -- and start conversion
  107. status[i] = 1
  108. if parasite then break end -- parasite sensor blocks bus during conversion
  109. started = true
  110. end
  111. end
  112. end
  113. tmr_create():alarm(750, tmr_ALARM_SINGLE, function() return readout(self) end)
  114. end)
  115. local function _search(self, lcb, lpin, search, save)
  116. self.temp = {}
  117. if search then self.sens = {}; status = {} end
  118. local sens = self.sens
  119. pin = lpin or pin
  120. local addr
  121. if not search and #sens == 0 then
  122. -- load addreses if available
  123. debugPrint ("geting addreses from flash")
  124. local s,check,a = pcall(dofile, "ds18b20_save.lc")
  125. if s and check == "ds18b20" then
  126. for i = 1, #a do sens[i] = a[i] end
  127. end
  128. debugPrint (#sens, "addreses found")
  129. end
  130. ow_setup(pin)
  131. if search or #sens == 0 then
  132. ow_reset_search(pin)
  133. -- ow_target_search(pin,0x28)
  134. -- search the first device
  135. addr = ow_search(pin)
  136. else
  137. for i, _ in ipairs(sens) do status[i] = 0 end
  138. end
  139. local function cycle()
  140. if addr then
  141. local crc=ow_crc8(addr:sub(1,7))
  142. if (crc==addr:byte(8)) and ((addr:byte(1)==DS1920FAMILY) or (addr:byte(1)==DS18B20FAMILY)) then
  143. ow_reset(pin)
  144. ow_select(pin, addr)
  145. ow_write(pin, READ_POWERSUPPLY, MODE)
  146. local parasite = (ow_read(pin)==0 and 1 or 0)
  147. sens[#sens+1]= addr..string_char(parasite)
  148. status[#sens] = 0
  149. debugPrint("contact: ", to_string(addr), parasite == 1 and "parasite" or "")
  150. end
  151. addr = ow_search(pin)
  152. node_task_post(node_task_LOW_PRIORITY, cycle)
  153. else
  154. ow_depower(pin)
  155. -- place powered sensors first
  156. table_sort(sens, function(a, b) return a:byte(9)<b:byte(9) end) -- parasite
  157. -- save sensor addreses
  158. if save then
  159. debugPrint ("saving addreses to flash")
  160. local addr_list = {}
  161. for i =1, #sens do
  162. local s = sens[i]
  163. addr_list[i] = to_string(s:sub(1,8), true)..('.."\\%u"'):format(s:byte(9))
  164. end
  165. local save_statement = 'return "ds18b20", {' .. table_concat(addr_list, ',') .. '}'
  166. debugPrint (save_statement)
  167. local save_file = file_open("ds18b20_save.lc","w")
  168. save_file:write(string_dump(loadstring(save_statement)))
  169. save_file:close()
  170. end
  171. -- end save sensor addreses
  172. if lcb then node_task_post(node_task_LOW_PRIORITY, lcb) end
  173. end
  174. end
  175. cycle()
  176. end
  177. local function read_temp(self, lcb, lpin, lunit, force_search, save_search)
  178. cb, unit = lcb, lunit or unit
  179. _search(self, function() return conversion(self) end, lpin, force_search, save_search)
  180. end
  181. -- Set module name as parameter of require and return module table
  182. local M = {
  183. sens = {},
  184. temp = {},
  185. C = 'C', F = 'F', K = 'K',
  186. read_temp = read_temp, enable_debug = enable_debug
  187. }
  188. _G[modname or 'ds18b20'] = M
  189. return M