main/task_wifi.c

changeset 53
cf91a3a20d0d
parent 52
4b5b28b0ad42
child 54
7b134c27fadb
equal deleted inserted replaced
52:4b5b28b0ad42 53:cf91a3a20d0d
16 uint16_t ap_num = MAX_AP_NUM; ///< Scan counter. 16 uint16_t ap_num = MAX_AP_NUM; ///< Scan counter.
17 wifi_ap_record_t *accessp_records; ///< [MAX_AP_NUM] records array with scan results. 17 wifi_ap_record_t *accessp_records; ///< [MAX_AP_NUM] records array with scan results.
18 wifi_config_t *task_wifi_ConfigSTA = NULL; ///< Current STA configuration. 18 wifi_config_t *task_wifi_ConfigSTA = NULL; ///< Current STA configuration.
19 WIFI_State *wifi_state = NULL; ///< Public state for other tasks. 19 WIFI_State *wifi_state = NULL; ///< Public state for other tasks.
20 20
21 wifi_scan_config_t scan_config = { ///< WiFi scanner configuration.
22 .ssid = 0,
23 .bssid = 0,
24 .channel = 0,
25 .show_hidden = false
26 };
21 27
22 uint8_t _wifi_ssid[33]; ///< Current SSID 28 uint8_t _wifi_ssid[33]; ///< Current SSID
23 bool _wifi_ScanAPs = false; ///< Scanning 29 bool _wifi_ScanAPs = false; ///< Scanning
24 bool _wifi_ScanDone = false; ///< Scan ready 30 bool _wifi_ScanDone = false; ///< Scan ready
25 uint16_t _wifi_Scanned = 0; ///< Total scanned APs. 31 uint16_t _wifi_Scanned = 0; ///< Total scanned APs.
67 * @brief local callback function. Is called when the timesync is valid 73 * @brief local callback function. Is called when the timesync is valid
68 * from a timeserver. Sets the global boolean System_TimeOk value. 74 * from a timeserver. Sets the global boolean System_TimeOk value.
69 * @param tv is the received time. Not used. 75 * @param tv is the received time. Not used.
70 */ 76 */
71 void time_sync_notification_cb(struct timeval *tv); 77 void time_sync_notification_cb(struct timeval *tv);
78
79 /**
80 * @brief Array with AP security names
81 */
82 const char *apsec[] = { "Open", "WEP", "WPA", "WPA2", "WPA WPA2", "Enterprise" };
72 83
73 84
74 /****************************************************************************/ 85 /****************************************************************************/
75 86
76 87
93 */ 104 */
94 if (strcmp(config.lastSSID, (char *)task_wifi_ConfigSTA->sta.ssid)) { 105 if (strcmp(config.lastSSID, (char *)task_wifi_ConfigSTA->sta.ssid)) {
95 sprintf(config.lastSSID, "%s", task_wifi_ConfigSTA->sta.ssid); 106 sprintf(config.lastSSID, "%s", task_wifi_ConfigSTA->sta.ssid);
96 write_config(); 107 write_config();
97 } 108 }
98 ESP_LOGI(TAG, "SaveStaConfig %s, record %d", wifiStation.SSID, record); 109 // ESP_LOGI(TAG, "SaveStaConfig %s, record %d", wifiStation.SSID, record);
99 } 110 }
100 111
101 return ESP_OK; 112 return ESP_OK;
102 } 113 }
103 114
142 _wifi_Scanned = ap_num; 153 _wifi_Scanned = ap_num;
143 _wifi_ScanDone = true; 154 _wifi_ScanDone = true;
144 break; 155 break;
145 156
146 case SYSTEM_EVENT_STA_START: 157 case SYSTEM_EVENT_STA_START:
147 ESP_LOGD(TAG, "Event STA started");
148 // Set the configured hostname for the dhcp client. 158 // Set the configured hostname for the dhcp client.
149 ESP_ERROR_CHECK(tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_STA, config.hostname)); 159 ESP_ERROR_CHECK(tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_STA, config.hostname));
150 esp_wifi_connect(); 160 esp_wifi_connect();
151 break; 161 break;
152 162
153 // SYSTEM_EVENT_STA_STOP 3 163 // SYSTEM_EVENT_STA_STOP 3
154 case SYSTEM_EVENT_STA_CONNECTED: 164 case SYSTEM_EVENT_STA_CONNECTED: {
155 ESP_LOGI(TAG, "Event STA connected"); 165 const system_event_sta_connected_t *connected = &event->event_info.connected;
166 ESP_LOGI(TAG, "Event STA connected, ssid:%s, ssid_len:%d, bssid:" MACSTR ", channel:%d, authmode:%s",
167 connected->ssid, connected->ssid_len, MAC2STR(connected->bssid), connected->channel, apsec[connected->authmode]);
156 wifi_ap_record_t ap_info; 168 wifi_ap_record_t ap_info;
157 esp_wifi_sta_get_ap_info(&ap_info); 169 esp_wifi_sta_get_ap_info(&ap_info);
158 if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) { 170 if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) {
159 wifi_state->STA_connected = true; 171 wifi_state->STA_connected = true;
160 wifi_state->STA_rssi = ap_info.rssi; 172 wifi_state->STA_rssi = ap_info.rssi;
162 xSemaphoreGive(xSemaphoreWiFi); 174 xSemaphoreGive(xSemaphoreWiFi);
163 } 175 }
164 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED); 176 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED);
165 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED); 177 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED);
166 break; 178 break;
167 179 }
168 case SYSTEM_EVENT_STA_DISCONNECTED: 180 case SYSTEM_EVENT_STA_DISCONNECTED: {
169 ESP_LOGI(TAG, "Event STA disconnected"); 181 const system_event_sta_disconnected_t *disconnected = &event->event_info.disconnected;
182 wifi_ap_record_t ap;
183
184 ESP_LOGI(TAG, "Event STA disconnected, ssid:%s, ssid_len:%d, bssid:" MACSTR ", reason:%d",
185 disconnected->ssid, disconnected->ssid_len, MAC2STR(disconnected->bssid), disconnected->reason);
170 if (xSemaphoreTake(xSemaphoreWiFi, 10) == pdTRUE) { 186 if (xSemaphoreTake(xSemaphoreWiFi, 10) == pdTRUE) {
171 wifi_state->STA_connected = false; 187 wifi_state->STA_connected = false;
172 wifi_state->STA_online = false; 188 wifi_state->STA_online = false;
173 wifi_state->STA_rssi = 0; 189 wifi_state->STA_rssi = 0;
174 xSemaphoreGive(xSemaphoreWiFi); 190 xSemaphoreGive(xSemaphoreWiFi);
175 } 191 }
176 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED); 192 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED);
177 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED); 193 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED);
178 sntp_stop(); 194 sntp_stop();
195
196 if (disconnected->reason == WIFI_REASON_NO_AP_FOUND && ! _wifi_ScanAPs) {
197 ESP_LOGI(TAG, "Request scan for another AP");
198 _wifi_ScanAPs = true;
199 _wifi_ScanDone = false;
200 ap_num = MAX_AP_NUM;
201 ESP_ERROR_CHECK(esp_wifi_scan_start(&scan_config, false));
202 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); // Keep looping active.
203 break;
204 }
205 if (disconnected->reason == WIFI_REASON_NO_AP_FOUND && _wifi_ScanAPs && _wifi_ScanDone) {
206 // ESP_LOGI(TAG, "Scan completed, look for another AP, found:%d", _wifi_Scanned);
207 _wifi_ScanAPs = false;
208 _wifi_ScanDone = false;
209 for (int i = 0; i < _wifi_Scanned; i++) {
210 ap = accessp_records[i];
211 if ((read_station(ap.ssid) != -1)) {
212 if (wifiStation.hide) {
213 continue; // Blacklisted.
214 }
215 /* We know this one */
216 wifi_config_t* config = task_wifi_ConfigSTA;
217 memset(config, 0x00, sizeof(wifi_config_t));
218 memcpy(config->sta.ssid, wifiStation.SSID, strlen(wifiStation.SSID));
219 memcpy(config->sta.password, wifiStation.Password, strlen(wifiStation.Password));
220 // ESP_LOGI(TAG, "new AP %s %s", wifiStation.SSID, wifiStation.Password);
221 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT);
222 break;
223 }
224 }
225 break;
226 }
179 227
180 /* 228 /*
181 * Meanwhile, try to reconnect. 229 * Reconnect previous AP.
182 */ 230 */
183 if (FetchStaConfig()) { 231 if (FetchStaConfig()) {
184 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); 232 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT);
185 } 233 }
186 break; 234 break;
187 235 }
188 // SYSTEM_EVENT_STA_AUTHMODE_CHANGE 6 236 // SYSTEM_EVENT_STA_AUTHMODE_CHANGE 6
189 case SYSTEM_EVENT_STA_GOT_IP: 237 case SYSTEM_EVENT_STA_GOT_IP:
190 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_HAS_IP); 238 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_HAS_IP);
191 tcpip_adapter_ip_info_t ip; 239 tcpip_adapter_ip_info_t ip;
192 memset(&ip, 0, sizeof(tcpip_adapter_ip_info_t)); 240 memset(&ip, 0, sizeof(tcpip_adapter_ip_info_t));
211 sntp_setservername(0, config.ntp_server); 259 sntp_setservername(0, config.ntp_server);
212 sntp_setservername(1, "pool.ntp.org"); // Will get you servers nearby 260 sntp_setservername(1, "pool.ntp.org"); // Will get you servers nearby
213 sntp_set_sync_mode(SNTP_SYNC_MODE_IMMED); 261 sntp_set_sync_mode(SNTP_SYNC_MODE_IMMED);
214 sntp_set_time_sync_notification_cb(time_sync_notification_cb); 262 sntp_set_time_sync_notification_cb(time_sync_notification_cb);
215 sntp_init(); 263 sntp_init();
264 #if 0
216 if (strlen(config.ntp_server)) 265 if (strlen(config.ntp_server))
217 ESP_LOGI(TAG, "NTP server %s", sntp_getservername(0)); 266 ESP_LOGI(TAG, "NTP server %s", sntp_getservername(0));
218 ESP_LOGI(TAG, "NTP server %s", sntp_getservername(1)); 267 ESP_LOGI(TAG, "NTP server %s", sntp_getservername(1));
268 #endif
219 break; 269 break;
220 270
221 case SYSTEM_EVENT_STA_LOST_IP: 271 case SYSTEM_EVENT_STA_LOST_IP:
222 ESP_LOGI(TAG, "Lost IP address"); 272 ESP_LOGI(TAG, "Lost IP address");
223 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_HAS_IP); 273 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_HAS_IP);
235 // SYSTEM_EVENT_STA_WPS_ER_FAILED 10 285 // SYSTEM_EVENT_STA_WPS_ER_FAILED 10
236 // SYSTEM_EVENT_STA_WPS_ER_TIMEOUT 11 286 // SYSTEM_EVENT_STA_WPS_ER_TIMEOUT 11
237 // SYSTEM_EVENT_STA_WPS_ER_PIN 12 287 // SYSTEM_EVENT_STA_WPS_ER_PIN 12
238 288
239 case SYSTEM_EVENT_AP_START: 289 case SYSTEM_EVENT_AP_START:
240 ESP_LOGD(TAG, "Event AP started");
241 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_AP_STARTED); 290 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_AP_STARTED);
242 if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) { 291 if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) {
243 wifi_state->AP_active = true; 292 wifi_state->AP_active = true;
244 wifi_state->AP_clients = 0; 293 wifi_state->AP_clients = 0;
245 xSemaphoreGive(xSemaphoreWiFi); 294 xSemaphoreGive(xSemaphoreWiFi);
246 } 295 }
247 break; 296 break;
248 297
249 case SYSTEM_EVENT_AP_STOP: 298 case SYSTEM_EVENT_AP_STOP:
250 ESP_LOGD(TAG, "Event AP stopped");
251 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_AP_STARTED); 299 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_AP_STARTED);
252 if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) { 300 if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) {
253 wifi_state->AP_active = false; 301 wifi_state->AP_active = false;
254 wifi_state->AP_clients = 0; 302 wifi_state->AP_clients = 0;
255 xSemaphoreGive(xSemaphoreWiFi); 303 xSemaphoreGive(xSemaphoreWiFi);
256 } 304 }
257 break; 305 break;
258 306
259 case SYSTEM_EVENT_AP_STACONNECTED: 307 case SYSTEM_EVENT_AP_STACONNECTED: {
260 if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) { 308 if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) {
261 wifi_state->AP_clients++; 309 wifi_state->AP_clients++;
262 xSemaphoreGive(xSemaphoreWiFi); 310 xSemaphoreGive(xSemaphoreWiFi);
263 } 311 }
264 ESP_LOGI(TAG, "Event AP new client, %d connections", wifi_state->AP_clients); 312 const system_event_ap_staconnected_t *staconnected = &event->event_info.sta_connected;
265 break; 313 ESP_LOGI(TAG, "Event AP connected, mac:" MACSTR ", aid:%d, conns:%d",
266 314 MAC2STR(staconnected->mac), staconnected->aid, wifi_state->AP_clients);
267 case SYSTEM_EVENT_AP_STADISCONNECTED: 315 break;
316 }
317 case SYSTEM_EVENT_AP_STADISCONNECTED: {
268 if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) { 318 if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) {
269 if (wifi_state->AP_clients > 0) 319 if (wifi_state->AP_clients > 0)
270 wifi_state->AP_clients--; 320 wifi_state->AP_clients--;
271 else 321 else
272 wifi_state->AP_clients = 0; 322 wifi_state->AP_clients = 0;
273 xSemaphoreGive(xSemaphoreWiFi); 323 xSemaphoreGive(xSemaphoreWiFi);
274 } 324 }
275 ESP_LOGI(TAG, "Event AP client disconnect, %d connections", wifi_state->AP_clients); 325 const system_event_ap_stadisconnected_t *stadisconnected = &event->event_info.sta_disconnected;
276 break; 326 ESP_LOGI(TAG, "Event AP disconnected, mac:" MACSTR ", aid:%d, conns:%d",
277 327 MAC2STR(stadisconnected->mac), stadisconnected->aid, wifi_state->AP_clients);
278 case SYSTEM_EVENT_AP_PROBEREQRECVED: 328 break;
279 break; 329 }
280 330 case SYSTEM_EVENT_AP_STAIPASSIGNED:
281 // SYSTEM_EVENT_GOT_IP6 18 331 break;
282 // SYSTEM_EVENT_ETH_START 19 332
283 // SYSTEM_EVENT_ETH_STOP 20 333 // SYSTEM_EVENT_AP_PROBEREQRECVED 18
284 // SYSTEM_EVENT_ETH_CONNECTED 21 334 // SYSTEM_EVENT_GOT_IP6 19
285 // SYSTEM_EVENT_ETH_DISCONNECTED 22 335 // SYSTEM_EVENT_ETH_START 20
286 // SYSTEM_EVENT_ETH_GOT_IP 23 336 // SYSTEM_EVENT_ETH_STOP 21
337 // SYSTEM_EVENT_ETH_CONNECTED 22
338 // SYSTEM_EVENT_ETH_DISCONNECTED 23
339 // SYSTEM_EVENT_ETH_GOT_IP 24
287 340
288 default: 341 default:
289 printf("Unknown event %d\n", event->event_id); 342 printf("Unknown event %d\n", event->event_id);
290 break; 343 break;
291 } 344 }
301 if (rc == SNTP_SYNC_STATUS_COMPLETED) { 354 if (rc == SNTP_SYNC_STATUS_COMPLETED) {
302 time(&now); 355 time(&now);
303 localtime_r(&now, &timeinfo); 356 localtime_r(&now, &timeinfo);
304 System_TimeOk = true; 357 System_TimeOk = true;
305 strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo); 358 strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo);
306 ESP_LOGI(TAG, "System time is set: %s", strftime_buf); 359 ESP_LOGI(TAG, "NTP time is set: %s", strftime_buf);
307 } else { 360 } else {
308 ESP_LOGI(TAG, "Notification of unknown time synchronization event rc=%d", rc); 361 ESP_LOGI(TAG, "NTP unknown time sync event rc=%d", rc);
309 } 362 }
310 } 363 }
311 364
312 365
313 366
345 wifi_state->AP_clients = 0; 398 wifi_state->AP_clients = 0;
346 wifi_state->AP_active = false; 399 wifi_state->AP_active = false;
347 wifi_state->STA_connected = false; 400 wifi_state->STA_connected = false;
348 wifi_state->STA_online = false; 401 wifi_state->STA_online = false;
349 wifi_state->STA_rssi = 0; 402 wifi_state->STA_rssi = 0;
350
351 /* wifi scanner config */
352 wifi_scan_config_t scan_config = {
353 .ssid = 0,
354 .bssid = 0,
355 .channel = 0,
356 .show_hidden = false
357 };
358 403
359 /* 404 /*
360 * start the softAP access point 405 * start the softAP access point
361 * stop DHCP server 406 * stop DHCP server
362 */ 407 */
376 ESP_ERROR_CHECK(tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_AP)); 421 ESP_ERROR_CHECK(tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_AP));
377 ESP_LOGI(TAG, "AP start dhcps ip: 192.168.1.1 nm: 255.255.255.0 gw: 192.168.1.1"); 422 ESP_LOGI(TAG, "AP start dhcps ip: 192.168.1.1 nm: 255.255.255.0 gw: 192.168.1.1");
378 423
379 /* start dhcp client */ 424 /* start dhcp client */
380 ESP_ERROR_CHECK(tcpip_adapter_dhcpc_start(TCPIP_ADAPTER_IF_STA)); 425 ESP_ERROR_CHECK(tcpip_adapter_dhcpc_start(TCPIP_ADAPTER_IF_STA));
381 ESP_LOGI(TAG, "Start DHCP client for STA interface.");
382 426
383 /* 427 /*
384 * init wifi as station + access point 428 * init wifi as station + access point
385 */ 429 */
386 wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT(); 430 wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT();
406 if (ret != ESP_OK) { 450 if (ret != ESP_OK) {
407 ESP_LOGE(TAG, "esp_wifi_set_config(WIFI_IF_AP, nnn) rc=%d", ret); 451 ESP_LOGE(TAG, "esp_wifi_set_config(WIFI_IF_AP, nnn) rc=%d", ret);
408 } 452 }
409 ESP_ERROR_CHECK(esp_wifi_start()); 453 ESP_ERROR_CHECK(esp_wifi_start());
410 454
411 ESP_LOGI(TAG, "SoftAP start ssid: `%s' pwd: `%s' channel: %d, hidden: %s", 455 ESP_LOGI(TAG, "AP start ssid:`%s' pwd:`%s' channel:%d, hidden:%s",
412 ap_config.ap.ssid, ap_config.ap.password, ap_config.ap.channel, ap_config.ap.ssid_hidden ? "yes":"no"); 456 ap_config.ap.ssid, ap_config.ap.password, ap_config.ap.channel, ap_config.ap.ssid_hidden ? "yes":"no");
413 457
414 /* 458 /*
415 * try to get access to previously saved wifi 459 * try to get access to previously saved wifi
416 */ 460 */
425 xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_AP_STARTED, pdFALSE, pdTRUE, portMAX_DELAY ); 469 xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_AP_STARTED, pdFALSE, pdTRUE, portMAX_DELAY );
426 EventBits_t uxBits; 470 EventBits_t uxBits;
427 471
428 for(;;) { 472 for(;;) {
429 473
430 // ESP_LOGI(TAG, "1 wait for %08x", TASK_WIFI_REQUEST_STA_CONNECT | TASK_WIFI_REQUEST_WIFI_SCAN | TASK_WIFI_REQUEST_STA_DISCONNECT);
431 /* actions that can trigger: request a connection, a scan, or a disconnection */ 474 /* actions that can trigger: request a connection, a scan, or a disconnection */
432 uxBits = xEventGroupWaitBits(xEventGroupWifi, 475 uxBits = xEventGroupWaitBits(xEventGroupWifi,
433 TASK_WIFI_REQUEST_STA_CONNECT | TASK_WIFI_REQUEST_WIFI_SCAN | TASK_WIFI_REQUEST_STA_DISCONNECT, 476 TASK_WIFI_REQUEST_STA_CONNECT | TASK_WIFI_REQUEST_WIFI_SCAN | TASK_WIFI_REQUEST_STA_DISCONNECT,
434 pdFALSE, pdFALSE, portMAX_DELAY ); 477 pdFALSE, pdFALSE, portMAX_DELAY );
435 // ESP_LOGI(TAG, "1 waitbits %08x", uxBits);
436 478
437 if (uxBits & TASK_WIFI_REQUEST_STA_DISCONNECT) { 479 if (uxBits & TASK_WIFI_REQUEST_STA_DISCONNECT) {
438 /* 480 /*
439 * user requested a disconnect, this will in effect disconnect the wifi but also erase NVS memory 481 * user requested a disconnect, this will in effect disconnect the wifi but also erase NVS memory
440 */ 482 */
452 config.lastSSID[0] = '\0'; 494 config.lastSSID[0] = '\0';
453 write_config(); 495 write_config();
454 496
455 /* finally: release the scan request bit */ 497 /* finally: release the scan request bit */
456 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_DISCONNECT); 498 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_DISCONNECT);
457 ESP_LOGI(TAG, "Request disconnect is finished.");
458 499
459 } else if (uxBits & TASK_WIFI_REQUEST_STA_CONNECT) { 500 } else if (uxBits & TASK_WIFI_REQUEST_STA_CONNECT) {
460 501
461 //someone requested a connection! 502 //someone requested a connection!
462 ESP_LOGI(TAG, "Request STA connect `%s'", task_wifi_ConfigSTA->sta.ssid); 503 ESP_LOGI(TAG, "Request STA connect `%s'", task_wifi_ConfigSTA->sta.ssid);
469 ESP_LOGE(TAG, "esp_wifi_connect() rc=%04x", (int)wifierror); 510 ESP_LOGE(TAG, "esp_wifi_connect() rc=%04x", (int)wifierror);
470 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_FAILED); 511 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_FAILED);
471 } 512 }
472 513
473 /* 514 /*
474 * 2 scenarios here: connection is successful and TASK_WIFI_HAS_IP will be posted 515 * 3 scenarios here: connection is successful and TASK_WIFI_STA_CONNECTED will be posted
475 * or it's a failure and we get a TASK_WIFI_STA_FAILED with a reason code. 516 * or it's a failure and we get a TASK_WIFI_STA_FAILED with a reason code.
517 * Or, option 3, the 5 seconds timeout is reached. This happens when the AP is not in range.
476 * Note that the reason code is not exploited. For all intent and purposes a failure is a failure. 518 * Note that the reason code is not exploited. For all intent and purposes a failure is a failure.
477 */ 519 */
478 // ESP_LOGI(TAG, "2 wait for %08x", TASK_WIFI_HAS_IP | TASK_WIFI_STA_FAILED); 520 // ESP_LOGI(TAG, "2 wait for %08x", TASK_WIFI_STA_CONNECTED | TASK_WIFI_STA_FAILED);
479 uxBits = xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_HAS_IP | TASK_WIFI_STA_FAILED, pdFALSE, pdFALSE, 10000 / portTICK_PERIOD_MS); 521 uxBits = xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_STA_CONNECTED | TASK_WIFI_STA_FAILED, pdFALSE, pdFALSE, 5000 / portTICK_PERIOD_MS);
480 // ESP_LOGI(TAG, "2 waitbits %08x", uxBits); 522 // ESP_LOGI(TAG, "2 waitbits %08x", uxBits & (TASK_WIFI_STA_CONNECTED | TASK_WIFI_STA_FAILED));
481 523
482 if (uxBits & (TASK_WIFI_HAS_IP | TASK_WIFI_STA_FAILED)) { 524 if (uxBits & (TASK_WIFI_STA_CONNECTED | TASK_WIFI_STA_FAILED)) {
483 /* 525 /*
484 * only save the config if the connection was successful! 526 * only save the config if the connection was successful!
485 */ 527 */
486 if(uxBits & TASK_WIFI_HAS_IP) { 528 if (uxBits & TASK_WIFI_STA_CONNECTED) {
487 /* save wifi config */ 529 /* save wifi config */
488 SaveStaConfig(); 530 SaveStaConfig();
489 } else { 531 } else {
490 ESP_LOGI(TAG, "Connection failed"); // TODO: Scan other SSID's for known networks. 532 ESP_LOGI(TAG, "Connection failed");
491 /* failed attempt to connect regardles of the reason */ 533 /* failed attempt to connect regardles of the reason */
492 534
493 /* otherwise: reset the config */ 535 /* otherwise: reset the config */
494 memset(task_wifi_ConfigSTA, 0x00, sizeof(wifi_config_t)); 536 memset(task_wifi_ConfigSTA, 0x00, sizeof(wifi_config_t));
495 } 537 }
502 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); 544 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT);
503 } 545 }
504 546
505 } else if (uxBits & TASK_WIFI_REQUEST_WIFI_SCAN) { 547 } else if (uxBits & TASK_WIFI_REQUEST_WIFI_SCAN) {
506 548
507 ESP_LOGI(TAG, "Request WiFi scan");
508 /* safe guard against overflow */ 549 /* safe guard against overflow */
509 ap_num = MAX_AP_NUM; 550 ap_num = MAX_AP_NUM;
510 ESP_ERROR_CHECK(esp_wifi_scan_start(&scan_config, false)); 551 ESP_ERROR_CHECK(esp_wifi_scan_start(&scan_config, false));
511 552
512 /* finally: release the scan request bit */ 553 /* finally: release the scan request bit */
513 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_WIFI_SCAN); 554 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_WIFI_SCAN);
514 } 555 }
515 556
516 /*
517 * Here we should check for reconnect actions.
518 */
519 // ESP_LOGI(TAG, "check reconnect");
520
521 } /* for(;;) */ 557 } /* for(;;) */
522 vTaskDelay( (TickType_t)10); 558 vTaskDelay( (TickType_t)10);
523 } 559 }
524 560
525
526 /**
527 * @brief Array with AP security names
528 */
529 const char *apsec[] = { "Open", "WEP", "WPA", "WPA2", "WPA WPA2", "Enterprise" };
530 561
531 562
532 /** 563 /**
533 * @brief Show an AP station as a button. The buttons are already defined. 564 * @brief Show an AP station as a button. The buttons are already defined.
534 * @param idx The index position on the display, 1 to 7. 565 * @param idx The index position on the display, 1 to 7.
787 Main_Screen = MAIN_TOOLS_SETUP_WIFI_NEW; 818 Main_Screen = MAIN_TOOLS_SETUP_WIFI_NEW;
788 } 819 }
789 ESP_ERROR_CHECK(esp_wifi_scan_stop()); 820 ESP_ERROR_CHECK(esp_wifi_scan_stop());
790 } else if (Choice == 0) { 821 } else if (Choice == 0) {
791 Main_Screen = MAIN_TOOLS_SETUP; 822 Main_Screen = MAIN_TOOLS_SETUP;
823 _wifi_ScanAPs = false;
824 _wifi_ScanDone = false;
792 ESP_ERROR_CHECK(esp_wifi_scan_stop()); 825 ESP_ERROR_CHECK(esp_wifi_scan_stop());
793 } else if (TimeSpent > 10) { 826 } else if (TimeSpent > 10) {
794 _wifi_ScanAPs = true; 827 _wifi_ScanAPs = true;
795 } 828 }
796 break; 829 break;

mercurial