-
Notifications
You must be signed in to change notification settings - Fork 1
/
PMSensor.cpp
97 lines (86 loc) · 2.09 KB
/
PMSensor.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
#include "PMSensor.h"
#include <Arduino.h>
PMSensor::PMSensor(aqi_measurement *aqi) { this->aqi = aqi; }
bool PMSensor::init() {
sensirion_i2c_init();
int8_t timeout = 5;
while (sps30_probe() != 0 && timeout > 0) {
// SPS sensor probing failed
timeout--;
delay(1500);
}
if (sps30_probe() != 0) {
return false;
}
// Auto clean every 4 days
sps30_set_fan_auto_cleaning_interval_days(4);
// Initialisation is successful if able to get a measurement
return sps30_start_measurement() == 0;
}
void PMSensor::update() {
sps30_read_measurement(&this->pm);
this->aqi->aqi_1p0 = this->getAQI(this->pm.mc_1p0);
this->aqi->aqi_2p5 = this->getAQI(this->pm.mc_2p5);
this->aqi->aqi_4p0 = this->getAQI(this->pm.mc_4p0);
this->aqi->aqi_10p0 = this->getAQI(this->pm.mc_10p0);
}
measurement PMSensor::getAQI(float concentration) {
float cLow = 0.0;
float cHi = 0.0;
int16_t aqiLow = 0;
int16_t aqiHi = 0;
char *libelle = "";
int color;
if (concentration <= 12.0) {
cLow = 0.0;
cHi = 12.0;
aqiLow = 0;
aqiHi = 50;
libelle = "Good";
color = 0x07E0;
} else if (concentration <= 35.4) {
cLow = 12.1;
cHi = 35.5;
aqiLow = 51;
aqiHi = 100;
libelle = "Moderate";
color = 0xFFE0;
} else if (concentration <= 55.4) {
cLow = 35.5;
cHi = 55.4;
aqiLow = 101;
aqiHi = 150;
libelle = "Unhealthy (Sensitive Groups)";
color = 0xFDA0;
} else if (concentration <= 150.4) {
cLow = 55.5;
cHi = 150.4;
aqiLow = 151;
aqiHi = 200;
libelle = "Unhealthy";
color = 0xF800;
} else if (concentration <= 250.4) {
cLow = 150.5;
cHi = 250.4;
aqiLow = 201;
aqiHi = 300;
libelle = "Very Unhealthy";
color = 0x915C;
} else {
cLow = 250.5;
cHi = 500.4;
aqiLow = 301;
aqiHi = 500;
libelle = "Hazardous";
color = 0x7800;
}
measurement aqi;
aqi.value = ((aqiHi - aqiLow) / (cHi - cLow)) * (concentration - cLow) + aqiLow;
aqi.libelle = libelle;
aqi.color = color;
aqi.rawValue = concentration;
if (aqi.value > 500) {
aqi.value = 500;
}
return aqi;
}