forked from pvtex/esp32-rfid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwifi.esp
240 lines (223 loc) · 5.6 KB
/
wifi.esp
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
void setEnableWifi()
{
doEnableWifi = true;
}
void onWifiConnect(WiFiEvent_t event, WiFiEventInfo_t inf0)
{
#ifdef DEBUG
Serial.println(F("[ INFO ] WiFi STA Connected"));
#endif
mqttReconnectTimer.detach();
if (!wifiReconnectTimer.active() && !config.fallbackMode)
{
wifiReconnectTimer.once(300, setEnableWifi);
}
ledWifiOff();
}
void onWifiDisconnect(WiFiEvent_t event, WiFiEventInfo_t inf0)
{
if (!WiFi.isConnected())
{
return;
}
#ifdef DEBUG
Serial.println(F("[ INFO ] WiFi STA Disconnected"));
#endif
mqttReconnectTimer.detach();
disconnectMqtt();
if (!wifiReconnectTimer.active() && !config.fallbackMode)
{
wifiReconnectTimer.once(300, setEnableWifi);
}
ledWifiOff();
}
void onWifiGotIP(WiFiEvent_t event, WiFiEventInfo_t info)
{
#ifdef DEBUG
Serial.print("[ INFO ] WiFi IP Connected: ");
Serial.println(WiFi.localIP().toString());
#endif
wifiReconnectTimer.detach();
ledWifiOn();
connectToMqtt();
}
bool startAP(IPAddress apip, IPAddress apsubnet, bool hidden, const char *ssid, const char *password = NULL)
{
#ifdef DEBUG
Serial.println(F("[ INFO ] ESP32-ACTL is running in AP Mode "));
#endif
WiFi.mode(WIFI_AP);
#ifdef DEBUG
Serial.print(F("[ INFO ] Configuring access point... "));
#endif
WiFi.softAPConfig(apip, apip, apsubnet);
bool success;
if (hidden)
{
success = WiFi.softAP(ssid, password, 3, true);
}
else
{
success = WiFi.softAP(ssid, password);
}
#ifdef DEBUG
Serial.println(success ? F("Ready") : F("Failed!"));
#endif
if (success)
{
ledWifiOn();
}
#ifdef DEBUG
IPAddress myIP = WiFi.softAPIP();
Serial.print(F("[ INFO ] AP IP address: "));
Serial.println(myIP);
Serial.printf("[ INFO ] AP SSID: %s\n", ssid);
#endif
return success;
}
// Fallback to AP Mode, so we can connect to ESP if there is no Internet connection
void fallbacktoAPMode()
{
config.accessPointMode = true;
#ifdef DEBUG
Serial.println(F("[ INFO ] ESP32-ACTL is running in Fallback AP Mode"));
#endif
WiFi.mode(WIFI_AP);
uint8_t macAddr[6];
WiFi.softAPmacAddress(macAddr);
char ssid[18];
sprintf(ssid, "ESP32-ACTL-%02x%02x%02x", macAddr[3], macAddr[4], macAddr[5]);
if (WiFi.softAP(ssid))
{
ledWifiOn();
#ifdef DEBUG
IPAddress myIP = WiFi.softAPIP();
Serial.print(F("[ INFO ] AP IP address: "));
Serial.println(myIP);
Serial.printf("[ INFO ] AP SSID: %s\n", ssid);
#endif
}
}
// Try to connect Wi-Fi
bool connectSTA(const char *ssid, const char *password, byte bssid[6])
{
bool useBSSID = false;
WiFi.mode(WIFI_STA);
WiFi.persistent(false);
if (!config.dhcpEnabled)
{
WiFi.config(config.ipAddress, config.gatewayIp, config.subnetIp, config.dnsIp);
}
#ifdef DEBUG
Serial.print(F("[ INFO ] Trying to connect WiFi: "));
Serial.println(ssid);
Serial.print(F("[ INFO ] WiFi BSSID: "));
#endif
for (int i = 0; i < 6; i++)
{
#ifdef DEBUG
Serial.printf("%02X", bssid[i]);
if (i < 5)
Serial.print(F(":"));
else
Serial.println();
#endif
if (bssid[i] != 0)
useBSSID = true;
}
if (useBSSID)
{
#ifdef DEBUG
Serial.println(F("[ INFO ] BSSID locked"));
#endif
WiFi.begin(ssid, password, 0, bssid);
}
else
{
#ifdef DEBUG
Serial.println(F("[ INFO ] any BSSID"));
#endif
WiFi.begin(ssid, password);
}
unsigned long now = millis();
uint8_t timeout = 15; // define when to time out in seconds
do
{
ledWifiStatus();
delay(500);
#ifdef DEBUG
if (!WiFi.isConnected())
Serial.print(F("."));
#endif
if (WiFi.isConnected())
break;
} while (millis() - now < timeout * 1000);
// We now out of the while loop, either time is out or we connected. check what happened
if (WiFi.isConnected())
{
String data = ssid;
data += " " + WiFi.localIP().toString();
writeEvent("INFO", "wifi", "WiFi is connected", data);
#ifdef DEBUG
Serial.println(F("[ INFO ] WiFi is connected"));
#endif
return true;
}
else
{
#ifdef DEBUG
Serial.println();
Serial.println(F("[ WARN ] Couldn't connect in time"));
#endif
if (!config.fallbackMode)
{
#ifdef DEBUG
Serial.println();
Serial.println(F("[ INFO ] trying to reconnect to WiFi"));
#endif
wifiReconnectTimer.once(300, setEnableWifi);
}
return false;
}
}
void disableWifi()
{
wiFiUptimeMillis = 0;
WiFi.disconnect(true);
WiFi.softAPdisconnect(true);
#ifdef DEBUG
Serial.println(F("Turn wifi off."));
#endif
}
void enableWifi()
{
wiFiUptimeMillis = 0;
if (config.accessPointMode)
{
startAP(config.accessPointIp, config.accessPointSubnetIp, config.networkHidden, config.ssid, config.wifiPassword);
}
else
{
bool connected = connectSTA(config.ssid, config.wifiPassword, config.bssid);
if (!connected && config.fallbackMode)
{
fallbacktoAPMode();
}
}
}
void setupWifi(bool configured)
{
if (!configured)
{
WiFi.hostname(config.deviceHostname);
fallbacktoAPMode();
}
else
{
WiFi.onEvent(onWifiConnect, WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_CONNECTED);
WiFi.onEvent(onWifiDisconnect, WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_DISCONNECTED);
WiFi.onEvent(onWifiGotIP, WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_GOT_IP);
WiFi.hostname(config.deviceHostname);
enableWifi();
}
}