1 /* WiFi station Example
2
3 This example code is in the Public Domain (or CC0 licensed, at your option.)
4
5 Unless required by applicable law or agreed to in writing, this
6 software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
7 CONDITIONS OF ANY KIND, either express or implied.
8 */
9 #include <string.h>
10 #include "freertos/FreeRTOS.h"
11 #include "freertos/task.h"
12 #include "freertos/event_groups.h"
13 #include "esp_system.h"
14 #include "esp_wifi.h"
15 #include "esp_event.h"
16 #include "esp_log.h"
17 #include "nvs_flash.h"
18
19 #include "lwip/err.h"
20 #include "lwip/sys.h"
21
22 /* The examples use WiFi configuration that you can set via project configuration menu
23
24 If you'd rather not, just change the below entries to strings with
25 the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
26 */
27 #define EXAMPLE_ESP_WIFI_SSID CONFIG_ESP_WIFI_SSID
28 #define EXAMPLE_ESP_WIFI_PASS CONFIG_ESP_WIFI_PASSWORD
29 #define EXAMPLE_ESP_MAXIMUM_RETRY CONFIG_ESP_MAXIMUM_RETRY
30
31 /* FreeRTOS event group to signal when we are connected*/
32 static EventGroupHandle_t s_wifi_event_group;
33
34 /* The event group allows multiple bits for each event, but we only care about two events:
35 * - we are connected to the AP with an IP
36 * - we failed to connect after the maximum amount of retries */
37 #define WIFI_CONNECTED_BIT BIT0
38 #define WIFI_FAIL_BIT BIT1
39
40 static const char *TAG = "wifi station";
41
42 static int s_retry_num = 0;
43
event_handler(void * arg,esp_event_base_t event_base,int32_t event_id,void * event_data)44 static void event_handler(void* arg, esp_event_base_t event_base,
45 int32_t event_id, void* event_data)
46 {
47 if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
48 esp_wifi_connect();
49 } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
50 if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY) {
51 esp_wifi_connect();
52 s_retry_num++;
53 ESP_LOGI(TAG, "retry to connect to the AP");
54 } else {
55 xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
56 }
57 ESP_LOGI(TAG,"connect to the AP fail");
58 } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
59 ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
60 ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
61 s_retry_num = 0;
62 xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
63 }
64 }
65
wifi_init_sta(void)66 void wifi_init_sta(void)
67 {
68 s_wifi_event_group = xEventGroupCreate();
69
70 ESP_ERROR_CHECK(esp_netif_init());
71
72 ESP_ERROR_CHECK(esp_event_loop_create_default());
73 esp_netif_create_default_wifi_sta();
74
75 wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
76 ESP_ERROR_CHECK(esp_wifi_init(&cfg));
77
78 esp_event_handler_instance_t instance_any_id;
79 esp_event_handler_instance_t instance_got_ip;
80 ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
81 ESP_EVENT_ANY_ID,
82 &event_handler,
83 NULL,
84 &instance_any_id));
85 ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
86 IP_EVENT_STA_GOT_IP,
87 &event_handler,
88 NULL,
89 &instance_got_ip));
90
91 wifi_config_t wifi_config = {
92 .sta = {
93 .ssid = EXAMPLE_ESP_WIFI_SSID,
94 .password = EXAMPLE_ESP_WIFI_PASS,
95 /* Setting a password implies station will connect to all security modes including WEP/WPA.
96 * However these modes are deprecated and not advisable to be used. Incase your Access point
97 * doesn't support WPA2, these mode can be enabled by commenting below line */
98 .threshold.authmode = WIFI_AUTH_WPA2_PSK,
99 },
100 };
101 ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
102 ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
103 ESP_ERROR_CHECK(esp_wifi_start() );
104
105 ESP_LOGI(TAG, "wifi_init_sta finished.");
106
107 /* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum
108 * number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */
109 EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
110 WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
111 pdFALSE,
112 pdFALSE,
113 portMAX_DELAY);
114
115 /* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
116 * happened. */
117 if (bits & WIFI_CONNECTED_BIT) {
118 ESP_LOGI(TAG, "connected to ap SSID:%s password:%s",
119 EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
120 } else if (bits & WIFI_FAIL_BIT) {
121 ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s",
122 EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
123 } else {
124 ESP_LOGE(TAG, "UNEXPECTED EVENT");
125 }
126
127 /* The event will not be processed after unregister */
128 ESP_ERROR_CHECK(esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, instance_got_ip));
129 ESP_ERROR_CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, instance_any_id));
130 vEventGroupDelete(s_wifi_event_group);
131 }
132
app_main(void)133 void app_main(void)
134 {
135 //Initialize NVS
136 esp_err_t ret = nvs_flash_init();
137 if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
138 ESP_ERROR_CHECK(nvs_flash_erase());
139 ret = nvs_flash_init();
140 }
141 ESP_ERROR_CHECK(ret);
142
143 ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
144 wifi_init_sta();
145 }
146