52 /** |
52 /** |
53 * @brief local callback function. Is called when the timesync is valid |
53 * @brief local callback function. Is called when the timesync is valid |
54 * from a timeserver. Sets the global boolean System_TimeOk value. |
54 * from a timeserver. Sets the global boolean System_TimeOk value. |
55 * @param tv is the received time. Not used. |
55 * @param tv is the received time. Not used. |
56 */ |
56 */ |
57 void time_sync_notification_cb(struct timeval *tv); |
57 //void time_sync_notification_cb(struct timeval *tv); |
58 |
58 |
59 /** |
59 /** |
60 * @brief Array with AP security names |
60 * @brief Array with AP security names |
61 */ |
61 */ |
62 const char *apsec[] = { "Open", "WEP", "WPA", "WPA2", "WPA WPA2", "Enterprise" }; |
62 const char *apsec[] = { "Open", "WEP", "WPA", "WPA2", "WPA WPA2", "Enterprise" }; |
228 memset(task_wifi_ConfigSTA, 0x00, sizeof(wifi_config_t)); |
228 memset(task_wifi_ConfigSTA, 0x00, sizeof(wifi_config_t)); |
229 |
229 |
230 xSemaphoreWiFi = xSemaphoreCreateMutex(); |
230 xSemaphoreWiFi = xSemaphoreCreateMutex(); |
231 wifi_state = malloc(sizeof(WIFI_State)); |
231 wifi_state = malloc(sizeof(WIFI_State)); |
232 memset(wifi_state, 0x00, sizeof(WIFI_State)); |
232 memset(wifi_state, 0x00, sizeof(WIFI_State)); |
233 // wifi_state->STA_connected = false; |
|
234 // wifi_state->STA_online = false; |
|
235 // wifi_state->STA_rssi = 0; |
|
236 |
233 |
237 /* stop dhcp server and start dhcp client */ |
234 /* stop dhcp server and start dhcp client */ |
238 ESP_ERROR_CHECK(tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP)); |
235 ESP_ERROR_CHECK(tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP)); |
239 ESP_ERROR_CHECK(tcpip_adapter_dhcpc_start(TCPIP_ADAPTER_IF_STA)); |
236 ESP_ERROR_CHECK(tcpip_adapter_dhcpc_start(TCPIP_ADAPTER_IF_STA)); |
240 |
237 |
263 |
260 |
264 if (uxBits & TASK_WIFI_REQUEST_STA_DISCONNECT) { |
261 if (uxBits & TASK_WIFI_REQUEST_STA_DISCONNECT) { |
265 /* |
262 /* |
266 * user requested a disconnect, this will in effect disconnect the wifi |
263 * user requested a disconnect, this will in effect disconnect the wifi |
267 */ |
264 */ |
268 ESP_LOGI(TAG, "Request STA disconnect"); |
265 ESP_LOGD(TAG, "Request STA disconnect"); |
269 ESP_ERROR_CHECK(esp_wifi_disconnect()); |
266 ESP_ERROR_CHECK(esp_wifi_disconnect()); |
270 xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED, pdFALSE, pdTRUE, portMAX_DELAY ); |
267 xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED, pdFALSE, pdTRUE, portMAX_DELAY ); |
271 |
268 |
272 /* finally: release the scan request bit */ |
269 /* finally: release the scan request bit */ |
273 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_DISCONNECT); |
270 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_DISCONNECT); |
274 |
271 |
275 } else if (uxBits & TASK_WIFI_REQUEST_STA_CONNECT) { |
272 } else if (uxBits & TASK_WIFI_REQUEST_STA_CONNECT) { |
276 |
273 |
277 ESP_LOGI(TAG, "Request STA connect"); |
274 ESP_LOGD(TAG, "Request STA connect"); |
278 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_FAILED); |
275 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_STA_FAILED); |
279 _wifi_ScanAPs = true; |
276 _wifi_ScanAPs = true; |
280 _wifi_ScanDone = false; |
277 _wifi_ScanDone = false; |
281 ap_num = MAX_AP_NUM; |
278 ap_num = MAX_AP_NUM; |
282 ESP_ERROR_CHECK(esp_wifi_scan_start(&scan_config, false)); |
279 ESP_ERROR_CHECK(esp_wifi_scan_start(&scan_config, false)); |
288 |
285 |
289 // Available Access Points. |
286 // Available Access Points. |
290 for (int i = 0; i < _wifi_Scanned; i++) { |
287 for (int i = 0; i < _wifi_Scanned; i++) { |
291 wifi_ap_record_t ap = accessp_records[i]; |
288 wifi_ap_record_t ap = accessp_records[i]; |
292 // Check if we know this AP in the database. |
289 // Check if we know this AP in the database. |
293 // ESP_LOGI(TAG, "%d %-20s ch: %2d rssi: %d %s", i, ap.ssid, ap.primary, ap.rssi, apsec[ap.authmode]); |
290 ESP_LOGD(TAG, "%d %-20s ch: %2d rssi: %d %s", i, ap.ssid, ap.primary, ap.rssi, apsec[ap.authmode]); |
294 if ((read_station(ap.ssid) >= 0)) { |
291 if ((read_station(ap.ssid) >= 0)) { |
295 /* ssid */ |
292 /* ssid */ |
296 size_t sz = sizeof(task_wifi_ConfigSTA->sta.ssid); |
293 size_t sz = sizeof(task_wifi_ConfigSTA->sta.ssid); |
297 memcpy(task_wifi_ConfigSTA->sta.ssid, wifiStation.SSID, sz); |
294 memcpy(task_wifi_ConfigSTA->sta.ssid, wifiStation.SSID, sz); |
298 /* password */ |
295 /* password */ |
304 } |
301 } |
305 if (found) { |
302 if (found) { |
306 /* |
303 /* |
307 * Now connect to the known SSID |
304 * Now connect to the known SSID |
308 */ |
305 */ |
309 // ESP_LOGI(TAG, "Connecting to `%s'", task_wifi_ConfigSTA->sta.ssid); |
306 ESP_LOGD(TAG, "Connecting to `%s'", task_wifi_ConfigSTA->sta.ssid); |
310 ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, task_wifi_ConfigSTA)); |
307 ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, task_wifi_ConfigSTA)); |
311 esp_err_t wifierror = esp_wifi_connect(); |
308 esp_err_t wifierror = esp_wifi_connect(); |
312 if (wifierror != ESP_OK) { |
309 if (wifierror != ESP_OK) { |
313 ESP_LOGE(TAG, "esp_wifi_connect() `%s' rc=%04x", task_wifi_ConfigSTA->sta.ssid, (int)wifierror); |
310 ESP_LOGE(TAG, "esp_wifi_connect() `%s' rc=%04x", task_wifi_ConfigSTA->sta.ssid, (int)wifierror); |
314 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_FAILED); |
311 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_FAILED); |