-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbme680_sensor.ino
74 lines (65 loc) · 1.85 KB
/
bme680_sensor.ino
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
/**
* @file bme680_sensor.cpp
* @author Bernd Giesecke ([email protected])
* @brief Initialization and reading of BME680 sensor
* @version 0.1
* @date 2021-08-21
*
* @copyright Copyright (c) 2021
*
*/
#include "app.h"
Adafruit_BME680 bme;
bool init_bme(void)
{
if (!bme.begin(0x76, false))
{
MYLOG("BME", "Could not find a valid BME680 sensor, check wiring!");
return false;
}
// Set up oversampling and filter initialization
bme.setTemperatureOversampling(BME680_OS_8X);
bme.setHumidityOversampling(BME680_OS_2X);
bme.setPressureOversampling(BME680_OS_4X);
bme.setIIRFilterSize(BME680_FILTER_SIZE_3);
bme.setGasHeater(320, 150); // 320*C for 150 ms
return true;
}
void start_bme(void)
{
MYLOG("BME", "Start BME reading");
bme.beginReading();
}
bool read_bme(void)
{
time_t wait_start = millis();
bool read_success = false;
while ((millis() - wait_start) < 5000)
{
if (bme.endReading())
{
read_success = true;
break;
}
}
if (!read_success)
{
return false;
}
int16_t temp_int = (int16_t)(bme.temperature * 10.0);
uint16_t humid_int = (uint16_t)(bme.humidity * 2);
uint16_t press_int = (uint16_t)(bme.pressure / 10);
uint16_t gasres_int = (uint16_t)(bme.gas_resistance / 10);
g_tracker_data.humid_1 = (uint8_t)(humid_int);
g_tracker_data.temp_1 = (uint8_t)(temp_int >> 8);
g_tracker_data.temp_2 = (uint8_t)(temp_int);
g_tracker_data.press_1 = (uint8_t)(press_int >> 8);
g_tracker_data.press_2 = (uint8_t)(press_int);
g_tracker_data.gas_1 = (uint8_t)(gasres_int >> 8);
g_tracker_data.gas_2 = (uint8_t)(gasres_int);
MYLOG("BME", "RH= %.2f T= %.2f", (float)(humid_int / 2.0), (float)(temp_int / 10.0));
MYLOG("BME", "P= %d R= %d", press_int * 10, gasres_int * 10);
MYLOG("BME", "RH= %.2f T= %.2f", bme.humidity, bme.temperature);
MYLOG("BME", "P= %ld R= %ld", bme.pressure, bme.gas_resistance);
return true;
}