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.

451 lines
16 KiB

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