main/task_wifi.c

changeset 35
9827c5a08c63
parent 24
74609f70411e
equal deleted inserted replaced
34:40231d010111 35:9827c5a08c63
51 if (connect) 51 if (connect)
52 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); 52 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT);
53 else 53 else
54 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_DISCONNECT); 54 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_DISCONNECT);
55 } 55 }
56
57 56
58 57
59 static void wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) 58 static void wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
60 { 59 {
61 switch (event_id) { 60 switch (event_id) {
73 72
74 case WIFI_EVENT_STA_CONNECTED: { 73 case WIFI_EVENT_STA_CONNECTED: {
75 wifi_ap_record_t ap_info; 74 wifi_ap_record_t ap_info;
76 esp_wifi_sta_get_ap_info(&ap_info); 75 esp_wifi_sta_get_ap_info(&ap_info);
77 ESP_LOGI(TAG, "Event STA connected rssi=%d", ap_info.rssi); 76 ESP_LOGI(TAG, "Event STA connected rssi=%d", ap_info.rssi);
78 if (xSemaphoreTake(xSemaphoreWiFi, 35) == pdTRUE) { 77 if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) {
79 wifi_state->STA_connected = true; 78 wifi_state->STA_connected = true;
80 wifi_state->STA_rssi = ap_info.rssi; 79 wifi_state->STA_rssi = ap_info.rssi;
81 xSemaphoreGive(xSemaphoreWiFi); 80 xSemaphoreGive(xSemaphoreWiFi);
82 } else { 81 } else {
83 ESP_LOGE(TAG, "wifi_event_handler() lock error WIFI_EVENT_STA_CONNECTED"); 82 ESP_LOGE(TAG, "wifi_event_handler() lock error WIFI_EVENT_STA_CONNECTED");
220 } 219 }
221 220
222 221
223 void task_wifi( void * pvParameters ) 222 void task_wifi( void * pvParameters )
224 { 223 {
225 // uint64_t starttime = 0;
226
227 ESP_LOGI(TAG, "Starting WiFi task"); 224 ESP_LOGI(TAG, "Starting WiFi task");
228 esp_log_level_set("wifi", ESP_LOG_ERROR); 225 esp_log_level_set("wifi", ESP_LOG_ERROR);
229 226
230 /* 227 /*
231 * memory allocation of objects used by the task 228 * memory allocation of objects used by the task
250 for(;;) { 247 for(;;) {
251 248
252 /* 249 /*
253 * Actions that can trigger: request a connection or a disconnection 250 * Actions that can trigger: request a connection or a disconnection
254 */ 251 */
255 uxBits = xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT | TASK_WIFI_REQUEST_STA_DISCONNECT, 252 uxBits = xEventGroupWaitBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT | TASK_WIFI_REQUEST_STA_DISCONNECT, pdFALSE, pdFALSE, portMAX_DELAY );
256 pdFALSE, pdFALSE, portMAX_DELAY );
257 253
258 if (uxBits & TASK_WIFI_REQUEST_STA_DISCONNECT) { 254 if (uxBits & TASK_WIFI_REQUEST_STA_DISCONNECT) {
259 /* 255 /*
260 * user requested a disconnect, this will in effect disconnect the wifi 256 * user requested a disconnect, this will in effect disconnect the wifi
261 */ 257 */
265 261
266 /* 262 /*
267 * Finally: release the request bit 263 * Finally: release the request bit
268 */ 264 */
269 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_DISCONNECT); 265 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_DISCONNECT);
270 // ESP_LOGI(TAG, "Connection time %llu", (esp_timer_get_time() / 1000) - starttime);
271 266
272 } else if (uxBits & TASK_WIFI_REQUEST_STA_CONNECT) { 267 } else if (uxBits & TASK_WIFI_REQUEST_STA_CONNECT) {
273 268
274 ESP_LOGI(TAG, "Request STA connect `%s' `%s'", ESP_WIFI_SSID, ESP_WIFI_PASS); 269 ESP_LOGI(TAG, "Request STA connect `%s' `%s'", ESP_WIFI_SSID, ESP_WIFI_PASS);
275 // starttime = esp_timer_get_time() / 1000;
276 wifi_connect(); 270 wifi_connect();
277 271
278 /* 272 /*
279 * 3 scenarios here: connection is successful and TASK_WIFI_STA_CONNECTED will be posted 273 * 3 scenarios here: connection is successful and TASK_WIFI_STA_CONNECTED will be posted
280 * or it's a failure and we get a TASK_WIFI_STA_FAILED with a reason code. 274 * or it's a failure and we get a TASK_WIFI_STA_FAILED with a reason code.

mercurial