-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwifi_funcs.h
544 lines (488 loc) · 19.3 KB
/
wifi_funcs.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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
#ifndef wifi_funcs_h
#define wifi_funcs_h
#include <creds.h>
#ifdef ESP8266
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#define WIFI_getChipId() ESP.getChipId()
#elif defined(ESP32)
#include <rom/rtc.h>
#include <WiFi.h>
#include <esp_wifi.h>
#include <ESPmDNS.h>
#define WIFI_getChipId() (uint32_t)ESP.getEfuseMac()
#endif
// const char* hostname = "esp8266REFLOW";
bool debug_wifi = true;
bool rebootAfterDowntime = true;
long downtimeRestart = 1*60000; // millis
long downms = 0;
uint8_t _lastrssiperc = 0; // store rssi
/** IP to String? */
String toStringIp(IPAddress ip) {
String res = "";
for (int i = 0; i < 3; i++) {
res += String((ip >> (8 * i)) & 0xFF) + ".";
}
res += String(((ip >> 8 * 3)) & 0xFF);
return res;
}
String WiFi_SSID(bool persistent) {
persistent = true;
#ifdef ESP8266
struct station_config conf;
if(persistent) wifi_station_get_config_default(&conf);
else wifi_station_get_config(&conf);
char tmp[33]; //ssid can be up to 32chars, => plus null term
memcpy(tmp, conf.ssid, sizeof(conf.ssid));
tmp[32] = 0; //nullterm in case of 32 char ssid
return String(reinterpret_cast<char*>(tmp));
#elif defined(ESP32)
if(persistent){
wifi_config_t conf;
esp_wifi_get_config(WIFI_IF_STA, &conf);
return String(reinterpret_cast<const char*>(conf.sta.ssid));
}
else {
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
return String();
}
wifi_ap_record_t info;
if(!esp_wifi_sta_get_ap_info(&info)) {
return String(reinterpret_cast<char*>(info.ssid));
}
return String();
}
#endif
}
String WiFi_psk(bool persistent) {
persistent = true;
#ifdef ESP8266
struct station_config conf;
if(persistent) wifi_station_get_config_default(&conf);
else wifi_station_get_config(&conf);
char tmp[65]; //psk is 64 bytes hex => plus null term
memcpy(tmp, conf.password, sizeof(conf.password));
tmp[64] = 0; //null term in case of 64 byte psk
return String(reinterpret_cast<char*>(tmp));
#elif defined(ESP32)
// only if wifi is init
if(WiFiGenericClass::getMode() == WIFI_MODE_NULL){
return String();
}
wifi_config_t conf;
esp_wifi_get_config(WIFI_IF_STA, &conf);
return String(reinterpret_cast<char*>(conf.sta.password));
#endif
}
bool wifiIsAutoConnect(){
return WiFi_SSID(true) != "";
}
bool wifiIsConnected(){
return WiFi.status() == WL_CONNECTED;
}
void setWiFiFastConnect(){
//fast scan
// set channel
// set bssid
// WiFi.begin(SSID,PASS,WiFi.channel(),WiFi.BSSID(),true);
}
String getDeviecID(){
String _wifissidprefix = "ESP";
String hostString = String(WIFI_getChipId(),HEX);
hostString.toUpperCase();
// char hostString[16] = {0};
// sprintf(hostString, "%06X", ESP.getChipId());
return _wifissidprefix + "_" + hostString;
}
void setWiFiHostname(const char* hostname){
// @todo add string templ
#ifdef ESP32
WiFi.setHostname(hostname);
#else
MDNS.begin(hostname);
WiFi.hostname(hostname);
#endif
}
String getHostname(){
#ifdef ESP32
return WiFi.getHostname(); // getHostName ( @todo return string or c.str?)
#else
return WiFi.hostname(); // getHostName
#endif
}
void WiFi_print_sta(){
if(wifiIsConnected()){
Serial.println("[WIFI] CONNECTED");
Serial.print("[WIFI] IP: ");
Serial.println(WiFi.localIP());
Serial.print("[WIFI] HOST: ");
Serial.println(getHostname());
Serial.print("[WIFI] BSSID: ");
Serial.println(WiFi.BSSIDstr());
Serial.print("[WIFI] RSSI: ");
Serial.println(WiFi.RSSI());
Serial.print("[WIFI] CHANNEL: ");
Serial.println(WiFi.channel());
} else {
Serial.println("[WIFI] NOT CONNECTED");
}
}
// enable wifi sta
// disable sleep
// timeout connect
// set hostname ?
void init_WiFi(int timeout){
// if(wifiIsConnected()){
// WiFi_print_sta();
// return;
// }
unsigned long start = millis();
Serial.println("[WIFI] mode STA");
WiFi.mode(WIFI_STA);
if(debug_wifi) WiFi.printDiag(Serial);
#ifdef ESP8266
WiFi.setSleepMode(WIFI_NONE_SLEEP);
#elif defined(ESP32)
// btStop();
WiFi.setSleep(false);
#endif
//
// WiFi.hostname(hostname);
// if(wifiIsAutoConnect) WiFi.begin();
// esp_wifi_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_11B| WIFI_PROTOCOL_11G|WIFI_PROTOCOL_11N|WIFI_PROTOCOL_LR);
WiFi.begin(SSID,PASS);
if(timeout > 0){
// use local timer loop
if(debug_wifi) Serial.println("[WIFI] Connecting to wifi, wait for timeout... [" + (String)timeout + " ms]\n");
while((WiFi.status() != WL_CONNECTED) && (millis()-start < timeout)){
if(debug_wifi) Serial.print(".");
delay(100);
}
}
else {
// waitForConnecrtResult default 60000
if(debug_wifi) Serial.println("[WIFI] Connecting to wifi, waitForConnectResult waiting..... ");
uint8_t status = WiFi.waitForConnectResult();
// while(status() != WL_CONNECTED){
// Serial.print(".");
// delay(500);
Serial.println((String)WiFi.status());
// }
}
if(debug_wifi) Serial.println("");
if(wifiIsConnected()){
setWiFiFastConnect();
Serial.println("[WIFI] connected in " + (String)(millis()-start/1000) + " ms");
if(debug_wifi) WiFi_print_sta();
}
else{
Serial.println("[ERROR] WIFI CONNECT FAILED");
Serial.println("[WIFI] waited for " + (String)(millis()-start/1000) + " ms");
// delay(1000);
// WiFi.begin(SSID,PASS);
}
delay(500);
}
void init_WiFi(){
init_WiFi(0);
}
void init_WiFi_saved(){
WiFi.begin();
}
uint8_t getRSSIasQuality(int8_t RSSI) {
uint8_t quality = 0;
if (RSSI <= -100) {
quality = 0;
} else if (RSSI >= -50) {
quality = 100;
} else {
quality = 2 * (RSSI + 100);
}
_lastrssiperc = quality;
return quality;
}
uint8_t getRSSIasQuality() {
return getRSSIasQuality(WiFi.RSSI());
}
/**
* [checkWifi description]
* @param restart [description]
*/
void checkWifi(bool recon = true, bool restart = false){
Serial.println("[TASK] checkWiFi");
if(WiFi.status() != WL_CONNECTED ){
if(downms == 0) downms = millis();
if(millis() > downms + downtimeRestart){
if(restart){
// reboot
#ifdef USENEOIND
indSetColor(np_red);
#endif
Serial.println("[ERROR] wifi not found, rebooting after timeout");
Serial.flush();
delay(1000);
ESP.restart();
}
else{
// reconnect
#ifdef USENEOIND
indSetColor(np_red);
#endif
Serial.println("[WIFI] WiFi is Disconnected for " + (String)(millis()-downms));
downms = millis();
if(recon) WiFi.reconnect();
}
}
}
else {
_lastrssiperc = getRSSIasQuality();
if(debug_wifi){
Serial.println("[WIFI] WiFi is CONNECTED");
Serial.println("[WIFI] RSSI: "+(String)_lastrssiperc);
}
#ifdef USENEOIND
indSetColor(np_green);
#endif
}
}
void checkWifi(){
checkWifi(true,false);
}
void enableWiFi(){
WiFi.mode(WIFI_STA);
init_WiFi(0);
}
void disableWiFi(){
WiFi.mode(WIFI_OFF);
#ifdef ESP32
WiFi.mode( WIFI_MODE_NULL );
btStop();
#endif
}
// uint32_t ResetReason(void)
// {
// user_interface.h
// REASON_DEFAULT_RST = 0, // "Power on" normal startup by power on
// REASON_WDT_RST = 1, // "Hardware Watchdog" hardware watch dog reset
// REASON_EXCEPTION_RST = 2, // "Exception" exception reset, GPIO status won’t change
// REASON_SOFT_WDT_RST = 3, // "Software Watchdog" software watch dog reset, GPIO status won’t change
// REASON_SOFT_RESTART = 4, // "Software/System restart" software restart ,system_restart , GPIO status won’t change
// REASON_DEEP_SLEEP_AWAKE = 5, // "Deep-Sleep Wake" wake up from deep-sleep
// REASON_EXT_SYS_RST = 6 // "External System" external system reset
// return ESP_ResetInfoReason();
// }
// if (resetInfo.reason == REASON_DEFAULT_RST) { // normal startup by power on
// strcpy_P(buff, PSTR("Power on"));
// } else if (resetInfo.reason == REASON_WDT_RST) { // hardware watch dog reset
// strcpy_P(buff, PSTR("Hardware Watchdog"));
// } else if (resetInfo.reason == REASON_EXCEPTION_RST) { // exception reset, GPIO status won’t change
// strcpy_P(buff, PSTR("Exception"));
// } else if (resetInfo.reason == REASON_SOFT_WDT_RST) { // software watch dog reset, GPIO status won’t change
// strcpy_P(buff, PSTR("Software Watchdog"));
// } else if (resetInfo.reason == REASON_SOFT_RESTART) { // software restart ,system_restart , GPIO status won’t change
// strcpy_P(buff, PSTR("Software/System restart"));
// } else if (resetInfo.reason == REASON_DEEP_SLEEP_AWAKE) { // wake up from deep-sleep
// strcpy_P(buff, PSTR("Deep-Sleep Wake"));
// } else if (resetInfo.reason == REASON_EXT_SYS_RST) { // external system reset
// strcpy_P(buff, PSTR("External System"));
// } else {
// strcpy_P(buff, PSTR("Unknown"));
// #ifdef ESP32
// String ESP32GetResetReason(uint32_t cpu_no) {
// // tools\sdk\include\esp32\rom\rtc.h
// switch (rtc_get_reset_reason( (RESET_REASON) cpu_no)) {
// case POWERON_RESET : return F("Vbat power on reset"); // 1
// case SW_RESET : return F("Software reset digital core"); // 3
// case OWDT_RESET : return F("Legacy watch dog reset digital core"); // 4
// case DEEPSLEEP_RESET : return F("Deep Sleep reset digital core"); // 5
// case SDIO_RESET : return F("Reset by SLC module, reset digital core"); // 6
// case TG0WDT_SYS_RESET : return F("Timer Group0 Watch dog reset digital core"); // 7
// case TG1WDT_SYS_RESET : return F("Timer Group1 Watch dog reset digital core"); // 8
// case RTCWDT_SYS_RESET : return F("RTC Watch dog Reset digital core"); // 9
// case INTRUSION_RESET : return F("Instrusion tested to reset CPU"); // 10
// case TGWDT_CPU_RESET : return F("Time Group reset CPU"); // 11
// case SW_CPU_RESET : return F("Software reset CPU"); // 12
// case RTCWDT_CPU_RESET : return F("RTC Watch dog Reset CPU"); // 13
// case EXT_CPU_RESET : return F("or APP CPU, reseted by PRO CPU"); // 14
// case RTCWDT_BROWN_OUT_RESET : return F("Reset when the vdd voltage is not stable"); // 15
// case RTCWDT_RTC_RESET : return F("RTC Watch dog reset digital core and rtc module"); // 16
// default : return F("NO_MEAN"); // 0
// }
// }
// #endif
// void esp32_resetreason(RESET_REASON reason)
// {
// switch ( reason)
// {
// case 1 : Serial.println ("Vbat power on reset");break;
// case 3 : Serial.println ("Software reset digital core");break;
// case 4 : Serial.println ("Legacy watch dog reset digital core");break;
// case 5 : Serial.println ("Deep Sleep reset digital core");break;
// case 6 : Serial.println ("Reset by SLC module, reset digital core");break;
// case 7 : Serial.println ("Timer Group0 Watch dog reset digital core");break;
// case 8 : Serial.println ("Timer Group1 Watch dog reset digital core");break;
// case 9 : Serial.println ("RTC Watch dog Reset digital core");break;
// case 10 : Serial.println ("Instrusion tested to reset CPU");break;
// case 11 : Serial.println ("Time Group reset CPU");break;
// case 12 : Serial.println ("Software reset CPU");break;
// case 13 : Serial.println ("RTC Watch dog Reset CPU");break;
// case 14 : Serial.println ("for APP CPU, reseted by PRO CPU");break;
// case 15 : Serial.println ("Reset when the vdd voltage is not stable");break;
// case 16 : Serial.println ("RTC Watch dog reset digital core and rtc module");break;
// default : Serial.println ("NO_MEAN");
// }
// }
// String ESP_getResetReason(void) {
// return ESP32GetResetReason(0); // CPU 0
// }
// uint32_t ESP_ResetInfoReason(void) {
// RESET_REASON reason = rtc_get_reset_reason(0);
// if (POWERON_RESET == reason) { return REASON_DEFAULT_RST; }
// if (SW_CPU_RESET == reason) { return REASON_SOFT_RESTART; }
// if (DEEPSLEEP_RESET == reason) { return REASON_DEEP_SLEEP_AWAKE; }
// if (SW_RESET == reason) { return REASON_EXT_SYS_RST; }
// }
// esp_wifi_get_mac((wifi_interface_t)interface, mac);
// sprintf(winstance, "%s [%02x:%02x:%02x:%02x:%02x:%02x]", _hostname.c_str(), mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
// esp_wifi_get_mac((wifi_interface_t)WIFI_IF_STA, eth_mac);
// snprintf(default_hostname, 32, "%s%02X%02X%02X", CONFIG_IDF_TARGET "-", eth_mac[3], eth_mac[4], eth_mac[5]);
// const char * const WIFI_STA_STATUS[] PROGMEM
// {
// "WL_IDLE_STATUS", // 0 STATION_IDLE
// "WL_NO_SSID_AVAIL", // 1 STATION_NO_AP_FOUND
// "WL_SCAN_COMPLETED", // 2
// "WL_CONNECTED", // 3 STATION_GOT_IP
// "WL_CONNECT_FAILED", // 4 STATION_CONNECT_FAIL, STATION_WRONG_PASSWORD(NI)
// "WL_CONNECTION_LOST", // 5
// "WL_DISCONNECTED", // 6
// "WL_STATION_WRONG_PASSWORD" // 7 KLUDGE
// };
// #ifdef ESP32
// const char * const AUTH_MODE_NAMES[] PROGMEM
// {
// "OPEN",
// "WEP",
// "WPA_PSK",
// "WPA2_PSK",
// "WPA_WPA2_PSK",
// "WPA2_ENTERPRISE",
// "MAX"
// };
// #elif defined(ESP8266)
// const char * const AUTH_MODE_NAMES[] PROGMEM
// {
// "",
// "",
// "WPA_PSK", // 2 ENC_TYPE_TKIP
// "",
// "WPA2_PSK", // 4 ENC_TYPE_CCMP
// "WEP", // 5 ENC_TYPE_WEP
// "",
// "OPEN", //7 ENC_TYPE_NONE
// "WPA_WPA2_PSK", // 8 ENC_TYPE_AUTO
// };
// #endif
// const char* const WIFI_MODES[] PROGMEM = { "NULL", "STA", "AP", "STA+AP" };
// typedef enum {
// NO_MEAN = 0,
// POWERON_RESET = 1, /**<1, Vbat power on reset*/
// RTC_SW_SYS_RESET = 3, /**<3, Software reset digital core*/
// DEEPSLEEP_RESET = 5, /**<5, Deep Sleep reset digital core*/
// TG0WDT_SYS_RESET = 7, /**<7, Timer Group0 Watch dog reset digital core*/
// TG1WDT_SYS_RESET = 8, /**<8, Timer Group1 Watch dog reset digital core*/
// RTCWDT_SYS_RESET = 9, /**<9, RTC Watch dog Reset digital core*/
// INTRUSION_RESET = 10, /**<10, Instrusion tested to reset CPU*/
// TG0WDT_CPU_RESET = 11, /**<11, Time Group0 reset CPU*/
// RTC_SW_CPU_RESET = 12, /**<12, Software reset CPU*/
// RTCWDT_CPU_RESET = 13, /**<13, RTC Watch dog Reset CPU*/
// RTCWDT_BROWN_OUT_RESET = 15, /**<15, Reset when the vdd voltage is not stable*/
// RTCWDT_RTC_RESET = 16, /**<16, RTC Watch dog reset digital core and rtc module*/
// TG1WDT_CPU_RESET = 17, /**<17, Time Group1 reset CPU*/
// SUPER_WDT_RESET = 18, /**<18, super watchdog reset digital core and rtc module*/
// GLITCH_RTC_RESET = 19, /**<19, glitch reset digital core and rtc module*/
// } RESET_REASON;
const char * const RESET_REASON_STR[] PROGMEM
{
"NO_MEAN" ,
"POWERON_RESET" , /**<1, Vbat power on reset*/
"",
"RTC_SW_SYS_RESET" , /**<3, Software reset digital core*/
"",
"DEEPSLEEP_RESET" , /**<5, Deep Sleep reset digital core*/
"",
"TG0WDT_SYS_RESET" , /**<7, Timer Group0 Watch dog reset digital core*/
"TG1WDT_SYS_RESET" , /**<8, Timer Group1 Watch dog reset digital core*/
"RTCWDT_SYS_RESET" , /**<9, RTC Watch dog Reset digital core*/
"INTRUSION_RESET" , /**<10, Instrusion tested to reset CPU*/
"TG0WDT_CPU_RESET" , /**<11, Time Group0 reset CPU*/
"RTC_SW_CPU_RESET" , /**<12, Software reset CPU*/
"RTCWDT_CPU_RESET" , /**<13, RTC Watch dog Reset CPU*/
"",
"RTCWDT_BROWN_OUT_RESET" , /**<15, Reset when the vdd voltage is not stable*/
"RTCWDT_RTC_RESET" , /**<16, RTC Watch dog reset digital core and rtc module*/
"TG1WDT_CPU_RESET" , /**<17, Time Group1 reset CPU*/
"SUPER_WDT_RESET" , /**<18, super watchdog reset digital core and rtc module*/
"GLITCH_RTC_RESET" /**<19, glitch reset digital core and rtc module*/
};
// typedef enum {
// NO_MEAN = "NO_MEAN",
// POWERON_RESET = "Vbat power on reset",
// RTC_SW_SYS_RESET = "Software reset digital core",
// DEEPSLEEP_RESET = "Deep Sleep reset digital core",
// TG0WDT_SYS_RESET = "Timer Group0 Watch dog reset digital core",
// TG1WDT_SYS_RESET = "Timer Group1 Watch dog reset digital core",
// RTCWDT_SYS_RESET = "RTC Watch dog Reset digital core",
// INTRUSION_RESET = "Instrusion tested to reset CPU",
// TG0WDT_CPU_RESET = "Time Group0 reset CPU",
// RTC_SW_CPU_RESET = "Software reset CPU",
// RTCWDT_CPU_RESET = "RTC Watch dog Reset CPU",
// RTCWDT_BROWN_OUT_RESET = "Reset when the vdd voltage is not stable",
// RTCWDT_RTC_RESET = "RTC Watch dog reset digital core and rtc module",
// TG1WDT_CPU_RESET = "Time Group1 reset CPU",
// SUPER_WDT_RESET = "super watchdog reset digital core and rtc module",
// GLITCH_RTC_RESET = "glitch reset digital core and rtc module"
// } RESET_REASON_STR_v;
void verbose_print_reset_reason(int reason)
{
switch ( reason)
{
case 1 : Serial.println ("Vbat power on reset");break;
case 3 : Serial.println ("Software reset digital core");break;
case 4 : Serial.println ("Legacy watch dog reset digital core");break;
case 5 : Serial.println ("Deep Sleep reset digital core");break;
case 6 : Serial.println ("Reset by SLC module, reset digital core");break;
case 7 : Serial.println ("Timer Group0 Watch dog reset digital core");break;
case 8 : Serial.println ("Timer Group1 Watch dog reset digital core");break;
case 9 : Serial.println ("RTC Watch dog Reset digital core");break;
case 10 : Serial.println ("Instrusion tested to reset CPU");break;
case 11 : Serial.println ("Time Group reset CPU");break;
case 12 : Serial.println ("Software reset CPU");break;
case 13 : Serial.println ("RTC Watch dog Reset CPU");break;
case 14 : Serial.println ("for APP CPU, reseted by PRO CPU");break;
case 15 : Serial.println ("Reset when the vdd voltage is not stable");break;
case 16 : Serial.println ("RTC Watch dog reset digital core and rtc module");break;
default : Serial.println ("NO_MEAN");
}
}
// @todo
String getResetReason(uint8_t cpu = 0){
int reason;
#ifdef ESP8266
return ESP.getResetReason();
#elif defined(ESP32)
// && defined(_ROM_RTC_H_)
// requires #include <esp32/rom/rtc.h>
Serial.print("[RESET]");verbose_print_reset_reason(rtc_get_reset_reason(cpu));
return RESET_REASON_STR[rtc_get_reset_reason(cpu)];
// return "NA";
#else
return (String)rtc_get_reset_reason(cpu) + " - UNKNOWN";
#endif
}
void processWiFi(){
#ifdef ESP8266
MDNS.update();
#endif
}
#endif