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.

258 lines
8.1 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. static unsigned char addresses[2];
  15. // ---------------------------------------------------------------------
  16. // Public
  17. // ---------------------------------------------------------------------
  18. BMX280Sensor(): BaseSensor() {
  19. _sensor_id = SENSOR_BMX280_ID;
  20. }
  21. void setAddress(unsigned char address) {
  22. if (_address == address) return;
  23. _address = address;
  24. _dirty = true;
  25. }
  26. // ---------------------------------------------------------------------
  27. unsigned char getAddress() {
  28. return _address;
  29. }
  30. // ---------------------------------------------------------------------
  31. // Sensor API
  32. // ---------------------------------------------------------------------
  33. // Initialization method, must be idempotent
  34. void begin() {
  35. if (!_dirty) return;
  36. _dirty = false;
  37. _chip = 0;
  38. // I2C auto-discover
  39. _address = lock_i2c(_address, sizeof(BMX280Sensor::addresses), BMX280Sensor::addresses);
  40. if (_address == 0) return;
  41. // Init
  42. init();
  43. }
  44. // Descriptive name of the sensor
  45. String description() {
  46. char buffer[20];
  47. snprintf(buffer, sizeof(buffer), "%s @ I2C (0x%02X)", _chip == BMX280_CHIP_BME280 ? "BME280" : "BMP280", _address);
  48. return String(buffer);
  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. // Load the configuration manifest
  101. static void manifest(JsonArray& sensors) {
  102. char buffer[10];
  103. JsonObject& sensor = sensors.createNestedObject();
  104. sensor["sensor_id"] = SENSOR_BMX280_ID;
  105. JsonArray& fields = sensor.createNestedArray("fields");
  106. {
  107. JsonObject& field = fields.createNestedObject();
  108. field["tag"] = UI_TAG_SELECT;
  109. field["name"] = "address";
  110. field["label"] = "Address";
  111. JsonArray& options = field.createNestedArray("options");
  112. {
  113. JsonObject& option = options.createNestedObject();
  114. option["name"] = "auto";
  115. option["value"] = 0;
  116. }
  117. for (unsigned char i=0; i< sizeof(BMX280Sensor::addresses); i++) {
  118. JsonObject& option = options.createNestedObject();
  119. snprintf(buffer, sizeof(buffer), "0x%02X", BMX280Sensor::addresses[i]);
  120. option["name"] = String(buffer);
  121. option["value"] = BMX280Sensor::addresses[i];
  122. }
  123. }
  124. };
  125. void getConfig(JsonObject& root) {
  126. root["sensor_id"] = _sensor_id;
  127. root["address"] = getAddress();
  128. };
  129. void setConfig(JsonObject& root) {
  130. if (root.containsKey("address")) setAddress(root["address"]);
  131. };
  132. protected:
  133. void init() {
  134. // Destroy previous instance if any
  135. if (bme) delete bme;
  136. bme = new BME280();
  137. bme->settings.commInterface = I2C_MODE;
  138. bme->settings.I2CAddress = _address;
  139. bme->settings.runMode = BMX280_MODE;
  140. bme->settings.tStandby = 0;
  141. bme->settings.filter = 0;
  142. bme->settings.tempOverSample = BMX280_TEMPERATURE;
  143. bme->settings.pressOverSample = BMX280_PRESSURE;
  144. bme->settings.humidOverSample = BMX280_HUMIDITY;
  145. // Fix when not measuring temperature, t_fine should have a sensible value
  146. if (BMX280_TEMPERATURE == 0) bme->t_fine = 100000; // aprox 20ºC
  147. // Make sure sensor had enough time to turn on. BMX280 requires 2ms to start up
  148. delay(10);
  149. // Check sensor correctly initialized
  150. _chip = bme->begin();
  151. if ((_chip != BMX280_CHIP_BME280) && (_chip != BMX280_CHIP_BMP280)) {
  152. _chip = 0;
  153. i2cReleaseLock(_address);
  154. _error = SENSOR_ERROR_UNKNOWN_ID;
  155. }
  156. #if BMX280_TEMPERATURE > 0
  157. ++_count;
  158. #endif
  159. #if BMX280_PRESSURE > 0
  160. ++_count;
  161. #endif
  162. #if BMX280_HUMIDITY > 0
  163. if (_chip == BMX280_CHIP_BME280) ++_count;
  164. #endif
  165. _measurement_delay = measurementTime();
  166. }
  167. unsigned long measurementTime() {
  168. // Measurement Time (as per BMX280 datasheet section 9.1)
  169. // T_max(ms) = 1.25
  170. // + (2.3 * T_oversampling)
  171. // + (2.3 * P_oversampling + 0.575)
  172. // + (2.4 * H_oversampling + 0.575)
  173. // ~ 9.3ms for current settings
  174. double t = 1.25;
  175. #if BMX280_TEMPERATURE > 0
  176. t += (2.3 * BMX280_TEMPERATURE);
  177. #endif
  178. #if BMX280_PRESSURE > 0
  179. t += (2.3 * BMX280_PRESSURE + 0.575);
  180. #endif
  181. #if BMX280_HUMIDITY > 0
  182. if (_chip == BMX280_CHIP_BME280) {
  183. t += (2.4 * BMX280_HUMIDITY + 0.575);
  184. }
  185. #endif
  186. return round(t + 1); // round up
  187. }
  188. void forceRead() {
  189. // We set the sensor in "forced mode" to force a reading.
  190. // After the reading the sensor will go back to sleep mode.
  191. uint8_t value = bme->readRegister(BME280_CTRL_MEAS_REG);
  192. value = (value & 0xFC) + 0x01;
  193. bme->writeRegister(BME280_CTRL_MEAS_REG, value);
  194. delay(_measurement_delay);
  195. }
  196. // ---------------------------------------------------------------------
  197. BME280 * bme;
  198. unsigned char _chip;
  199. unsigned long _measurement_delay;
  200. };
  201. // Static inizializations
  202. unsigned char BMX280Sensor::addresses[2] = {0x76, 0x77};