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.

214 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. i2cReleaseLock(_address);
  122. _error = SENSOR_ERROR_UNKNOWN_ID;
  123. }
  124. #if BMX280_TEMPERATURE > 0
  125. ++_count;
  126. #endif
  127. #if BMX280_PRESSURE > 0
  128. ++_count;
  129. #endif
  130. #if BMX280_HUMIDITY > 0
  131. if (_chip == BMX280_CHIP_BME280) ++_count;
  132. #endif
  133. _measurement_delay = measurementTime();
  134. }
  135. unsigned long measurementTime() {
  136. // Measurement Time (as per BMX280 datasheet section 9.1)
  137. // T_max(ms) = 1.25
  138. // + (2.3 * T_oversampling)
  139. // + (2.3 * P_oversampling + 0.575)
  140. // + (2.4 * H_oversampling + 0.575)
  141. // ~ 9.3ms for current settings
  142. double t = 1.25;
  143. #if BMX280_TEMPERATURE > 0
  144. t += (2.3 * BMX280_TEMPERATURE);
  145. #endif
  146. #if BMX280_PRESSURE > 0
  147. t += (2.3 * BMX280_PRESSURE + 0.575);
  148. #endif
  149. #if BMX280_HUMIDITY > 0
  150. if (_chip == BMX280_CHIP_BME280) {
  151. t += (2.4 * BMX280_HUMIDITY + 0.575);
  152. }
  153. #endif
  154. return round(t + 1); // round up
  155. }
  156. void forceRead() {
  157. // We set the sensor in "forced mode" to force a reading.
  158. // After the reading the sensor will go back to sleep mode.
  159. uint8_t value = bme->readRegister(BME280_CTRL_MEAS_REG);
  160. value = (value & 0xFC) + 0x01;
  161. bme->writeRegister(BME280_CTRL_MEAS_REG, value);
  162. delay(_measurement_delay);
  163. }
  164. // ---------------------------------------------------------------------
  165. BME280 * bme;
  166. unsigned char _chip;
  167. unsigned long _measurement_delay;
  168. };