GET wifi status as connected or not connected

microwrk234
Posts: 4
Joined: Tue Nov 22, 2022 11:25 am

Re: GET wifi status as connected or not connected

Postby microwrk234 » Fri Nov 25, 2022 4:10 am

  1. [/#include <string.h>
  2. #include "freertos/FreeRTOS.h"
  3. #include "freertos/task.h"
  4. #include "freertos/event_groups.h"
  5. #include "esp_system.h"
  6. #include "esp_wifi.h"
  7. #include "esp_event_loop.h"
  8. #include "esp_log.h"
  9. #include "nvs_flash.h"
  10.  
  11. #include "lwip/err.h"
  12. #include "lwip/sys.h"
  13.  
  14. #define EXAMPLE_ESP_WIFI_SSID      CONFIG_ESP_WIFI_SSID
  15. #define EXAMPLE_ESP_WIFI_PASS      CONFIG_ESP_WIFI_PASSWORD
  16. #define EXAMPLE_ESP_MAXIMUM_RETRY  CONFIG_ESP_MAXIMUM_RETRY
  17.  
  18. static EventGroupHandle_t s_wifi_event_group;
  19. const int WIFI_CONNECTED_BIT = BIT0;
  20. static const char *TAG = "wifi station";
  21. static int s_retry_num = 0;
  22.  
  23.  
  24. static void check_status()
  25. {
  26.     EventBits_t uxBits;
  27.     uxBits = xEventGroupGetBits(s_wifi_event_group);
  28.     if(uxBits & WIFI_CONNECTED_BIT)
  29.     {
  30.         printf ("wifi connected\n");
  31.     }
  32.    
  33.     else if (!(uxBits & WIFI_CONNECTED_BIT))
  34.     {
  35.         printf ("wifi disconnected\n");
  36.     }
  37.    
  38. }
  39.  
  40. static esp_err_t event_handler(void *ctx, system_event_t *event)
  41. {
  42.     switch(event->event_id)
  43.     {
  44.     case SYSTEM_EVENT_STA_START:
  45.         esp_wifi_connect();
  46.         break;
  47.     case SYSTEM_EVENT_STA_GOT_IP:
  48.         ESP_LOGI(TAG, "got ip:%s",
  49.                  ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip));
  50.         s_retry_num = 0;
  51.         xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
  52.        
  53.         check_status(); // To update the wifi status
  54.         break;
  55.        
  56.     case SYSTEM_EVENT_STA_DISCONNECTED:
  57.     {
  58.         esp_wifi_connect();
  59.         xEventGroupClearBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
  60.        
  61.         check_status(); // To update the wifi status
  62.         break;       
  63.     }
  64.     default:
  65.         break;
  66.     }
  67.     return ESP_OK;
  68. }
  69.  
  70. void wifi_init_sta()
  71. {
  72.     s_wifi_event_group = xEventGroupCreate();
  73.  
  74.     tcpip_adapter_init();
  75.     ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL) );
  76.  
  77.     wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  78.     ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  79.     wifi_config_t wifi_config = {
  80.         .sta = {
  81.             .ssid = EXAMPLE_ESP_WIFI_SSID,
  82.             .password = EXAMPLE_ESP_WIFI_PASS
  83.         },
  84.     };
  85.  
  86.     ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
  87.     ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
  88.     ESP_ERROR_CHECK(esp_wifi_start() );
  89.  
  90.     ESP_LOGI(TAG, "wifi_init_sta finished.");
  91.     ESP_LOGI(TAG, "connect to ap SSID:%s password:%s",
  92.              EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
  93. }
  94.  
  95. void app_main()
  96. {
  97.     //Initialize NVS
  98.     esp_err_t ret = nvs_flash_init();
  99.     if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  100.       ESP_ERROR_CHECK(nvs_flash_erase());
  101.       ret = nvs_flash_init();
  102.     }
  103.     ESP_ERROR_CHECK(ret);
  104.    
  105.     ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
  106.     wifi_init_sta();
  107. }]

Who is online

Users browsing this forum: No registered users and 64 guests