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.

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