-
Notifications
You must be signed in to change notification settings - Fork 29
/
WiFi_nonBlocking.ino
100 lines (88 loc) · 2.32 KB
/
WiFi_nonBlocking.ino
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
/*by Me No Dev
@me-no-dev
Fev 02 10:11
*/
#include "WiFi.h"
#define STA_SSID "your-ssid"
#define STA_PASS "your-password"
#define AP_SSID "esp32"
static volatile bool wifi_connected = false;
void WiFiEvent(WiFiEvent_t event){
switch(event) {
case SYSTEM_EVENT_AP_START:
//can set ap hostname here
WiFi.softAPsetHostname(AP_SSID);
//enable ap ipv6 here
WiFi.softAPenableIpV6();
break;
case SYSTEM_EVENT_STA_START:
//set sta hostname here
WiFi.setHostname(AP_SSID);
break;
case SYSTEM_EVENT_STA_CONNECTED:
//enable sta ipv6 here
WiFi.enableIpV6();
break;
case SYSTEM_EVENT_AP_STA_GOT_IP6:
//both interfaces get the same event
Serial.print("STA IPv6: ");
Serial.println(WiFi.localIPv6());
Serial.print("AP IPv6: ");
Serial.println(WiFi.softAPIPv6());
break;
case SYSTEM_EVENT_STA_GOT_IP:
wifiOnConnect();
wifi_connected = true;
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
wifi_connected = false;
wifiOnDisconnect();
break;
default:
break;
}
}
void setup(){
Serial.begin(115200);
WiFi.onEvent(WiFiEvent);
WiFi.mode(WIFI_MODE_APSTA);
WiFi.softAP(AP_SSID);
WiFi.begin(STA_SSID, STA_PASS);
Serial.println("AP Started");
Serial.print("AP SSID: ");
Serial.println(AP_SSID);
Serial.print("AP IPv4: ");
Serial.println(WiFi.softAPIP());
}
void loop(){
if(wifi_connected){
wifiConnectedLoop();
} else {
wifiDisconnectedLoop();
}
}
//when wifi connects
void wifiOnConnect(){
Serial.println("STA Connected");
Serial.print("STA SSID: ");
Serial.println(WiFi.SSID());
Serial.print("STA IPv4: ");
Serial.println(WiFi.localIP());
Serial.print("STA IPv6: ");
Serial.println(WiFi.localIPv6());
}
//when wifi disconnects
void wifiOnDisconnect(){
Serial.println("STA Disconnected");
delay(1000);
WiFi.begin(STA_SSID, STA_PASS);
}
//while wifi is connected
void wifiConnectedLoop(){
Serial.print("RSSI: ");
Serial.println(WiFi.RSSI());
delay(1000);
}
//while wifi is not connected
void wifiDisconnectedLoop(){
}