/
BlueDot_BME280.cpp
452 lines (379 loc) · 15.8 KB
/
BlueDot_BME280.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#if defined(_AVR_)
#include <util/delay.h>
#endif
#include "BlueDot_BME280.h"
#include "Wire.h"
#include "SPI.h"
BlueDot_BME280::BlueDot_BME280()
{
parameter.communication;
parameter.I2CAddress;
parameter.sensorMode;
parameter.IIRfilter;
parameter.tempOversampling;
parameter.pressOversampling;
parameter.humidOversampling;
parameter.pressureSeaLevel = 0;
parameter.tempOutsideCelsius = 999;
parameter.tempOutsideFahrenheit = 999;
}
uint8_t BlueDot_BME280::init(void)
{
//In order to start the BME280 we need to go through the following steps:
//1. Choose the Communication Protocol (I2C, Software SPI or Hardware SPI)
//2. Read Compensation Coefficients
//3. Set up IIR Filter (Infinite Impulse Response Filter)
//4. Set up Oversampling Factors and Sensor Run Mode
//5. Check Communication (ask and verificate chip ID)
//1. Communication Protocol
//#########################
//
if (parameter.communication == 1) //Software SPI Communication
{
digitalWrite(parameter.SPI_cs, HIGH); //Chip Select Pin to HIGH
pinMode(parameter.SPI_cs, OUTPUT); //Chip Select Pin as Output
pinMode(parameter.SPI_sck, OUTPUT); //Clock as Output
pinMode(parameter.SPI_mosi, OUTPUT); //MOSI as Output
pinMode(parameter.SPI_miso, INPUT); //MISO as Input
}
if (parameter.communication == 2) //Hardware SPI Communication
{
digitalWrite(parameter.SPI_cs, HIGH); //Chip Select Pin to HIGH
pinMode(parameter.SPI_cs, OUTPUT); //Chip Select Pin as Output
SPI.begin(); //Initialize SPI library
SPI.setBitOrder(MSBFIRST); //Most significant Bit first
SPI.setClockDivider(SPI_CLOCK_DIV4); //Sets SPI clock to 1/4th of the system clock (i.e. 4000 kHz for Arduino Uno)
SPI.setDataMode(SPI_MODE0); //Set Byte Transfer to (0,0) Mode
}
else //Default I2C Communication
{
Wire.begin(); //Default value for Arduino Boards
//Wire.begin(0,2); //Use this for NodeMCU board; SDA = GPIO0 = D3; SCL = GPIO2 = D4
}
//2. Reading Compensation Coefficients
//####################################
//After a measurement the device gives values for temperature, pressure and humidity
//These are all uncompensated and uncablibrated values
//To correct these values we need the calibration coefficients
//These are stored into the device during production
readCoefficients();
//3. Set up IIR Filter
//####################
//The BME280 features an internal IIR (Infinite Impulse Response) Filter
//The IIR Filter suppresses high frequency fluctuations (i. e. pressure changes due to slamming doors)
//It improves the pressure and temperature resolution to 20 bits
//The resolution of the humidity measurement is fixed at 16 bits and is not affected by the filter
//When enabled, we can set up the filter coefficient (2, 4, 8 or 16)
//This coefficient defines the filter's time constant (please refer to Datasheet)
writeIIRFilter();
//4. Set up Oversampling Factors and Sensor Mode
//##############################################
//Oversampling heavily influences the noise in the data (please refer to the Datasheet for more Info)
//The BME280 Datasheet provides settings suggestions for different applications
//Finally we write all those values to their respective registers
writeCTRLMeas();
//5. Check Communication
//######################
//All BME280 devices share the same chip ID: 0x60
//If we read anything else than 0x60, we interrupt the program
//In this case, please check the wiring to the device
//Also check the correct I2C Address (either 0x76 or 0x77)
return checkID();
}
//##########################################################################
//SET UP FUNCTIONS
//##########################################################################
uint8_t BlueDot_BME280::checkID(void)
{
uint8_t chipID;
chipID = readByte(BME280_CHIP_ID);
return chipID;
}
//##########################################################################
void BlueDot_BME280::readCoefficients(void)
{
bme280_coefficients.dig_T1 = ((uint16_t)(readByte(BME280_DIG_T1_MSB) << 8) + readByte(BME280_DIG_T1_LSB));
bme280_coefficients.dig_T2 = ((int16_t)(readByte(BME280_DIG_T2_MSB) << 8) + readByte(BME280_DIG_T2_LSB));
bme280_coefficients.dig_T3 = ((int16_t)(readByte(BME280_DIG_T3_MSB) << 8) + readByte(BME280_DIG_T3_LSB));
bme280_coefficients.dig_P1 = ((uint16_t)(readByte(BME280_DIG_P1_MSB) << 8) + readByte(BME280_DIG_P1_LSB));
bme280_coefficients.dig_P2 = ((int16_t)(readByte(BME280_DIG_P2_MSB) << 8) + readByte(BME280_DIG_P2_LSB));
bme280_coefficients.dig_P3 = ((int16_t)(readByte(BME280_DIG_P3_MSB) << 8) + readByte(BME280_DIG_P3_LSB));
bme280_coefficients.dig_P4 = ((int16_t)(readByte(BME280_DIG_P4_MSB) << 8) + readByte(BME280_DIG_P4_LSB));
bme280_coefficients.dig_P5 = ((int16_t)(readByte(BME280_DIG_P5_MSB) << 8) + readByte(BME280_DIG_P5_LSB));
bme280_coefficients.dig_P6 = ((int16_t)(readByte(BME280_DIG_P6_MSB) << 8) + readByte(BME280_DIG_P6_LSB));
bme280_coefficients.dig_P7 = ((int16_t)(readByte(BME280_DIG_P7_MSB) << 8) + readByte(BME280_DIG_P7_LSB));
bme280_coefficients.dig_P8 = ((int16_t)(readByte(BME280_DIG_P8_MSB) << 8) + readByte(BME280_DIG_P8_LSB));
bme280_coefficients.dig_P9 = ((int16_t)(readByte(BME280_DIG_P9_MSB) << 8) + readByte(BME280_DIG_P9_LSB));
bme280_coefficients.dig_H1 = ((uint8_t)(readByte(BME280_DIG_H1)));
bme280_coefficients.dig_H2 = ((int16_t)(readByte(BME280_DIG_H2_MSB) << 8) + readByte(BME280_DIG_H2_LSB));
bme280_coefficients.dig_H3 = ((uint8_t)(readByte(BME280_DIG_H3)));
bme280_coefficients.dig_H4 = ((int16_t)((readByte(BME280_DIG_H4_MSB) << 4) + (readByte(BME280_DIG_H4_LSB) & 0x0F)));
bme280_coefficients.dig_H5 = ((int16_t)((readByte(BME280_DIG_H5_MSB) << 4) + ((readByte(BME280_DIG_H4_LSB) >> 4 ) & 0x0F)));
bme280_coefficients.dig_H6 = ((uint8_t)(readByte(BME280_DIG_H6)));
}
//##########################################################################
void BlueDot_BME280::writeIIRFilter(void)
{
//We set up the IIR Filter through bits 4, 3 and 2 from Config Register (0xF5)]
//The other bits from this register won't be used in this program and remain 0
//Please refer to the BME280 Datasheet for more information
byte value;
value = (parameter.IIRfilter << 2) & 0b00011100;
writeByte(BME280_CONFIG, value);
}
//##########################################################################
void BlueDot_BME280::writeCTRLMeas(void)
{
byte value;
value = parameter.humidOversampling & 0b00000111;
writeByte(BME280_CTRL_HUM, value);
value = (parameter.tempOversampling << 5) & 0b11100000;
value |= (parameter.pressOversampling << 2) & 0b00011100;
value |= parameter.sensorMode & 0b00000011;
writeByte(BME280_CTRL_MEAS, value);
}
//##########################################################################
//DATA READOUT FUNCTIONS
//##########################################################################
float BlueDot_BME280::readPressure(void)
{
if (parameter.pressOversampling == 0b000) //disabling the pressure measurement function
{
return 0;
}
else
{
readTempC();
int32_t adc_P;
adc_P = (uint32_t)readByte(BME280_PRESSURE_MSB) << 12;
adc_P |= (uint32_t)readByte(BME280_PRESSURE_LSB) << 4;
adc_P |= (readByte(BME280_PRESSURE_XLSB) >> 4 )& 0b00001111;
int64_t var1, var2, P;
var1 = ((int64_t)t_fine) - 128000;
var2 = var1 * var1 * (int64_t)bme280_coefficients.dig_P6;
var2 = var2 + ((var1 * (int64_t)bme280_coefficients.dig_P5)<<17);
var2 = var2 + (((int64_t)bme280_coefficients.dig_P4)<<35);
var1 = ((var1 * var1 * (int64_t)bme280_coefficients.dig_P3)>>8) + ((var1 * (int64_t)bme280_coefficients.dig_P2)<<12);
var1 = (((((int64_t)1)<<47)+var1))*((int64_t)bme280_coefficients.dig_P1)>>33;
if (var1 == 0)
{
return 0; // avoid exception caused by division by zero
}
P = 1048576 - adc_P;
P = (((P << 31) - var2)*3125)/var1;
var1 = (((int64_t)bme280_coefficients.dig_P9) * (P >> 13) * (P >> 13)) >> 25;
var2 = (((int64_t)bme280_coefficients.dig_P8) * P) >> 19;
P = ((P + var1 + var2) >> 8) + (((int64_t)bme280_coefficients.dig_P7)<<4);
P = P >> 8; // /256
return (float)P/100;
}
}
//##########################################################################
float BlueDot_BME280::convertTempKelvin(void)
{
//Temperature in Kelvin is needed for the conversion of pressure to altitude
//Both tempOutsideCelsius and tempOutsideFahrenheit are set to 999 as default (see .h file)
//If the user chooses to read temperature in Celsius, tempOutsideFahrenheit remains 999
//If the user chooses to read temperature in Fahrenheit instead, tempOutsideCelsius remains 999
//If both values are used, then the temperature in Celsius will be used for the conversion
//If none of them are used, then the default value of 288.15 will be used (i.e. 273.15 + 15)
float tempOutsideKelvin;
if (parameter.tempOutsideCelsius != 999 & parameter.tempOutsideFahrenheit == 999 )
{
tempOutsideKelvin = parameter.tempOutsideCelsius;
tempOutsideKelvin = tempOutsideKelvin + 273.15;
return tempOutsideKelvin;
}
if (parameter.tempOutsideCelsius != 999 & parameter.tempOutsideFahrenheit != 999 )
{
tempOutsideKelvin = parameter.tempOutsideCelsius;
tempOutsideKelvin = tempOutsideKelvin + 273.15;
return tempOutsideKelvin;
}
if (parameter.tempOutsideFahrenheit != 999 & parameter.tempOutsideCelsius == 999)
{
tempOutsideKelvin = (parameter.tempOutsideFahrenheit - 32);
tempOutsideKelvin = tempOutsideKelvin * 5;
tempOutsideKelvin = tempOutsideKelvin / 9;
tempOutsideKelvin = tempOutsideKelvin + 273.15;
return tempOutsideKelvin;
}
if (parameter.tempOutsideFahrenheit == 999 & parameter.tempOutsideCelsius == 999)
{
tempOutsideKelvin = 273.15 + 15;
return tempOutsideKelvin;
}
tempOutsideKelvin = 273.15 + 15;
return tempOutsideKelvin;
}
//##########################################################################
float BlueDot_BME280::readAltitudeFeet(void)
{
float heightOutput = 0;
float tempOutsideKelvin = convertTempKelvin();
heightOutput = readPressure();
heightOutput = (heightOutput/parameter.pressureSeaLevel);
heightOutput = pow(heightOutput, 0.190284);
heightOutput = 1 - heightOutput;
heightOutput = heightOutput * tempOutsideKelvin;
heightOutput = heightOutput / 0.0065;
heightOutput = heightOutput / 0.3048;
return heightOutput;
}
//##########################################################################
float BlueDot_BME280::readAltitudeMeter(void)
{
float heightOutput = 0;
float tempOutsideKelvin = convertTempKelvin();
heightOutput = readPressure();
heightOutput = (heightOutput/parameter.pressureSeaLevel);
heightOutput = pow(heightOutput, 0.190284);
heightOutput = 1 - heightOutput;
heightOutput = heightOutput * tempOutsideKelvin;
heightOutput = heightOutput / 0.0065;
return heightOutput;
}
//##########################################################################
float BlueDot_BME280::readHumidity(void)
{
if (parameter.humidOversampling == 0b000) //disabling the humitidy measurement function
{
return 0;
}
else
{
int32_t adc_H;
adc_H = (uint32_t)readByte(BME280_HUMIDITY_MSB) << 8;
adc_H |= (uint32_t)readByte(BME280_HUMIDITY_LSB);
int32_t var1;
var1 = (t_fine - ((int32_t)76800));
var1 = (((((adc_H << 14) - (((int32_t)bme280_coefficients.dig_H4) << 20) - (((int32_t)bme280_coefficients.dig_H5) * var1)) +
((int32_t)16384)) >> 15) * (((((((var1 * ((int32_t)bme280_coefficients.dig_H6)) >> 10) * (((var1 * ((int32_t)bme280_coefficients.dig_H3)) >> 11) + ((int32_t)32768))) >> 10) + ((int32_t)2097152)) *
((int32_t)bme280_coefficients.dig_H2) + 8192) >> 14));
var1 = (var1 - (((((var1 >> 15) * (var1 >> 15)) >> 7) * ((int32_t)bme280_coefficients.dig_H1)) >> 4));
var1 = (var1 < 0 ? 0 : var1);
var1 = (var1 > 419430400 ? 419430400 : var1);
float H = (var1>>12);
H = H /1024.0;
return H;
}
}
//##########################################################################
float BlueDot_BME280::readTempC(void)
{
if (parameter.tempOversampling == 0b000) //disabling the temperature measurement function
{
return 0;
}
else
{
int32_t adc_T;
adc_T = (uint32_t)readByte(BME280_TEMPERATURE_MSB) << 12;
adc_T |= (uint32_t)readByte(BME280_TEMPERATURE_LSB) << 4;
adc_T |= (readByte(BME280_TEMPERATURE_XLSB) >> 4 )& 0b00001111;
int64_t var1, var2;
var1 = ((((adc_T>>3) - ((int32_t)bme280_coefficients.dig_T1<<1))) * ((int32_t)bme280_coefficients.dig_T2)) >> 11;
var2 = (((((adc_T>>4) - ((int32_t)bme280_coefficients.dig_T1)) * ((adc_T>>4) - ((int32_t)bme280_coefficients.dig_T1))) >> 12) *
((int32_t)bme280_coefficients.dig_T3)) >> 14;
t_fine = var1 + var2;
float T = (t_fine * 5 + 128) >> 8;
T = T / 100;
return T;
}
}
//##########################################################################
float BlueDot_BME280::readTempF(void)
{
if (parameter.tempOversampling == 0b000) //disabling the temperature measurement function
{
return 0;
}
else
{
int32_t adc_T;
adc_T = (uint32_t)readByte(BME280_TEMPERATURE_MSB) << 12;
adc_T |= (uint32_t)readByte(BME280_TEMPERATURE_LSB) << 4;
adc_T |= (readByte(BME280_TEMPERATURE_XLSB) >> 4 )& 0b00001111;
int64_t var1, var2;
var1 = ((((adc_T>>3) - ((int32_t)bme280_coefficients.dig_T1<<1))) * ((int32_t)bme280_coefficients.dig_T2)) >> 11;
var2 = (((((adc_T>>4) - ((int32_t)bme280_coefficients.dig_T1)) * ((adc_T>>4) - ((int32_t)bme280_coefficients.dig_T1))) >> 12) *
((int32_t)bme280_coefficients.dig_T3)) >> 14;
t_fine = var1 + var2;
float T = (t_fine * 5 + 128) >> 8;
T = T / 100;
T = (T * 1.8) + 32;
return T;
}
}
//##########################################################################
//BASIC FUNCTIONS
//##########################################################################
void BlueDot_BME280::writeByte(byte reg, byte value)
{
if (parameter.communication == 1) //Software SPI
{
digitalWrite(parameter.SPI_cs, LOW);
spiTransfer(reg & 0x7F);
spiTransfer(value);
digitalWrite(parameter.SPI_cs, HIGH);
}
if (parameter.communication == 2) //Hardware SPI
{
digitalWrite(parameter.SPI_cs, LOW);
SPI.transfer(reg & 0x7F);
SPI.transfer(value);
digitalWrite(parameter.SPI_cs, HIGH);
}
else //I2C (default)
{
Wire.beginTransmission(parameter.I2CAddress);
Wire.write(reg);
Wire.write(value);
Wire.endTransmission();
}
}
//##########################################################################
uint8_t BlueDot_BME280::readByte(byte reg)
{
uint8_t value;
if (parameter.communication == 1) //Software SPI
{
digitalWrite(parameter.SPI_cs, LOW);
spiTransfer(reg | 0x80);
value = spiTransfer(0);
digitalWrite(parameter.SPI_cs, HIGH);
return value;
}
if (parameter.communication == 2) //Hardware SPI
{
digitalWrite(parameter.SPI_cs, LOW);
SPI.transfer(reg | 0x80);
value = SPI.transfer(0);
digitalWrite(parameter.SPI_cs, HIGH);
return value;
}
else //I2C (default)
{
Wire.beginTransmission(parameter.I2CAddress);
Wire.write(reg);
Wire.endTransmission();
Wire.requestFrom(parameter.I2CAddress,1);
value = Wire.read();
return value;
}
}
//##########################################################################
uint8_t BlueDot_BME280::spiTransfer(uint8_t data) //Software SPI done through bit-banging
{
uint8_t reply = 0;
for (int counter = 7; counter >= 0; counter--)
{
reply <<= 1;
digitalWrite(parameter.SPI_sck, LOW);
digitalWrite(parameter.SPI_mosi, data & (1<<counter));
digitalWrite(parameter.SPI_sck, HIGH);
if (digitalRead(parameter.SPI_miso))
reply |= 1;
}
return reply;
}