BME680 Library  0.5
Library for the Bosch BME680 Air Quality sensor
bme680.h
Go to the documentation of this file.
1 
19 #include "simpletools.h" // Include simple tools
20 
21 
22 
23 
24 #ifndef BME680_H
25 #define BME680_H
26 
27 #if defined(__cplusplus)
28 extern "C" {
29 #endif
30 
32 #ifndef CELSIUS
33  #define CELSIUS (0)
34 #endif
35 
37 #ifndef FAHRENHEIT
38  #define FAHRENHEIT (1)
39 #endif
40 
42 #ifndef KELVIN
43  #define KELVIN (2)
44 #endif
45 
47 #ifndef PASCALS
48  #define PASCALS (0)
49 #endif
50 
52 #ifndef MMHG
53  #define MMHG (1)
54 #endif
55 
57 #ifndef INHG
58  #define INHG (2)
59 #endif
60 
62 #ifndef PSI
63  #define PSI (3)
64 #endif
65 
67 #ifndef METERS
68  #define METERS (0)
69 #endif
70 
72 #ifndef FEET
73  #define FEET (1)
74 #endif
75 
76 
78 #ifndef BME680_OS_NONE
79  #define BME680_OS_NONE (0)
80 #endif
81 
83 #ifndef BME680_OS_1X
84  #define BME680_OS_1X (1)
85 #endif
86 
88 #ifndef BME680_OS_2X
89  #define BME680_OS_2X (2)
90 #endif
91 
93 #ifndef BME680_OS_4X
94  #define BME680_OS_4X (3)
95 #endif
96 
98 #ifndef BME680_OS_8X
99  #define BME680_OS_8X (4)
100 #endif
101 
103 #ifndef BME680_OS_16X
104  #define BME680_OS_16X (5)
105 #endif
106 
107 
109 */
110 #ifndef BME680_FILTER_SIZE_0
111  #define BME680_FILTER_SIZE_0 (0)
112 #endif
113 
115 #ifndef BME680_FILTER_SIZE_1
116  #define BME680_FILTER_SIZE_1 (1)
117 #endif
118 
120 #ifndef BME680_FILTER_SIZE_3
121  #define BME680_FILTER_SIZE_3 (2)
122 #endif
123 
125 #ifndef BME680_FILTER_SIZE_7
126  #define BME680_FILTER_SIZE_7 (3)
127 #endif
128 
130 #ifndef BME680_FILTER_SIZE_15
131  #define BME680_FILTER_SIZE_15 (4)
132 #endif
133 
135 #ifndef BME680_FILTER_SIZE_31
136  #define BME680_FILTER_SIZE_31 (5)
137 #endif
138 
140 #ifndef BME680_FILTER_SIZE_63
141  #define BME680_FILTER_SIZE_63 (6)
142 #endif
143 
145 #ifndef BME680_FILTER_SIZE_127
146  #define BME680_FILTER_SIZE_127 (7)
147 #endif
148 
149 
151 #ifndef BME680_SETTING_AMB_TEMP
152  #define BME680_SETTING_AMB_TEMP (0)
153 #endif
154 
156 #ifndef BME680_SETTING_HEATER_TEMP
157  #define BME680_SETTING_HEATER_TEMP (1)
158 #endif
159 
161 #ifndef BME680_SETTING_HEATER_DURATION
162  #define BME680_SETTING_HEATER_DURATION (2)
163 #endif
164 
166 #ifndef BME680_SETTING_FILTER_SIZE
167  #define BME680_SETTING_FILTER_SIZE (3)
168 #endif
169 
171 #ifndef BME680_SETTING_TEMP_SAMPLES
172  #define BME680_SETTING_TEMP_SAMPLES (4)
173 #endif
174 
176 #ifndef BME680_SETTING_PRESSURE_SAMPLES
177  #define BME680_SETTING_PRESSURE_SAMPLES (5)
178 #endif
179 
181 #ifndef BME680_SETTING_HUMIDITY_SAMPLES
182  #define BME680_SETTING_HUMIDITY_SAMPLES (6)
183 #endif
184 
185 
186 typedef struct bme680_st {
187  // Setup
188  unsigned char dev_id; // Device ID
189  char intf; // SPI/i2c interface
190  i2c *i2c_ptr; // i2c bus
191  char sdi_pin; // SPI pins
192  char sdo_pin;
193  char clk_pin;
194  char amb_temp; // Ambient temperature in (C)
195  unsigned short heater_temp; // Heater temperature (C)
196  unsigned char heater_duration; // Heater duration (ms)
197  unsigned char filter_size;
198  unsigned char temp_oversample;
199  unsigned char pres_oversample;
200  unsigned char hum_oversample;
201  short sensor_reading_period;
202 
203  // Calibration Data
204  unsigned short par_h1; // Variables to store calibrated humidity data
205  unsigned short par_h2;
206  char par_h3;
207  char par_h4;
208  char par_h5;
209  unsigned char par_h6;
210  char par_h7;
211  char par_gh1; // Variables to store calibrated gas data
212  short par_gh2;
213  char par_gh3;
214  unsigned short par_t1; // Variables to store calibrated temperature data
215  short par_t2;
216  char par_t3; // Variables to store calibrated pressure data
217  unsigned short par_p1;
218  short par_p2;
219  char par_p3;
220  short par_p4;
221  short par_p5;
222  char par_p6;
223  char par_p7;
224  short par_p8;
225  short par_p9;
226  unsigned char par_p10;
227  float t_fine; // Variable to store t_fine size
228  unsigned char res_heat_range; // Variable to store heater resistance range
229  char res_heat_val; // Variable to store heater resistance value
230  char range_sw_err; // Variable to store error range
231 
232  // Readings
233  unsigned char status; // Contains new_data, gasm_valid & heat_stab
234  unsigned char gas_index; // The index of the heater profile used
235  unsigned char meas_index; // Measurement index to track order
236  float temperature; // Temperature in degree celsius
237  float pressure; // Pressure in Pascal
238  float altitude;
239  float humidity; // Humidity in % relative humidity x1000
240  float gas_resistance; // Gas resistance in Ohms
241 } bme680_t;
242 
243 
244 typedef bme680_t bme680;
245 
255 bme680_t *bme680_openI2C(i2c *i2c_bus, char i2c_addr);
256 
270 bme680_t *bme680_openSPI(char pin_sdo, char pin_clk, char pin_sdi, char pin_cs);
271 
278 void bme680_begin(bme680_t *dev);
279 
289 char bme680_loadSettings(bme680_t *dev);
290 
305 char bme680_readSensor(bme680_t *dev);
306 
319 void bme680_write(bme680_t *dev, unsigned char reg_addr, unsigned char *reg_data, unsigned short len);
320 
333 void bme680_read(bme680_t *dev, unsigned char reg_addr, unsigned char *reg_data, unsigned short len);
334 
352 void bme680_configure(bme680_t *dev, char setting, int value);
353 
359 void bme680_close(bme680_t *dev);
360 
369 float bme680_temperature(bme680_t *dev, char unit);
370 
379 float bme680_pressure(bme680_t *dev, char unit);
380 
393 float bme680_altitude(bme680_t *dev, char unit);
394 
401 float bme680_humidity(bme680_t *dev);
402 
409 float bme680_gasResistance(bme680_t *dev);
410 
417 void bme680_heaterEnable(bme680_t *dev);
418 
425 void bme680_heaterDisable(bme680_t *dev);
426 
427 #if defined(__cplusplus)
428 }
429 #endif
430 /* __cplusplus */
431 #endif
432 /* BME680_H */
433 
float bme680_gasResistance(bme680_t *dev)
Retrieve the relative humidity as a percentage from the last read of the BME680 device.
Definition: bme680_gasResistance.c:21
char bme680_loadSettings(bme680_t *dev)
Loads settings values that have been saved in the bme680_t structure into the BME680 sensor...
Definition: bme680_settings.c:21
void bme680_read(bme680_t *dev, unsigned char reg_addr, unsigned char *reg_data, unsigned short len)
Internal function used to write values to the specified registers of the BME680 sensor.
Definition: bme680_comms.c:21
void bme680_begin(bme680_t *dev)
Internal function used to set the values in the bme680_t structure to their default values...
Definition: bme680_begin.c:20
Definition: bme680.h:186
char bme680_readSensor(bme680_t *dev)
Instructs the BME680 sensor to take a reading. Readings take approximately 200ms and should be retir...
Definition: bme680_read.c:20
void bme680_heaterEnable(bme680_t *dev)
Allows the BME680 to send current to the heating element for its gas sensor.
Definition: bme680_heater.c:21
void bme680_configure(bme680_t *dev, char setting, int value)
Change configurable settings for the BME680 sensor.
Definition: bme680_settings.c:72
void bme680_close(bme680_t *dev)
Close and free the memory used by the bme680 device structure.
Definition: bme680_init.c:52
float bme680_pressure(bme680_t *dev, char unit)
Retrieve the temperature from the last read of the BME680 device.
Definition: bme680_pressure.c:20
void bme680_write(bme680_t *dev, unsigned char reg_addr, unsigned char *reg_data, unsigned short len)
Internal function used to read values from the specified registers of the BME680 sensor.
Definition: bme680_comms.c:36
bme680_t * bme680_openI2C(i2c *i2c_bus, char i2c_addr)
Initialize a BME680 sensor into i2c mode.
Definition: bme680_init.c:20
bme680_t * bme680_openSPI(char pin_sdo, char pin_clk, char pin_sdi, char pin_cs)
Initialize a BME680 sensor into 4-wire SPI mode.
Definition: bme680_init.c:35
float bme680_humidity(bme680_t *dev)
Retrieve the relative humidity as a percentage from the last read of the BME680 device.
Definition: bme680_humidity.c:21
float bme680_altitude(bme680_t *dev, char unit)
Retrieve the estimated altitude from the last read of the BME680 device.
Definition: bme680_altitude.c:20
float bme680_temperature(bme680_t *dev, char unit)
Retrieve the temperature from the last read of the BME680 device.
Definition: bme680_temperature.c:21
void bme680_heaterDisable(bme680_t *dev)
Turns off the current that the BME680 sends to the heating element for its gas sensor.
Definition: bme680_heater.c:30