ds18b20.lua 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. if 1/2 == 0 then
  64. -- integer version
  65. if unit == 'F' then
  66. t = (t * 18)/10 + 320000
  67. elseif unit == 'K' then
  68. t = t + 2731500
  69. end
  70. local sgn = t<0 and -1 or 1
  71. local tA = sgn*t
  72. local tH=tA/10000
  73. local tL=(tA%10000)/1000 + ((tA%1000)/100 >= 5 and 1 or 0)
  74. if tH and (t~=850000) then
  75. temp[addr]=(sgn<0 and "-" or "")..tH.."."..tL
  76. debugPrint(to_string(addr),(sgn<0 and "-" or "")..tH.."."..tL)
  77. status[i] = 2
  78. end
  79. -- end integer version
  80. else
  81. -- float version
  82. if t and (math_floor(t/10000)~=85) then
  83. t = t / 10000
  84. if unit == 'F' then
  85. t = t * 18/10 + 32
  86. elseif unit == 'K' then
  87. t = t + 27315/100
  88. end
  89. self.temp[addr]=t
  90. debugPrint(to_string(addr), t)
  91. status[i] = 2
  92. end
  93. -- end float version
  94. end
  95. end
  96. next = next or status[i] == 0
  97. end
  98. if next then
  99. node_task_post(node_task_LOW_PRIORITY, function() return conversion(self) end)
  100. else
  101. --sens = {}
  102. if cb then
  103. node_task_post(node_task_LOW_PRIORITY, function() return cb(temp) end)
  104. end
  105. end
  106. end
  107. conversion = (function (self)
  108. local sens = self.sens
  109. local powered_only = true
  110. for _, s in ipairs(sens) do powered_only = powered_only and s:byte(9) ~= 1 end
  111. if powered_only then
  112. debugPrint("starting conversion: all sensors")
  113. ow_reset(pin)
  114. ow_skip(pin) -- select the sensor
  115. ow_write(pin, CONVERT_T, MODE) -- and start conversion
  116. for i, _ in ipairs(sens) do status[i] = 1 end
  117. else
  118. for i, s in ipairs(sens) do
  119. if status[i] == 0 then
  120. local addr, parasite = s:sub(1,8), s:byte(9)
  121. debugPrint("starting conversion:", to_string(addr), parasite == 1 and "parasite" or " ")
  122. ow_reset(pin)
  123. ow_select(pin, addr) -- select the sensor
  124. ow_write(pin, CONVERT_T, MODE) -- and start conversion
  125. status[i] = 1
  126. if parasite == 1 then break end -- parasite sensor blocks bus during conversion
  127. end
  128. end
  129. end
  130. tmr_create():alarm(750, tmr_ALARM_SINGLE, function() return readout(self) end)
  131. end)
  132. local function _search(self, lcb, lpin, search, save)
  133. self.temp = {}
  134. if search then self.sens = {}; status = {} end
  135. local sens = self.sens
  136. pin = lpin or pin
  137. local addr
  138. if not search and #sens == 0 then
  139. -- load addreses if available
  140. debugPrint ("geting addreses from flash")
  141. local s,check,a = pcall(dofile, "ds18b20_save.lc")
  142. if s and check == "ds18b20" then
  143. for i = 1, #a do sens[i] = a[i] end
  144. end
  145. debugPrint (#sens, "addreses found")
  146. end
  147. ow_setup(pin)
  148. if search or #sens == 0 then
  149. ow_reset_search(pin)
  150. -- ow_target_search(pin,0x28)
  151. -- search the first device
  152. addr = ow_search(pin)
  153. else
  154. for i, _ in ipairs(sens) do status[i] = 0 end
  155. end
  156. local function cycle()
  157. debugPrint("cycle")
  158. if addr then
  159. local crc=ow_crc8(addr:sub(1,7))
  160. if (crc==addr:byte(8)) and ((addr:byte(1)==DS1920FAMILY) or (addr:byte(1)==DS18B20FAMILY)) then
  161. ow_reset(pin)
  162. ow_select(pin, addr)
  163. ow_write(pin, READ_POWERSUPPLY, MODE)
  164. local parasite = (ow_read(pin)==0 and 1 or 0)
  165. sens[#sens+1]= addr..string_char(parasite) -- {addr=addr, parasite=parasite, status=0}
  166. debugPrint("contact: ", to_string(addr), parasite == 1 and "parasite" or " ")
  167. end
  168. addr = ow_search(pin)
  169. node_task_post(node_task_LOW_PRIORITY, cycle)
  170. else
  171. ow_depower(pin)
  172. -- place powered sensors first
  173. table_sort(sens, function(a, b) return a:byte(9)<b:byte(9) end) -- parasite
  174. -- save sensor addreses
  175. if save then
  176. debugPrint ("saving addreses to flash")
  177. local addr_list = {}
  178. for i =1, #sens do
  179. local s = sens[i]
  180. addr_list[i] = to_string(s:sub(1,8), true)..('.."\\%u"'):format(s:byte(9))
  181. end
  182. local save_statement = 'return "ds18b20", {' .. table_concat(addr_list, ',') .. '}'
  183. debugPrint (save_statement)
  184. local save_file = file_open("ds18b20_save.lc","w")
  185. save_file:write(string_dump(loadstring(save_statement)))
  186. save_file:close()
  187. end
  188. -- end save sensor addreses
  189. if lcb then node_task_post(node_task_LOW_PRIORITY, lcb) end
  190. end
  191. end
  192. cycle()
  193. end
  194. local function read_temp(self, lcb, lpin, lunit, force_search, save_search)
  195. cb, unit = lcb, lunit or unit
  196. _search(self, function() return conversion(self) end, lpin, force_search, save_search)
  197. end
  198. -- Set module name as parameter of require and return module table
  199. local M = {
  200. sens = {},
  201. temp = {},
  202. C = 'C', F = 'F', K = 'K',
  203. read_temp = read_temp, enable_debug = enable_debug
  204. }
  205. _G[modname or 'ds18b20'] = M
  206. return M