-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathoutdoor_module.ino
249 lines (219 loc) · 8.35 KB
/
outdoor_module.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
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
#include <Wire.h>
#include <Arduino.h>
#include <NOxGasIndexAlgorithm.h>
#include <SensirionI2CSgp41.h>
#include <VOCGasIndexAlgorithm.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <HardwareSerial.h>
//#include <TinyGPS++.h>
#include "PMS.h"
#define SEALEVELPRESSURE_HPA (1013.25)
#define RXD2 16 // To sensor TXD
#define TXD2 17 // To sensor RXD
SensirionI2CSgp41 sgp41;
Adafruit_BME280 bme;
VOCGasIndexAlgorithm voc_algorithm;
NOxGasIndexAlgorithm nox_algorithm;
// time in seconds needed for NOx conditioning
uint16_t conditioning_s = 10;
bool status;
String val1;
String val2;
String val3;
struct pms5003data {
uint16_t framelen;
uint16_t pm10_standard, pm25_standard, pm100_standard;
uint16_t pm10_env, pm25_env, pm100_env;
uint16_t particles_03um, particles_05um, particles_10um, particles_25um, particles_50um, particles_100um;
uint16_t unused;
uint16_t checksum;
};
struct pms5003data data;
void setup() {
Serial.begin(115200);
while (!Serial) {
delay(100);
}
Serial1.begin(9600, SERIAL_8N1, RXD2, TXD2);
Wire.begin();
sgp41.begin(Wire);
status = bme.begin(0x76);
delay(1000); // needed on some Arduino boards in order to have Serial ready
int32_t index_offset;
int32_t learning_time_offset_hours;
int32_t learning_time_gain_hours;
int32_t gating_max_duration_minutes;
int32_t std_initial;
int32_t gain_factor;
voc_algorithm.get_tuning_parameters(
index_offset, learning_time_offset_hours, learning_time_gain_hours,
gating_max_duration_minutes, std_initial, gain_factor);
Serial.println("\nVOC Gas Index Algorithm parameters");
Serial.print("Index offset:\t");
Serial.println(index_offset);
Serial.print("Learing time offset hours:\t");
Serial.println(learning_time_offset_hours);
Serial.print("Learing time gain hours:\t");
Serial.println(learning_time_gain_hours);
Serial.print("Gating max duration minutes:\t");
Serial.println(gating_max_duration_minutes);
Serial.print("Std inital:\t");
Serial.println(std_initial);
Serial.print("Gain factor:\t");
Serial.println(gain_factor);
nox_algorithm.get_tuning_parameters(
index_offset, learning_time_offset_hours, learning_time_gain_hours,
gating_max_duration_minutes, std_initial, gain_factor);
Serial.println("\nNOx Gas Index Algorithm parameters");
Serial.print("Index offset:\t");
Serial.println(index_offset);
Serial.print("Learing time offset hours:\t");
Serial.println(learning_time_offset_hours);
Serial.print("Gating max duration minutes:\t");
Serial.println(gating_max_duration_minutes);
Serial.print("Gain factor:\t");
Serial.println(gain_factor);
Serial.println("");
}
void loop() {
uint16_t error;
float humidity = 0; // %RH
float temperature = 0; // degreeC
float pressure = 0; //hPa
float appaltitude = 0; //m
uint16_t srawVoc = 0;
uint16_t srawNox = 0;
uint16_t defaultCompenstaionRh = 0x8000; // in ticks as defined by SGP41
uint16_t defaultCompenstaionT = 0x6666; // in ticks as defined by SGP41
uint16_t compensationRh = 0; // in ticks as defined by SGP41
uint16_t compensationT = 0; // in ticks as defined by SGP41
// 1. Sleep: Measure every second (1Hz), as defined by the Gas Index
// Algorithm
// prerequisite
delay(1000);
// 2. Measure temperature and humidity for SGP internal compensation
//error = sht4x.measureHighPrecision(temperature, humidity);
//if (error) {
if (!status){
//Serial.print("SHT4x - Error trying to execute measureHighPrecision(): ");
Serial.print("BME280 - Error trying to execute begin");
//errorToString(error, errorMessage, 256);
//Serial.println(errorMessage);
Serial.println("Fallback to use default values for humidity and "
"temperature compensation for SGP41");
compensationRh = defaultCompenstaionRh;
compensationT = defaultCompenstaionT;
} else {
humidity = bme.readHumidity();
temperature = bme.readTemperature();
pressure = (bme.readPressure()/100.0F);
appaltitude = bme.readAltitude(SEALEVELPRESSURE_HPA);
Serial.print("Temperature:");
Serial.print(temperature);
Serial.print("\t");
Serial.print("Relative Humidity:");
Serial.println(humidity);
Serial.print("Pressure:");
Serial.print(pressure);
Serial.print("\t");
Serial.print("Approx. Altitude:");
Serial.println(appaltitude);
// convert temperature and humidity to ticks as defined by SGP41
// interface
// NOTE: in case you read RH and T raw signals check out the
// ticks specification in the datasheet, as they can be different for
// different sensors
compensationT = static_cast<uint16_t>((temperature + 45) * 65535 / 175);
compensationRh = static_cast<uint16_t>(humidity * 65535 / 100);
}
// 3. Measure SGP4x signals
if (conditioning_s > 0) {
// During NOx conditioning (10s) SRAW NOx will remain 0
error =
sgp41.executeConditioning(compensationRh, compensationT, srawVoc);
conditioning_s--;
} else {
error = sgp41.measureRawSignals(compensationRh, compensationT, srawVoc,
srawNox);
}
// 4. Process raw signals by Gas Index Algorithm to get the VOC and NOx
// index
// values
if (error) {
Serial.print("SGP41 - Error trying to execute measureRawSignals(): ");
//errorToString(error, errorMessage, 256);
//Serial.println(errorMessage);
} else {
int32_t voc_index = voc_algorithm.process(srawVoc);
int32_t nox_index = nox_algorithm.process(srawNox);
Serial.print("VOC Index: ");
Serial.print(voc_index);
Serial.print("\t");
Serial.print("NOx Index: ");
Serial.println(nox_index);
}
if (readPMSdata(&Serial1)) {
// reading data was successful!
Serial.println();
Serial.println("---------------------------------------");
Serial.println("Concentration Units (standard)");
Serial.print("PM 1.0: "); Serial.print(data.pm10_standard);
Serial.print("\t\tPM 2.5: "); Serial.print(data.pm25_standard);
Serial.print("\t\tPM 10: "); Serial.println(data.pm100_standard);
Serial.println("---------------------------------------");
Serial.println("Concentration Units (environmental)");
Serial.print("PM 1.0: "); Serial.print(data.pm10_env);
Serial.print("\t\tPM 2.5: "); Serial.print(data.pm25_env);
Serial.print("\t\tPM 10: "); Serial.println(data.pm100_env);
Serial.println("---------------------------------------");
Serial.print("Particles > 0.3um / 0.1L air:"); Serial.println(data.particles_03um);
Serial.print("Particles > 0.5um / 0.1L air:"); Serial.println(data.particles_05um);
Serial.print("Particles > 1.0um / 0.1L air:"); Serial.println(data.particles_10um);
Serial.print("Particles > 2.5um / 0.1L air:"); Serial.println(data.particles_25um);
Serial.print("Particles > 5.0um / 0.1L air:"); Serial.println(data.particles_50um);
Serial.print("Particles > 10.0 um / 0.1L air:"); Serial.println(data.particles_100um);
Serial.println("---------------------------------------");
}
}
boolean readPMSdata(Stream *s) {
if (! s->available()) {
return false;
}
// Read a byte at a time until we get to the special '0x42' start-byte
if (s->peek() != 0x42) {
s->read();
return false;
}
// Now read all 32 bytes
if (s->available() < 32) {
return false;
}
uint8_t buffer[32];
uint16_t sum = 0;
s->readBytes(buffer, 32);
// get checksum ready
for (uint8_t i = 0; i < 30; i++) {
sum += buffer[i];
}
/* debugging
for (uint8_t i=2; i<32; i++) {
Serial.print("0x"); Serial.print(buffer[i], HEX); Serial.print(", ");
}
Serial.println();
*/
// The data comes in endian'd, this solves it so it works on all platforms
uint16_t buffer_u16[15];
for (uint8_t i = 0; i < 15; i++) {
buffer_u16[i] = buffer[2 + i * 2 + 1];
buffer_u16[i] += (buffer[2 + i * 2] << 8);
}
// put it into a nice struct :)
memcpy((void *)&data, (void *)buffer_u16, 30);
if (sum != data.checksum) {
Serial.println("Checksum failure");
return false;
}
// success!
return true;
}