41 const int TASK_WIFI_REQUEST_WIFI_SCAN = BIT0; ///< When set, means a client requested to scan wireless networks. |
43 const int TASK_WIFI_REQUEST_WIFI_SCAN = BIT0; ///< When set, means a client requested to scan wireless networks. |
42 const int TASK_WIFI_REQUEST_STA_DISCONNECT = BIT1; ///< When set, means a client requested to disconnect from currently connected AP. |
44 const int TASK_WIFI_REQUEST_STA_DISCONNECT = BIT1; ///< When set, means a client requested to disconnect from currently connected AP. |
43 const int TASK_WIFI_REQUEST_STA_CONNECT = BIT2; ///< When set, means a client requested to connect to an access point. |
45 const int TASK_WIFI_REQUEST_STA_CONNECT = BIT2; ///< When set, means a client requested to connect to an access point. |
44 |
46 |
45 const int TASK_WIFI_HAS_IP = BIT3; ///< Indicate that we have an IP address |
47 const int TASK_WIFI_HAS_IP = BIT3; ///< Indicate that we have an IP address |
46 const int TASK_WIFI_AP_STARTED = BIT4; ///< Indicate that the SoftAP is started |
|
47 const int TASK_WIFI_STA_FAILED = BIT5; ///< Indicate that we could not get a connection to AP as station. |
48 const int TASK_WIFI_STA_FAILED = BIT5; ///< Indicate that we could not get a connection to AP as station. |
48 const int TASK_WIFI_STA_DISCONNECTED = BIT6; ///< Indicate that we are disconnected from an ap station. |
49 const int TASK_WIFI_STA_DISCONNECTED = BIT6; ///< Indicate that we are disconnected from an ap station. |
49 const int TASK_WIFI_STA_CONNECTED = BIT7; ///< Indicate that we are connected to AP as station, flip of BIT6. |
50 const int TASK_WIFI_STA_CONNECTED = BIT7; ///< Indicate that we are connected to AP as station, flip of BIT6. |
50 |
51 |
51 |
52 |
234 if (FetchStaConfig()) { |
235 if (FetchStaConfig()) { |
235 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); |
236 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_REQUEST_STA_CONNECT); |
236 } |
237 } |
237 break; |
238 break; |
238 } |
239 } |
239 case WIFI_EVENT_AP_START: |
|
240 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_AP_STARTED); |
|
241 if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) { |
|
242 wifi_state->AP_active = true; |
|
243 wifi_state->AP_clients = 0; |
|
244 xSemaphoreGive(xSemaphoreWiFi); |
|
245 } else { |
|
246 ESP_LOGE(TAG, "wifi_event_handler() lock error WIFI_EVENT_AP_START"); |
|
247 } |
|
248 break; |
|
249 |
|
250 case WIFI_EVENT_AP_STOP: |
|
251 xEventGroupClearBits(xEventGroupWifi, TASK_WIFI_AP_STARTED); |
|
252 if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) { |
|
253 wifi_state->AP_active = false; |
|
254 wifi_state->AP_clients = 0; |
|
255 xSemaphoreGive(xSemaphoreWiFi); |
|
256 } else { |
|
257 ESP_LOGE(TAG, "wifi_event_handler() lock error WIFI_EVENT_AP_STOP"); |
|
258 } |
|
259 break; |
|
260 |
|
261 case WIFI_EVENT_AP_STACONNECTED: { |
|
262 if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) { |
|
263 wifi_state->AP_clients++; |
|
264 xSemaphoreGive(xSemaphoreWiFi); |
|
265 } else { |
|
266 ESP_LOGE(TAG, "wifi_event_handler() lock error WIFI_EVENT_AP_STACONNECTED"); |
|
267 } |
|
268 wifi_event_ap_staconnected_t* staconnected = (wifi_event_ap_staconnected_t*) event_data; |
|
269 ESP_LOGI(TAG, "Event AP connected, mac:" MACSTR ", aid:%d, conns:%d", |
|
270 MAC2STR(staconnected->mac), staconnected->aid, wifi_state->AP_clients); |
|
271 break; |
|
272 } |
|
273 case WIFI_EVENT_AP_STADISCONNECTED: { |
|
274 if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) { |
|
275 if (wifi_state->AP_clients > 0) |
|
276 wifi_state->AP_clients--; |
|
277 else |
|
278 wifi_state->AP_clients = 0; |
|
279 xSemaphoreGive(xSemaphoreWiFi); |
|
280 } else { |
|
281 ESP_LOGE(TAG, "wifi_event_handler() lock error WIFI_EVENT_AP_STADISCONNECTED"); |
|
282 } |
|
283 wifi_event_ap_stadisconnected_t* stadisconnected = (wifi_event_ap_stadisconnected_t*) event_data; |
|
284 ESP_LOGI(TAG, "Event AP disconnected, mac:" MACSTR ", aid:%d, conns:%d", |
|
285 MAC2STR(stadisconnected->mac), stadisconnected->aid, wifi_state->AP_clients); |
|
286 break; |
|
287 } |
|
288 default: |
240 default: |
289 ESP_LOGW(TAG, "Unknown WiFi event %d", event_id); |
241 ESP_LOGW(TAG, "Unknown WiFi event %d", event_id); |
290 break; |
242 break; |
291 } |
243 } |
292 } |
244 } |
300 case IP_EVENT_STA_GOT_IP: |
252 case IP_EVENT_STA_GOT_IP: |
301 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_HAS_IP); |
253 xEventGroupSetBits(xEventGroupWifi, TASK_WIFI_HAS_IP); |
302 ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; |
254 ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; |
303 if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) { |
255 if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) { |
304 wifi_state->STA_online = true; |
256 wifi_state->STA_online = true; |
305 snprintf(wifi_state->STA_ip, 16, "%s", ip4addr_ntoa(&event->ip_info.ip)); |
257 snprintf(wifi_state->STA_ip, 16, IPSTR, IP2STR(&event->ip_info.ip)); |
306 snprintf(wifi_state->STA_nm, 16, "%s", ip4addr_ntoa(&event->ip_info.netmask)); |
258 snprintf(wifi_state->STA_nm, 16, IPSTR, IP2STR(&event->ip_info.netmask)); |
307 snprintf(wifi_state->STA_gw, 16, "%s", ip4addr_ntoa(&event->ip_info.gw)); |
259 snprintf(wifi_state->STA_gw, 16, IPSTR, IP2STR(&event->ip_info.gw)); |
308 xSemaphoreGive(xSemaphoreWiFi); |
260 xSemaphoreGive(xSemaphoreWiFi); |
309 } else { |
261 } else { |
310 ESP_LOGE(TAG, "got_ip_event_handler() lock error IP_EVENT_STA_GOT_IP"); |
262 ESP_LOGE(TAG, "got_ip_event_handler() lock error IP_EVENT_STA_GOT_IP"); |
311 } |
263 } |
312 |
264 |
383 |
335 |
384 /* |
336 /* |
385 * Initialize NVS |
337 * Initialize NVS |
386 */ |
338 */ |
387 ret = nvs_flash_init(); |
339 ret = nvs_flash_init(); |
388 if (ret == ESP_ERR_NVS_NO_FREE_PAGES) { |
340 if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { |
389 ESP_ERROR_CHECK(nvs_flash_erase()); |
341 ESP_ERROR_CHECK(nvs_flash_erase()); |
390 ret = nvs_flash_init(); |
342 ret = nvs_flash_init(); |
391 } |
343 } |
392 ESP_ERROR_CHECK(ret); |
344 ESP_ERROR_CHECK(ret); |
393 |
345 |
394 /* event handler and event group for the wifi driver */ |
346 /* event handler and event group for the wifi driver */ |
395 xEventGroupWifi = xEventGroupCreate(); |
347 xEventGroupWifi = xEventGroupCreate(); |
396 /* initialize the tcp stack */ |
348 /* initialize the tcp stack */ |
397 tcpip_adapter_init(); |
349 ESP_ERROR_CHECK(esp_netif_init()); |
398 ESP_ERROR_CHECK(esp_event_loop_create_default()); |
350 ESP_ERROR_CHECK(esp_event_loop_create_default()); |
|
351 sta_netif = esp_netif_create_default_wifi_sta(); |
|
352 assert(sta_netif); |
399 |
353 |
400 /* |
354 /* |
401 * memory allocation of objects used by the task |
355 * memory allocation of objects used by the task |
402 */ |
356 */ |
403 accessp_records = (wifi_ap_record_t*)malloc(sizeof(wifi_ap_record_t) * MAX_AP_NUM); |
357 accessp_records = (wifi_ap_record_t*)malloc(sizeof(wifi_ap_record_t) * MAX_AP_NUM); |
404 task_wifi_ConfigSTA = (wifi_config_t*)malloc(sizeof(wifi_config_t)); |
358 task_wifi_ConfigSTA = (wifi_config_t*)malloc(sizeof(wifi_config_t)); |
405 memset(task_wifi_ConfigSTA, 0x00, sizeof(wifi_config_t)); |
359 memset(task_wifi_ConfigSTA, 0x00, sizeof(wifi_config_t)); |
406 |
360 |
407 xSemaphoreWiFi = xSemaphoreCreateMutex(); |
361 xSemaphoreWiFi = xSemaphoreCreateMutex(); |
408 wifi_state = malloc(sizeof(WIFI_State)); |
362 wifi_state = malloc(sizeof(WIFI_State)); |
409 wifi_state->AP_clients = 0; |
|
410 wifi_state->AP_active = false; |
|
411 wifi_state->STA_connected = false; |
363 wifi_state->STA_connected = false; |
412 wifi_state->STA_online = false; |
364 wifi_state->STA_online = false; |
413 wifi_state->STA_rssi = 0; |
365 wifi_state->STA_rssi = 0; |
414 |
366 |
415 /* |
367 /* |
416 * start the softAP access point |
368 * init wifi as station |
417 * stop DHCP server |
|
418 */ |
|
419 ESP_ERROR_CHECK(tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP)); |
|
420 |
|
421 /* |
|
422 * Assign a static IP to the AP network interface |
|
423 */ |
|
424 tcpip_adapter_ip_info_t info; |
|
425 memset(&info, 0x00, sizeof(info)); |
|
426 IP4_ADDR(&info.ip, 192, 168, 1, 1); |
|
427 IP4_ADDR(&info.gw, 192, 168, 1, 1); |
|
428 IP4_ADDR(&info.netmask, 255, 255, 255, 0); |
|
429 ESP_ERROR_CHECK(tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_AP, &info)); |
|
430 |
|
431 /* start dhcp server */ |
|
432 ESP_ERROR_CHECK(tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_AP)); |
|
433 ESP_LOGI(TAG, "AP start dhcps ip: 192.168.1.1 nm: 255.255.255.0 gw: 192.168.1.1"); |
|
434 |
|
435 /* start dhcp client */ |
|
436 ESP_ERROR_CHECK(tcpip_adapter_dhcpc_start(TCPIP_ADAPTER_IF_STA)); |
|
437 |
|
438 /* |
|
439 * init wifi as station + access point |
|
440 */ |
369 */ |
441 wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT(); |
370 wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT(); |
442 ESP_ERROR_CHECK(esp_wifi_init(&wifi_init_config)); |
371 ESP_ERROR_CHECK(esp_wifi_init(&wifi_init_config)); |
443 |
372 |
444 ESP_ERROR_CHECK( esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL) ); |
373 esp_event_handler_instance_t instance_any_id; |
445 ESP_ERROR_CHECK( esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID /*IP_EVENT_STA_GOT_IP*/, &got_ip_event_handler, NULL) ); |
374 esp_event_handler_instance_t instance_got_ip; |
|
375 ESP_ERROR_CHECK( esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL, &instance_any_id) ); |
|
376 ESP_ERROR_CHECK( esp_event_handler_instance_register(IP_EVENT, ESP_EVENT_ANY_ID, &got_ip_event_handler, NULL, &instance_got_ip) ); |
446 |
377 |
447 ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); |
378 ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM)); |
448 ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_APSTA)); |
379 ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); |
449 ESP_ERROR_CHECK(esp_wifi_set_bandwidth(WIFI_IF_AP, config.ap_bandwidth)); |
380 |
450 |
|
451 /* configure the softAP and start it */ |
|
452 wifi_config_t ap_config = { |
|
453 .ap = { |
|
454 .ssid_len = 0, |
|
455 .channel = config.ap_channel, |
|
456 .authmode = WIFI_AUTH_WPA2_PSK, |
|
457 .ssid_hidden = config.ap_ssid_hidden, |
|
458 .max_connection = AP_MAX_CONNECTIONS, |
|
459 .beacon_interval = 100, |
|
460 }, |
|
461 }; |
|
462 memcpy(ap_config.ap.ssid, config.ap_ssid , sizeof(config.ap_ssid)); |
|
463 memcpy(ap_config.ap.password, config.ap_pwd, sizeof(config.ap_pwd)); |
|
464 ret = esp_wifi_set_config(WIFI_IF_AP, &ap_config); |
|
465 if (ret != ESP_OK) { |
|
466 ESP_LOGE(TAG, "esp_wifi_set_config(WIFI_IF_AP, nnn) rc=%d", ret); |
|
467 } |
|
468 ESP_ERROR_CHECK(esp_wifi_start()); |
381 ESP_ERROR_CHECK(esp_wifi_start()); |
469 |
|
470 ESP_LOGI(TAG, "AP start ssid:`%s' pwd:`%s' channel:%d, hidden:%s", |
|
471 ap_config.ap.ssid, ap_config.ap.password, ap_config.ap.channel, ap_config.ap.ssid_hidden ? "yes":"no"); |
|
472 |
382 |
473 /* |
383 /* |
474 * try to get access to previously saved wifi |
384 * try to get access to previously saved wifi |
475 */ |
385 */ |
476 if (FetchStaConfig()) { |
386 if (FetchStaConfig()) { |
651 esp_wifi_sta_get_ap_info(&ap_info); |
560 esp_wifi_sta_get_ap_info(&ap_info); |
652 |
561 |
653 wifi_config_t *wconfig = task_wifi_ConfigSTA /*task_wifi_GetWifiStaConfig( ) */; |
562 wifi_config_t *wconfig = task_wifi_ConfigSTA /*task_wifi_GetWifiStaConfig( ) */; |
654 if (wconfig) { |
563 if (wconfig) { |
655 |
564 |
656 tcpip_adapter_ip_info_t ip_info; |
565 esp_netif_ip_info_t ip_info; |
657 ESP_ERROR_CHECK(tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip_info)); |
566 ESP_ERROR_CHECK(esp_netif_get_ip_info(sta_netif, &ip_info)); |
658 char ip[IP4ADDR_STRLEN_MAX]; |
567 char ip[IP4ADDR_STRLEN_MAX]; |
659 char gw[IP4ADDR_STRLEN_MAX]; |
568 char gw[IP4ADDR_STRLEN_MAX]; |
660 char netmask[IP4ADDR_STRLEN_MAX]; |
569 char netmask[IP4ADDR_STRLEN_MAX]; |
661 strcpy(ip, ip4addr_ntoa(&ip_info.ip)); |
570 sprintf(ip, IPSTR, IP2STR(&ip_info.ip)); |
662 strcpy(netmask, ip4addr_ntoa(&ip_info.netmask)); |
571 sprintf(netmask, IPSTR, IP2STR(&ip_info.netmask)); |
663 strcpy(gw, ip4addr_ntoa(&ip_info.gw)); |
572 sprintf(gw, IPSTR, IP2STR(&ip_info.gw)); |
664 TFT_setFont(DEFAULT_FONT, NULL); |
573 TFT_setFont(DEFAULT_FONT, NULL); |
665 _fg = TFT_WHITE; |
574 _fg = TFT_WHITE; |
666 TFT_print((char *)"SSID", 155 - TFT_getStringWidth((char *)"SSID"), 40); |
575 TFT_print((char *)"SSID", 155 - TFT_getStringWidth((char *)"SSID"), 40); |
667 TFT_print((char *)"Kanaal", 155 - TFT_getStringWidth((char *)"Kanaal"), 60); |
576 TFT_print((char *)"Kanaal", 155 - TFT_getStringWidth((char *)"Kanaal"), 60); |
668 TFT_print((char *)"Rssi", 155 - TFT_getStringWidth((char *)"Rssi"), 80); |
577 TFT_print((char *)"Rssi", 155 - TFT_getStringWidth((char *)"Rssi"), 80); |