This repository has been archived by the owner on Jun 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtt-CO2meter.ino
104 lines (86 loc) · 1.99 KB
/
mqtt-CO2meter.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
/*
ESP8266 based CO2 logger
created 21-04-2019
by Bram van Deventer
https://github.com/bram2202/mqtt-C02meter
*/
#include "Settings.h"
#include <ArduinoOTA.h>
#include "MQTTPublisher.h"
#include "OledScreen.h"
#include "WifiConnector.h"
#include "Co2Sensor.h"
// Vars
int lastEco2 = 0;
int lastTVoc = 0;
int sendEco2 = 0;
int sendTVoc = 0;
bool hasMQTT = false;
bool hasWIFI = false;
MQTTPublisher mqqtPublisher(DEBUGE_MODE);
OledScreen oledScreen(DEBUGE_MODE);
Co2Sensor co2Sensor(DEBUGE_MODE);
WifiConnector wifiConnector(DEBUGE_MODE);
WiFiUDP ntpUDP;
void setup() {
if(DEBUGE_MODE)
Serial.begin(115200);
Serial.println("Booting");
if(USE_SCREEN)
{
// Start Screen
oledScreen.start();
}
// Start Co2 sensor
co2Sensor.start();
// Start wifi
wifiConnector.start();
// Start OTA
ArduinoOTA.setHostname("CO2Logger");
ArduinoOTA.onStart([]() {
if(DEBUGE_MODE)
{
Serial.println("Start Ota");
}
});
ArduinoOTA.onEnd([]() {
if(DEBUGE_MODE)
{
Serial.println("\nEnd Ota");
}
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
if(DEBUGE_MODE)
{
Serial.printf("OTA Progress: %u%%\r", (progress / (total / 100)));
}
});
ArduinoOTA.onError([](ota_error_t error) {
if(DEBUGE_MODE){
Serial.printf("OTA Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
}
});
ArduinoOTA.begin();
// Start processes
mqqtPublisher.start();
}
void loop() {
ArduinoOTA.handle();
yield();
co2Sensor.handle();
yield();
mqqtPublisher.handle();
yield();
if(USE_SCREEN)
{
oledScreen.handle();
yield();
}
wifiConnector.handle();
yield();
}