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.

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