-
Notifications
You must be signed in to change notification settings - Fork 4
/
outdoor_with_kstobbe.ino
253 lines (225 loc) · 7.86 KB
/
outdoor_with_kstobbe.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
250
251
252
253
#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;
#define PMS_SET_PIN 26
#define PMS_RST_PIN 25
#define PMS_READ_INTERVAL 9U
#define PMS_READ_DELAY 1U
uint8_t pms_tick_count = PMS_READ_INTERVAL;
PMS pms(Serial2);
PMS::DATA data;
static void setup_pins(void) {
pinMode(PMS_RST_PIN, OUTPUT);
digitalWrite(PMS_RST_PIN, HIGH);
pinMode(PMS_SET_PIN, OUTPUT);
digitalWrite(PMS_SET_PIN, HIGH);
}
static void toggle_set(bool sleep) {
if (sleep) {
digitalWrite(PMS_SET_PIN, LOW);
} else {
digitalWrite(PMS_SET_PIN, HIGH);
}
delay(500);
}
static void toggle_reset(void) {
digitalWrite(PMS_RST_PIN, LOW);
delay(500);
digitalWrite(PMS_RST_PIN, HIGH);
delay(500);
}
bool pms5003_init(void) {
setup_pins();
Serial2.begin(9600, SERIAL_8N1, 16, 17);
delay(1000);
pms_tick_count = PMS_READ_INTERVAL;
return true;
}
bool pms5003_read(uint16_t *pmSp1_0, uint16_t *pmSp2_5, uint16_t *pmSp10_0, uint16_t *pmAe1_0, uint16_t *pmAe2_5, uint16_t *pmAe10_0) {
bool result = false;
if ((NULL == pmSp1_0) || (NULL == pmSp2_5) || (NULL == pmSp10_0) || (NULL == pmAe1_0) || (NULL == pmAe2_5) || (NULL == pmAe10_0)) {
result = false;
} else {
pms_tick_count++;
if (pms_tick_count == PMS_READ_DELAY) {
while (Serial2.available()){
Serial2.read();
}
if (pms.readUntil(data, 2*PMS::SINGLE_RESPONSE_TIME)){
*pmSp1_0 = data.PM_AE_UG_1_0;
*pmSp2_5 = data.PM_AE_UG_2_5;
*pmSp10_0 = data.PM_AE_UG_10_0;
*pmAe1_0 = data.PM_SP_UG_1_0;
*pmAe2_5 = data.PM_SP_UG_2_5;
*pmAe1_0 = data.PM_SP_UG_10_0;
result = true;
} else {
toggle_reset();
}
toggle_set(true);
} else if (pms_tick_count >= PMS_READ_INTERVAL) {
toggle_set(false);
pms_tick_count = 0;
}
}
return result;
}
void setup() {
Serial.begin(9600);
while (!Serial) {
delay(100);
}
Serial2.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
uint16_t pmSp1_0 = 0;
uint16_t pmSp2_5 = 0;
uint16_t pmSp10_0 = 0;
uint16_t pmAe1_0 = 0;
uint16_t pmAe2_5 = 0;
uint16_t pmAe10_0 = 0;
// 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 (pms5003_read(&pmSp1_0, &pmSp2_5, &pmSp10_0, &pmAe1_0, &pmAe2_5, &pmAe10_0)) {
Serial.println(String(pmSp1_0));
Serial.println(String(pmSp2_5));
Serial.println(String(pmSp10_0));
Serial.println(String(pmAe1_0));
Serial.println(String(pmAe2_5));
Serial.println(String(pmAe10_0));
}
delay(500);
}