Skip to content

Commit b584fc5

Browse files
added Blynk Edgent and Anomaly detection sketches
1 parent d59d97a commit b584fc5

File tree

19 files changed

+2856
-0
lines changed

19 files changed

+2856
-0
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/**
2+
* @file BlynkEdgent.h
3+
* @author Blynk Inc.
4+
* @license This project is released under the MIT License (MIT)
5+
* @copyright Copyright (c) 2021 Blynk Inc.
6+
* @date May 2021
7+
* @brief
8+
*
9+
*/
10+
11+
extern "C" {
12+
void app_loop();
13+
void eraseMcuConfig();
14+
void restartMCU();
15+
}
16+
17+
#include "Settings.h"
18+
#include <BlynkSimpleWioTerminal.h>
19+
20+
#ifndef BLYNK_NEW_LIBRARY
21+
#error "Old version of Blynk library is in use. Please replace it with the new one."
22+
#endif
23+
24+
#include "BlynkState.h"
25+
#include "ConfigStore.h"
26+
#include "ResetButton.h"
27+
#include "ConfigMode.h"
28+
#include "Indicator.h"
29+
//#include "OTA.h"
30+
31+
inline
32+
void BlynkState::set(State m) {
33+
if (state != m && m < MODE_MAX_VALUE) {
34+
DEBUG_PRINT(String(StateStr[state]) + " => " + StateStr[m]);
35+
state = m;
36+
37+
// You can put your state handling here,
38+
// i.e. implement custom indication
39+
}
40+
}
41+
42+
void printDeviceBanner()
43+
{
44+
Blynk.printBanner();
45+
DEBUG_PRINT("--------------------------");
46+
DEBUG_PRINT(String("Product: ") + BLYNK_DEVICE_NAME);
47+
DEBUG_PRINT(String("Hardware: ") + BOARD_HARDWARE_VERSION);
48+
DEBUG_PRINT(String("Firmware: ") + BLYNK_FIRMWARE_VERSION " (build " __DATE__ " " __TIME__ ")");
49+
if (configStore.getFlag(CONFIG_FLAG_VALID)) {
50+
DEBUG_PRINT(String("Token: ...") + (configStore.cloudToken+28));
51+
}
52+
DEBUG_PRINT(String("Device: ") + BLYNK_INFO_DEVICE);
53+
DEBUG_PRINT(String("MAC: ") + WiFi.macAddress());
54+
DEBUG_PRINT("--------------------------");
55+
}
56+
57+
void runBlynkWithChecks() {
58+
Blynk.run();
59+
if (BlynkState::get() == MODE_RUNNING) {
60+
if (!Blynk.connected()) {
61+
if (WiFi.status() == WL_CONNECTED) {
62+
BlynkState::set(MODE_CONNECTING_CLOUD);
63+
} else {
64+
BlynkState::set(MODE_CONNECTING_NET);
65+
}
66+
}
67+
}
68+
}
69+
70+
class Edgent {
71+
72+
public:
73+
void begin()
74+
{
75+
//indicator_init();
76+
button_init();
77+
config_init();
78+
79+
WiFi.persistent(false);
80+
WiFi.enableSTA(true); // Needed to get MAC
81+
82+
printDeviceBanner();
83+
84+
if (configStore.getFlag(CONFIG_FLAG_VALID)) {
85+
BlynkState::set(MODE_CONNECTING_NET);
86+
} else if (config_load_blnkopt()) {
87+
DEBUG_PRINT("Firmware is preprovisioned");
88+
BlynkState::set(MODE_CONNECTING_NET);
89+
} else {
90+
BlynkState::set(MODE_WAIT_CONFIG);
91+
}
92+
}
93+
94+
void run() {
95+
app_loop();
96+
switch (BlynkState::get()) {
97+
case MODE_WAIT_CONFIG:
98+
case MODE_CONFIGURING: enterConfigMode(); break;
99+
case MODE_CONNECTING_NET: enterConnectNet(); break;
100+
case MODE_CONNECTING_CLOUD: enterConnectCloud(); break;
101+
case MODE_RUNNING: runBlynkWithChecks(); break;
102+
//case MODE_OTA_UPGRADE: enterOTA(); break;
103+
case MODE_SWITCH_TO_STA: enterSwitchToSTA(); break;
104+
case MODE_RESET_CONFIG: enterResetConfig(); break;
105+
default: enterError(); break;
106+
}
107+
}
108+
109+
};
110+
111+
Edgent BlynkEdgent;
112+
BlynkTimer timer;
113+
114+
void app_loop() {
115+
timer.run();
116+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @file BlynkState.h
3+
* @author Blynk Inc.
4+
* @license This project is released under the MIT License (MIT)
5+
* @copyright Copyright (c) 2021 Blynk Inc.
6+
* @date May 2021
7+
* @brief
8+
*
9+
*/
10+
11+
enum State {
12+
MODE_WAIT_CONFIG,
13+
MODE_CONFIGURING,
14+
MODE_CONNECTING_NET,
15+
MODE_CONNECTING_CLOUD,
16+
MODE_RUNNING,
17+
//MODE_OTA_UPGRADE,
18+
MODE_SWITCH_TO_STA,
19+
MODE_RESET_CONFIG,
20+
MODE_ERROR,
21+
22+
MODE_MAX_VALUE
23+
};
24+
25+
#if defined(APP_DEBUG)
26+
const char* StateStr[MODE_MAX_VALUE+1] = {
27+
"WAIT_CONFIG",
28+
"CONFIGURING",
29+
"CONNECTING_NET",
30+
"CONNECTING_CLOUD",
31+
"RUNNING",
32+
//"OTA_UPGRADE",
33+
"SWITCH_TO_STA",
34+
"RESET_CONFIG",
35+
"ERROR",
36+
37+
"INIT"
38+
};
39+
#endif
40+
41+
namespace BlynkState
42+
{
43+
volatile State state = MODE_MAX_VALUE;
44+
45+
State get() { return state; }
46+
bool is (State m) { return (state == m); }
47+
void set(State m);
48+
};

0 commit comments

Comments
 (0)