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.

411 lines
15 KiB

7 years ago
  1. // -----------------------------------------------------------------------------
  2. // BME280/BMP280 Sensor over I2C
  3. // Copyright (C) 2017-2018 by Xose Pérez <xose dot perez at gmail dot com>
  4. // -----------------------------------------------------------------------------
  5. #if SENSOR_SUPPORT && BMX280_SUPPORT
  6. #pragma once
  7. #include "Arduino.h"
  8. #include "I2CSensor.h"
  9. #define BMX280_CHIP_BMP280 0x58
  10. #define BMX280_CHIP_BME280 0x60
  11. #define BMX280_REGISTER_DIG_T1 0x88
  12. #define BMX280_REGISTER_DIG_T2 0x8A
  13. #define BMX280_REGISTER_DIG_T3 0x8C
  14. #define BMX280_REGISTER_DIG_P1 0x8E
  15. #define BMX280_REGISTER_DIG_P2 0x90
  16. #define BMX280_REGISTER_DIG_P3 0x92
  17. #define BMX280_REGISTER_DIG_P4 0x94
  18. #define BMX280_REGISTER_DIG_P5 0x96
  19. #define BMX280_REGISTER_DIG_P6 0x98
  20. #define BMX280_REGISTER_DIG_P7 0x9A
  21. #define BMX280_REGISTER_DIG_P8 0x9C
  22. #define BMX280_REGISTER_DIG_P9 0x9E
  23. #define BMX280_REGISTER_DIG_H1 0xA1
  24. #define BMX280_REGISTER_DIG_H2 0xE1
  25. #define BMX280_REGISTER_DIG_H3 0xE3
  26. #define BMX280_REGISTER_DIG_H4 0xE4
  27. #define BMX280_REGISTER_DIG_H5 0xE5
  28. #define BMX280_REGISTER_DIG_H6 0xE7
  29. #define BMX280_REGISTER_CHIPID 0xD0
  30. #define BMX280_REGISTER_VERSION 0xD1
  31. #define BMX280_REGISTER_SOFTRESET 0xE0
  32. #define BMX280_REGISTER_CAL26 0xE1
  33. #define BMX280_REGISTER_CONTROLHUMID 0xF2
  34. #define BMX280_REGISTER_CONTROL 0xF4
  35. #define BMX280_REGISTER_CONFIG 0xF5
  36. #define BMX280_REGISTER_PRESSUREDATA 0xF7
  37. #define BMX280_REGISTER_TEMPDATA 0xFA
  38. #define BMX280_REGISTER_HUMIDDATA 0xFD
  39. class BMX280Sensor : public I2CSensor {
  40. public:
  41. static unsigned char addresses[2];
  42. // ---------------------------------------------------------------------
  43. // Public
  44. // ---------------------------------------------------------------------
  45. BMX280Sensor(): I2CSensor() {
  46. _sensor_id = SENSOR_BMX280_ID;
  47. }
  48. // ---------------------------------------------------------------------
  49. // Sensor API
  50. // ---------------------------------------------------------------------
  51. // Initialization method, must be idempotent
  52. void begin() {
  53. if (!_dirty) return;
  54. _dirty = false;
  55. _chip = 0;
  56. // I2C auto-discover
  57. _address = _begin_i2c(_address, sizeof(BMX280Sensor::addresses), BMX280Sensor::addresses);
  58. if (_address == 0) return;
  59. // Init
  60. _init();
  61. }
  62. // Descriptive name of the sensor
  63. String description() {
  64. char buffer[20];
  65. snprintf(buffer, sizeof(buffer), "%s @ I2C (0x%02X)", _chip == BMX280_CHIP_BME280 ? "BME280" : "BMP280", _address);
  66. return String(buffer);
  67. }
  68. // Type for slot # index
  69. unsigned char type(unsigned char index) {
  70. unsigned char i = 0;
  71. #if BMX280_TEMPERATURE > 0
  72. if (index == i++) return MAGNITUDE_TEMPERATURE;
  73. #endif
  74. #if BMX280_PRESSURE > 0
  75. if (index == i++) return MAGNITUDE_PRESSURE;
  76. #endif
  77. #if BMX280_HUMIDITY > 0
  78. if (_chip == BMX280_CHIP_BME280) {
  79. if (index == i) return MAGNITUDE_HUMIDITY;
  80. }
  81. #endif
  82. return MAGNITUDE_NONE;
  83. }
  84. // Pre-read hook (usually to populate registers with up-to-date data)
  85. virtual void pre() {
  86. if (_chip == 0) {
  87. _error = SENSOR_ERROR_UNKNOWN_ID;
  88. return;
  89. }
  90. _error = SENSOR_ERROR_OK;
  91. #if BMX280_MODE == 1
  92. _forceRead();
  93. #endif
  94. _read();
  95. }
  96. // Current value for slot # index
  97. double value(unsigned char index) {
  98. unsigned char i = 0;
  99. #if BMX280_TEMPERATURE > 0
  100. if (index == i++) return _temperature;
  101. #endif
  102. #if BMX280_PRESSURE > 0
  103. if (index == i++) return _pressure / 100;
  104. #endif
  105. #if BMX280_HUMIDITY > 0
  106. if (_chip == BMX280_CHIP_BME280) {
  107. if (index == i) return _humidity;
  108. }
  109. #endif
  110. return 0;
  111. }
  112. // Load the configuration manifest
  113. static void manifest(JsonArray& sensors) {
  114. char buffer[10];
  115. JsonObject& sensor = sensors.createNestedObject();
  116. sensor["sensor_id"] = SENSOR_BMX280_ID;
  117. JsonArray& fields = sensor.createNestedArray("fields");
  118. {
  119. JsonObject& field = fields.createNestedObject();
  120. field["tag"] = UI_TAG_SELECT;
  121. field["name"] = "address";
  122. field["label"] = "Address";
  123. JsonArray& options = field.createNestedArray("options");
  124. {
  125. JsonObject& option = options.createNestedObject();
  126. option["name"] = "auto";
  127. option["value"] = 0;
  128. }
  129. for (unsigned char i=0; i< sizeof(BMX280Sensor::addresses); i++) {
  130. JsonObject& option = options.createNestedObject();
  131. snprintf(buffer, sizeof(buffer), "0x%02X", BMX280Sensor::addresses[i]);
  132. option["name"] = String(buffer);
  133. option["value"] = BMX280Sensor::addresses[i];
  134. }
  135. }
  136. };
  137. void getConfig(JsonObject& root) {
  138. root["sensor_id"] = _sensor_id;
  139. root["address"] = _address;
  140. };
  141. void setConfig(JsonObject& root) {
  142. if (root.containsKey("address")) setAddress(root["address"]);
  143. };
  144. protected:
  145. void _init() {
  146. // Make sure sensor had enough time to turn on. BMX280 requires 2ms to start up
  147. delay(10);
  148. // Check sensor correctly initialized
  149. _chip = i2c_read_uint8(_address, BMX280_REGISTER_CHIPID);
  150. if ((_chip != BMX280_CHIP_BME280) && (_chip != BMX280_CHIP_BMP280)) {
  151. _chip = 0;
  152. i2cReleaseLock(_address);
  153. _error = SENSOR_ERROR_UNKNOWN_ID;
  154. }
  155. #if BMX280_TEMPERATURE > 0
  156. ++_count;
  157. #endif
  158. #if BMX280_PRESSURE > 0
  159. ++_count;
  160. #endif
  161. #if BMX280_HUMIDITY > 0
  162. if (_chip == BMX280_CHIP_BME280) ++_count;
  163. #endif
  164. _readCoefficients();
  165. unsigned char data = 0;
  166. i2c_write_uint8(_address, BMX280_REGISTER_CONTROL, data);
  167. data = (BMX280_STANDBY << 0x5) & 0xE0;
  168. data |= (BMX280_FILTER << 0x02) & 0x1C;
  169. i2c_write_uint8(_address, BMX280_REGISTER_CONFIG, data);
  170. data = (BMX280_HUMIDITY) & 0x07;
  171. i2c_write_uint8(_address, BMX280_REGISTER_CONTROLHUMID, data);
  172. data = (BMX280_TEMPERATURE << 5) & 0xE0;
  173. data |= (BMX280_PRESSURE << 2) & 0x1C;
  174. data |= (BMX280_MODE) & 0x03;
  175. i2c_write_uint8(_address, BMX280_REGISTER_CONTROL, data);
  176. _measurement_delay = _measurementTime();
  177. }
  178. void _readCoefficients() {
  179. _bmx280_calib.dig_T1 = i2c_read_uint16_le(_address, BMX280_REGISTER_DIG_T1);
  180. _bmx280_calib.dig_T2 = i2c_read_int16_le(_address, BMX280_REGISTER_DIG_T2);
  181. _bmx280_calib.dig_T3 = i2c_read_int16_le(_address, BMX280_REGISTER_DIG_T3);
  182. _bmx280_calib.dig_P1 = i2c_read_uint16_le(_address, BMX280_REGISTER_DIG_P1);
  183. _bmx280_calib.dig_P2 = i2c_read_int16_le(_address, BMX280_REGISTER_DIG_P2);
  184. _bmx280_calib.dig_P3 = i2c_read_int16_le(_address, BMX280_REGISTER_DIG_P3);
  185. _bmx280_calib.dig_P4 = i2c_read_int16_le(_address, BMX280_REGISTER_DIG_P4);
  186. _bmx280_calib.dig_P5 = i2c_read_int16_le(_address, BMX280_REGISTER_DIG_P5);
  187. _bmx280_calib.dig_P6 = i2c_read_int16_le(_address, BMX280_REGISTER_DIG_P6);
  188. _bmx280_calib.dig_P7 = i2c_read_int16_le(_address, BMX280_REGISTER_DIG_P7);
  189. _bmx280_calib.dig_P8 = i2c_read_int16_le(_address, BMX280_REGISTER_DIG_P8);
  190. _bmx280_calib.dig_P9 = i2c_read_int16_le(_address, BMX280_REGISTER_DIG_P9);
  191. _bmx280_calib.dig_H1 = i2c_read_uint8(_address, BMX280_REGISTER_DIG_H1);
  192. _bmx280_calib.dig_H2 = i2c_read_int16_le(_address, BMX280_REGISTER_DIG_H2);
  193. _bmx280_calib.dig_H3 = i2c_read_uint8(_address, BMX280_REGISTER_DIG_H3);
  194. _bmx280_calib.dig_H4 = (i2c_read_uint8(_address, BMX280_REGISTER_DIG_H4) << 4) | (i2c_read_uint8(_address, BMX280_REGISTER_DIG_H4+1) & 0xF);
  195. _bmx280_calib.dig_H5 = (i2c_read_uint8(_address, BMX280_REGISTER_DIG_H5+1) << 4) | (i2c_read_uint8(_address, BMX280_REGISTER_DIG_H5) >> 4);
  196. _bmx280_calib.dig_H6 = (int8_t) i2c_read_uint8(_address, BMX280_REGISTER_DIG_H6);
  197. }
  198. unsigned long _measurementTime() {
  199. // Measurement Time (as per BMX280 datasheet section 9.1)
  200. // T_max(ms) = 1.25
  201. // + (2.3 * T_oversampling)
  202. // + (2.3 * P_oversampling + 0.575)
  203. // + (2.4 * H_oversampling + 0.575)
  204. // ~ 9.3ms for current settings
  205. double t = 1.25;
  206. #if BMX280_TEMPERATURE > 0
  207. t += (2.3 * BMX280_TEMPERATURE);
  208. #endif
  209. #if BMX280_PRESSURE > 0
  210. t += (2.3 * BMX280_PRESSURE + 0.575);
  211. #endif
  212. #if BMX280_HUMIDITY > 0
  213. if (_chip == BMX280_CHIP_BME280) {
  214. t += (2.4 * BMX280_HUMIDITY + 0.575);
  215. }
  216. #endif
  217. return round(t + 1); // round up
  218. }
  219. void _forceRead() {
  220. // We set the sensor in "forced mode" to force a reading.
  221. // After the reading the sensor will go back to sleep mode.
  222. uint8_t value = i2c_read_uint8(_address, BMX280_REGISTER_CONTROL);
  223. value = (value & 0xFC) + 0x01;
  224. i2c_write_uint8(_address, BMX280_REGISTER_CONTROL, value);
  225. delay(_measurement_delay);
  226. }
  227. void _read() {
  228. #if BMX280_TEMPERATURE > 0
  229. int32_t adc_T = i2c_read_uint16(_address, BMX280_REGISTER_TEMPDATA);
  230. adc_T <<= 8;
  231. adc_T |= i2c_read_uint8(_address, BMX280_REGISTER_TEMPDATA+2);
  232. adc_T >>= 4;
  233. int32_t var1t = ((((adc_T>>3) -
  234. ((int32_t)_bmx280_calib.dig_T1 <<1))) *
  235. ((int32_t)_bmx280_calib.dig_T2)) >> 11;
  236. int32_t var2t = (((((adc_T>>4) -
  237. ((int32_t)_bmx280_calib.dig_T1)) *
  238. ((adc_T>>4) - ((int32_t)_bmx280_calib.dig_T1))) >> 12) *
  239. ((int32_t)_bmx280_calib.dig_T3)) >> 14;
  240. int32_t t_fine = var1t + var2t;
  241. double T = (t_fine * 5 + 128) >> 8;
  242. _temperature = T / 100;
  243. #else
  244. int32_t t_fine = 102374; // ~20ºC
  245. #endif
  246. // -----------------------------------------------------------------
  247. #if BMX280_PRESSURE > 0
  248. int64_t var1, var2, p;
  249. int32_t adc_P = i2c_read_uint16(_address, BMX280_REGISTER_PRESSUREDATA);
  250. adc_P <<= 8;
  251. adc_P |= i2c_read_uint8(_address, BMX280_REGISTER_PRESSUREDATA+2);
  252. adc_P >>= 4;
  253. var1 = ((int64_t)t_fine) - 128000;
  254. var2 = var1 * var1 * (int64_t)_bmx280_calib.dig_P6;
  255. var2 = var2 + ((var1*(int64_t)_bmx280_calib.dig_P5)<<17);
  256. var2 = var2 + (((int64_t)_bmx280_calib.dig_P4)<<35);
  257. var1 = ((var1 * var1 * (int64_t)_bmx280_calib.dig_P3)>>8) +
  258. ((var1 * (int64_t)_bmx280_calib.dig_P2)<<12);
  259. var1 = (((((int64_t)1)<<47)+var1))*((int64_t)_bmx280_calib.dig_P1)>>33;
  260. if (var1 == 0) return; // avoid exception caused by division by zero
  261. p = 1048576 - adc_P;
  262. p = (((p<<31) - var2)*3125) / var1;
  263. var1 = (((int64_t)_bmx280_calib.dig_P9) * (p>>13) * (p>>13)) >> 25;
  264. var2 = (((int64_t)_bmx280_calib.dig_P8) * p) >> 19;
  265. p = ((p + var1 + var2) >> 8) + (((int64_t)_bmx280_calib.dig_P7)<<4);
  266. _pressure = (double) p / 256;
  267. #endif
  268. // -----------------------------------------------------------------
  269. #if BMX280_HUMIDITY > 0
  270. if (_chip == BMX280_CHIP_BME280) {
  271. int32_t adc_H = i2c_read_uint16(_address, BMX280_REGISTER_HUMIDDATA);
  272. int32_t v_x1_u32r;
  273. v_x1_u32r = (t_fine - ((int32_t)76800));
  274. v_x1_u32r = (((((adc_H << 14) - (((int32_t)_bmx280_calib.dig_H4) << 20) -
  275. (((int32_t)_bmx280_calib.dig_H5) * v_x1_u32r)) + ((int32_t)16384)) >> 15) *
  276. (((((((v_x1_u32r * ((int32_t)_bmx280_calib.dig_H6)) >> 10) *
  277. (((v_x1_u32r * ((int32_t)_bmx280_calib.dig_H3)) >> 11) + ((int32_t)32768))) >> 10) +
  278. ((int32_t)2097152)) * ((int32_t)_bmx280_calib.dig_H2) + 8192) >> 14));
  279. v_x1_u32r = (v_x1_u32r - (((((v_x1_u32r >> 15) * (v_x1_u32r >> 15)) >> 7) *
  280. ((int32_t)_bmx280_calib.dig_H1)) >> 4));
  281. v_x1_u32r = (v_x1_u32r < 0) ? 0 : v_x1_u32r;
  282. v_x1_u32r = (v_x1_u32r > 419430400) ? 419430400 : v_x1_u32r;
  283. double h = (v_x1_u32r >> 12);
  284. _humidity = h / 1024.0;
  285. }
  286. #endif
  287. }
  288. // ---------------------------------------------------------------------
  289. unsigned char _chip;
  290. unsigned long _measurement_delay;
  291. double _temperature = 0;
  292. double _pressure = 0;
  293. double _humidity = 0;
  294. typedef struct {
  295. uint16_t dig_T1;
  296. int16_t dig_T2;
  297. int16_t dig_T3;
  298. uint16_t dig_P1;
  299. int16_t dig_P2;
  300. int16_t dig_P3;
  301. int16_t dig_P4;
  302. int16_t dig_P5;
  303. int16_t dig_P6;
  304. int16_t dig_P7;
  305. int16_t dig_P8;
  306. int16_t dig_P9;
  307. uint8_t dig_H1;
  308. int16_t dig_H2;
  309. uint8_t dig_H3;
  310. int16_t dig_H4;
  311. int16_t dig_H5;
  312. int8_t dig_H6;
  313. } bmx280_calib_t;
  314. bmx280_calib_t _bmx280_calib;
  315. };
  316. // Static inizializations
  317. unsigned char BMX280Sensor::addresses[2] = {0x76, 0x77};
  318. #endif // SENSOR_SUPPORT && BMX280_SUPPORT