HDC1000.lua 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. -------------------------------------------------------
  2. -- This library was written for the Texas Instruments
  3. -- HDC1000 temperature and humidity sensor.
  4. -- It should work for the HDC1008 too.
  5. -- Written by Francesco Truzzi (francesco@truzzi.me)
  6. -- Released under GNU GPL v2.0 license.
  7. -------------------------------------------------------
  8. -------------- NON-DEFAULT CONFIG VALUES --------------
  9. ------------- config() optional arguments -------------
  10. -- HDC1000_HEAT_OFF 0x00 (heater)
  11. -- HDC1000_TEMP_11BIT 0x40 (resolution)
  12. -- HDC1000_HUMI_11BIT 0x01 (resolution)
  13. -- HDC1000_HUMI_8BIT 0x20 (resolution)
  14. -------------------------------------------------------
  15. local modname = ...
  16. local M = {}
  17. _G[modname] = M
  18. local id = 0
  19. local i2c = i2c
  20. local delay = 20000
  21. local _drdyn_pin
  22. local HDC1000_ADDR = 0x40
  23. local HDC1000_TEMP = 0x00
  24. local HDC1000_HUMI = 0x01
  25. local HDC1000_CONFIG = 0x02
  26. local HDC1000_HEAT_ON = 0x20
  27. local HDC1000_TEMP_HUMI_14BIT = 0x00
  28. -- reads 16bits from the sensor
  29. local function read16()
  30. i2c.start(id)
  31. i2c.address(id, HDC1000_ADDR, i2c.RECEIVER)
  32. data_temp = i2c.read(0, 2)
  33. i2c.stop(id)
  34. data = bit.lshift(string.byte(data_temp, 1, 1), 8) + string.byte(data_temp, 2, 2)
  35. return data
  36. end
  37. -- sets the register to read next
  38. local function setReadRegister(register)
  39. i2c.start(id)
  40. i2c.address(id, HDC1000_ADDR, i2c.TRANSMITTER)
  41. i2c.write(id, register)
  42. i2c.stop(id)
  43. end
  44. -- writes the 2 configuration bytes
  45. local function writeConfig(config)
  46. i2c.start(id)
  47. i2c.address(id, HDC1000_ADDR, i2c.TRANSMITTER)
  48. i2c.write(id, HDC1000_CONFIG, config, 0x00)
  49. i2c.stop(id)
  50. end
  51. -- returns true if battery voltage is < 2.7V, false otherwise
  52. function M.batteryDead()
  53. setReadRegister(HDC1000_CONFIG)
  54. return(bit.isset(read16(), 11))
  55. end
  56. -- initalize i2c
  57. function M.init(sda, scl, drdyn_pin)
  58. _drdyn_pin = drdyn_pin
  59. i2c.setup(id, sda, scl, i2c.SLOW)
  60. end
  61. function M.config(addr, resolution, heater)
  62. -- default values are set if the function is called with no arguments
  63. HDC1000_ADDR = addr or HDC1000_ADDR
  64. resolution = resolution or HDC1000_TEMP_HUMI_14BIT
  65. heater = heater or HDC1000_HEAT_ON
  66. writeConfig(bit.bor(resolution, heater))
  67. end
  68. -- outputs temperature in Celsius degrees
  69. function M.getHumi()
  70. setReadRegister(HDC1000_HUMI)
  71. if(_drdyn_pin ~= false) then
  72. gpio.mode(_drdyn_pin, gpio.INPUT)
  73. while(gpio.read(_drdyn_pin)==1) do
  74. end
  75. else tmr.delay(delay) end
  76. return(read16()/65535.0*100)
  77. end
  78. -- outputs humidity in %RH
  79. function M.getTemp()
  80. setReadRegister(HDC1000_TEMP)
  81. if(_drdyn_pin ~= false) then
  82. gpio.mode(_drdyn_pin, gpio.INPUT)
  83. while(gpio.read(_drdyn_pin)==1) do
  84. end
  85. else tmr.delay(delay) end
  86. return(read16()/65535.0*165-40)
  87. end
  88. return M