forked from melkati/CO2-Gadget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCO2_Gadget.ino
360 lines (326 loc) · 14.8 KB
/
CO2_Gadget.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// clang-format off
/*****************************************************************************************************/
/********* GENERAL GLOBAL DEFINITIONS AND OPTIONS *********/
/*****************************************************************************************************/
/* If you are NOT using PlatformIO (You are using Arduino IDE) you must define your options bellow */
/* If you ARE using PlarformIO (NOT Arduino IDE) you must define your options in platformio.ini file */
/* THIS SECTION IS OUTDATED, IF YOU WANT TO USE ARDUINO YOU WILL HAVE TO SHORT IT OUT BY YOURSELF */
/* I WILL PREPARE THE CODE AND WRITE NEW INSTRUCTIONS AS TIME PERMITS. */
/* */
/**/ #ifndef PLATFORMIO
/**/ #define SUPPORT_OTA // Needs SUPPORT_WIFI - CURRENTLY NOT WORKING AWAITING FIX
/**/ #define SUPPORT_TFT
/**/ #define DEBUG_ARDUINOMENU
/**/ #define UNITHOSTNAME "CO2-Gadget"
/**/ // #define ALTERNATIVE_I2C_PINS // For the compact build as shown at https://emariete.com/medidor-co2-display-tft-color-ttgo-t-display-sensirion-scd30/
/**/ #endif
/*****************************************************************************************************/
// clang-format on
// Next data always defined to be able to configure in menu
String hostName = UNITHOSTNAME;
String rootTopic = UNITHOSTNAME;
String mqttClientId = UNITHOSTNAME;
String mqttBroker = MQTT_BROKER_SERVER;
String mqttUser = "";
String mqttPass = "";
String wifiSSID = WIFI_SSID_CREDENTIALS;
String wifiPass = WIFI_PW_CREDENTIALS;
String mDNSName = "Unset";
bool activeBLE = true;
bool activeWIFI = true;
bool activeMQTT = true;
bool debugSensors = false;
bool displayReverse = false;
bool showFahrenheit = false;
uint16_t measurementInterval = 10;
bool inMenu = false;
bool bleInitialized = false;
int8_t selectedCO2Sensor = -1;
uint32_t DisplayBrightness = 100;
// Variables to control automatic display off to save power
uint32_t actualDisplayBrightness = 100; // To know if it's on or off
bool displayOffOnExternalPower = false;
uint16_t timeToDisplayOff = 0; // Time in seconds to turn off the display to save power.
uint64_t nextTimeToDisplayOff = millis() + (timeToDisplayOff*1000); // Next time display should turn off
uint64_t lastButtonUpTimeStamp = millis(); // Last time button UP was pressed
#ifdef BUILD_GIT
#undef BUILD_GIT
#endif // ifdef BUILD_GIT
#define BUILD_GIT __DATE__
#undef I2C_SDA
#undef I2C_SCL
#ifdef ALTERNATIVE_I2C_PINS
#define I2C_SDA 22
#define I2C_SCL 21
#else
#define I2C_SDA 21
#define I2C_SCL 22
#endif
#include <Wire.h>
#include "driver/adc.h"
#include "soc/soc.h" // disable brownout problems
#include "soc/rtc_cntl_reg.h" // disable brownout problems
#include <WiFi.h>
#ifdef SUPPORT_MDNS
#include <ESPmDNS.h>
#endif
// #include <WiFiUdp.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
#ifdef SUPPORT_OTA
#include <AsyncElegantOTA.h>
#endif
#include <FS.h>
#include <SPIFFS.h>
// Function and enum definitions
void reverseButtons(bool reversed);
enum notificationTypes { notifyNothing, notifyInfo, notifyWarning, notifyError };
bool displayNotification(String notificationText, notificationTypes notificationType);
// clang-format off
/*****************************************************************************************************/
/********* *********/
/********* SETUP SENSORS *********/
/********* *********/
/*****************************************************************************************************/
// clang-format on
#include <CO2_Gadget_Sensors.h>
// clang-format off
/*****************************************************************************************************/
/********* *********/
/********* INCLUDE WIFI FUNCTIONALITY *********/
/********* *********/
/*****************************************************************************************************/
// clang-format on
#include <CO2_Gadget_WIFI.h>
// clang-format off
/*****************************************************************************************************/
/********* *********/
/********* SETUP MQTT FUNCTIONALITY *********/
/********* *********/
/*****************************************************************************************************/
// clang-format on
#include "CO2_Gadget_MQTT.h"
// clang-format off
/*****************************************************************************************************/
/********* *********/
/********* SETUP BLE FUNCTIONALITY *********/
/********* *********/
/*****************************************************************************************************/
// clang-format on
#ifdef SUPPORT_BLE
#include "CO2_Gadget_BLE.h"
#endif
// clang-format off
/*****************************************************************************************************/
/********* *********/
/********* SETUP OTA FUNCTIONALITY *********/
/********* *********/
/*****************************************************************************************************/
// clang-format on
#ifdef SUPPORT_OTA
#include <AsyncElegantOTA.h>
#endif
// clang-format off
/*****************************************************************************************************/
/********* *********/
/********* INCLUDE BATTERY FUNCTIONALITY *********/
/********* *********/
/*****************************************************************************************************/
// clang-format on
#define ADC_PIN 34
uint16_t vRef = 1100;
uint16_t batteryDischargedMillivolts = 3500; // Voltage of battery when we consider it discharged (0%).
uint16_t batteryFullyChargedMillivolts = 4200; // Voltage of battery when it is considered fully charged (100%).
#include "CO2_Gadget_Battery.h"
// clang-format off
/*****************************************************************************************************/
/********* *********/
/********* INCLUDE OLED DISPLAY FUNCTIONALITY (UNFINISHED WIP) *********/
/********* *********/
/*****************************************************************************************************/
// clang-format on
#if defined SUPPORT_OLED
#include <CO2_Gadget_OLED.h>
#endif
// clang-format off
/*****************************************************************************************************/
/********* *********/
/********* INCLUDE TFT DISPLAY FUNCTIONALITY *********/
/********* *********/
/*****************************************************************************************************/
// clang-format on
#if defined SUPPORT_TFT
#include "CO2_Gadget_TFT.h"
#endif
// clang-format off
/*****************************************************************************************************/
/********* *********/
/********* FUNCTIONALITY TO STORE PREFERENCES IN NON VOLATILE MEMORY *********/
/********* *********/
/*****************************************************************************************************/
// clang-format on
#include "CO2_Gadget_Preferences.h"
// clang-format off
/*****************************************************************************************************/
/********* *********/
/********* INCLUDE MENU FUNCIONALITY *********/
/********* *********/
/*****************************************************************************************************/
// clang-format on
#include "CO2_Gadget_Menu.h"
// clang-format off
/*****************************************************************************************************/
/********* *********/
/********* SETUP PUSH BUTTONS FUNCTIONALITY *********/
/********* *********/
/*****************************************************************************************************/
// clang-format on
#include "CO2_Gadget_Buttons.h"
/*****************************************************************************************************/
static int64_t lastReadingsCommunicationTime = 0;
static int startCheckingAfterUs = 1900000;
void processPendingCommands() {
if (pendingCalibration == true) {
if (calibrationValue != 0) {
printf("-->[MAIN] Calibrating CO2 sensor at %d PPM\n", calibrationValue);
pendingCalibration = false;
sensors.setCO2RecalibrationFactor(calibrationValue);
} else {
printf("-->[MAIN] Avoiding calibrating CO2 sensor with invalid value at %d PPM\n",
calibrationValue);
pendingCalibration = false;
}
}
if (pendingAmbientPressure == true) {
if (ambientPressureValue != 0) {
printf("-->[MAIN] Setting AmbientPressure for CO2 sensor at %d mbar\n",
ambientPressureValue);
pendingAmbientPressure = false;
sensors.scd30.setAmbientPressure(ambientPressureValue);
} else {
printf("-->[MAIN] Avoiding setting AmbientPressure for CO2 sensor with invalid "
"value at %d mbar\n",
ambientPressureValue);
pendingAmbientPressure = false;
}
}
}
void initGPIO() {
pinMode(GREEN_PIN, OUTPUT);
digitalWrite(GREEN_PIN, LOW);
pinMode(ORANGE_PIN, OUTPUT);
digitalWrite(ORANGE_PIN, LOW);
pinMode(RED_PIN, OUTPUT);
digitalWrite(RED_PIN, LOW);
}
void alarmsLoop() {
if (co2>=co2OrangeRange) {
digitalWrite(GREEN_PIN, GREEN_PIN_LOW);
}
if (co2<co2OrangeRange) {
digitalWrite(GREEN_PIN, GREEN_PIN_HIGH);
}
if (co2>=co2OrangeRange) {
digitalWrite(ORANGE_PIN, ORANGE_PIN_HIGH);
}
if (co2<co2OrangeRange-PIN_HYSTERESIS) {
digitalWrite(ORANGE_PIN, ORANGE_PIN_LOW);
}
if (co2>co2RedRange) {
digitalWrite(RED_PIN, RED_PIN_HIGH);
}
if (co2<=co2RedRange-PIN_HYSTERESIS) {
digitalWrite(RED_PIN, RED_PIN_LOW);
}
}
void readingsLoop() {
if (esp_timer_get_time() - lastReadingsCommunicationTime >= startCheckingAfterUs) {
if (newReadingsAvailable) {
lastReadingsCommunicationTime = esp_timer_get_time();
newReadingsAvailable = false;
nav.idleChanged = true; // Must redraw display as there are new readings
#ifdef SUPPORT_BLE
publishBLE();
#endif
// Provide the sensor values for Tools -> Serial Monitor or Serial Plotter
// Serial.printf("CO2[ppm]:%d\tTemperature[\u00B0C]:%.2f\tHumidity[%%]:%.2f\n", co2, temp, hum);
if ((activeWIFI) && (WiFi.status() != WL_CONNECTED)) {
Serial.println("-->[MAIN] WiFi not connected");
}
#ifdef SUPPORT_MQTT
publishMQTT();
#endif
}
}
}
void displayLoop() {
if (timeToDisplayOff == 0) // TFT Always ON
return;
// If configured not to turn off the display on external power
// and actual voltage is more than those of a maximum loaded batery + 5%, do
// nothing and return
if ((!displayOffOnExternalPower) &&
(battery_voltage * 1000 > batteryFullyChargedMillivolts +
(batteryFullyChargedMillivolts * 5 / 100)))
{
if(actualDisplayBrightness == 0) // When connect USB & TFT is OFF -> TURN IT ON
{
setDisplayBrightness(DisplayBrightness); // Turn on the display at DisplayBrightness brightness
actualDisplayBrightness = DisplayBrightness;
}
return;
}
if (millis() > nextTimeToDisplayOff) {
Serial.println("-->[MAIN] Turning off display to save power");
turnOffDisplay();
actualDisplayBrightness = 0;
nextTimeToDisplayOff = nextTimeToDisplayOff + (timeToDisplayOff * 1000);
}
}
void setup() {
uint32_t brown_reg_temp =
READ_PERI_REG(RTC_CNTL_BROWN_OUT_REG); // save WatchDog register
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); // disable brownout detector
Serial.begin(115200);
delay(100);
// Serial.printf("Total heap: %d", ESP.getHeapSize());
// Serial.printf("Free heap: %d", ESP.getFreeHeap());
// Serial.printf("Total PSRAM: %d", ESP.getPsramSize());
// Serial.printf("Free PSRAM: %d", ESP.getFreePsram());
Serial.printf("\n-->[MAIN] CO2 Gadget Version: %s%s Flavour: %s\nStarting up...\n", CO2_GADGET_VERSION, CO2_GADGET_REV, FLAVOUR);
Serial.printf("\n-->[MAIN] Version compiled: %s at %s\n", __DATE__, __TIME__);
setCpuFrequencyMhz(80); // Lower CPU frecuency to reduce power consumption
initPreferences();
initBattery();
initGPIO();
#if defined SUPPORT_OLED
initDisplayOLED();
#endif
#if defined SUPPORT_TFT
initDisplayTFT();
#endif
#ifdef SUPPORT_BLE
initBLE();
#endif
initWifi();
initSensors();
#ifdef SUPPORT_MQTT
initMQTT();
#endif
menu_init();
buttonsInit();
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG,
brown_reg_temp); // enable brownout detector
Serial.println("-->[STUP] Ready.");
}
void loop() {
mqttClientLoop();
sensorsLoop();
alarmsLoop();
processPendingCommands();
readingsLoop();
OTALoop();
displayLoop();
buttonsLoop();
menuLoop();
}