|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Unlicense OR CC0-1.0 |
| 5 | + */ |
| 6 | +/* WiFi example |
| 7 | +
|
| 8 | + This example code is in the Public Domain (or CC0 licensed, at your option.) |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, this |
| 11 | + software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 | + CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +*/ |
| 14 | +#include <string.h> |
| 15 | +#include "freertos/FreeRTOS.h" |
| 16 | +#include "freertos/task.h" |
| 17 | +#include "freertos/event_groups.h" |
| 18 | +#include "esp_system.h" |
| 19 | +#include "esp_wifi.h" |
| 20 | +#include "esp_event.h" |
| 21 | +#include "esp_log.h" |
| 22 | +#include "nvs_flash.h" |
| 23 | + |
| 24 | +#include "lwip/err.h" |
| 25 | +#include "lwip/sys.h" |
| 26 | + |
| 27 | +/* The examples use WiFi configuration that you can set via flashing nvs partition */ |
| 28 | + |
| 29 | +/* FreeRTOS event group to signal when we are connected*/ |
| 30 | +static EventGroupHandle_t s_wifi_event_group; |
| 31 | + |
| 32 | +/* The event group allows multiple bits for each event, but we only care about two events: |
| 33 | + * - we are connected to the AP with an IP |
| 34 | + * - we failed to connect after the maximum amount of retries */ |
| 35 | +#define WIFI_CONNECTED_BIT BIT0 |
| 36 | +#define WIFI_FAIL_BIT BIT1 |
| 37 | + |
| 38 | +#define MAXIMUM_RETRY_COUNT 5 |
| 39 | + |
| 40 | +static const char *TAG = "wifi_nvs_config"; |
| 41 | + |
| 42 | +static int s_retry_num = 0; |
| 43 | + |
| 44 | + |
| 45 | +static void event_handler(void* arg, esp_event_base_t event_base, |
| 46 | + int32_t event_id, void* event_data) |
| 47 | +{ |
| 48 | + if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_START) { |
| 49 | + ESP_LOGI(TAG, "SoftAP started successfully!"); |
| 50 | + } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_STACONNECTED) { |
| 51 | + wifi_event_ap_staconnected_t* event = (wifi_event_ap_staconnected_t*) event_data; |
| 52 | + ESP_LOGI(TAG, "station join, AID=%d", event->aid); |
| 53 | + } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_STADISCONNECTED) { |
| 54 | + wifi_event_ap_stadisconnected_t* event = (wifi_event_ap_stadisconnected_t*) event_data; |
| 55 | + ESP_LOGI(TAG, "station leave, AID=%d, reason=%d", event->aid, event->reason); |
| 56 | + } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) { |
| 57 | + esp_wifi_connect(); |
| 58 | + } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { |
| 59 | + if (s_retry_num < MAXIMUM_RETRY_COUNT) { |
| 60 | + esp_wifi_connect(); |
| 61 | + s_retry_num++; |
| 62 | + ESP_LOGI(TAG, "retry to connect to the AP"); |
| 63 | + } else { |
| 64 | + xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT); |
| 65 | + } |
| 66 | + ESP_LOGI(TAG,"connect to the AP fail"); |
| 67 | + } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { |
| 68 | + ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; |
| 69 | + ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip)); |
| 70 | + s_retry_num = 0; |
| 71 | + xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +void wifi_init(void) |
| 76 | +{ |
| 77 | + s_wifi_event_group = xEventGroupCreate(); |
| 78 | + |
| 79 | + ESP_ERROR_CHECK(esp_netif_init()); |
| 80 | + |
| 81 | + ESP_ERROR_CHECK(esp_event_loop_create_default()); |
| 82 | + |
| 83 | + esp_netif_create_default_wifi_sta(); |
| 84 | + esp_netif_create_default_wifi_ap(); |
| 85 | + |
| 86 | + wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); |
| 87 | + ESP_ERROR_CHECK(esp_wifi_init(&cfg)); |
| 88 | + |
| 89 | + esp_event_handler_instance_t instance_any_id; |
| 90 | + ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, |
| 91 | + ESP_EVENT_ANY_ID, |
| 92 | + &event_handler, |
| 93 | + NULL, |
| 94 | + &instance_any_id)); |
| 95 | + esp_event_handler_instance_t instance_got_ip; |
| 96 | + ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, |
| 97 | + IP_EVENT_STA_GOT_IP, |
| 98 | + &event_handler, |
| 99 | + NULL, |
| 100 | + &instance_got_ip)); |
| 101 | + |
| 102 | + ESP_ERROR_CHECK(esp_wifi_start() ); |
| 103 | + |
| 104 | + ESP_LOGI(TAG, "wifi_init finished."); |
| 105 | + |
| 106 | + /* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum |
| 107 | + * number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */ |
| 108 | + EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group, |
| 109 | + WIFI_CONNECTED_BIT | WIFI_FAIL_BIT, |
| 110 | + pdFALSE, |
| 111 | + pdFALSE, |
| 112 | + portMAX_DELAY); |
| 113 | + |
| 114 | + /* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually |
| 115 | + * happened. */ |
| 116 | + if (bits & WIFI_CONNECTED_BIT) { |
| 117 | + ESP_LOGI(TAG, "Successfully connected to AP"); |
| 118 | + } else if (bits & WIFI_FAIL_BIT) { |
| 119 | + ESP_LOGI(TAG, "Failed to connect to AP"); |
| 120 | + } else { |
| 121 | + ESP_LOGE(TAG, "UNEXPECTED EVENT"); |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +void app_main(void) |
| 126 | +{ |
| 127 | + //Initialize NVS |
| 128 | + esp_err_t ret = nvs_flash_init(); |
| 129 | + if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { |
| 130 | + ESP_ERROR_CHECK(nvs_flash_erase()); |
| 131 | + ret = nvs_flash_init(); |
| 132 | + } |
| 133 | + ESP_ERROR_CHECK(ret); |
| 134 | + |
| 135 | + wifi_init(); |
| 136 | +} |
0 commit comments