forked from switchdoclabs/SDL_ESP32_OurWeather
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SDL_ESP32_BC24_GETIP.h
330 lines (266 loc) · 7.18 KB
/
SDL_ESP32_BC24_GETIP.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
//
// SDL_ESP_BC24_GETIP
// Collection of routines to get IP from AP Access Point
// SwitchDoc
// June 2018 Version 1.0
//
//
// 1) SmartConfig
// 3) WPS Button
// 4) Local AP
// definitions
// BC24 - define if you have a BC24 (that has an ESP32 on board)
// BC24DEBUG - define if you want debugging output
void WiFiEvent(WiFiEvent_t event, system_event_info_t info);
int WPSReconnectCount = 0; // for timeout on WPS
// SmartConfig
bool SmartConfigGetIP(long waitForStartSeconds, long secondsToWaitAfterStart)
{
bool myWiFiPresent;
myWiFiPresent = false;
// setup WiFi
//Init WiFi as Station, start SmartConfig
esp_err_t error_code = 0;
//WiFi.mode(WIFI_AP_STA);
WiFi.mode(WIFI_AP);
#ifdef BC24DEBUG
Serial.print("error_code=");
Serial.println(error_code);
#endif
WiFi.beginSmartConfig();
//Wait for SmartConfig packet from mobile
Serial.println("Waiting for SmartConfig.");
for (int i = 0; i < waitForStartSeconds; i++)
{
if (WiFi.smartConfigDone())
{
Serial.println("");
Serial.println("SmartConfig received.");
//Wait for WiFi to connect to AP
Serial.println("Waiting for SmartConfig WiFi Start");
for (int i = 0; i < secondsToWaitAfterStart ; i++)
{
if (WiFi.status() == WL_CONNECTED)
{
Serial.println("");
Serial.println("WiFI Connected.");
myWiFiPresent = true;
blinkLED(5, 300); // blink five OK!
break;
}
delay(500);
Serial.print(".");
//blinkLED(2, 300); // blink 2 in progress
myWiFiPresent = false;
#ifdef BC24
BC24ThreeBlink(White, 1000);
#else
vTaskDelay(1000 / portTICK_PERIOD_MS);
#endif
}
Serial.println();
break;
}
//delay(500);
Serial.print(".");
//blinkLED(2, 300); // blink 2 in progress
#ifdef BC24
BC24ThreeBlink(Blue, 1000);
#else
vTaskDelay(1000 / portTICK_PERIOD_MS);
#endif
}
if (myWiFiPresent == false)
{
esp_smartconfig_stop();
Serial.println("No Wifi Present from Smart Config");
#ifdef BC24
BC24ThreeBlink(Red, 1000);
#else
vTaskDelay(1000 / portTICK_PERIOD_MS);
#endif
}
else
{
Serial.println("Wifi Present from Smart Config");
}
return myWiFiPresent;
}
//
// WPS Button GET IP
//
bool WPSGetIP(long timeOutSeconds)
{
bool myWiFiPresent;
myWiFiPresent = false;
// set timeout
esp_err_t error_code = 0;
Serial.println("Now Try WPS Button");
WiFi.disconnect();
// Smart Config Failed - now try WPS Button
#ifdef BC24
BC24ThreeBlink(White, 1000);
#else
vTaskDelay(1000 / portTICK_PERIOD_MS);
#endif
long timeout;
timeout = millis() + timeOutSeconds * 1000;
WiFi.onEvent(WiFiEvent);
WiFi.mode(WIFI_MODE_STA);
Serial.print("error_code=");
Serial.println(error_code);
Serial.println("Starting WPS");
#ifdef BC24
BC24TwoBlink(White, 1000);
#else
vTaskDelay(1000 / portTICK_PERIOD_MS);
#endif
error_code = esp_wifi_wps_enable(&config);
#ifdef BC24DEBUG
Serial.print("error_code=");
Serial.println(error_code, HEX);
#endif
error_code = esp_wifi_wps_start(0);
#ifdef BC24DEBUG
Serial.print("error_code=");
Serial.println(error_code, HEX);
#endif
// attempt to connect to Wifi network:
while (WiFi.status() != WL_CONNECTED) {
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
#ifdef BC24
BC24TwoBlink(White, 1000);
#else
vTaskDelay(1000 / portTICK_PERIOD_MS);
#endif
Serial.print(".");
blinkLED(3, 300); // blink 3 in progress
if (timeout < millis())
{
break;
}
}
if (WiFi.status() != WL_CONNECTED)
{
esp_wifi_wps_disable();
Serial.println("WPS Failure");
myWiFiPresent = false;
#ifdef BC24
BC24TwoBlink(Red, 1000);
#else
vTaskDelay(1000 / portTICK_PERIOD_MS);
#endif
}
else
{
myWiFiPresent = true;
blinkLED(5, 300); // blink 5 OK
Serial.println("WPS Success - WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
#ifdef BC24
BC24TwoBlink(Green, 1000);
#else
vTaskDelay(1000 / portTICK_PERIOD_MS);
#endif
}
return myWiFiPresent;
}
//
// Use Local AP (192.168.4.1)
//
bool localAPGetIP(long apTimeOutSeconds)
{
bool myWiFiPresent;
myWiFiPresent = false;
// set up AP point for reading ssid/password since SmartConfig didn't work
// Set up Wifi
#define WL_MAC_ADDR_LENGTH 6
// Append the last two bytes of the MAC (HEX'd) to string to make unique
uint8_t mac[WL_MAC_ADDR_LENGTH];
WiFi.softAPmacAddress(mac);
String macID = String(mac[WL_MAC_ADDR_LENGTH - 2], HEX) +
String(mac[WL_MAC_ADDR_LENGTH - 1], HEX);
macID.toUpperCase();
APssid = "OurWeather-" + macID;
//WiFiManager
//Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wifiManager;
#ifdef OWDEBUG
wifiManager.setDebugOutput(true);
#else
wifiManager.setDebugOutput(false);
#endif
//reset saved settings
//wifiManager.resetSettings();
#ifdef BC24
BC24OneBlink(White, 1000);
xSemaphoreGive( xSemaphoreSingleBlink); // Turn on single blink
#else
vTaskDelay(1000 / portTICK_PERIOD_MS);
#endif
blinkLED(1, 300); // blink 1, ready to connect
//set callback that gets called when connecting to previous WiFi fails, and enters Access Point mode
wifiManager.setAPCallback(configModeCallback);
//fetches ssid and pass and tries to connect
//if it does not connect it starts an access point with the specified name
wifiManager.setTimeout(apTimeOutSeconds);
//and goes into a blocking loop awaiting configuration
if (!wifiManager.startConfigPortal(APssid.c_str())) {
Serial.println("failed to connect and hit timeout");
//reset and try again, or maybe put it to deep sleep
//ESP.reset();
//delay(1000);
myWiFiPresent = false;
blinkLED(3, 1000); // blink 3 long fial
}
if (WiFi.status() == WL_CONNECTED)
{
myWiFiPresent = true;
blinkLED(5, 300); // blink 5 OK
}
return myWiFiPresent;
}
// for WPS
void WiFiEvent(WiFiEvent_t event, system_event_info_t info) {
switch (event) {
case SYSTEM_EVENT_STA_START:
Serial.println("Station Mode Started");
break;
case SYSTEM_EVENT_STA_GOT_IP:
Serial.println("Connected to :" + String(WiFi.SSID()));
Serial.print("Got IP: ");
Serial.println(WiFi.localIP());
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
Serial.println("Disconnected from station, attempting reconnection");
if (WPSReconnectCount < 10)
{
WiFi.reconnect();
WPSReconnectCount++;
}
esp_wifi_wps_disable();
delay(10);
break;
case SYSTEM_EVENT_STA_WPS_ER_SUCCESS:
Serial.println("WPS Successful, stopping WPS and connecting to: " + String(WiFi.SSID()));
esp_wifi_wps_disable();
delay(10);
WiFi.begin();
break;
case SYSTEM_EVENT_STA_WPS_ER_FAILED:
Serial.println("WPS Failed");
esp_wifi_wps_disable();
//esp_wifi_wps_enable(&config);
//esp_wifi_wps_start(0);
break;
case SYSTEM_EVENT_STA_WPS_ER_TIMEOUT:
Serial.println("WPS Timeout");
esp_wifi_wps_disable();
//esp_wifi_wps_enable(&config);
//esp_wifi_wps_start(0);
break;
default:
break;
}
}