-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
130 lines (105 loc) · 3.01 KB
/
main.cpp
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
#include <Arduino.h>
#include <ArduinoLog.h>
#if !( defined(ESP32) ) && !( defined(ESP8266) )
#error This code is intended to run on ESP8266 platform! Please check your Tools->Board setting.
#endif
#include "Configuration.h"
#include "wifi/WifiManager.h"
#include "Device.h"
#ifdef ESP32
#elif ESP8266
ADC_MODE(ADC_TOUT);
#endif
CWifiManager *wifiManager;
CDevice *device;
unsigned long tsSmoothBoot;
bool smoothBoot;
unsigned long tsMillisBooted;
void setup() {
randomSeed(analogRead(0));
pinMode(D0, WAKEUP_PULLUP);
pinMode(INTERNAL_LED_PIN, OUTPUT);
intLEDOn();
#ifndef DISABLE_LOGGING
Serial.begin(115200); while (!Serial); delay(100);
Log.begin(LOG_LEVEL, &Serial);
Log.infoln(F("\n\nInitializing..."));
#ifdef WEB_LOGGING
Log.addHandler(&logStream);
Log.infoln(F("Initializing web log..."));
#endif
#endif
if (EEPROM_initAndCheckFactoryReset() >= 3) {
Log.warningln("Factory reset conditions met!");
EEPROM_wipe();
}
tsSmoothBoot = millis();
smoothBoot = false;
EEPROM_loadConfig();
Log.infoln("Configuration loaded");
device = new CDevice();
wifiManager = new CWifiManager(device);
#ifdef OLED
wifiManager->setDisplay(device->display());
#endif
if (wifiManager->isError()) {
Log.errorln("wifiManager->isError()=%i", wifiManager->isError());
while(true) {
intLEDBlink(250);
delay(250);
}
}
Log.infoln("Initialized");
}
void loop() {
if (!smoothBoot && millis() - tsSmoothBoot > FACTORY_RESET_CLEAR_TIMER_MS) {
smoothBoot = true;
EEPROM_clearFactoryReset();
tsMillisBooted = millis();
if (configuration.deepSleepDurationSec == 0) {
// Keep the LED on if expected to sleep
intLEDOff();
}
Log.noticeln("Device booted smoothly!");
}
#ifdef OLED
device->display()->clearDisplay();
#endif
device->loop();
wifiManager->loop();
#ifdef OLED
device->display()->display();
#endif
if (wifiManager->isRebootNeeded()) {
return;
}
// Conditions for deep sleep:
// - Min time elapsed since smooth boot (to catch up on any MQTT messages)
// - Smooth boot
// - Wifi not in AP mode
// - Succesfully submitted 1 sensor reading over MQTT
if (smoothBoot
&& configuration.deepSleepDurationSec > 0
&& millis() - tsMillisBooted > DEEP_SLEEP_MIN_AWAKE_MS
&& wifiManager->isJobDone() ) {
delay(100);
intLEDOff();
Log.noticeln("Initiating deep sleep for %u usec", configuration.deepSleepDurationSec );
#ifdef ESP32
digitalWrite(INTERNAL_LED_PIN, LOW);
ESP.deepSleep((uint64_t)configuration.deepSleepDurationSec * 1e6);
#elif ESP8266
digitalWrite(INTERNAL_LED_PIN, HIGH);
ESP.deepSleep((uint64_t)configuration.deepSleepDurationSec * 1e6);
#endif
}
if (configuration.deepSleepDurationSec > 0 && device->getUptime() > configuration.deepSleepDurationSec * 1000) {
Log.noticeln("Device is not sleeping right, resetting to save battery");
#ifdef ESP32
ESP.restart();
#elif ESP8266
ESP.reset();
#endif
}
yield();
}