-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConsole.h
225 lines (188 loc) · 7.11 KB
/
Console.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
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
#include <Blynk/BlynkConsole.h>
extern "C" {
#include "esp_partition.h"
#include "esp_ota_ops.h"
}
BlynkConsole edgentConsole;
void console_init()
{
#ifdef BLYNK_PRINT
edgentConsole.begin(BLYNK_PRINT);
#endif
edgentConsole.print("\n>");
edgentConsole.addCommand("reboot", []() {
edgentConsole.print(R"json({"status":"OK","msg":"rebooting wifi module"})json" "\n");
delay(100);
restartMCU();
});
edgentConsole.addCommand("config", [](int argc, const char** argv) {
if (argc < 1 || 0 == strcmp(argv[0], "start")) {
BlynkState::set(MODE_WAIT_CONFIG);
} else if (0 == strcmp(argv[0], "erase")) {
BlynkState::set(MODE_RESET_CONFIG);
}
});
edgentConsole.addCommand("devinfo", []() {
edgentConsole.printf(
R"json({"name":"%s","board":"%s","tmpl_id":"%s","fw_type":"%s","fw_ver":"%s"})json" "\n",
getWiFiName().c_str(),
BLYNK_TEMPLATE_NAME,
BLYNK_TEMPLATE_ID,
BLYNK_FIRMWARE_TYPE,
BLYNK_FIRMWARE_VERSION
);
});
edgentConsole.addCommand("connect", [](int argc, const char** argv) {
if (argc < 2) {
edgentConsole.print(R"json({"status":"error","msg":"invalid arguments. expected: <auth> <ssid> <pass>"})json" "\n");
return;
}
String auth = argv[0];
String ssid = argv[1];
String pass = (argc >= 3) ? argv[2] : "";
if (auth.length() != 32) {
edgentConsole.print(R"json({"status":"error","msg":"invalid token size"})json" "\n");
return;
}
edgentConsole.print(R"json({"status":"OK","msg":"trying to connect..."})json" "\n");
configStore = configDefault;
CopyString(ssid, configStore.wifiSSID);
CopyString(pass, configStore.wifiPass);
CopyString(auth, configStore.cloudToken);
BlynkState::set(MODE_SWITCH_TO_STA);
});
edgentConsole.addCommand("wifi", [](int argc, const char* argv[]) {
if (argc < 1 || 0 == strcmp(argv[0], "show")) {
edgentConsole.printf(
"mac:%s ip:%s (%s [%s] %ddBm)\n",
getWiFiMacAddress().c_str(),
WiFi.localIP().toString().c_str(),
getWiFiNetworkSSID().c_str(),
getWiFiNetworkBSSID().c_str(),
WiFi.RSSI()
);
} else if (0 == strcmp(argv[0], "scan")) {
int found = WiFi.scanNetworks();
for (int i = 0; i < found; i++) {
bool current = (WiFi.SSID(i) == WiFi.SSID());
edgentConsole.printf(
"%s %s [%s] %s ch:%d rssi:%d\n",
(current ? "*" : " "), WiFi.SSID(i).c_str(),
macToString(WiFi.BSSID(i)).c_str(),
wifiSecToStr(WiFi.encryptionType(i)),
WiFi.channel(i), WiFi.RSSI(i)
);
}
WiFi.scanDelete();
}
});
edgentConsole.addCommand("firmware", [](int argc, const char** argv) {
if (argc < 1 || 0 == strcmp(argv[0], "info")) {
unsigned sketchSize = ESP.getSketchSize();
edgentConsole.printf(" Version: %s (build %s)\n", BLYNK_FIRMWARE_VERSION, __DATE__ " " __TIME__);
edgentConsole.printf(" Type: %s\n", BLYNK_FIRMWARE_TYPE);
edgentConsole.printf(" Platform: %s\n", BLYNK_INFO_DEVICE);
edgentConsole.printf(" SDK: %s\n", ESP.getSdkVersion());
if (const esp_partition_t* running = esp_ota_get_running_partition()) {
edgentConsole.printf(" Partition: %s (%dK)\n", running->label, running->size / 1024);
edgentConsole.printf(" App size: %dK (%d%%)\n", sketchSize/1024, (sketchSize*100)/(running->size));
edgentConsole.printf(" App MD5: %s\n", ESP.getSketchMD5().c_str());
}
} else if (0 == strcmp(argv[0], "rollback")) {
if (Update.rollBack()) {
edgentConsole.print(R"json({"status":"ok"})json" "\n");
edgentTimer.setTimeout(50, restartMCU);
} else {
edgentConsole.print(R"json({"status":"error"})json" "\n");
}
}
});
edgentConsole.addCommand("status", [](int argc, const char** argv) {
const int64_t t = esp_timer_get_time() / 1000000;
unsigned secs = t % BLYNK_SECS_PER_MIN;
unsigned mins = (t / BLYNK_SECS_PER_MIN) % BLYNK_SECS_PER_MIN;
unsigned hrs = (t % BLYNK_SECS_PER_DAY) / BLYNK_SECS_PER_HOUR;
unsigned days = t / BLYNK_SECS_PER_DAY;
edgentConsole.printf(" Uptime: %dd %dh %dm %ds\n", days, hrs, mins, secs);
edgentConsole.printf(" Chip: %s rev %d\n", ESP.getChipModel(), ESP.getChipRevision());
edgentConsole.printf(" Flash: %dK\n", ESP.getFlashChipSize() / 1024);
edgentConsole.printf(" Stack unused: %d\n", uxTaskGetStackHighWaterMark(NULL));
edgentConsole.printf(" Heap free: %d / %d\n", ESP.getFreeHeap(), ESP.getHeapSize());
edgentConsole.printf(" max alloc: %d\n", ESP.getMaxAllocHeap());
edgentConsole.printf(" min free: %d\n", ESP.getMinFreeHeap());
if (ESP.getPsramSize()) {
edgentConsole.printf(" PSRAM free: %d / %d\n", ESP.getFreePsram(), ESP.getPsramSize());
}
#ifdef BLYNK_FS
uint32_t fs_total = BLYNK_FS.totalBytes();
edgentConsole.printf(" FS free: %d / %d\n", (fs_total-BLYNK_FS.usedBytes()), fs_total);
#endif
});
#ifdef BLYNK_FS
edgentConsole.addCommand("ls", [](int argc, const char** argv) {
const char* path = (argc < 1) ? "/" : argv[0];
File rootDir = BLYNK_FS.open(path);
while (File f = rootDir.openNextFile()) {
#if defined(BLYNK_USE_SPIFFS) && (ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 0, 0))
String fn = f.name();
#else
String fn = f.path();
#endif
MD5Builder md5;
md5.begin();
md5.addStream(f, f.size());
md5.calculate();
String md5str = md5.toString();
edgentConsole.printf("%8d %-24s %s\n",
f.size(), fn.c_str(),
md5str.substring(0,8).c_str());
}
});
edgentConsole.addCommand("rm", [](int argc, const char** argv) {
if (argc < 1) return;
for (int i=0; i<argc; i++) {
const char* fn = argv[i];
if (BLYNK_FS.remove(fn)) {
edgentConsole.printf("Removed %s\n", fn);
} else {
edgentConsole.printf("Removing %s failed\n", fn);
}
}
});
edgentConsole.addCommand("mv", [](int argc, const char** argv) {
if (argc != 2) return;
if (!BLYNK_FS.rename(argv[0], argv[1])) {
edgentConsole.print("Rename failed\n");
}
});
edgentConsole.addCommand("cat", [](int argc, const char** argv) {
if (argc != 1) return;
if (!BLYNK_FS.exists(argv[0])) {
edgentConsole.print("File not found\n");
return;
}
if (File f = BLYNK_FS.open(argv[0], FILE_READ)) {
while (f.available()) {
edgentConsole.print((char)f.read());
}
edgentConsole.print("\n");
} else {
edgentConsole.print("Cannot open file\n");
}
});
edgentConsole.addCommand("echo", [](int argc, const char** argv) {
if (argc != 2) return;
if (File f = BLYNK_FS.open(argv[1], FILE_WRITE)) {
if (!f.print(argv[0])) {
edgentConsole.print("Cannot write file\n");
}
} else {
edgentConsole.print("Cannot open file\n");
}
});
#endif
}
BLYNK_WRITE(InternalPinDBG) {
String cmd = String(param.asStr()) + "\n";
edgentConsole.runCommand((char*)cmd.c_str());
}