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.

251 lines
7.9 KiB

7 years ago
  1. // -----------------------------------------------------------------------------
  2. // BME280/BMP280 Sensor over I2C
  3. // Uses SparkFun BME280 library
  4. // Copyright (C) 2017-2018 by Xose Pérez <xose dot perez at gmail dot com>
  5. // -----------------------------------------------------------------------------
  6. #if SENSOR_SUPPORT && BMX280_SUPPORT
  7. #pragma once
  8. #include "Arduino.h"
  9. #include "I2CSensor.h"
  10. #include <SparkFunBME280.h>
  11. #define BMX280_CHIP_BMP280 0x58
  12. #define BMX280_CHIP_BME280 0x60
  13. class BMX280Sensor : public I2CSensor {
  14. public:
  15. static unsigned char addresses[2];
  16. // ---------------------------------------------------------------------
  17. // Public
  18. // ---------------------------------------------------------------------
  19. BMX280Sensor(): I2CSensor() {
  20. _sensor_id = SENSOR_BMX280_ID;
  21. _bme = new BME280();
  22. }
  23. ~BMX280Sensor() {
  24. delete _bme;
  25. }
  26. // ---------------------------------------------------------------------
  27. // Sensor API
  28. // ---------------------------------------------------------------------
  29. // Initialization method, must be idempotent
  30. void begin() {
  31. if (!_dirty) return;
  32. _dirty = false;
  33. _chip = 0;
  34. // I2C auto-discover
  35. _address = _begin_i2c(_address, sizeof(BMX280Sensor::addresses), BMX280Sensor::addresses);
  36. if (_address == 0) return;
  37. // Init
  38. _init();
  39. }
  40. // Descriptive name of the sensor
  41. String description() {
  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. // Type for slot # index
  47. unsigned char 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. // Load the configuration manifest
  97. static void manifest(JsonArray& sensors) {
  98. char buffer[10];
  99. JsonObject& sensor = sensors.createNestedObject();
  100. sensor["sensor_id"] = SENSOR_BMX280_ID;
  101. JsonArray& fields = sensor.createNestedArray("fields");
  102. {
  103. JsonObject& field = fields.createNestedObject();
  104. field["tag"] = UI_TAG_SELECT;
  105. field["name"] = "address";
  106. field["label"] = "Address";
  107. JsonArray& options = field.createNestedArray("options");
  108. {
  109. JsonObject& option = options.createNestedObject();
  110. option["name"] = "auto";
  111. option["value"] = 0;
  112. }
  113. for (unsigned char i=0; i< sizeof(BMX280Sensor::addresses); i++) {
  114. JsonObject& option = options.createNestedObject();
  115. snprintf(buffer, sizeof(buffer), "0x%02X", BMX280Sensor::addresses[i]);
  116. option["name"] = String(buffer);
  117. option["value"] = BMX280Sensor::addresses[i];
  118. }
  119. }
  120. };
  121. void getConfig(JsonObject& root) {
  122. root["sensor_id"] = _sensor_id;
  123. root["address"] = _address;
  124. };
  125. void setConfig(JsonObject& root) {
  126. if (root.containsKey("address")) setAddress(root["address"]);
  127. };
  128. protected:
  129. void _init() {
  130. _bme->settings.commInterface = I2C_MODE;
  131. _bme->settings.I2CAddress = _address;
  132. _bme->settings.runMode = BMX280_MODE;
  133. _bme->settings.tStandby = 0;
  134. _bme->settings.filter = 0;
  135. _bme->settings.tempOverSample = BMX280_TEMPERATURE;
  136. _bme->settings.pressOverSample = BMX280_PRESSURE;
  137. _bme->settings.humidOverSample = BMX280_HUMIDITY;
  138. // Fix when not measuring temperature, t_fine should have a sensible value
  139. if (BMX280_TEMPERATURE == 0) _bme->t_fine = 100000; // aprox 20ºC
  140. // Make sure sensor had enough time to turn on. BMX280 requires 2ms to start up
  141. delay(10);
  142. // Check sensor correctly initialized
  143. _chip = _bme->begin();
  144. if ((_chip != BMX280_CHIP_BME280) && (_chip != BMX280_CHIP_BMP280)) {
  145. _chip = 0;
  146. i2cReleaseLock(_address);
  147. _error = SENSOR_ERROR_UNKNOWN_ID;
  148. }
  149. #if BMX280_TEMPERATURE > 0
  150. ++_count;
  151. #endif
  152. #if BMX280_PRESSURE > 0
  153. ++_count;
  154. #endif
  155. #if BMX280_HUMIDITY > 0
  156. if (_chip == BMX280_CHIP_BME280) ++_count;
  157. #endif
  158. _measurement_delay = measurementTime();
  159. }
  160. unsigned long measurementTime() {
  161. // Measurement Time (as per BMX280 datasheet section 9.1)
  162. // T_max(ms) = 1.25
  163. // + (2.3 * T_oversampling)
  164. // + (2.3 * P_oversampling + 0.575)
  165. // + (2.4 * H_oversampling + 0.575)
  166. // ~ 9.3ms for current settings
  167. double t = 1.25;
  168. #if BMX280_TEMPERATURE > 0
  169. t += (2.3 * BMX280_TEMPERATURE);
  170. #endif
  171. #if BMX280_PRESSURE > 0
  172. t += (2.3 * BMX280_PRESSURE + 0.575);
  173. #endif
  174. #if BMX280_HUMIDITY > 0
  175. if (_chip == BMX280_CHIP_BME280) {
  176. t += (2.4 * BMX280_HUMIDITY + 0.575);
  177. }
  178. #endif
  179. return round(t + 1); // round up
  180. }
  181. void forceRead() {
  182. // We set the sensor in "forced mode" to force a reading.
  183. // After the reading the sensor will go back to sleep mode.
  184. uint8_t value = _bme->readRegister(BME280_CTRL_MEAS_REG);
  185. value = (value & 0xFC) + 0x01;
  186. _bme->writeRegister(BME280_CTRL_MEAS_REG, value);
  187. delay(_measurement_delay);
  188. }
  189. // ---------------------------------------------------------------------
  190. BME280 * _bme = NULL;
  191. unsigned char _chip;
  192. unsigned long _measurement_delay;
  193. };
  194. // Static inizializations
  195. unsigned char BMX280Sensor::addresses[2] = {0x76, 0x77};
  196. #endif // SENSOR_SUPPORT && BMX280_SUPPORT