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.

420 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. if (index < _count) {
  71. _error = SENSOR_ERROR_OK;
  72. unsigned char i = 0;
  73. #if BMX280_TEMPERATURE > 0
  74. if (index == i++) return MAGNITUDE_TEMPERATURE;
  75. #endif
  76. #if BMX280_PRESSURE > 0
  77. if (index == i++) return MAGNITUDE_PRESSURE;
  78. #endif
  79. #if BMX280_HUMIDITY > 0
  80. if (_chip == BMX280_CHIP_BME280) {
  81. if (index == i) return MAGNITUDE_HUMIDITY;
  82. }
  83. #endif
  84. }
  85. _error = SENSOR_ERROR_OUT_OF_RANGE;
  86. return MAGNITUDE_NONE;
  87. }
  88. // Pre-read hook (usually to populate registers with up-to-date data)
  89. virtual void pre() {
  90. if (_chip == 0) {
  91. _error = SENSOR_ERROR_UNKNOWN_ID;
  92. return;
  93. }
  94. #if BMX280_MODE == 1
  95. _forceRead();
  96. #endif
  97. _read();
  98. }
  99. // Current value for slot # index
  100. double value(unsigned char index) {
  101. if (index < _count) {
  102. _error = SENSOR_ERROR_OK;
  103. unsigned char i = 0;
  104. #if BMX280_TEMPERATURE > 0
  105. if (index == i++) return _temperature;
  106. #endif
  107. #if BMX280_PRESSURE > 0
  108. if (index == i++) return _pressure / 100;
  109. #endif
  110. #if BMX280_HUMIDITY > 0
  111. if (_chip == BMX280_CHIP_BME280) {
  112. if (index == i) return _humidity;
  113. }
  114. #endif
  115. }
  116. _error = SENSOR_ERROR_OUT_OF_RANGE;
  117. return 0;
  118. }
  119. // Load the configuration manifest
  120. static void manifest(JsonArray& sensors) {
  121. char buffer[10];
  122. JsonObject& sensor = sensors.createNestedObject();
  123. sensor["sensor_id"] = SENSOR_BMX280_ID;
  124. JsonArray& fields = sensor.createNestedArray("fields");
  125. {
  126. JsonObject& field = fields.createNestedObject();
  127. field["tag"] = UI_TAG_SELECT;
  128. field["name"] = "address";
  129. field["label"] = "Address";
  130. JsonArray& options = field.createNestedArray("options");
  131. {
  132. JsonObject& option = options.createNestedObject();
  133. option["name"] = "auto";
  134. option["value"] = 0;
  135. }
  136. for (unsigned char i=0; i< sizeof(BMX280Sensor::addresses); i++) {
  137. JsonObject& option = options.createNestedObject();
  138. snprintf(buffer, sizeof(buffer), "0x%02X", BMX280Sensor::addresses[i]);
  139. option["name"] = String(buffer);
  140. option["value"] = BMX280Sensor::addresses[i];
  141. }
  142. }
  143. };
  144. void getConfig(JsonObject& root) {
  145. root["sensor_id"] = _sensor_id;
  146. root["address"] = _address;
  147. };
  148. void setConfig(JsonObject& root) {
  149. if (root.containsKey("address")) setAddress(root["address"]);
  150. };
  151. protected:
  152. void _init() {
  153. // Make sure sensor had enough time to turn on. BMX280 requires 2ms to start up
  154. delay(10);
  155. // Check sensor correctly initialized
  156. _chip = i2c_read_uint8(_address, BMX280_REGISTER_CHIPID);
  157. if ((_chip != BMX280_CHIP_BME280) && (_chip != BMX280_CHIP_BMP280)) {
  158. _chip = 0;
  159. i2cReleaseLock(_address);
  160. _error = SENSOR_ERROR_UNKNOWN_ID;
  161. }
  162. #if BMX280_TEMPERATURE > 0
  163. ++_count;
  164. #endif
  165. #if BMX280_PRESSURE > 0
  166. ++_count;
  167. #endif
  168. #if BMX280_HUMIDITY > 0
  169. if (_chip == BMX280_CHIP_BME280) ++_count;
  170. #endif
  171. _readCoefficients();
  172. unsigned char data = 0;
  173. i2c_write_uint8(_address, BMX280_REGISTER_CONTROL, data);
  174. data = (BMX280_STANDBY << 0x5) & 0xE0;
  175. data |= (BMX280_FILTER << 0x02) & 0x1C;
  176. i2c_write_uint8(_address, BMX280_REGISTER_CONFIG, data);
  177. data = (BMX280_HUMIDITY) & 0x07;
  178. i2c_write_uint8(_address, BMX280_REGISTER_CONTROLHUMID, data);
  179. data = (BMX280_TEMPERATURE << 5) & 0xE0;
  180. data |= (BMX280_PRESSURE << 2) & 0x1C;
  181. data |= (BMX280_MODE) & 0x03;
  182. i2c_write_uint8(_address, BMX280_REGISTER_CONTROL, data);
  183. _measurement_delay = _measurementTime();
  184. }
  185. void _readCoefficients() {
  186. _bmx280_calib.dig_T1 = i2c_read_uint16_le(_address, BMX280_REGISTER_DIG_T1);
  187. _bmx280_calib.dig_T2 = i2c_read_int16_le(_address, BMX280_REGISTER_DIG_T2);
  188. _bmx280_calib.dig_T3 = i2c_read_int16_le(_address, BMX280_REGISTER_DIG_T3);
  189. _bmx280_calib.dig_P1 = i2c_read_uint16_le(_address, BMX280_REGISTER_DIG_P1);
  190. _bmx280_calib.dig_P2 = i2c_read_int16_le(_address, BMX280_REGISTER_DIG_P2);
  191. _bmx280_calib.dig_P3 = i2c_read_int16_le(_address, BMX280_REGISTER_DIG_P3);
  192. _bmx280_calib.dig_P4 = i2c_read_int16_le(_address, BMX280_REGISTER_DIG_P4);
  193. _bmx280_calib.dig_P5 = i2c_read_int16_le(_address, BMX280_REGISTER_DIG_P5);
  194. _bmx280_calib.dig_P6 = i2c_read_int16_le(_address, BMX280_REGISTER_DIG_P6);
  195. _bmx280_calib.dig_P7 = i2c_read_int16_le(_address, BMX280_REGISTER_DIG_P7);
  196. _bmx280_calib.dig_P8 = i2c_read_int16_le(_address, BMX280_REGISTER_DIG_P8);
  197. _bmx280_calib.dig_P9 = i2c_read_int16_le(_address, BMX280_REGISTER_DIG_P9);
  198. _bmx280_calib.dig_H1 = i2c_read_uint8(_address, BMX280_REGISTER_DIG_H1);
  199. _bmx280_calib.dig_H2 = i2c_read_int16_le(_address, BMX280_REGISTER_DIG_H2);
  200. _bmx280_calib.dig_H3 = i2c_read_uint8(_address, BMX280_REGISTER_DIG_H3);
  201. _bmx280_calib.dig_H4 = (i2c_read_uint8(_address, BMX280_REGISTER_DIG_H4) << 4) | (i2c_read_uint8(_address, BMX280_REGISTER_DIG_H4+1) & 0xF);
  202. _bmx280_calib.dig_H5 = (i2c_read_uint8(_address, BMX280_REGISTER_DIG_H5+1) << 4) | (i2c_read_uint8(_address, BMX280_REGISTER_DIG_H5) >> 4);
  203. _bmx280_calib.dig_H6 = (int8_t) i2c_read_uint8(_address, BMX280_REGISTER_DIG_H6);
  204. }
  205. unsigned long _measurementTime() {
  206. // Measurement Time (as per BMX280 datasheet section 9.1)
  207. // T_max(ms) = 1.25
  208. // + (2.3 * T_oversampling)
  209. // + (2.3 * P_oversampling + 0.575)
  210. // + (2.4 * H_oversampling + 0.575)
  211. // ~ 9.3ms for current settings
  212. double t = 1.25;
  213. #if BMX280_TEMPERATURE > 0
  214. t += (2.3 * BMX280_TEMPERATURE);
  215. #endif
  216. #if BMX280_PRESSURE > 0
  217. t += (2.3 * BMX280_PRESSURE + 0.575);
  218. #endif
  219. #if BMX280_HUMIDITY > 0
  220. if (_chip == BMX280_CHIP_BME280) {
  221. t += (2.4 * BMX280_HUMIDITY + 0.575);
  222. }
  223. #endif
  224. return round(t + 1); // round up
  225. }
  226. void _forceRead() {
  227. // We set the sensor in "forced mode" to force a reading.
  228. // After the reading the sensor will go back to sleep mode.
  229. uint8_t value = i2c_read_uint8(_address, BMX280_REGISTER_CONTROL);
  230. value = (value & 0xFC) + 0x01;
  231. i2c_write_uint8(_address, BMX280_REGISTER_CONTROL, value);
  232. delay(_measurement_delay);
  233. }
  234. void _read() {
  235. #if BMX280_TEMPERATURE > 0
  236. int32_t adc_T = i2c_read_uint16(_address, BMX280_REGISTER_TEMPDATA);
  237. adc_T <<= 8;
  238. adc_T |= i2c_read_uint8(_address, BMX280_REGISTER_TEMPDATA+2);
  239. adc_T >>= 4;
  240. int32_t var1t = ((((adc_T>>3) -
  241. ((int32_t)_bmx280_calib.dig_T1 <<1))) *
  242. ((int32_t)_bmx280_calib.dig_T2)) >> 11;
  243. int32_t var2t = (((((adc_T>>4) -
  244. ((int32_t)_bmx280_calib.dig_T1)) *
  245. ((adc_T>>4) - ((int32_t)_bmx280_calib.dig_T1))) >> 12) *
  246. ((int32_t)_bmx280_calib.dig_T3)) >> 14;
  247. int32_t t_fine = var1t + var2t;
  248. double T = (t_fine * 5 + 128) >> 8;
  249. _temperature = T / 100;
  250. #else
  251. int32_t t_fine = 102374; // ~20ºC
  252. #endif
  253. // -----------------------------------------------------------------
  254. #if BMX280_PRESSURE > 0
  255. int64_t var1, var2, p;
  256. int32_t adc_P = i2c_read_uint16(_address, BMX280_REGISTER_PRESSUREDATA);
  257. adc_P <<= 8;
  258. adc_P |= i2c_read_uint8(_address, BMX280_REGISTER_PRESSUREDATA+2);
  259. adc_P >>= 4;
  260. var1 = ((int64_t)t_fine) - 128000;
  261. var2 = var1 * var1 * (int64_t)_bmx280_calib.dig_P6;
  262. var2 = var2 + ((var1*(int64_t)_bmx280_calib.dig_P5)<<17);
  263. var2 = var2 + (((int64_t)_bmx280_calib.dig_P4)<<35);
  264. var1 = ((var1 * var1 * (int64_t)_bmx280_calib.dig_P3)>>8) +
  265. ((var1 * (int64_t)_bmx280_calib.dig_P2)<<12);
  266. var1 = (((((int64_t)1)<<47)+var1))*((int64_t)_bmx280_calib.dig_P1)>>33;
  267. if (var1 == 0) return; // avoid exception caused by division by zero
  268. p = 1048576 - adc_P;
  269. p = (((p<<31) - var2)*3125) / var1;
  270. var1 = (((int64_t)_bmx280_calib.dig_P9) * (p>>13) * (p>>13)) >> 25;
  271. var2 = (((int64_t)_bmx280_calib.dig_P8) * p) >> 19;
  272. p = ((p + var1 + var2) >> 8) + (((int64_t)_bmx280_calib.dig_P7)<<4);
  273. _pressure = (double) p / 256;
  274. #endif
  275. // -----------------------------------------------------------------
  276. #if BMX280_HUMIDITY > 0
  277. if (_chip == BMX280_CHIP_BME280) {
  278. int32_t adc_H = i2c_read_uint16(_address, BMX280_REGISTER_HUMIDDATA);
  279. int32_t v_x1_u32r;
  280. v_x1_u32r = (t_fine - ((int32_t)76800));
  281. v_x1_u32r = (((((adc_H << 14) - (((int32_t)_bmx280_calib.dig_H4) << 20) -
  282. (((int32_t)_bmx280_calib.dig_H5) * v_x1_u32r)) + ((int32_t)16384)) >> 15) *
  283. (((((((v_x1_u32r * ((int32_t)_bmx280_calib.dig_H6)) >> 10) *
  284. (((v_x1_u32r * ((int32_t)_bmx280_calib.dig_H3)) >> 11) + ((int32_t)32768))) >> 10) +
  285. ((int32_t)2097152)) * ((int32_t)_bmx280_calib.dig_H2) + 8192) >> 14));
  286. v_x1_u32r = (v_x1_u32r - (((((v_x1_u32r >> 15) * (v_x1_u32r >> 15)) >> 7) *
  287. ((int32_t)_bmx280_calib.dig_H1)) >> 4));
  288. v_x1_u32r = (v_x1_u32r < 0) ? 0 : v_x1_u32r;
  289. v_x1_u32r = (v_x1_u32r > 419430400) ? 419430400 : v_x1_u32r;
  290. double h = (v_x1_u32r >> 12);
  291. _humidity = h / 1024.0;
  292. }
  293. #endif
  294. }
  295. // ---------------------------------------------------------------------
  296. unsigned char _chip;
  297. unsigned long _measurement_delay;
  298. double _temperature = 0;
  299. double _pressure = 0;
  300. double _humidity = 0;
  301. typedef struct {
  302. uint16_t dig_T1;
  303. int16_t dig_T2;
  304. int16_t dig_T3;
  305. uint16_t dig_P1;
  306. int16_t dig_P2;
  307. int16_t dig_P3;
  308. int16_t dig_P4;
  309. int16_t dig_P5;
  310. int16_t dig_P6;
  311. int16_t dig_P7;
  312. int16_t dig_P8;
  313. int16_t dig_P9;
  314. uint8_t dig_H1;
  315. int16_t dig_H2;
  316. uint8_t dig_H3;
  317. int16_t dig_H4;
  318. int16_t dig_H5;
  319. int8_t dig_H6;
  320. } bmx280_calib_t;
  321. bmx280_calib_t _bmx280_calib;
  322. };
  323. // Static inizializations
  324. unsigned char BMX280Sensor::addresses[2] = {0x76, 0x77};
  325. #endif // SENSOR_SUPPORT && BMX280_SUPPORT