-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSustainCore.ino
95 lines (75 loc) · 1.9 KB
/
SustainCore.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
#ifdef __arm__
// should use uinstd.h to define sbrk but Due causes a conflict
extern "C" char* sbrk(int incr);
#else // __ARM__
extern char *__brkval;
#endif // __arm__
#define MINUTES(x) (x*60e3)
//#define TEST_SENSORS
typedef struct {
float temperature;
float humidity;
}dhtSensor_t;
typedef struct {
float eco2;
float tvoc;
}ccs811Sensor_t;
dhtSensor_t dhtSensor;
ccs811Sensor_t gasSensor;
void setup() {
Serial.begin(115200);
Serial.println("Sustain core spinning up...");
connectToWifi();
initMqttClient();
initDHT();
initCCS811();
initPM2_5Sensor();
//ESP.deepSleep(10e6);
}
void loop() {
mqttPoll();
uint8_t result = readDHT(&dhtSensor);
pollCCS811(&gasSensor);
int dustConcUgPerM3 = readPM2_5Sensor();
// Check to see if it failed to read, if it did, restart the loop
if (result) {
delay(500);
return;
}
// If we lose connection for some reason
if (!mqttIsConnected())
{
reconnectMqttClient();
delay(500);
return;
}
#ifndef TEST_SENSORS
mqttSetTopic("environment/temperature");
mqttSendMsg(dhtSensor.temperature);
mqttSetTopic("environment/humidity");
mqttSendMsg(dhtSensor.humidity);
mqttSetTopic("environment/pm2_5");
mqttSendMsg(dustConcUgPerM3);
setCCS811Env(dhtSensor.temperature, dhtSensor.humidity);
if ((gasSensor.eco2 >= 0.0) || (gasSensor.tvoc >= 0.0))
{
mqttSetTopic("environment/eco2");
mqttSendMsg(gasSensor.eco2);
mqttSetTopic("environment/tvoc");
mqttSendMsg(gasSensor.tvoc);
}
else
{
Serial.println("Unable to read CCS811. Attempting to re-init sensor");
//initCCS811();
}
// Wasn't able to post something, retry
if (result) {
delay(500);
return;
}
// Wait a few seconds between measurements.
Serial.println("Deep sleeping...");
delay(MINUTES(1));
#endif
}