-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tricorder_BME688.cpp
145 lines (127 loc) · 3.5 KB
/
Tricorder_BME688.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
#include "Tricorder_BME688.h"
#define BME_SCK 13
#define BME_MISO 12
#define BME_MOSI 11
#define BME_CS 10
#define SEALEVELPRESSURE_HPA (1013.25)
Tricorder_BME688::Tricorder_BME688(){
sensor_name = "BME688";
read_after = 0;
heaterTemp = 0;
heaterTime = 0;
sensor_options.push_back(sensor_option(
"enabled",
OP_Boolean,
[&](nlohmann::json val)->bool{
enabled = val.get<bool>();
return true;
},
[&]()->nlohmann::json{ return enabled; }
));
sensor_options.push_back(sensor_option(
"tempOverSampling",
OP_Integer,
[&](nlohmann::json val)->bool{
sensor_obj.setTemperatureOversampling(val.get<long>());
return true;
},
[&]()->nlohmann::json{ return "unimplemented"; }
));
sensor_options.push_back(sensor_option(
"pressureOverSampling",
OP_Integer,
[&](nlohmann::json val)->bool{
sensor_obj.setPressureOversampling(val.get<long>());
return true;
},
[&]()->nlohmann::json{ return "unimplemented"; }
));
sensor_options.push_back(sensor_option(
"humidityOverSampling",
OP_Integer,
[&](nlohmann::json val)->bool{
sensor_obj.setHumidityOversampling(val.get<long>());
return true;
},
[&]()->nlohmann::json{ return "unimplemented"; }
));
sensor_options.push_back(sensor_option(
"IIRFilterSize",
OP_Integer,
[&](nlohmann::json val)->bool{
sensor_obj.setIIRFilterSize(val.get<long>());
return true;
},
[&]()->nlohmann::json{ return "unimplemented"; }
));
sensor_options.push_back(sensor_option(
"heaterTemp",
OP_Integer,
[&](nlohmann::json val)->bool{
heaterTemp = val.get<long>();
return true;
},
[&]()->nlohmann::json{ return "unimplemented"; }
));
sensor_options.push_back(sensor_option(
"heaterTime",
OP_Integer,
[&](nlohmann::json val)->bool{
heaterTime = val.get<long>();
return true;
},
[&]()->nlohmann::json{ return "unimplemented"; }
));
sensor_options.push_back(sensor_option(
"runHeat",
OP_Boolean,
[&](nlohmann::json val)->bool{
if(val.get<bool>()){ sensor_obj.setGasHeater(heaterTemp, heaterTime); }
return true;
},
[&]()->nlohmann::json{ return "unreadable"; }
));
sensor_options.push_back(sensor_option(
"ODR",
OP_Integer,
[&](nlohmann::json val)->bool{
sensor_obj.setODR(val.get<long>());
return true;
},
[&]()->nlohmann::json{ return "unimplemented"; }
));
}
bool Tricorder_BME688::sensor_init(){
sensor_obj = Adafruit_BME680();
initialized = sensor_obj.begin();
// Set up oversampling and filter initialization
if(initialized){
sensor_obj.setTemperatureOversampling(BME680_OS_8X);
sensor_obj.setHumidityOversampling(BME680_OS_2X);
sensor_obj.setPressureOversampling(BME680_OS_4X);
sensor_obj.setIIRFilterSize(BME680_FILTER_SIZE_3);
sensor_obj.setGasHeater(320, 150); // 320*C for 150 ms
}
return initialized;
}
bool Tricorder_BME688::read_sensor(){
if(read_after == 0){
read_after = sensor_obj.beginReading();
}else if(millis() > read_after){
if(sensor_obj.endReading()){
read_after = sensor_obj.beginReading();
return true;
}
}
return false;
}
nlohmann::json Tricorder_BME688::populate_data(){
nlohmann::json data_frame = {
{"temperature", sensor_obj.temperature},
{"pressure", sensor_obj.pressure/100.0},
{"humidity", sensor_obj.humidity},
{"gas", sensor_obj.gas_resistance/1000.0},
//{"altitude", sensor_obj.readAltitude(SEALEVELPRESSURE_HPA)}
};
return data_frame;
}