111 memset(task_wifi_ConfigSTA, 0x00, sizeof(wifi_config_t)); |
111 memset(task_wifi_ConfigSTA, 0x00, sizeof(wifi_config_t)); |
112 |
112 |
113 /* |
113 /* |
114 * Search last connected AP as station. |
114 * Search last connected AP as station. |
115 */ |
115 */ |
116 if (strlen(config.lastSSID)) { |
116 if (strlen(config.lastSSID) && (read_station((uint8_t *)config.lastSSID) >= 0)) { |
117 read_station((uint8_t *)config.lastSSID); |
|
118 |
117 |
119 /* ssid */ |
118 /* ssid */ |
120 size_t sz = sizeof(task_wifi_ConfigSTA->sta.ssid); |
119 size_t sz = sizeof(task_wifi_ConfigSTA->sta.ssid); |
121 memcpy(task_wifi_ConfigSTA->sta.ssid, wifiStation.SSID, sz); |
120 memcpy(task_wifi_ConfigSTA->sta.ssid, wifiStation.SSID, sz); |
122 |
121 |
200 xSemaphoreGive(xSemaphoreWiFi); |
199 xSemaphoreGive(xSemaphoreWiFi); |
201 } |
200 } |
202 } |
201 } |
203 /* |
202 /* |
204 * There doesn't seem to be support for configuring NTP via DHCP so |
203 * There doesn't seem to be support for configuring NTP via DHCP so |
205 * we need to hardcode the ntp servers. |
204 * we need to hardcode the ntp servers. The preffered server can be |
|
205 * set via the setup screen. It should be on your LAN, else leave it |
|
206 * empty. And if you are on a different lan someday, there is no extra |
|
207 * delay because the hostname will not be found. |
206 */ |
208 */ |
207 sntp_stop(); |
209 sntp_stop(); |
208 if (strlen(config.ntp_server)) |
210 if (strlen(config.ntp_server)) |
209 sntp_setservername(0, config.ntp_server); |
211 sntp_setservername(0, config.ntp_server); |
210 sntp_setservername(1, "nl.pool.ntp.org"); |
212 sntp_setservername(1, "pool.ntp.org"); // Will get you servers nearby |
211 sntp_set_sync_mode(SNTP_SYNC_MODE_IMMED); |
213 sntp_set_sync_mode(SNTP_SYNC_MODE_IMMED); |
212 sntp_set_time_sync_notification_cb(time_sync_notification_cb); |
214 sntp_set_time_sync_notification_cb(time_sync_notification_cb); |
213 sntp_init(); |
215 sntp_init(); |
214 if (strlen(config.ntp_server)) |
216 if (strlen(config.ntp_server)) |
215 ESP_LOGI(TAG, "NTP server %s", sntp_getservername(0)); |
217 ESP_LOGI(TAG, "NTP server %s", sntp_getservername(0)); |
421 xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_AP_STARTED, pdFALSE, pdTRUE, portMAX_DELAY ); |
423 xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_AP_STARTED, pdFALSE, pdTRUE, portMAX_DELAY ); |
422 EventBits_t uxBits; |
424 EventBits_t uxBits; |
423 |
425 |
424 for(;;) { |
426 for(;;) { |
425 |
427 |
|
428 // ESP_LOGI(TAG, "1 wait for %08x", TASK_WIFI_REQUEST_STA_CONNECT | TASK_WIFI_REQUEST_WIFI_SCAN | TASK_WIFI_REQUEST_STA_DISCONNECT); |
426 /* actions that can trigger: request a connection, a scan, or a disconnection */ |
429 /* actions that can trigger: request a connection, a scan, or a disconnection */ |
427 uxBits = xEventGroupWaitBits(xEventGroupWifi, |
430 uxBits = xEventGroupWaitBits(xEventGroupWifi, |
428 TASK_WIFI_REQUEST_STA_CONNECT | TASK_WIFI_REQUEST_WIFI_SCAN | TASK_WIFI_REQUEST_STA_DISCONNECT, |
431 TASK_WIFI_REQUEST_STA_CONNECT | TASK_WIFI_REQUEST_WIFI_SCAN | TASK_WIFI_REQUEST_STA_DISCONNECT, |
429 pdFALSE, pdFALSE, portMAX_DELAY ); |
432 pdFALSE, pdFALSE, portMAX_DELAY ); |
|
433 // ESP_LOGI(TAG, "1 waitbits %08x", uxBits); |
430 |
434 |
431 if (uxBits & TASK_WIFI_REQUEST_STA_DISCONNECT) { |
435 if (uxBits & TASK_WIFI_REQUEST_STA_DISCONNECT) { |
432 /* |
436 /* |
433 * user requested a disconnect, this will in effect disconnect the wifi but also erase NVS memory |
437 * user requested a disconnect, this will in effect disconnect the wifi but also erase NVS memory |
434 */ |
438 */ |
467 /* |
471 /* |
468 * 2 scenarios here: connection is successful and TASK_WIFI_HAS_IP will be posted |
472 * 2 scenarios here: connection is successful and TASK_WIFI_HAS_IP will be posted |
469 * or it's a failure and we get a TASK_WIFI_STA_FAILED with a reason code. |
473 * or it's a failure and we get a TASK_WIFI_STA_FAILED with a reason code. |
470 * Note that the reason code is not exploited. For all intent and purposes a failure is a failure. |
474 * Note that the reason code is not exploited. For all intent and purposes a failure is a failure. |
471 */ |
475 */ |
472 uxBits = xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_HAS_IP | TASK_WIFI_STA_FAILED, pdFALSE, pdFALSE, portMAX_DELAY ); |
476 // ESP_LOGI(TAG, "2 wait for %08x", TASK_WIFI_HAS_IP | TASK_WIFI_STA_FAILED); |
|
477 uxBits = xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_HAS_IP | TASK_WIFI_STA_FAILED, pdFALSE, pdFALSE, 10000 / portTICK_PERIOD_MS); |
|
478 // ESP_LOGI(TAG, "2 waitbits %08x", uxBits); |
473 |
479 |
474 if (uxBits & (TASK_WIFI_HAS_IP | TASK_WIFI_STA_FAILED)) { |
480 if (uxBits & (TASK_WIFI_HAS_IP | TASK_WIFI_STA_FAILED)) { |
475 /* |
481 /* |
476 * only save the config if the connection was successful! |
482 * only save the config if the connection was successful! |
477 */ |
483 */ |
483 /* failed attempt to connect regardles of the reason */ |
489 /* failed attempt to connect regardles of the reason */ |
484 |
490 |
485 /* otherwise: reset the config */ |
491 /* otherwise: reset the config */ |
486 memset(task_wifi_ConfigSTA, 0x00, sizeof(wifi_config_t)); |
492 memset(task_wifi_ConfigSTA, 0x00, sizeof(wifi_config_t)); |
487 } |
493 } |
|
494 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); |
488 } else { |
495 } else { |
489 /* hit portMAX_DELAY limit ? */ |
496 /* hit 10 seconds timeout */ |
490 abort(); |
497 ESP_LOGI(TAG, "Connection timeout"); |
|
498 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); |
|
499 vTaskDelay(100 / portTICK_PERIOD_MS); |
|
500 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); |
491 } |
501 } |
492 |
502 |
493 /* finally: release the connection request bit */ |
|
494 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); |
|
495 |
|
496 } else if (uxBits & TASK_WIFI_REQUEST_WIFI_SCAN) { |
503 } else if (uxBits & TASK_WIFI_REQUEST_WIFI_SCAN) { |
497 |
504 |
|
505 ESP_LOGI(TAG, "Request WiFi scan"); |
498 /* safe guard against overflow */ |
506 /* safe guard against overflow */ |
499 ap_num = MAX_AP_NUM; |
507 ap_num = MAX_AP_NUM; |
500 ESP_ERROR_CHECK(esp_wifi_scan_start(&scan_config, false)); |
508 ESP_ERROR_CHECK(esp_wifi_scan_start(&scan_config, false)); |
501 |
509 |
502 /* finally: release the scan request bit */ |
510 /* finally: release the scan request bit */ |