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.

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