-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlynkEdgent.h
119 lines (101 loc) · 3.3 KB
/
BlynkEdgent.h
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
extern "C" {
#include "user_interface.h"
void app_loop();
}
#include "Settings.h"
#include <BlynkSimpleEsp8266_SSL.h>
#ifndef BLYNK_NEW_LIBRARY
#error "Old version of Blynk library is in use. Please replace it with the new one."
#endif
#if !defined(BLYNK_TEMPLATE_ID) || !defined(BLYNK_DEVICE_NAME)
#error "Please specify your BLYNK_TEMPLATE_ID and BLYNK_DEVICE_NAME"
#endif
#include "BlynkState.h"
#include "ConfigStore.h"
#include "ResetButton.h"
#include "ConfigMode.h"
#include "Indicator.h"
#include "OTA.h"
#include "Console.h"
inline
void BlynkState::set(State m) {
if (state != m && m < MODE_MAX_VALUE) {
DEBUG_PRINT(String(StateStr[state]) + " => " + StateStr[m]);
state = m;
// You can put your state handling here,
// i.e. implement custom indication
}
}
void printDeviceBanner()
{
Blynk.printBanner();
DEBUG_PRINT("--------------------------");
DEBUG_PRINT(String("Product: ") + BLYNK_DEVICE_NAME);
DEBUG_PRINT(String("Firmware: ") + BLYNK_FIRMWARE_VERSION " (build " __DATE__ " " __TIME__ ")");
if (configStore.getFlag(CONFIG_FLAG_VALID)) {
DEBUG_PRINT(String("Token: ...") + (configStore.cloudToken+28));
}
DEBUG_PRINT(String("Device: ") + BLYNK_INFO_DEVICE + " @ " + ESP.getCpuFreqMHz() + "MHz");
DEBUG_PRINT(String("MAC: ") + WiFi.macAddress());
DEBUG_PRINT(String("Flash: ") + ESP.getFlashChipRealSize() / 1024 + "K");
String coreVer = ESP.getCoreVersion();
coreVer.replace("_", ".");
DEBUG_PRINT(String("ESP core: ") + coreVer);
DEBUG_PRINT(String("ESP SDK: ") + ESP.getSdkVersion());
DEBUG_PRINT(String("Boot Ver: ") + ESP.getBootVersion());
DEBUG_PRINT(String("Boot Mode:") + ESP.getBootMode());
DEBUG_PRINT(String("FW info: ") + ESP.getSketchSize() + "/" + ESP.getFreeSketchSpace() + ", MD5:" + ESP.getSketchMD5());
DEBUG_PRINT(String("Free mem: ") + ESP.getFreeHeap());
DEBUG_PRINT("--------------------------");
}
void runBlynkWithChecks() {
Blynk.run();
if (BlynkState::get() == MODE_RUNNING) {
if (!Blynk.connected()) {
if (WiFi.status() == WL_CONNECTED) {
BlynkState::set(MODE_CONNECTING_CLOUD);
} else {
BlynkState::set(MODE_CONNECTING_NET);
}
}
}
}
class Edgent {
public:
void begin()
{
indicator_init();
button_init();
config_init();
console_init();
printDeviceBanner();
if (configStore.getFlag(CONFIG_FLAG_VALID)) {
BlynkState::set(MODE_CONNECTING_NET);
} else if (config_load_blnkopt()) {
DEBUG_PRINT("Firmware is preprovisioned");
BlynkState::set(MODE_CONNECTING_NET);
} else {
BlynkState::set(MODE_WAIT_CONFIG);
}
}
void run() {
app_loop();
switch (BlynkState::get()) {
case MODE_WAIT_CONFIG:
case MODE_CONFIGURING: enterConfigMode(); break;
case MODE_CONNECTING_NET: enterConnectNet(); break;
case MODE_CONNECTING_CLOUD: enterConnectCloud(); break;
case MODE_RUNNING: runBlynkWithChecks(); break;
case MODE_OTA_UPGRADE: enterOTA(); break;
case MODE_SWITCH_TO_STA: enterSwitchToSTA(); break;
case MODE_RESET_CONFIG: enterResetConfig(); break;
default: enterError(); break;
}
}
};
Edgent BlynkEdgent;
BlynkTimer edgentTimer;
void app_loop() {
edgentTimer.run();
edgentConsole.run();
}