Advertisement
Guest User

ds18b20.lua

a guest
May 3rd, 2015
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.49 KB | None | 0 0
  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.  
  9. -- Set module name as parameter of require
  10. local modname = ...
  11. local M = {}
  12. _G[modname] = M
  13. --------------------------------------------------------------------------------
  14. -- Local used variables
  15. --------------------------------------------------------------------------------
  16. -- DS18B20 dq pin
  17. local pin = nil
  18. -- DS18B20 default pin
  19. local defaultPin = 9
  20. --------------------------------------------------------------------------------
  21. -- Local used modules
  22. --------------------------------------------------------------------------------
  23. -- Table module
  24. local table = table
  25. -- String module
  26. local string = string
  27. -- One wire module
  28. local ow = ow
  29. -- Timer module
  30. local tmr = tmr
  31. -- Limited to local environment
  32. setfenv(1,M)
  33. --------------------------------------------------------------------------------
  34. -- Implementation
  35. --------------------------------------------------------------------------------
  36. C = 0
  37. F = 1
  38. K = 2
  39. function setup(dq)
  40.   pin = dq
  41.   if(pin == nil) then
  42.     pin = defaultPin
  43.   end
  44.   ow.setup(pin)
  45. end
  46.  
  47. function addrs()
  48.   setup(pin)
  49.   tbl = {}
  50.   ow.reset_search(pin)
  51.   repeat
  52.     addr = ow.search(pin)
  53.     if(addr ~= nil) then
  54.       table.insert(tbl, addr)
  55.     end
  56.     tmr.wdclr()
  57.   until (addr == nil)
  58.   ow.reset_search(pin)
  59.   return tbl
  60. end
  61.  
  62. function readNumber(addr, unit)
  63.   result = nil
  64.   setup(pin)
  65.   flag = false
  66.   if(addr == nil) then
  67.     ow.reset_search(pin)
  68.     count = 0
  69.     repeat
  70.       count = count + 1
  71.       addr = ow.search(pin)
  72.       tmr.wdclr()
  73.     until((addr ~= nil) or (count > 100))
  74.     ow.reset_search(pin)
  75.   end
  76.   if(addr == nil) then
  77.     return result
  78.   end
  79.   crc = ow.crc8(string.sub(addr,1,7))
  80.   if (crc == addr:byte(8)) then
  81.     if ((addr:byte(1) == 0x10) or (addr:byte(1) == 0x28)) then
  82.       -- print("Device is a DS18S20 family device.")
  83.       ow.reset(pin)
  84.       ow.select(pin, addr)
  85.       ow.write(pin, 0x44, 1)
  86.       tmr.delay(1000000)
  87.       present = ow.reset(pin)
  88.       ow.select(pin, addr)
  89.       ow.write(pin,0xBE,1)
  90.       -- print("P="..present)
  91.       data = nil
  92.       data = string.char(ow.read(pin))
  93.       for i = 1, 8 do
  94.         data = data .. string.char(ow.read(pin))
  95.       end
  96.       -- print(data:byte(1,9))
  97.       crc = ow.crc8(string.sub(data,1,8))
  98.       -- print("CRC="..crc)
  99.       if (crc == data:byte(9)) then
  100.         t = (data:byte(1) + data:byte(2) * 256)
  101.         if (t > 32767) then
  102.           t = t - 65536
  103.         end
  104.         if(unit == nil or unit == C) then
  105.           t = t * 625
  106.         elseif(unit == F) then
  107.           t = t * 1125 + 320000
  108.         elseif(unit == K) then
  109.           t = t * 625 + 2731500
  110.         else
  111.           return nil
  112.         end
  113.         t = t / 10000
  114.         -- print("Temperature="..t1.."."..t2.." Centigrade")
  115.         -- result = t1.."."..t2
  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.  
  128. function read(addr, unit)
  129.   t = readNumber(addr, unit)
  130.   if (t == nil) then
  131.     return nil
  132.   else
  133.     return t
  134.   end
  135. end
  136.  
  137. -- Return module table
  138. return M
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement