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.

243 lines
7.8 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 "I2CSensor.h"
  9. #include <SparkFunBME280.h>
  10. #define BMX280_CHIP_BMP280 0x58
  11. #define BMX280_CHIP_BME280 0x60
  12. class BMX280Sensor : public I2CSensor {
  13. public:
  14. static unsigned char addresses[2];
  15. // ---------------------------------------------------------------------
  16. // Public
  17. // ---------------------------------------------------------------------
  18. BMX280Sensor(): I2CSensor() {
  19. _sensor_id = SENSOR_BMX280_ID;
  20. _bme = new BME280();
  21. }
  22. // ---------------------------------------------------------------------
  23. // Sensor API
  24. // ---------------------------------------------------------------------
  25. // Initialization method, must be idempotent
  26. void begin() {
  27. if (!_dirty) return;
  28. _dirty = false;
  29. _chip = 0;
  30. // I2C auto-discover
  31. _address = _begin_i2c(_address, sizeof(BMX280Sensor::addresses), BMX280Sensor::addresses);
  32. if (_address == 0) return;
  33. // Init
  34. _init();
  35. }
  36. // Descriptive name of the sensor
  37. String description() {
  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. // Type for slot # index
  43. magnitude_t type(unsigned char index) {
  44. if (index < _count) {
  45. _error = SENSOR_ERROR_OK;
  46. unsigned char i = 0;
  47. #if BMX280_TEMPERATURE > 0
  48. if (index == i++) return MAGNITUDE_TEMPERATURE;
  49. #endif
  50. #if BMX280_PRESSURE > 0
  51. if (index == i++) return MAGNITUDE_PRESSURE;
  52. #endif
  53. #if BMX280_HUMIDITY > 0
  54. if (_chip == BMX280_CHIP_BME280) {
  55. if (index == i) return MAGNITUDE_HUMIDITY;
  56. }
  57. #endif
  58. }
  59. _error = SENSOR_ERROR_OUT_OF_RANGE;
  60. return MAGNITUDE_NONE;
  61. }
  62. // Pre-read hook (usually to populate registers with up-to-date data)
  63. virtual void pre() {
  64. if (_chip == 0) {
  65. _error = SENSOR_ERROR_UNKNOWN_ID;
  66. return;
  67. }
  68. #if BMX280_MODE == 1
  69. forceRead();
  70. #endif
  71. }
  72. // Current value for slot # index
  73. double value(unsigned char index) {
  74. if (index < _count) {
  75. _error = SENSOR_ERROR_OK;
  76. unsigned char i = 0;
  77. #if BMX280_TEMPERATURE > 0
  78. if (index == i++) return _bme->readTempC();
  79. #endif
  80. #if BMX280_PRESSURE > 0
  81. if (index == i++) return _bme->readFloatPressure() / 100;
  82. #endif
  83. #if BMX280_HUMIDITY > 0
  84. if (_chip == BMX280_CHIP_BME280) {
  85. if (index == i) return _bme->readFloatHumidity();
  86. }
  87. #endif
  88. }
  89. _error = SENSOR_ERROR_OUT_OF_RANGE;
  90. return 0;
  91. }
  92. // Load the configuration manifest
  93. static void manifest(JsonArray& sensors) {
  94. char buffer[10];
  95. JsonObject& sensor = sensors.createNestedObject();
  96. sensor["sensor_id"] = SENSOR_BMX280_ID;
  97. JsonArray& fields = sensor.createNestedArray("fields");
  98. {
  99. JsonObject& field = fields.createNestedObject();
  100. field["tag"] = UI_TAG_SELECT;
  101. field["name"] = "address";
  102. field["label"] = "Address";
  103. JsonArray& options = field.createNestedArray("options");
  104. {
  105. JsonObject& option = options.createNestedObject();
  106. option["name"] = "auto";
  107. option["value"] = 0;
  108. }
  109. for (unsigned char i=0; i< sizeof(BMX280Sensor::addresses); i++) {
  110. JsonObject& option = options.createNestedObject();
  111. snprintf(buffer, sizeof(buffer), "0x%02X", BMX280Sensor::addresses[i]);
  112. option["name"] = String(buffer);
  113. option["value"] = BMX280Sensor::addresses[i];
  114. }
  115. }
  116. };
  117. void getConfig(JsonObject& root) {
  118. root["sensor_id"] = _sensor_id;
  119. root["address"] = getAddress();
  120. };
  121. void setConfig(JsonObject& root) {
  122. if (root.containsKey("address")) setAddress(root["address"]);
  123. };
  124. protected:
  125. void _init() {
  126. _bme->settings.commInterface = I2C_MODE;
  127. _bme->settings.I2CAddress = _address;
  128. _bme->settings.runMode = BMX280_MODE;
  129. _bme->settings.tStandby = 0;
  130. _bme->settings.filter = 0;
  131. _bme->settings.tempOverSample = BMX280_TEMPERATURE;
  132. _bme->settings.pressOverSample = BMX280_PRESSURE;
  133. _bme->settings.humidOverSample = BMX280_HUMIDITY;
  134. // Fix when not measuring temperature, t_fine should have a sensible value
  135. if (BMX280_TEMPERATURE == 0) _bme->t_fine = 100000; // aprox 20ºC
  136. // Make sure sensor had enough time to turn on. BMX280 requires 2ms to start up
  137. delay(10);
  138. // Check sensor correctly initialized
  139. _chip = _bme->begin();
  140. if ((_chip != BMX280_CHIP_BME280) && (_chip != BMX280_CHIP_BMP280)) {
  141. _chip = 0;
  142. i2cReleaseLock(_address);
  143. _error = SENSOR_ERROR_UNKNOWN_ID;
  144. }
  145. #if BMX280_TEMPERATURE > 0
  146. ++_count;
  147. #endif
  148. #if BMX280_PRESSURE > 0
  149. ++_count;
  150. #endif
  151. #if BMX280_HUMIDITY > 0
  152. if (_chip == BMX280_CHIP_BME280) ++_count;
  153. #endif
  154. _measurement_delay = measurementTime();
  155. }
  156. unsigned long measurementTime() {
  157. // Measurement Time (as per BMX280 datasheet section 9.1)
  158. // T_max(ms) = 1.25
  159. // + (2.3 * T_oversampling)
  160. // + (2.3 * P_oversampling + 0.575)
  161. // + (2.4 * H_oversampling + 0.575)
  162. // ~ 9.3ms for current settings
  163. double t = 1.25;
  164. #if BMX280_TEMPERATURE > 0
  165. t += (2.3 * BMX280_TEMPERATURE);
  166. #endif
  167. #if BMX280_PRESSURE > 0
  168. t += (2.3 * BMX280_PRESSURE + 0.575);
  169. #endif
  170. #if BMX280_HUMIDITY > 0
  171. if (_chip == BMX280_CHIP_BME280) {
  172. t += (2.4 * BMX280_HUMIDITY + 0.575);
  173. }
  174. #endif
  175. return round(t + 1); // round up
  176. }
  177. void forceRead() {
  178. // We set the sensor in "forced mode" to force a reading.
  179. // After the reading the sensor will go back to sleep mode.
  180. uint8_t value = _bme->readRegister(BME280_CTRL_MEAS_REG);
  181. value = (value & 0xFC) + 0x01;
  182. _bme->writeRegister(BME280_CTRL_MEAS_REG, value);
  183. delay(_measurement_delay);
  184. }
  185. // ---------------------------------------------------------------------
  186. BME280 * _bme = NULL;
  187. unsigned char _chip;
  188. unsigned long _measurement_delay;
  189. };
  190. // Static inizializations
  191. unsigned char BMX280Sensor::addresses[2] = {0x76, 0x77};