HDC1000.lua 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. local data_temp = i2c.read(0, 2)
  33. i2c.stop(id)
  34. local 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. -- setup i2c
  57. function M.setup(drdyn_pin)
  58. _drdyn_pin = drdyn_pin
  59. end
  60. function M.config(addr, resolution, heater)
  61. -- default values are set if the function is called with no arguments
  62. HDC1000_ADDR = addr or HDC1000_ADDR
  63. resolution = resolution or HDC1000_TEMP_HUMI_14BIT
  64. heater = heater or HDC1000_HEAT_ON
  65. writeConfig(bit.bor(resolution, heater))
  66. end
  67. -- outputs temperature in Celsius degrees
  68. function M.getHumi()
  69. setReadRegister(HDC1000_HUMI)
  70. if(_drdyn_pin ~= false) then
  71. gpio.mode(_drdyn_pin, gpio.INPUT)
  72. while(gpio.read(_drdyn_pin)==1) do
  73. end
  74. else tmr.delay(delay) end
  75. return(read16()/65535.0*100)
  76. end
  77. -- outputs humidity in %RH
  78. function M.getTemp()
  79. setReadRegister(HDC1000_TEMP)
  80. if(_drdyn_pin ~= false) then
  81. gpio.mode(_drdyn_pin, gpio.INPUT)
  82. while(gpio.read(_drdyn_pin)==1) do
  83. end
  84. else tmr.delay(delay) end
  85. return(read16()/65535.0*165-40)
  86. end
  87. return M