-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.ino
126 lines (112 loc) · 3.03 KB
/
main.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
#include <Button2.h>;
#include <esp_adc_cal.h>
#include "Screen.h"
#include "PMSensor.h"
#include "VOCSensor.h"
#include "Cloud.h"
#include "Context.h"
// Configuration
char* wifiSsid = "";
char* wifiPassword = "";
char* mqttServer = "";
char* mqttUser = "";
char* mqttPassword = "";
Context context;
Screen screen = Screen(&context);
PMSensor pms = PMSensor(&context.AQI);
VOCSensor voc = VOCSensor(&context.gas);
Cloud cloud = Cloud(deviceName, wifiSsid, wifiPassword, mqttServer, mqttUser, mqttPassword);
Button2 btnTop(35);
Button2 btnBottom(0);
unsigned long lastIntervalCheck = 0;
const long readInterval = 15000; // 15s
void setup()
{
screen.showSplash("Connecting WiFi");
if (!cloud.connect()) {
screen.showError("Fail!");
delay(500);
}
screen.showSplash("Initialise gas sensor");
delay(500);
if (!voc.init()) {
screen.showError("Fail!");
delay(500);
}
screen.showSplash("Initialise particle sensor");
if (!pms.init()) {
screen.showError("Fail!");
delay(500);
} else {
/* start measurement and wait for 10s to ensure the sensor has a
* stable flow and possible remaining particles are cleaned out */
screen.waitForIt("Cleaning extra dust", 10);
pms.update();
}
setButtonsHandler();
screen.setView(SCREEN_PM25);
screen.refresh();
}
void loop()
{
btnTop.loop();
btnBottom.loop();
// Data update loop
if (timesUp(readInterval)) {
pms.update();
voc.update();
screen.refresh();
cloud.send("home/livingroom/airquality", formatCloudData().c_str());
}
delay(5);
}
void setButtonsHandler()
{
btnTop.setPressedHandler([](Button2 &b) {
screen.nextView();
screen.refresh();
});
btnBottom.setPressedHandler([](Button2 &b) {
screen.toggleBacklight();
});
}
bool timesUp(long interval) {
unsigned long now = millis();
if (now - lastIntervalCheck >= readInterval) {
lastIntervalCheck = now;
return true;
} else {
return false;
}
}
String formatCloudData() {
char cloudDataBuffer[355];
sprintf(cloudDataBuffer, "{"
"\"tvoc\":%d,"
"\"eco2\":%d,"
"\"mc_1p0\":%f,"
"\"mc_2p5\":%f,"
"\"mc_4p0\":%f,"
"\"mc_10p0\":%f,"
"\"aqi_1p0\":%d,"
"\"aqi_2p5\":%d,"
"\"aqi_4p0\":%d,"
"\"aqi_10p0\":%d,"
"\"aqi_main_value\":%d,"
"\"aqi_main_libelle\":\"%s\""
"}",
context.gas.tvoc.value,
context.gas.eco2.value,
context.AQI.aqi_1p0.rawValue,
context.AQI.aqi_2p5.rawValue,
context.AQI.aqi_4p0.rawValue,
context.AQI.aqi_10p0.rawValue,
context.AQI.aqi_1p0.value,
context.AQI.aqi_2p5.value,
context.AQI.aqi_4p0.value,
context.AQI.aqi_10p0.value,
context.AQI.aqi_2p5.value,
context.AQI.aqi_2p5.libelle
);
return String(cloudDataBuffer);
}