/
WiFi.ino
178 lines (155 loc) · 4.59 KB
/
WiFi.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
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
String WifiGetAPssid() {
String ssid = "ACC_";
ssid += String(chipMAC);
return (ssid);
}
//********************************************************************************
// Set Wifi AP Mode config
//********************************************************************************
void WifiAPconfig() {
// Disabled softAPConfig as a workaround for this issue:
// https://github.com/espressif/arduino-esp32/issues/2025
// WiFi.softAPConfig(apIP, apGW, apNM);
delay(100);
WiFi.softAP(WifiGetAPssid().c_str(), DEFAULT_PASSWORD);
delay(100);
// We start in AP mode
WifiAPMode(true);
String log("WIFI : AP Mode SSID will be ");
log = log + WifiGetAPssid();
log = log + F(" with address ");
log = log + apIP.toString();
addLog(LOG_LEVEL_INFO, log);
}
//********************************************************************************
// Set Wifi AP Mode
//********************************************************************************
void WifiAPMode(boolean state) {
if (WifiIsAP()) {
//want to disable?
if (!state) {
WiFi.mode(WIFI_STA);
addLog(LOG_LEVEL_INFO, F("WIFI : AP Mode disabled"));
} else {
addLog(LOG_LEVEL_INFO, F("WIFI : AP Mode already enabled"));
}
} else {
//want to enable?
if (state) {
WiFi.mode(WIFI_AP_STA);
addLog(LOG_LEVEL_INFO, F("WIFI : AP Mode enabled"));
}
}
}
bool WifiIsAP() {
byte wifimode = WiFi.getMode();
return (wifimode == 2 || wifimode == 3); //apmode is enabled
}
//********************************************************************************
// Configure network and connect to Wifi SSID and SSID2
//********************************************************************************
boolean WifiConnect(byte connectAttempts) {
String log = "";
char hostname[40];
strncpy(hostname, WifiGetAPssid().c_str(), sizeof(hostname));
WiFi.setHostname(hostname);
//try to connect to the ap
if (WifiConnectSSID(SecuritySettings.WifiSSID, SecuritySettings.WifiKey, connectAttempts)) {
nextSyncTime = sysTime;
now();
return (true);
}
addLog(LOG_LEVEL_ERROR, F("WIFI : Could not connect to AP!"));
// Unable to connect to wifi. Enable soft AP.
WifiAPMode(true);
return (false);
}
//********************************************************************************
// Connect to Wifi specific SSID
//********************************************************************************
boolean WifiConnectSSID(char WifiSSID[], char WifiKey[], byte connectAttempts)
{
String log;
//already connected, need to disconnect first
if (WiFi.status() == WL_CONNECTED)
return (true);
//no ssid specified
if ((WifiSSID[0] == 0) || (strcasecmp(WifiSSID, "ssid") == 0))
return (false);
for (byte tryConnect = 1; tryConnect <= connectAttempts; tryConnect++)
{
log = F("WIFI : Connecting ");
log += WifiSSID;
log += F(" attempt #");
log += tryConnect;
addLog(LOG_LEVEL_INFO, log);
if (tryConnect == 1)
WiFi.begin(WifiSSID, WifiKey);
else
WiFi.begin();
//wait until it connects
for (byte x = 0; x < 200; x++)
{
if (WiFi.status() != WL_CONNECTED)
{
statusLED(false);
delay(50);
}
else
break;
}
if (WiFi.status() == WL_CONNECTED)
{
// if (Settings.UseNTP) {
// initTime();
// }
log = F("WIFI : Connected! IP: ");
log += formatIP(WiFi.localIP());
log += F(" (");
log += WifiGetAPssid();
log += F(")");
addLog(LOG_LEVEL_INFO, log);
statusLED(true);
return (true);
}
else
{
// log = F("WIFI : Disconnecting!");
// addLog(LOG_LEVEL_INFO, log);
for (byte x = 0; x < 20; x++)
{
statusLED(true);
delay(50);
}
}
}
return false;
}
int getWiFiStrength(int points){
long rssi = 0;
long averageRSSI=0;
for (int i=0;i < points;i++){
rssi += WiFi.RSSI();
delay(20);
}
averageRSSI=rssi/points;
return averageRSSI;
}
void updateAPstatus() {
// turn off WiFi AP when timeout is reached
if (timerAPoff != 0 && timeOutReached(timerAPoff)) {
addLog(LOG_LEVEL_INFO, "WIFI : AP timeout reached");
timerAPoff = 0;
WifiAPMode(false);
}
// not connected, set timerAPoff to 10 seconds
if(WiFi.status() != WL_CONNECTED && timerAPoff != 0) {
timerAPoff = millis() + 10000L;
}
// trun on WiFi AP if connection is lost
if(WiFi.status() != WL_CONNECTED && !WifiIsAP()) {
timerAPoff = millis() + 10000L;
addLog(LOG_LEVEL_DEBUG, "WIFI : Connection lost, switching on WiFi AP");
WifiAPMode(true);
}
}