bme280.lua 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. --[[
  2. BME280 Lua module
  3. Requires i2c and bme280_math module
  4. Written by Lukas Voborsky, @voborsky
  5. ]]
  6. local bme280 = {}
  7. -- bme280.setup()
  8. -- bme280:setup()
  9. -- bme280:read()
  10. -- bme280:altitude()
  11. -- bme280:dewpoint()
  12. -- bme280:qfe2qnh()
  13. -- bme280:startreadout()
  14. local type, assert = type, assert
  15. local table_concat, math_floor = table.concat, math.floor
  16. local i2c_start, i2c_stop, i2c_address, i2c_read, i2c_write, i2c_TRANSMITTER, i2c_RECEIVER =
  17. i2c.start, i2c.stop, i2c.address, i2c.read, i2c.write, i2c.TRANSMITTER, i2c.RECEIVER
  18. local bme280_math_setup, bme280_math_read, bme280_math_qfe2qnh, bme280_math_altitude, bme280_math_dewpoint =
  19. bme280_math.setup, bme280_math.read, bme280_math.qfe2qnh, bme280_math.altitude, bme280_math.dewpoint
  20. local tmr_create, tmr_ALARM_SINGLE = tmr.create, tmr.ALARM_SINGLE
  21. local BME280_I2C_ADDRESS1 = 0x76
  22. local BME280_I2C_ADDRESS2 = 0x77
  23. local BME280_REGISTER_CONTROL = 0xF4
  24. local BME280_REGISTER_CONTROL_HUM = 0xF2
  25. local BME280_REGISTER_CONFIG= 0xF5
  26. local BME280_REGISTER_CHIPID = 0xD0
  27. local BME280_REGISTER_DIG_T = 0x88 -- 0x88-0x8D ( 6)
  28. local BME280_REGISTER_DIG_P = 0x8E -- 0x8E-0x9F (18)
  29. local BME280_REGISTER_DIG_H1 = 0xA1 -- 0xA1 ( 1)
  30. local BME280_REGISTER_DIG_H2 = 0xE1 -- 0xE1-0xE7 ( 7)
  31. local BME280_REGISTER_PRESS = 0xF7 -- 0xF7-0xF9
  32. -- local BME280_FORCED_MODE = 0x01
  33. -- maximum measurement time in ms for maximum oversampling for all measures
  34. -- 113 > 1.25 + 2.3*16 + 2.3*16 + 0.575 + 2.3*16 + 0.575 ms
  35. local BME280_SAMPLING_DELAY =113
  36. -- Local functions
  37. local read_reg
  38. local write_reg
  39. local bme280_setup
  40. local bme280_read
  41. local bme280_startreadout
  42. -- -- Note that the space between debug and the arglist is there for a reason
  43. -- -- so that a simple global edit " debug(" -> "-- debug(" or v.v. to
  44. -- -- toggle debug compiled into the module.
  45. -- local print, node_heap = print, node.heap
  46. -- local function debug (fmt, ...) -- upval: cnt (, print, node_heap)
  47. -- if not bme280.debug then return end
  48. -- if (...) then fmt = fmt:format(...) end
  49. -- print("[bme280]", node_heap(), fmt)
  50. -- end
  51. --------------------------- Set up the bme280 object ----------------------------
  52. -- bme280 has method setup to create the sensor object and setup the sensor
  53. -- object created by bme280.setup() has methods: read, qfe2qnh, altitude, dewpoint
  54. ---------------------------------------------------------------------------------
  55. function bme280.setup(id, addr, temp_oss, press_oss, humi_oss, power_mode, inactive_duration, IIR_filter, full_init)
  56. return bme280_setup(nil, id,
  57. addr, temp_oss, press_oss, humi_oss, power_mode, inactive_duration, IIR_filter, full_init)
  58. end
  59. ------------------------------------------------------------------------------
  60. function bme280_setup(self, id, addr,
  61. temp_oss, press_oss, humi_oss, power_mode, inactive_duration, IIR_filter, full_init)
  62. addr = (addr==2) and BME280_I2C_ADDRESS2 or BME280_I2C_ADDRESS1
  63. full_init = full_init or true
  64. -- debug("%d %x %d", id, addr, BME280_REGISTER_CHIPID)
  65. local chipid = read_reg(id, addr, BME280_REGISTER_CHIPID, 1)
  66. if not chipid then
  67. return nil
  68. end
  69. -- debug("chip_id: %x", chipid:byte(1))
  70. local isbme = (chipid:byte(1) == 0x60)
  71. local buf = {}
  72. buf[1] = read_reg(id, addr, BME280_REGISTER_DIG_T, 6)
  73. buf[2] = read_reg(id, addr, BME280_REGISTER_DIG_P, 18)
  74. if (isbme) then
  75. buf[3] = read_reg(id, addr, BME280_REGISTER_DIG_H1, 1)
  76. buf[4] = read_reg(id, addr, BME280_REGISTER_DIG_H2, 7)
  77. end
  78. local sensor, config = bme280_math_setup(table_concat(buf),
  79. temp_oss, press_oss, humi_oss, power_mode, inactive_duration, IIR_filter)
  80. self = self or {
  81. setup = bme280_setup,
  82. read = bme280_read,
  83. startreadout = bme280_startreadout,
  84. qfe2qnh = bme280_math_qfe2qnh,
  85. altitude = bme280_math_altitude,
  86. dewpoint = bme280_math_dewpoint
  87. }
  88. self.id, self.addr = id, addr
  89. self._sensor, self._config, self._isbme = sensor, config, isbme
  90. if (full_init) then
  91. write_reg(id, addr, BME280_REGISTER_CONFIG, config[1])
  92. if (isbme) then write_reg(id, addr, BME280_REGISTER_CONTROL_HUM, config[2]) end
  93. write_reg(id, addr, BME280_REGISTER_CONTROL, config[3])
  94. end
  95. return self
  96. end
  97. function bme280_read(self, alt)
  98. local buf = read_reg(self.id, self.addr, BME280_REGISTER_PRESS, 8) -- registers are P[3], T[3], H[2]
  99. if buf then
  100. return bme280_math_read(self._sensor, buf, alt)
  101. else
  102. return nil
  103. end
  104. end
  105. function bme280_startreadout(self, callback, delay, alt)
  106. assert(type(callback) == "function", "invalid callback parameter")
  107. delay = delay or BME280_SAMPLING_DELAY
  108. if self._isbme then write_reg(self.id, self.addr, BME280_REGISTER_CONTROL_HUM, self._config[2]) end
  109. write_reg(self.id, self.addr, BME280_REGISTER_CONTROL, 4*math_floor(self._config[3]:byte(1)/4)+ 1) -- LUA51
  110. -- 4*math_floor(self._config[3]:byte(1)/4)+ 1
  111. -- an awful way to avoid bit operations but calculate (config[3] & 0xFC) | BME280_FORCED_MODE
  112. -- Lua 5.3: write_reg(self.id, self.addr, BME280_REGISTER_CONTROL, (self._config[3]:byte(1) & 0xFC) | 1)
  113. tmr_create():alarm(delay, tmr_ALARM_SINGLE,
  114. function()
  115. callback(bme280_read(self, alt))
  116. end
  117. )
  118. end
  119. function write_reg(id, dev_addr, reg_addr, data)
  120. i2c_start(id)
  121. if not i2c_address(id, dev_addr, i2c_TRANSMITTER) then
  122. -- debug("No ACK on address: %x", dev_addr)
  123. return nil
  124. end
  125. i2c_write(id, reg_addr)
  126. local c = i2c_write(id, data)
  127. i2c_stop(id)
  128. return c
  129. end
  130. function read_reg(id, dev_addr, reg_addr, n)
  131. i2c_start(id)
  132. if not i2c_address(id, dev_addr, i2c_TRANSMITTER) then
  133. -- debug("No ACK on address: %x", dev_addr)
  134. return nil
  135. end
  136. i2c_write(id, reg_addr)
  137. i2c_stop(id)
  138. i2c_start(id)
  139. i2c_address(id, dev_addr, i2c_RECEIVER)
  140. local c = i2c_read(id, n)
  141. i2c_stop(id)
  142. return c
  143. end
  144. ------------------------------------------------ -----------------------------
  145. return bme280