ds18b20-example.lua 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. t = require("ds18b20")
  2. pin = 3 -- gpio0 = 3, gpio2 = 4
  3. local function readout(temp)
  4. if t.sens then
  5. print("Total number of DS18B20 sensors: ".. #t.sens)
  6. for i, s in ipairs(t.sens) do
  7. print(string.format(" sensor #%d address: %s%s", i, ('%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X'):format(s:byte(1,8)), s:byte(9) == 1 and " (parasite)" or ""))
  8. end
  9. end
  10. for addr, temp in pairs(temp) do
  11. print(string.format("Sensor %s: %s °C", ('%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X'):format(addr:byte(1,8)), temp))
  12. end
  13. -- Module can be released when it is no longer needed
  14. --t = nil
  15. --package.loaded["ds18b20"]=nil
  16. end
  17. t:enable_debug()
  18. file.remove("ds18b20_save.lc") -- remove saved addresses
  19. print("=============================================", node.heap())
  20. print("first call, no addresses in flash, search is performed")
  21. t:read_temp(readout, pin, t.C)
  22. tmr.create():alarm(2000, tmr.ALARM_SINGLE, function()
  23. print("=============================================", node.heap())
  24. print("second readout, no new search, found addresses are used")
  25. t:read_temp(readout, pin)
  26. tmr.create():alarm(2000, tmr.ALARM_SINGLE, function()
  27. print("=============================================", node.heap())
  28. print("force search again")
  29. t:read_temp(readout, pin, nil, true)
  30. tmr.create():alarm(2000, tmr.ALARM_SINGLE, function()
  31. print("=============================================", node.heap())
  32. print("save search results")
  33. t:read_temp(readout, pin, nil, false, true)
  34. tmr.create():alarm(2000, tmr.ALARM_SINGLE, function()
  35. print("=============================================", node.heap())
  36. print("use saved addresses")
  37. t.sens={}
  38. t:read_temp(readout, pin)
  39. end)
  40. end)
  41. end)
  42. end)