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.

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