104 */ |
104 */ |
105 if (strcmp(config.lastSSID, (char *)task_wifi_ConfigSTA->sta.ssid)) { |
105 if (strcmp(config.lastSSID, (char *)task_wifi_ConfigSTA->sta.ssid)) { |
106 sprintf(config.lastSSID, "%s", task_wifi_ConfigSTA->sta.ssid); |
106 sprintf(config.lastSSID, "%s", task_wifi_ConfigSTA->sta.ssid); |
107 write_config(); |
107 write_config(); |
108 } |
108 } |
109 // ESP_LOGI(TAG, "SaveStaConfig %s, record %d", wifiStation.SSID, record); |
109 ESP_LOGD(TAG, "SaveStaConfig %s, record %d", wifiStation.SSID, record); |
110 } |
110 } |
111 |
111 |
112 return ESP_OK; |
112 return ESP_OK; |
113 } |
113 } |
114 |
114 |
179 } |
179 } |
180 case SYSTEM_EVENT_STA_DISCONNECTED: { |
180 case SYSTEM_EVENT_STA_DISCONNECTED: { |
181 const system_event_sta_disconnected_t *disconnected = &event->event_info.disconnected; |
181 const system_event_sta_disconnected_t *disconnected = &event->event_info.disconnected; |
182 wifi_ap_record_t ap; |
182 wifi_ap_record_t ap; |
183 |
183 |
184 ESP_LOGI(TAG, "Event STA disconnected, ssid:%s, ssid_len:%d, bssid:" MACSTR ", reason:%d", |
184 ESP_LOGW(TAG, "Event STA disconnected, ssid:%s, ssid_len:%d, bssid:" MACSTR ", reason:%d", |
185 disconnected->ssid, disconnected->ssid_len, MAC2STR(disconnected->bssid), disconnected->reason); |
185 disconnected->ssid, disconnected->ssid_len, MAC2STR(disconnected->bssid), disconnected->reason); |
186 if (xSemaphoreTake(xSemaphoreWiFi, 10) == pdTRUE) { |
186 if (xSemaphoreTake(xSemaphoreWiFi, 10) == pdTRUE) { |
187 wifi_state->STA_connected = false; |
187 wifi_state->STA_connected = false; |
188 wifi_state->STA_online = false; |
188 wifi_state->STA_online = false; |
189 wifi_state->STA_rssi = 0; |
189 wifi_state->STA_rssi = 0; |
201 ESP_ERROR_CHECK(esp_wifi_scan_start(&scan_config, false)); |
201 ESP_ERROR_CHECK(esp_wifi_scan_start(&scan_config, false)); |
202 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); // Keep looping active. |
202 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); // Keep looping active. |
203 break; |
203 break; |
204 } |
204 } |
205 if (disconnected->reason == WIFI_REASON_NO_AP_FOUND && _wifi_ScanAPs && _wifi_ScanDone) { |
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); |
206 ESP_LOGD(TAG, "Scan completed, look for another AP, found:%d", _wifi_Scanned); |
207 _wifi_ScanAPs = false; |
207 _wifi_ScanAPs = false; |
208 _wifi_ScanDone = false; |
208 _wifi_ScanDone = false; |
209 for (int i = 0; i < _wifi_Scanned; i++) { |
209 for (int i = 0; i < _wifi_Scanned; i++) { |
210 ap = accessp_records[i]; |
210 ap = accessp_records[i]; |
211 if ((read_station(ap.ssid) != -1)) { |
211 if ((read_station(ap.ssid) != -1)) { |
215 /* We know this one */ |
215 /* We know this one */ |
216 wifi_config_t* config = task_wifi_ConfigSTA; |
216 wifi_config_t* config = task_wifi_ConfigSTA; |
217 memset(config, 0x00, sizeof(wifi_config_t)); |
217 memset(config, 0x00, sizeof(wifi_config_t)); |
218 memcpy(config->sta.ssid, wifiStation.SSID, strlen(wifiStation.SSID)); |
218 memcpy(config->sta.ssid, wifiStation.SSID, strlen(wifiStation.SSID)); |
219 memcpy(config->sta.password, wifiStation.Password, strlen(wifiStation.Password)); |
219 memcpy(config->sta.password, wifiStation.Password, strlen(wifiStation.Password)); |
220 // ESP_LOGI(TAG, "new AP %s %s", wifiStation.SSID, wifiStation.Password); |
220 ESP_LOGD(TAG, "new AP %s %s", wifiStation.SSID, wifiStation.Password); |
221 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); |
221 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); |
222 break; |
222 break; |
223 } |
223 } |
224 } |
224 } |
225 break; |
225 break; |
267 ESP_LOGI(TAG, "NTP server %s", sntp_getservername(1)); |
267 ESP_LOGI(TAG, "NTP server %s", sntp_getservername(1)); |
268 #endif |
268 #endif |
269 break; |
269 break; |
270 |
270 |
271 case SYSTEM_EVENT_STA_LOST_IP: |
271 case SYSTEM_EVENT_STA_LOST_IP: |
272 ESP_LOGI(TAG, "Lost IP address"); |
272 ESP_LOGW(TAG, "Lost IP address"); |
273 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_HAS_IP); |
273 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_HAS_IP); |
274 if (xSemaphoreTake(xSemaphoreWiFi, 10) == pdTRUE) { |
274 if (xSemaphoreTake(xSemaphoreWiFi, 10) == pdTRUE) { |
275 wifi_state->STA_ip[0] = '\0'; |
275 wifi_state->STA_ip[0] = '\0'; |
276 wifi_state->STA_nm[0] = '\0'; |
276 wifi_state->STA_nm[0] = '\0'; |
277 wifi_state->STA_gw[0] = '\0'; |
277 wifi_state->STA_gw[0] = '\0'; |
527 */ |
527 */ |
528 if (uxBits & TASK_WIFI_STA_CONNECTED) { |
528 if (uxBits & TASK_WIFI_STA_CONNECTED) { |
529 /* save wifi config */ |
529 /* save wifi config */ |
530 SaveStaConfig(); |
530 SaveStaConfig(); |
531 } else { |
531 } else { |
532 ESP_LOGI(TAG, "Connection failed"); |
532 ESP_LOGW(TAG, "Connection failed"); |
533 /* failed attempt to connect regardles of the reason */ |
533 /* failed attempt to connect regardles of the reason */ |
534 |
534 |
535 /* otherwise: reset the config */ |
535 /* otherwise: reset the config */ |
536 memset(task_wifi_ConfigSTA, 0x00, sizeof(wifi_config_t)); |
536 memset(task_wifi_ConfigSTA, 0x00, sizeof(wifi_config_t)); |
537 } |
537 } |
538 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); |
538 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); |
539 } else { |
539 } else { |
540 /* hit 10 seconds timeout */ |
540 /* hit 10 seconds timeout */ |
541 ESP_LOGI(TAG, "Connection timeout"); |
541 ESP_LOGW(TAG, "Connection timeout"); |
542 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); |
542 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); |
543 vTaskDelay(100 / portTICK_PERIOD_MS); |
543 vTaskDelay(100 / portTICK_PERIOD_MS); |
544 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); |
544 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); |
545 } |
545 } |
546 |
546 |