Adafruit_BME280.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. /*!
  2. * @file Adafruit_BME280.cpp
  3. *
  4. * @mainpage Adafruit BME280 humidity, temperature & pressure sensor
  5. *
  6. * @section intro_sec Introduction
  7. *
  8. * Driver for the BME280 humidity, temperature & pressure sensor
  9. *
  10. * These sensors use I2C or SPI to communicate, 2 or 4 pins are required
  11. * to interface.
  12. *
  13. * Designed specifically to work with the Adafruit BME280 Breakout
  14. * ----> http://www.adafruit.com/products/2652
  15. *
  16. * Adafruit invests time and resources providing this open source code,
  17. * please support Adafruit and open-source hardware by purchasing
  18. * products from Adafruit!
  19. *
  20. * @section author Author
  21. *
  22. * Written by Kevin "KTOWN" Townsend for Adafruit Industries.
  23. *
  24. * @section license License
  25. *
  26. * BSD license, all text here must be included in any redistribution.
  27. * See the LICENSE file for details.
  28. *
  29. */
  30. #include "Adafruit_BME280.h"
  31. #include "Arduino.h"
  32. /*!
  33. * @brief class constructor
  34. */
  35. Adafruit_BME280::Adafruit_BME280() {}
  36. /*!
  37. * @brief class constructor if using hardware SPI
  38. * @param cspin the chip select pin to use
  39. * @param *theSPI
  40. * optional SPI object
  41. */
  42. Adafruit_BME280::Adafruit_BME280(int8_t cspin, SPIClass *theSPI) {
  43. spi_dev = new Adafruit_SPIDevice(cspin, 1000000, SPI_BITORDER_MSBFIRST,
  44. SPI_MODE0, theSPI);
  45. }
  46. /*!
  47. * @brief class constructor if using software SPI
  48. * @param cspin the chip select pin to use
  49. * @param mosipin the MOSI pin to use
  50. * @param misopin the MISO pin to use
  51. * @param sckpin the SCK pin to use
  52. */
  53. Adafruit_BME280::Adafruit_BME280(int8_t cspin, int8_t mosipin, int8_t misopin,
  54. int8_t sckpin) {
  55. spi_dev = new Adafruit_SPIDevice(cspin, sckpin, misopin, mosipin);
  56. }
  57. Adafruit_BME280::~Adafruit_BME280(void) {
  58. if (spi_dev) {
  59. delete spi_dev;
  60. }
  61. if (i2c_dev) {
  62. delete i2c_dev;
  63. }
  64. if (temp_sensor) {
  65. delete temp_sensor;
  66. }
  67. if (pressure_sensor) {
  68. delete pressure_sensor;
  69. }
  70. if (humidity_sensor) {
  71. delete humidity_sensor;
  72. }
  73. }
  74. /*!
  75. * @brief Initialise sensor with given parameters / settings
  76. * @param addr the I2C address the device can be found on
  77. * @param theWire the I2C object to use, defaults to &Wire
  78. * @returns true on success, false otherwise
  79. */
  80. bool Adafruit_BME280::begin(uint8_t addr, TwoWire *theWire) {
  81. if (spi_dev == NULL) {
  82. // I2C mode
  83. if (i2c_dev)
  84. delete i2c_dev;
  85. i2c_dev = new Adafruit_I2CDevice(addr, theWire);
  86. if (!i2c_dev->begin())
  87. return false;
  88. } else {
  89. // SPI mode
  90. if (!spi_dev->begin())
  91. return false;
  92. }
  93. return init();
  94. }
  95. /*!
  96. * @brief Initialise sensor with given parameters / settings
  97. * @returns true on success, false otherwise
  98. */
  99. bool Adafruit_BME280::init() {
  100. // check if sensor, i.e. the chip ID is correct
  101. _sensorID = read8(BME280_REGISTER_CHIPID);
  102. if (_sensorID != 0x60)
  103. return false;
  104. // reset the device using soft-reset
  105. // this makes sure the IIR is off, etc.
  106. write8(BME280_REGISTER_SOFTRESET, 0xB6);
  107. // wait for chip to wake up.
  108. delay(10);
  109. // if chip is still reading calibration, delay
  110. while (isReadingCalibration())
  111. delay(10);
  112. readCoefficients(); // read trimming parameters, see DS 4.2.2
  113. setSampling(); // use defaults
  114. delay(100);
  115. return true;
  116. }
  117. /*!
  118. * @brief setup sensor with given parameters / settings
  119. *
  120. * @param mode the power mode to use for the sensor
  121. * @param tempSampling the temp samping rate to use
  122. * @param pressSampling the pressure sampling rate to use
  123. * @param humSampling the humidity sampling rate to use
  124. * @param filter the filter mode to use
  125. * @param duration the standby duration to use
  126. */
  127. void Adafruit_BME280::setSampling(sensor_mode mode,
  128. sensor_sampling tempSampling,
  129. sensor_sampling pressSampling,
  130. sensor_sampling humSampling,
  131. sensor_filter filter,
  132. standby_duration duration) {
  133. _measReg.mode = mode;
  134. _measReg.osrs_t = tempSampling;
  135. _measReg.osrs_p = pressSampling;
  136. _humReg.osrs_h = humSampling;
  137. _configReg.filter = filter;
  138. _configReg.t_sb = duration;
  139. _configReg.spi3w_en = 0;
  140. // making sure sensor is in sleep mode before setting configuration
  141. // as it otherwise may be ignored
  142. write8(BME280_REGISTER_CONTROL, MODE_SLEEP);
  143. // you must make sure to also set REGISTER_CONTROL after setting the
  144. // CONTROLHUMID register, otherwise the values won't be applied (see
  145. // DS 5.4.3)
  146. write8(BME280_REGISTER_CONTROLHUMID, _humReg.get());
  147. write8(BME280_REGISTER_CONFIG, _configReg.get());
  148. write8(BME280_REGISTER_CONTROL, _measReg.get());
  149. }
  150. /*!
  151. * @brief Writes an 8 bit value over I2C or SPI
  152. * @param reg the register address to write to
  153. * @param value the value to write to the register
  154. */
  155. void Adafruit_BME280::write8(byte reg, byte value) {
  156. byte buffer[2];
  157. buffer[1] = value;
  158. if (i2c_dev) {
  159. buffer[0] = reg;
  160. i2c_dev->write(buffer, 2);
  161. } else {
  162. buffer[0] = reg & ~0x80;
  163. spi_dev->write(buffer, 2);
  164. }
  165. }
  166. /*!
  167. * @brief Reads an 8 bit value over I2C or SPI
  168. * @param reg the register address to read from
  169. * @returns the data byte read from the device
  170. */
  171. uint8_t Adafruit_BME280::read8(byte reg) {
  172. uint8_t buffer[1];
  173. if (i2c_dev) {
  174. buffer[0] = uint8_t(reg);
  175. i2c_dev->write_then_read(buffer, 1, buffer, 1);
  176. } else {
  177. buffer[0] = uint8_t(reg | 0x80);
  178. spi_dev->write_then_read(buffer, 1, buffer, 1);
  179. }
  180. return buffer[0];
  181. }
  182. /*!
  183. * @brief Reads a 16 bit value over I2C or SPI
  184. * @param reg the register address to read from
  185. * @returns the 16 bit data value read from the device
  186. */
  187. uint16_t Adafruit_BME280::read16(byte reg) {
  188. uint8_t buffer[2];
  189. if (i2c_dev) {
  190. buffer[0] = uint8_t(reg);
  191. i2c_dev->write_then_read(buffer, 1, buffer, 2);
  192. } else {
  193. buffer[0] = uint8_t(reg | 0x80);
  194. spi_dev->write_then_read(buffer, 1, buffer, 2);
  195. }
  196. return uint16_t(buffer[0]) << 8 | uint16_t(buffer[1]);
  197. }
  198. /*!
  199. * @brief Reads a signed 16 bit little endian value over I2C or SPI
  200. * @param reg the register address to read from
  201. * @returns the 16 bit data value read from the device
  202. */
  203. uint16_t Adafruit_BME280::read16_LE(byte reg) {
  204. uint16_t temp = read16(reg);
  205. return (temp >> 8) | (temp << 8);
  206. }
  207. /*!
  208. * @brief Reads a signed 16 bit value over I2C or SPI
  209. * @param reg the register address to read from
  210. * @returns the 16 bit data value read from the device
  211. */
  212. int16_t Adafruit_BME280::readS16(byte reg) { return (int16_t)read16(reg); }
  213. /*!
  214. * @brief Reads a signed little endian 16 bit value over I2C or SPI
  215. * @param reg the register address to read from
  216. * @returns the 16 bit data value read from the device
  217. */
  218. int16_t Adafruit_BME280::readS16_LE(byte reg) {
  219. return (int16_t)read16_LE(reg);
  220. }
  221. /*!
  222. * @brief Reads a 24 bit value over I2C
  223. * @param reg the register address to read from
  224. * @returns the 24 bit data value read from the device
  225. */
  226. uint32_t Adafruit_BME280::read24(byte reg) {
  227. uint8_t buffer[3];
  228. if (i2c_dev) {
  229. buffer[0] = uint8_t(reg);
  230. i2c_dev->write_then_read(buffer, 1, buffer, 3);
  231. } else {
  232. buffer[0] = uint8_t(reg | 0x80);
  233. spi_dev->write_then_read(buffer, 1, buffer, 3);
  234. }
  235. return uint32_t(buffer[0]) << 16 | uint32_t(buffer[1]) << 8 |
  236. uint32_t(buffer[2]);
  237. }
  238. /*!
  239. * @brief Take a new measurement (only possible in forced mode)
  240. @returns true in case of success else false
  241. */
  242. bool Adafruit_BME280::takeForcedMeasurement(void) {
  243. bool return_value = false;
  244. // If we are in forced mode, the BME sensor goes back to sleep after each
  245. // measurement and we need to set it to forced mode once at this point, so
  246. // it will take the next measurement and then return to sleep again.
  247. // In normal mode simply does new measurements periodically.
  248. if (_measReg.mode == MODE_FORCED) {
  249. return_value = true;
  250. // set to forced mode, i.e. "take next measurement"
  251. write8(BME280_REGISTER_CONTROL, _measReg.get());
  252. // Store current time to measure the timeout
  253. uint32_t timeout_start = millis();
  254. // wait until measurement has been completed, otherwise we would read the
  255. // the values from the last measurement or the timeout occurred after 2 sec.
  256. while (read8(BME280_REGISTER_STATUS) & 0x08) {
  257. // In case of a timeout, stop the while loop
  258. if ((millis() - timeout_start) > 2000) {
  259. return_value = false;
  260. break;
  261. }
  262. delay(1);
  263. }
  264. }
  265. return return_value;
  266. }
  267. /*!
  268. * @brief Reads the factory-set coefficients
  269. */
  270. void Adafruit_BME280::readCoefficients(void) {
  271. _bme280_calib.dig_T1 = read16_LE(BME280_REGISTER_DIG_T1);
  272. _bme280_calib.dig_T2 = readS16_LE(BME280_REGISTER_DIG_T2);
  273. _bme280_calib.dig_T3 = readS16_LE(BME280_REGISTER_DIG_T3);
  274. _bme280_calib.dig_P1 = read16_LE(BME280_REGISTER_DIG_P1);
  275. _bme280_calib.dig_P2 = readS16_LE(BME280_REGISTER_DIG_P2);
  276. _bme280_calib.dig_P3 = readS16_LE(BME280_REGISTER_DIG_P3);
  277. _bme280_calib.dig_P4 = readS16_LE(BME280_REGISTER_DIG_P4);
  278. _bme280_calib.dig_P5 = readS16_LE(BME280_REGISTER_DIG_P5);
  279. _bme280_calib.dig_P6 = readS16_LE(BME280_REGISTER_DIG_P6);
  280. _bme280_calib.dig_P7 = readS16_LE(BME280_REGISTER_DIG_P7);
  281. _bme280_calib.dig_P8 = readS16_LE(BME280_REGISTER_DIG_P8);
  282. _bme280_calib.dig_P9 = readS16_LE(BME280_REGISTER_DIG_P9);
  283. _bme280_calib.dig_H1 = read8(BME280_REGISTER_DIG_H1);
  284. _bme280_calib.dig_H2 = readS16_LE(BME280_REGISTER_DIG_H2);
  285. _bme280_calib.dig_H3 = read8(BME280_REGISTER_DIG_H3);
  286. _bme280_calib.dig_H4 = ((int8_t)read8(BME280_REGISTER_DIG_H4) << 4) |
  287. (read8(BME280_REGISTER_DIG_H4 + 1) & 0xF);
  288. _bme280_calib.dig_H5 = ((int8_t)read8(BME280_REGISTER_DIG_H5 + 1) << 4) |
  289. (read8(BME280_REGISTER_DIG_H5) >> 4);
  290. _bme280_calib.dig_H6 = (int8_t)read8(BME280_REGISTER_DIG_H6);
  291. }
  292. /*!
  293. * @brief return true if chip is busy reading cal data
  294. * @returns true if reading calibration, false otherwise
  295. */
  296. bool Adafruit_BME280::isReadingCalibration(void) {
  297. uint8_t const rStatus = read8(BME280_REGISTER_STATUS);
  298. return (rStatus & (1 << 0)) != 0;
  299. }
  300. /*!
  301. * @brief Returns the temperature from the sensor
  302. * @returns the temperature read from the device or NaN if sampling off
  303. */
  304. float Adafruit_BME280::readTemperature(void) {
  305. int32_t var1, var2;
  306. if (_measReg.osrs_t == sensor_sampling::SAMPLING_NONE)
  307. return NAN;
  308. int32_t adc_T = read24(BME280_REGISTER_TEMPDATA);
  309. adc_T >>= 4;
  310. var1 = (int32_t)((adc_T / 8) - ((int32_t)_bme280_calib.dig_T1 * 2));
  311. var1 = (var1 * ((int32_t)_bme280_calib.dig_T2)) / 2048;
  312. var2 = (int32_t)((adc_T / 16) - ((int32_t)_bme280_calib.dig_T1));
  313. var2 = (((var2 * var2) / 4096) * ((int32_t)_bme280_calib.dig_T3)) / 16384;
  314. t_fine = var1 + var2 + t_fine_adjust;
  315. int32_t T = (t_fine * 5 + 128) / 256;
  316. return (float)T / 100;
  317. }
  318. /*!
  319. * @brief Returns the pressure from the sensor
  320. * @returns the pressure value (in Pascal) or NaN if sampling off
  321. */
  322. float Adafruit_BME280::readPressure(void) {
  323. int64_t var1, var2, var3, var4;
  324. if (_measReg.osrs_p == sensor_sampling::SAMPLING_NONE)
  325. return NAN;
  326. readTemperature(); // must be done first to get t_fine
  327. int32_t adc_P = read24(BME280_REGISTER_PRESSUREDATA);
  328. adc_P >>= 4;
  329. var1 = ((int64_t)t_fine) - 128000;
  330. var2 = var1 * var1 * (int64_t)_bme280_calib.dig_P6;
  331. var2 = var2 + ((var1 * (int64_t)_bme280_calib.dig_P5) * 131072);
  332. var2 = var2 + (((int64_t)_bme280_calib.dig_P4) * 34359738368);
  333. var1 = ((var1 * var1 * (int64_t)_bme280_calib.dig_P3) / 256) +
  334. ((var1 * ((int64_t)_bme280_calib.dig_P2) * 4096));
  335. var3 = ((int64_t)1) * 140737488355328;
  336. var1 = (var3 + var1) * ((int64_t)_bme280_calib.dig_P1) / 8589934592;
  337. if (var1 == 0) {
  338. return 0; // avoid exception caused by division by zero
  339. }
  340. var4 = 1048576 - adc_P;
  341. var4 = (((var4 * 2147483648) - var2) * 3125) / var1;
  342. var1 = (((int64_t)_bme280_calib.dig_P9) * (var4 / 8192) * (var4 / 8192)) /
  343. 33554432;
  344. var2 = (((int64_t)_bme280_calib.dig_P8) * var4) / 524288;
  345. var4 = ((var4 + var1 + var2) / 256) + (((int64_t)_bme280_calib.dig_P7) * 16);
  346. float P = var4 / 256.0;
  347. return P;
  348. }
  349. /*!
  350. * @brief Returns the humidity from the sensor
  351. * @returns the humidity value read from the device or NaN if sampling off
  352. */
  353. float Adafruit_BME280::readHumidity(void) {
  354. int32_t var1, var2, var3, var4, var5;
  355. if (_humReg.osrs_h == sensor_sampling::SAMPLING_NONE)
  356. return NAN;
  357. readTemperature(); // must be done first to get t_fine
  358. int32_t adc_H = read16(BME280_REGISTER_HUMIDDATA);
  359. var1 = t_fine - ((int32_t)76800);
  360. var2 = (int32_t)(adc_H * 16384);
  361. var3 = (int32_t)(((int32_t)_bme280_calib.dig_H4) * 1048576);
  362. var4 = ((int32_t)_bme280_calib.dig_H5) * var1;
  363. var5 = (((var2 - var3) - var4) + (int32_t)16384) / 32768;
  364. var2 = (var1 * ((int32_t)_bme280_calib.dig_H6)) / 1024;
  365. var3 = (var1 * ((int32_t)_bme280_calib.dig_H3)) / 2048;
  366. var4 = ((var2 * (var3 + (int32_t)32768)) / 1024) + (int32_t)2097152;
  367. var2 = ((var4 * ((int32_t)_bme280_calib.dig_H2)) + 8192) / 16384;
  368. var3 = var5 * var2;
  369. var4 = ((var3 / 32768) * (var3 / 32768)) / 128;
  370. var5 = var3 - ((var4 * ((int32_t)_bme280_calib.dig_H1)) / 16);
  371. var5 = (var5 < 0 ? 0 : var5);
  372. var5 = (var5 > 419430400 ? 419430400 : var5);
  373. uint32_t H = (uint32_t)(var5 / 4096);
  374. return (float)H / 1024.0;
  375. }
  376. /*!
  377. * Calculates the altitude (in meters) from the specified atmospheric
  378. * pressure (in hPa), and sea-level pressure (in hPa).
  379. * @param seaLevel Sea-level pressure in hPa
  380. * @returns the altitude value read from the device
  381. */
  382. float Adafruit_BME280::readAltitude(float seaLevel) {
  383. // Equation taken from BMP180 datasheet (page 16):
  384. // http://www.adafruit.com/datasheets/BST-BMP180-DS000-09.pdf
  385. // Note that using the equation from wikipedia can give bad results
  386. // at high altitude. See this thread for more information:
  387. // http://forums.adafruit.com/viewtopic.php?f=22&t=58064
  388. float atmospheric = readPressure() / 100.0F;
  389. return 44330.0 * (1.0 - pow(atmospheric / seaLevel, 0.1903));
  390. }
  391. /*!
  392. * Calculates the pressure at sea level (in hPa) from the specified
  393. * altitude (in meters), and atmospheric pressure (in hPa).
  394. * @param altitude Altitude in meters
  395. * @param atmospheric Atmospheric pressure in hPa
  396. * @returns the pressure at sea level (in hPa) from the specified altitude
  397. */
  398. float Adafruit_BME280::seaLevelForAltitude(float altitude, float atmospheric) {
  399. // Equation taken from BMP180 datasheet (page 17):
  400. // http://www.adafruit.com/datasheets/BST-BMP180-DS000-09.pdf
  401. // Note that using the equation from wikipedia can give bad results
  402. // at high altitude. See this thread for more information:
  403. // http://forums.adafruit.com/viewtopic.php?f=22&t=58064
  404. return atmospheric / pow(1.0 - (altitude / 44330.0), 5.255);
  405. }
  406. /*!
  407. * Returns Sensor ID found by init() for diagnostics
  408. * @returns Sensor ID 0x60 for BME280, 0x56, 0x57, 0x58 BMP280
  409. */
  410. uint32_t Adafruit_BME280::sensorID(void) { return _sensorID; }
  411. /*!
  412. * Returns the current temperature compensation value in degrees Celsius
  413. * @returns the current temperature compensation value in degrees Celsius
  414. */
  415. float Adafruit_BME280::getTemperatureCompensation(void) {
  416. return float((t_fine_adjust * 5) >> 8) / 100.0;
  417. };
  418. /*!
  419. * Sets a value to be added to each temperature reading. This adjusted
  420. * temperature is used in pressure and humidity readings.
  421. * @param adjustment Value to be added to each temperature reading in Celsius
  422. */
  423. void Adafruit_BME280::setTemperatureCompensation(float adjustment) {
  424. // convert the value in C into and adjustment to t_fine
  425. t_fine_adjust = ((int32_t(adjustment * 100) << 8)) / 5;
  426. };
  427. /*!
  428. @brief Gets an Adafruit Unified Sensor object for the temp sensor component
  429. @return Adafruit_Sensor pointer to temperature sensor
  430. */
  431. Adafruit_Sensor *Adafruit_BME280::getTemperatureSensor(void) {
  432. if (!temp_sensor) {
  433. temp_sensor = new Adafruit_BME280_Temp(this);
  434. }
  435. return temp_sensor;
  436. }
  437. /*!
  438. @brief Gets an Adafruit Unified Sensor object for the pressure sensor
  439. component
  440. @return Adafruit_Sensor pointer to pressure sensor
  441. */
  442. Adafruit_Sensor *Adafruit_BME280::getPressureSensor(void) {
  443. if (!pressure_sensor) {
  444. pressure_sensor = new Adafruit_BME280_Pressure(this);
  445. }
  446. return pressure_sensor;
  447. }
  448. /*!
  449. @brief Gets an Adafruit Unified Sensor object for the humidity sensor
  450. component
  451. @return Adafruit_Sensor pointer to humidity sensor
  452. */
  453. Adafruit_Sensor *Adafruit_BME280::getHumiditySensor(void) {
  454. if (!humidity_sensor) {
  455. humidity_sensor = new Adafruit_BME280_Humidity(this);
  456. }
  457. return humidity_sensor;
  458. }
  459. /**************************************************************************/
  460. /*!
  461. @brief Gets the sensor_t data for the BME280's temperature sensor
  462. */
  463. /**************************************************************************/
  464. void Adafruit_BME280_Temp::getSensor(sensor_t *sensor) {
  465. /* Clear the sensor_t object */
  466. memset(sensor, 0, sizeof(sensor_t));
  467. /* Insert the sensor name in the fixed length char array */
  468. strncpy(sensor->name, "BME280", sizeof(sensor->name) - 1);
  469. sensor->name[sizeof(sensor->name) - 1] = 0;
  470. sensor->version = 1;
  471. sensor->sensor_id = _sensorID;
  472. sensor->type = SENSOR_TYPE_AMBIENT_TEMPERATURE;
  473. sensor->min_delay = 0;
  474. sensor->min_value = -40.0; /* Temperature range -40 ~ +85 C */
  475. sensor->max_value = +85.0;
  476. sensor->resolution = 0.01; /* 0.01 C */
  477. }
  478. /**************************************************************************/
  479. /*!
  480. @brief Gets the temperature as a standard sensor event
  481. @param event Sensor event object that will be populated
  482. @returns True
  483. */
  484. /**************************************************************************/
  485. bool Adafruit_BME280_Temp::getEvent(sensors_event_t *event) {
  486. /* Clear the event */
  487. memset(event, 0, sizeof(sensors_event_t));
  488. event->version = sizeof(sensors_event_t);
  489. event->sensor_id = _sensorID;
  490. event->type = SENSOR_TYPE_AMBIENT_TEMPERATURE;
  491. event->timestamp = millis();
  492. event->temperature = _theBME280->readTemperature();
  493. return true;
  494. }
  495. /**************************************************************************/
  496. /*!
  497. @brief Gets the sensor_t data for the BME280's pressure sensor
  498. */
  499. /**************************************************************************/
  500. void Adafruit_BME280_Pressure::getSensor(sensor_t *sensor) {
  501. /* Clear the sensor_t object */
  502. memset(sensor, 0, sizeof(sensor_t));
  503. /* Insert the sensor name in the fixed length char array */
  504. strncpy(sensor->name, "BME280", sizeof(sensor->name) - 1);
  505. sensor->name[sizeof(sensor->name) - 1] = 0;
  506. sensor->version = 1;
  507. sensor->sensor_id = _sensorID;
  508. sensor->type = SENSOR_TYPE_PRESSURE;
  509. sensor->min_delay = 0;
  510. sensor->min_value = 300.0; /* 300 ~ 1100 hPa */
  511. sensor->max_value = 1100.0;
  512. sensor->resolution = 0.012; /* 0.12 hPa relative */
  513. }
  514. /**************************************************************************/
  515. /*!
  516. @brief Gets the pressure as a standard sensor event
  517. @param event Sensor event object that will be populated
  518. @returns True
  519. */
  520. /**************************************************************************/
  521. bool Adafruit_BME280_Pressure::getEvent(sensors_event_t *event) {
  522. /* Clear the event */
  523. memset(event, 0, sizeof(sensors_event_t));
  524. event->version = sizeof(sensors_event_t);
  525. event->sensor_id = _sensorID;
  526. event->type = SENSOR_TYPE_PRESSURE;
  527. event->timestamp = millis();
  528. event->pressure = _theBME280->readPressure() / 100; // convert Pa to hPa
  529. return true;
  530. }
  531. /**************************************************************************/
  532. /*!
  533. @brief Gets the sensor_t data for the BME280's humidity sensor
  534. */
  535. /**************************************************************************/
  536. void Adafruit_BME280_Humidity::getSensor(sensor_t *sensor) {
  537. /* Clear the sensor_t object */
  538. memset(sensor, 0, sizeof(sensor_t));
  539. /* Insert the sensor name in the fixed length char array */
  540. strncpy(sensor->name, "BME280", sizeof(sensor->name) - 1);
  541. sensor->name[sizeof(sensor->name) - 1] = 0;
  542. sensor->version = 1;
  543. sensor->sensor_id = _sensorID;
  544. sensor->type = SENSOR_TYPE_RELATIVE_HUMIDITY;
  545. sensor->min_delay = 0;
  546. sensor->min_value = 0;
  547. sensor->max_value = 100; /* 0 - 100 % */
  548. sensor->resolution = 3; /* 3% accuracy */
  549. }
  550. /**************************************************************************/
  551. /*!
  552. @brief Gets the humidity as a standard sensor event
  553. @param event Sensor event object that will be populated
  554. @returns True
  555. */
  556. /**************************************************************************/
  557. bool Adafruit_BME280_Humidity::getEvent(sensors_event_t *event) {
  558. /* Clear the event */
  559. memset(event, 0, sizeof(sensors_event_t));
  560. event->version = sizeof(sensors_event_t);
  561. event->sensor_id = _sensorID;
  562. event->type = SENSOR_TYPE_RELATIVE_HUMIDITY;
  563. event->timestamp = millis();
  564. event->relative_humidity = _theBME280->readHumidity();
  565. return true;
  566. }