-
Notifications
You must be signed in to change notification settings - Fork 0
/
WiFi.cpp
103 lines (97 loc) · 2.51 KB
/
WiFi.cpp
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
#include "SmartClock.h"
short ConfigDoneFlag = 0;
void SaveConfigCallback()
{
ConfigDoneFlag = 1;
}
bool ConnectWiFi()
{
WiFiManager wm;
String SSID;
String Password;
// wm.resetSettings();
wm.setTitle("SmartClock");
wm.setBreakAfterConfig(true);
wm.setConfigPortalBlocking(false);
wm.setSaveConfigCallback(SaveConfigCallback);
if (!wm.getWiFiIsSaved())
{ //无已保存WiFi时(首次开机)
ConfigDoneFlag = 0;
ShowWiFiConfigInfo();
wm.startConfigPortal("SmartClock");
wm.startWebPortal();
while (ConfigDoneFlag == 0)
{
wm.process();
if (Shaking() == IS_SHAKING)
{
wm.stopConfigPortal();
wm.stopWebPortal();
return FstConnectWiFiErr;
}
}
if (WiFi.status() == WL_CONNECTED)
{
ShowWiFiConfigSuccess();
delay(2000);
ESP.reset();
return FstConnectWiFiDone;
}
else
{
ShowWiFiConnectedErr();
delay(3000);
return FstConnectWiFiErr;
}
}
else
{
short UCWiFiTimes = 0;
SSID = wm.getWiFiSSID();
Password = wm.getWiFiPass();
WiFi.begin(SSID, Password);
while (WiFi.status() != WL_CONNECTED && UCWiFiTimes <= 40)
{
if (WiFi.status() == WL_WRONG_PASSWORD || WiFi.status() == WL_NO_SSID_AVAIL)
{
break;
}
UCWiFiTimes++;
delay(1000);
}
if (WiFi.status() == WL_CONNECTED)
{
return FstConnectWiFiDone;
}
else
{
ConfigDoneFlag = 0;
ShowWiFiConfigInfo();
wm.startConfigPortal("SmartClock");
wm.startWebPortal();
while (ConfigDoneFlag == 0)
{
wm.process();
if (Shaking() == IS_SHAKING)
{
wm.stopConfigPortal();
wm.stopWebPortal();
return FstConnectWiFiErr;
}
}
if (WiFi.status() == WL_CONNECTED)
{
ShowWiFiConfigSuccess();
delay(2000);
ESP.reset();
return FstConnectWiFiDone;
}
else
{
ShowWiFiConnectedErr();
delay(3000);
return FstConnectWiFiErr;
}
}
}
}