-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspg30.ino
78 lines (67 loc) · 2.41 KB
/
spg30.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
// we need to include sensirion_ess.h
#include <sensirion_ess.h>
// Create an instance of SensirionESS
SensirionESS ess;
void setup()
{
Serial.begin(9600);
delay(1000); // let console settle
// First step is to initialize the sensors; this should only fail if
// the board is defect, or the connection isn't working. Since there's nothing
// we can do if this fails, the code will loop forever if an error is detected
if (ess.initSensors() != 0) {
Serial.print("Error while initializing sensors: ");
Serial.print(ess.getError());
Serial.print("\n");
while (1) { // loop forever
delay(1000);
}
}
// The SGP sensor has product type information and feature set stored
// the following code reads it out, and prints it to the serial console.
// This is purely to demo the function calls, and is not necessary to operate
// the sensor
int type = ess.getProductType();
int fsVersion = ess.getFeatureSetVersion();
Serial.print((type == SensirionESS::PRODUCT_TYPE_SGP30) ? "SGP30" : "SGPC3");
Serial.print(" detected, running feature set version ");
Serial.println(fsVersion);
}
void loop() {
float temp, rh, tvoc, eco2 = -1;
// we'll start by triggering a measurement of the VOC/CO2 sensor;
// it's important to do this first to make sure sleep timing is
// correct. If the command succeeds, the local variables will
// be set to the values we just read; if it fails, they'll be -1
if (ess.measureIAQ() != 0) {
Serial.print("Error while measuring IAQ: ");
Serial.print(ess.getError());
Serial.print("\n");
} else {
tvoc = ess.getTVOC();
eco2 = ess.getECO2(); // SGP30 only
}
// next, we'll trigger the humidity and temperature measurement
if (ess.measureRHT() != 0) {
Serial.print("Error while measuring RHT: ");
Serial.print(ess.getError());
Serial.print("\n");
} else {
temp = ess.getTemperature();
rh = ess.getHumidity();
}
// finally, let's print those to the serial console
Serial.print(temp);
Serial.print(" ");
Serial.print(rh);
Serial.print(" ");
Serial.print(tvoc);
Serial.print(" ");
if (ess.getProductType() == SensirionESS::PRODUCT_TYPE_SGP30) {
Serial.print(eco2);
}
Serial.print("\n");
// and then, we'll use remainingWaitTimeMS() to ensure the correct
// Measurement rate
delay(ess.remainingWaitTimeMS());
}