ds18b20-integer.lua 6.9 KB

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