Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

209 lines
6.4 KiB

  1. // -----------------------------------------------------------------------------
  2. // BME280/BMP280 Sensor over I2C
  3. // Uses SparkFun BME280 library
  4. // Copyright (C) 2017 by Xose Pérez <xose dot perez at gmail dot com>
  5. // -----------------------------------------------------------------------------
  6. #pragma once
  7. #include "Arduino.h"
  8. #include "BaseSensor.h"
  9. #include <SparkFunBME280.h>
  10. #define BMX280_CHIP_BMP280 0x58
  11. #define BMX280_CHIP_BME280 0x60
  12. class BMX280Sensor : public BaseSensor {
  13. public:
  14. // ---------------------------------------------------------------------
  15. // Public
  16. // ---------------------------------------------------------------------
  17. void setAddress(unsigned char address) {
  18. _address = address;
  19. }
  20. // ---------------------------------------------------------------------
  21. // Sensor API
  22. // ---------------------------------------------------------------------
  23. // Initialization method, must be idempotent
  24. void begin() {
  25. // Discover
  26. if (_address == 0) {
  27. unsigned char addresses[] = {0x76, 0x77};
  28. _address = i2cFindFirst(2, addresses);
  29. }
  30. if (_address == 0) {
  31. _error = SENSOR_ERROR_UNKNOWN_ID;
  32. _chip = 0;
  33. return;
  34. }
  35. // Init
  36. init();
  37. }
  38. // Descriptive name of the sensor
  39. String name() {
  40. char buffer[20];
  41. snprintf(buffer, sizeof(buffer), "%s @ I2C (0x%02X)", _chip == BMX280_CHIP_BME280 ? "BME280" : "BMP280", _address);
  42. return String(buffer);
  43. }
  44. // Descriptive name of the slot # index
  45. String slot(unsigned char index) {
  46. return name();
  47. }
  48. // Type for slot # index
  49. magnitude_t type(unsigned char index) {
  50. if (index < _count) {
  51. _error = SENSOR_ERROR_OK;
  52. unsigned char i = 0;
  53. #if BMX280_TEMPERATURE > 0
  54. if (index == i++) return MAGNITUDE_TEMPERATURE;
  55. #endif
  56. #if BMX280_PRESSURE > 0
  57. if (index == i++) return MAGNITUDE_PRESSURE;
  58. #endif
  59. #if BMX280_HUMIDITY > 0
  60. if (_chip == BMX280_CHIP_BME280) {
  61. if (index == i) return MAGNITUDE_HUMIDITY;
  62. }
  63. #endif
  64. }
  65. _error = SENSOR_ERROR_OUT_OF_RANGE;
  66. return MAGNITUDE_NONE;
  67. }
  68. // Pre-read hook (usually to populate registers with up-to-date data)
  69. virtual void pre() {
  70. if (_chip == 0) {
  71. _error = SENSOR_ERROR_UNKNOWN_ID;
  72. return;
  73. }
  74. #if BMX280_MODE == 1
  75. forceRead();
  76. #endif
  77. }
  78. // Current value for slot # index
  79. double value(unsigned char index) {
  80. if (index < _count) {
  81. _error = SENSOR_ERROR_OK;
  82. unsigned char i = 0;
  83. #if BMX280_TEMPERATURE > 0
  84. if (index == i++) return bme->readTempC();
  85. #endif
  86. #if BMX280_PRESSURE > 0
  87. if (index == i++) return bme->readFloatPressure() / 100;
  88. #endif
  89. #if BMX280_HUMIDITY > 0
  90. if (_chip == BMX280_CHIP_BME280) {
  91. if (index == i) return bme->readFloatHumidity();
  92. }
  93. #endif
  94. }
  95. _error = SENSOR_ERROR_OUT_OF_RANGE;
  96. return 0;
  97. }
  98. protected:
  99. void init() {
  100. // Destroy previous instance if any
  101. if (bme) delete bme;
  102. bme = new BME280();
  103. bme->settings.commInterface = I2C_MODE;
  104. bme->settings.I2CAddress = _address;
  105. bme->settings.runMode = BMX280_MODE;
  106. bme->settings.tStandby = 0;
  107. bme->settings.filter = 0;
  108. bme->settings.tempOverSample = BMX280_TEMPERATURE;
  109. bme->settings.pressOverSample = BMX280_PRESSURE;
  110. bme->settings.humidOverSample = BMX280_HUMIDITY;
  111. // Fix when not measuring temperature, t_fine should have a sensible value
  112. if (BMX280_TEMPERATURE == 0) bme->t_fine = 100000; // aprox 20ºC
  113. // Make sure sensor had enough time to turn on. BMX280 requires 2ms to start up
  114. delay(10);
  115. // Check sensor correctly initialized
  116. _chip = bme->begin();
  117. if ((_chip != BMX280_CHIP_BME280) && (_chip != BMX280_CHIP_BMP280)) {
  118. _chip = 0;
  119. _error = SENSOR_ERROR_UNKNOWN_ID;
  120. }
  121. #if BMX280_TEMPERATURE > 0
  122. ++_count;
  123. #endif
  124. #if BMX280_PRESSURE > 0
  125. ++_count;
  126. #endif
  127. #if BMX280_HUMIDITY > 0
  128. if (_chip == BMX280_CHIP_BME280) ++_count;
  129. #endif
  130. _measurement_delay = measurementTime();
  131. }
  132. unsigned long measurementTime() {
  133. // Measurement Time (as per BMX280 datasheet section 9.1)
  134. // T_max(ms) = 1.25
  135. // + (2.3 * T_oversampling)
  136. // + (2.3 * P_oversampling + 0.575)
  137. // + (2.4 * H_oversampling + 0.575)
  138. // ~ 9.3ms for current settings
  139. double t = 1.25;
  140. #if BMX280_TEMPERATURE > 0
  141. t += (2.3 * BMX280_TEMPERATURE);
  142. #endif
  143. #if BMX280_PRESSURE > 0
  144. t += (2.3 * BMX280_PRESSURE + 0.575);
  145. #endif
  146. #if BMX280_HUMIDITY > 0
  147. if (_chip == BMX280_CHIP_BME280) {
  148. t += (2.4 * BMX280_HUMIDITY + 0.575);
  149. }
  150. #endif
  151. return round(t + 1); // round up
  152. }
  153. void forceRead() {
  154. // We set the sensor in "forced mode" to force a reading.
  155. // After the reading the sensor will go back to sleep mode.
  156. uint8_t value = bme->readRegister(BME280_CTRL_MEAS_REG);
  157. value = (value & 0xFC) + 0x01;
  158. bme->writeRegister(BME280_CTRL_MEAS_REG, value);
  159. delay(_measurement_delay);
  160. }
  161. // ---------------------------------------------------------------------
  162. BME280 * bme;
  163. unsigned char _chip;
  164. unsigned char _address = 0;
  165. unsigned long _measurement_delay;
  166. };