forked from andreiva/raspberry-pi-bme280
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bme280.c
212 lines (169 loc) · 8.51 KB
/
bme280.c
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
/***************************************************************************
Modified BSD License
====================
Copyright © 2016, Andrei Vainik
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the organization nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This piece of code was combined from several sources
https://github.com/adafruit/Adafruit_BME280_Library
https://cdn-shop.adafruit.com/datasheets/BST-BME280_DS001-10.pdf
https://projects.drogon.net/raspberry-pi/wiringpi/i2c-library/
Compensation functions and altitude function originally from:
https://github.com/adafruit/Adafruit_BME280_Library/blob/master/Adafruit_BME280.cpp
***************************************************************************
This is a library for the BME280 humidity, temperature & pressure sensor
Designed specifically to work with the Adafruit BME280 Breakout
----> http://www.adafruit.com/products/2650
These sensors use I2C or SPI to communicate, 2 or 4 pins are required
to interface.
Adafruit invests time and resources providing this open source code,
please support Adafruit andopen-source hardware by purchasing products
from Adafruit!
Written by Limor Fried & Kevin Townsend for Adafruit Industries.
BSD license, all text above must be included in any redistribution
***************************************************************************
****************************************************************************/
#include <stdio.h>
#include <errno.h>
#include <stdint.h>
#include <time.h>
#include <math.h>
#include <wiringPiI2C.h>
#include "bme280.h"
int main() {
int fd = wiringPiI2CSetup(BME280_ADDRESS);
if(fd < 0) {
printf("Device not found");
return -1;
}
bme280_calib_data cal;
readCalibrationData(fd, &cal);
wiringPiI2CWriteReg8(fd, 0xf2, 0x01); // humidity oversampling x 1
wiringPiI2CWriteReg8(fd, 0xf4, 0x25); // pressure and temperature oversampling x 1, mode normal
bme280_raw_data raw;
getRawData(fd, &raw);
int32_t t_fine = getTemperatureCalibration(&cal, raw.temperature);
float t = compensateTemperature(t_fine); // C
float p = compensatePressure(raw.pressure, &cal, t_fine) / 100; // hPa
float h = compensateHumidity(raw.humidity, &cal, t_fine); // %
float a = getAltitude(p); // meters
printf("{\"sensor\":\"bme280\", \"humidity\":%.2f, \"pressure\":%.2f,"
" \"temperature\":%.2f, \"altitude\":%.2f, \"timestamp\":%d}\n",
h, p, t, a, (int)time(NULL));
return 0;
}
int32_t getTemperatureCalibration(bme280_calib_data *cal, int32_t adc_T) {
int32_t var1 = ((((adc_T>>3) - ((int32_t)cal->dig_T1 <<1))) *
((int32_t)cal->dig_T2)) >> 11;
int32_t var2 = (((((adc_T>>4) - ((int32_t)cal->dig_T1)) *
((adc_T>>4) - ((int32_t)cal->dig_T1))) >> 12) *
((int32_t)cal->dig_T3)) >> 14;
return var1 + var2;
}
void readCalibrationData(int fd, bme280_calib_data *data) {
data->dig_T1 = (uint16_t)wiringPiI2CReadReg16(fd, BME280_REGISTER_DIG_T1);
data->dig_T2 = (int16_t)wiringPiI2CReadReg16(fd, BME280_REGISTER_DIG_T2);
data->dig_T3 = (int16_t)wiringPiI2CReadReg16(fd, BME280_REGISTER_DIG_T3);
data->dig_P1 = (uint16_t)wiringPiI2CReadReg16(fd, BME280_REGISTER_DIG_P1);
data->dig_P2 = (int16_t)wiringPiI2CReadReg16(fd, BME280_REGISTER_DIG_P2);
data->dig_P3 = (int16_t)wiringPiI2CReadReg16(fd, BME280_REGISTER_DIG_P3);
data->dig_P4 = (int16_t)wiringPiI2CReadReg16(fd, BME280_REGISTER_DIG_P4);
data->dig_P5 = (int16_t)wiringPiI2CReadReg16(fd, BME280_REGISTER_DIG_P5);
data->dig_P6 = (int16_t)wiringPiI2CReadReg16(fd, BME280_REGISTER_DIG_P6);
data->dig_P7 = (int16_t)wiringPiI2CReadReg16(fd, BME280_REGISTER_DIG_P7);
data->dig_P8 = (int16_t)wiringPiI2CReadReg16(fd, BME280_REGISTER_DIG_P8);
data->dig_P9 = (int16_t)wiringPiI2CReadReg16(fd, BME280_REGISTER_DIG_P9);
data->dig_H1 = (uint8_t)wiringPiI2CReadReg8(fd, BME280_REGISTER_DIG_H1);
data->dig_H2 = (int16_t)wiringPiI2CReadReg16(fd, BME280_REGISTER_DIG_H2);
data->dig_H3 = (uint8_t)wiringPiI2CReadReg8(fd, BME280_REGISTER_DIG_H3);
data->dig_H4 = (wiringPiI2CReadReg8(fd, BME280_REGISTER_DIG_H4) << 4) | (wiringPiI2CReadReg8(fd, BME280_REGISTER_DIG_H4+1) & 0xF);
data->dig_H5 = (wiringPiI2CReadReg8(fd, BME280_REGISTER_DIG_H5+1) << 4) | (wiringPiI2CReadReg8(fd, BME280_REGISTER_DIG_H5) >> 4);
data->dig_H6 = (int8_t)wiringPiI2CReadReg8(fd, BME280_REGISTER_DIG_H6);
}
float compensateTemperature(int32_t t_fine) {
float T = (t_fine * 5 + 128) >> 8;
return T/100;
}
float compensatePressure(int32_t adc_P, bme280_calib_data *cal, int32_t t_fine) {
int64_t var1, var2, p;
var1 = ((int64_t)t_fine) - 128000;
var2 = var1 * var1 * (int64_t)cal->dig_P6;
var2 = var2 + ((var1*(int64_t)cal->dig_P5)<<17);
var2 = var2 + (((int64_t)cal->dig_P4)<<35);
var1 = ((var1 * var1 * (int64_t)cal->dig_P3)>>8) +
((var1 * (int64_t)cal->dig_P2)<<12);
var1 = (((((int64_t)1)<<47)+var1))*((int64_t)cal->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)cal->dig_P9) * (p>>13) * (p>>13)) >> 25;
var2 = (((int64_t)cal->dig_P8) * p) >> 19;
p = ((p + var1 + var2) >> 8) + (((int64_t)cal->dig_P7)<<4);
return (float)p/256;
}
float compensateHumidity(int32_t adc_H, bme280_calib_data *cal, int32_t t_fine) {
int32_t v_x1_u32r;
v_x1_u32r = (t_fine - ((int32_t)76800));
v_x1_u32r = (((((adc_H << 14) - (((int32_t)cal->dig_H4) << 20) -
(((int32_t)cal->dig_H5) * v_x1_u32r)) + ((int32_t)16384)) >> 15) *
(((((((v_x1_u32r * ((int32_t)cal->dig_H6)) >> 10) *
(((v_x1_u32r * ((int32_t)cal->dig_H3)) >> 11) + ((int32_t)32768))) >> 10) +
((int32_t)2097152)) * ((int32_t)cal->dig_H2) + 8192) >> 14));
v_x1_u32r = (v_x1_u32r - (((((v_x1_u32r >> 15) * (v_x1_u32r >> 15)) >> 7) *
((int32_t)cal->dig_H1)) >> 4));
v_x1_u32r = (v_x1_u32r < 0) ? 0 : v_x1_u32r;
v_x1_u32r = (v_x1_u32r > 419430400) ? 419430400 : v_x1_u32r;
float h = (v_x1_u32r>>12);
return h / 1024.0;
}
void getRawData(int fd, bme280_raw_data *raw) {
wiringPiI2CWrite(fd, 0xf7);
raw->pmsb = wiringPiI2CRead(fd);
raw->plsb = wiringPiI2CRead(fd);
raw->pxsb = wiringPiI2CRead(fd);
raw->tmsb = wiringPiI2CRead(fd);
raw->tlsb = wiringPiI2CRead(fd);
raw->txsb = wiringPiI2CRead(fd);
raw->hmsb = wiringPiI2CRead(fd);
raw->hlsb = wiringPiI2CRead(fd);
raw->temperature = 0;
raw->temperature = (raw->temperature | raw->tmsb) << 8;
raw->temperature = (raw->temperature | raw->tlsb) << 8;
raw->temperature = (raw->temperature | raw->txsb) >> 4;
raw->pressure = 0;
raw->pressure = (raw->pressure | raw->pmsb) << 8;
raw->pressure = (raw->pressure | raw->plsb) << 8;
raw->pressure = (raw->pressure | raw->pxsb) >> 4;
raw->humidity = 0;
raw->humidity = (raw->humidity | raw->hmsb) << 8;
raw->humidity = (raw->humidity | raw->hlsb);
}
float getAltitude(float pressure) {
// Equation taken from BMP180 datasheet (page 16):
// http://www.adafruit.com/datasheets/BST-BMP180-DS000-09.pdf
// Note that using the equation from wikipedia can give bad results
// at high altitude. See this thread for more information:
// http://forums.adafruit.com/viewtopic.php?f=22&t=58064
return 44330.0 * (1.0 - pow(pressure / MEAN_SEA_LEVEL_PRESSURE, 0.190294957));
}