-
Notifications
You must be signed in to change notification settings - Fork 1
/
sensors.h
193 lines (155 loc) · 4.85 KB
/
sensors.h
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
#include "Adafruit_SHT31.h"
#include <Adafruit_SGP30.h>
#include <Effortless_SPIFFS.h>
Adafruit_SHT31 sht31 = Adafruit_SHT31();
Adafruit_SGP30 sgp;
eSPIFFS fileSystem;
bool sensorsReady = false; // For initial warmup of sensors
byte sensorCount = 0;
// sgp30
int eco2, tvoc;
uint16_t TVOC_base, eCO2_base;
// sht31
float humidity, temperature;
// Dust Sensor
#define DUST_ADDR 8
union u_tag {
float density;
byte density_byte[4];
} u;
void sendDustData();
void sendSHT31Data();
void sendSGP30Data();
void spiffsSaveBaseline();
void spiffsLoadBaseline();
/* return absolute humidity [mg/m^3] with approximation formula
* @param temperature [°C]
* @param humidity [%RH]
*/
uint32_t getAbsoluteHumidity(float temperature, float humidity) {
// approximation formula from Sensirion SGP30 Driver Integration chapter 3.15
const float absoluteHumidity = 216.7f * ((humidity / 100.0f) * 6.112f * exp((17.62f * temperature) / (243.12f + temperature)) / (273.15f + temperature)); // [g/m^3]
const uint32_t absoluteHumidityScaled = static_cast<uint32_t>(1000.0f * absoluteHumidity); // [mg/m^3]
return absoluteHumidityScaled;
}
void sensorSetup()
{
sht31.begin(0x44);
sgp.begin();
spiffsLoadBaseline(); // Load previously saved baseline
sendSHT31Data(); // We call this on setup as SGP30 gets temperature and humidity compensation from this sensor
}
void sensorloop()
{
EVERY_N_SECONDS(10) // Every N seconds because I want it to
{ // Dust
sendDustData();
}
EVERY_N_SECONDS(10) // Used to be every 8 seconds because datasheet of SHT31 8-30 sec sample rate
{ // SHT31 Temp and Humidity
sendSHT31Data();
}
EVERY_N_SECONDS(10) // Used to be every 1 second because datasheet says 1HZ sampling rate is best
{ // SGP30 eCO2 and TVOC
sendSGP30Data();
}
EVERY_N_MINUTES(60) // Saves current baseline every hour as stated in SGP30 datasheet
{
spiffsSaveBaseline();
}
}
void sendSGP30Data()
{
if(sensorsReady == false)
{
if(sensorCount <= 3) // Wait for warmup
{
sensorCount++;
}
else
{
sensorsReady = true;
}
}
sgp.setHumidity(getAbsoluteHumidity(temperature, humidity));
sgp.IAQmeasure();
eco2 = sgp.eCO2; // ppb
tvoc = sgp.TVOC; // ppm
if(sensorsReady)
{
char eco2Char[10];
char tvocChar[10];
itoa(eco2, eco2Char, 10);
itoa(tvoc, tvocChar, 10);
mqttClient.publish("esp32/sensor/eco2", MQTT_QOS, false, eco2Char);
mqttClient.publish("esp32/sensor/tvoc", MQTT_QOS, false, tvocChar);
}
else
{
mqttClient.publish("esp32", MQTT_QOS, false, "NOT READY");
}
}
void sendSHT31Data()
{
temperature = sht31.readTemperature();
humidity = sht31.readHumidity();
if(sensorsReady)
{
char temperatureChar[10];
char humidityChar[10];
//snprintf(temperatureChar, "%.2f", temperature);
//snprintf(humidityChar, "%.2f", humidity);
dtostrf(temperature, 4, 2, temperatureChar);
dtostrf(humidity, 4, 2, humidityChar);
mqttClient.publish("esp32/sensor/temperature", MQTT_QOS, false, temperatureChar);
mqttClient.publish("esp32/sensor/humidity", MQTT_QOS, false, humidityChar);
}
}
void sendDustData()
{
// Request dust sensor data from arduino nano
Wire.requestFrom(DUST_ADDR, 4);
if(Wire.available() == 4)
{
for(byte i = 0; i <= 4; i++)
{
u.density_byte[i] = Wire.read();
}
}
if(sensorsReady)
{
char dustChar[10];
//snprintf(dustChar, "%.2f", u.density);
dtostrf(u.density, 4, 2,dustChar);
mqttClient.publish("esp32/sensor/dust", MQTT_QOS, false, dustChar);
}
}
void spiffsSaveBaseline()
{
uint16_t eCO2_base_current, TVOC_base_current, eCO2_base_spiffs, TVOC_base_spiffs;
sgp.getIAQBaseline(&eCO2_base_current, &TVOC_base_current);
fileSystem.openFromFile("/eco2baseline.txt", eCO2_base_spiffs);
fileSystem.openFromFile("/tvocbaseline.txt", TVOC_base_spiffs);
if(eCO2_base_current != eCO2_base_spiffs)
{
char eco2BaselineChar[10];
itoa(eCO2_base_current, eco2BaselineChar, 10);
mqttClient.publish("esp32/sensor/eco2Baseline", MQTT_QOS, false, eco2BaselineChar);
fileSystem.saveToFile("/eco2baseline.txt", eCO2_base_current);
}
if(TVOC_base_current != TVOC_base_spiffs)
{
char tvocBaselineChar[10];
itoa(TVOC_base_current, tvocBaselineChar, 10);
mqttClient.publish("esp32/sensor/tvocBaseline", MQTT_QOS, false, tvocBaselineChar);
fileSystem.saveToFile("/tvocbaseline.txt", TVOC_base_current);
}
}
void spiffsLoadBaseline()
{
uint16_t eCO2_base_load, TVOC_base_load;
fileSystem.openFromFile("/eco2baseline.txt", eCO2_base_load);
fileSystem.openFromFile("/tvocbaseline.txt", TVOC_base_load);
// If you have a baseline measurement from before you can assign it to start, to 'self-calibrate'
sgp.setIAQBaseline(eCO2_base_load, TVOC_base_load); // Will vary for each sensor! (eCO2_baseline, TVOC_baseline)
}