main/task_wifi.c

changeset 37
358bbd5b608e
parent 32
7717ac1d2f7f
child 44
e52d11b8f252
equal deleted inserted replaced
36:e2a43e7c4035 37:358bbd5b608e
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" };
161 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED); 161 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_STA_DISCONNECTED);
162 break; 162 break;
163 } 163 }
164 164
165 default: 165 default:
166 ESP_LOGI(TAG, "Unknown WiFi event %d", event_id); 166 ESP_LOGW(TAG, "Unknown WiFi event %d", event_id);
167 break; 167 break;
168 } 168 }
169 } 169 }
170 170
171 171
201 ESP_LOGE(TAG, "got_ip_event_handler() lock error IP_EVENT_STA_LOST_IP"); 201 ESP_LOGE(TAG, "got_ip_event_handler() lock error IP_EVENT_STA_LOST_IP");
202 } 202 }
203 break; 203 break;
204 204
205 default: 205 default:
206 ESP_LOGI(TAG, "Unknown IP event %d", event_id); 206 ESP_LOGW(TAG, "Unknown IP event %d", event_id);
207 break; 207 break;
208 } 208 }
209 } 209 }
210 210
211 211
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);

mercurial