1 /* Local Ctrl 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 
10 #include <string.h>
11 #include "freertos/FreeRTOS.h"
12 #include "freertos/task.h"
13 #include "freertos/event_groups.h"
14 #include "esp_system.h"
15 #include "esp_wifi.h"
16 #include "esp_event.h"
17 #include "esp_log.h"
18 #include "nvs_flash.h"
19 #include "sdkconfig.h"
20 
21 #include "lwip/err.h"
22 #include "lwip/sys.h"
23 
24 /* The examples use WiFi configuration that you can set via 'idf.py menuconfig'.
25 
26    If you'd rather not, just change the below entries to strings with
27    the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
28 */
29 #define EXAMPLE_ESP_WIFI_SSID      CONFIG_EXAMPLE_WIFI_SSID
30 #define EXAMPLE_ESP_WIFI_PASS      CONFIG_EXAMPLE_WIFI_PASSWORD
31 #define EXAMPLE_ESP_MAXIMUM_RETRY  CONFIG_EXAMPLE_MAXIMUM_RETRY
32 
33 /* FreeRTOS event group to signal when we are connected*/
34 static EventGroupHandle_t s_wifi_event_group;
35 
36 /* The event group allows multiple bits for each event, but we only care about two events:
37  * - we are connected to the AP with an IP
38  * - we failed to connect after the maximum amount of retries */
39 #define WIFI_CONNECTED_BIT BIT0
40 #define WIFI_FAIL_BIT      BIT1
41 
42 static const char *TAG = "local_ctrl_example";
43 
44 static int s_retry_num = 0;
45 
event_handler(void * arg,esp_event_base_t event_base,int32_t event_id,void * event_data)46 static void event_handler(void* arg, esp_event_base_t event_base,
47                                 int32_t event_id, void* event_data)
48 {
49     if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
50         esp_wifi_connect();
51     } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
52         if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY) {
53             esp_wifi_connect();
54             s_retry_num++;
55             ESP_LOGI(TAG, "retry to connect to the AP");
56         } else {
57             xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
58         }
59         ESP_LOGI(TAG,"connect to the AP fail");
60     } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
61         ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
62         ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
63         s_retry_num = 0;
64         xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
65     }
66 }
67 
wifi_init_sta(void)68 esp_err_t wifi_init_sta(void)
69 {
70     esp_err_t ret_value = ESP_OK;
71     s_wifi_event_group = xEventGroupCreate();
72 
73     ESP_ERROR_CHECK(esp_netif_init());
74 
75     ESP_ERROR_CHECK(esp_event_loop_create_default());
76     esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta();
77     assert(sta_netif);
78 
79     wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
80     ESP_ERROR_CHECK(esp_wifi_init(&cfg));
81 
82     ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
83     ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL));
84 
85     wifi_config_t wifi_config = {
86         .sta = {
87             .ssid = EXAMPLE_ESP_WIFI_SSID,
88             .password = EXAMPLE_ESP_WIFI_PASS
89         },
90     };
91     ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
92     ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
93     ESP_ERROR_CHECK(esp_wifi_start() );
94 
95     ESP_LOGI(TAG, "wifi_init_sta finished.");
96 
97     /* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum
98      * number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */
99     EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
100             WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
101             pdFALSE,
102             pdFALSE,
103             portMAX_DELAY);
104 
105     /* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
106      * happened. */
107     if (bits & WIFI_CONNECTED_BIT) {
108         ESP_LOGI(TAG, "connected to ap SSID:%s password:%s",
109                  EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
110     } else if (bits & WIFI_FAIL_BIT) {
111         ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s",
112                  EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
113         ret_value = ESP_FAIL;
114     } else {
115         ESP_LOGE(TAG, "UNEXPECTED EVENT");
116         ret_value = ESP_ERR_INVALID_STATE;
117     }
118 
119     ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler));
120     ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler));
121     vEventGroupDelete(s_wifi_event_group);
122     return ret_value;
123 }
124 
125 /* Function responsible for configuring and starting the esp_local_ctrl service.
126  * See local_ctrl_service.c for implementation */
127 extern void start_esp_local_ctrl_service(void);
128 
app_main(void)129 void app_main(void)
130 {
131     //Initialize NVS
132     esp_err_t ret = nvs_flash_init();
133     if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
134       ESP_ERROR_CHECK(nvs_flash_erase());
135       ret = nvs_flash_init();
136     }
137     ESP_ERROR_CHECK(ret);
138 
139     ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
140     if (wifi_init_sta() == ESP_OK) {
141         start_esp_local_ctrl_service();
142     } else {
143         ESP_LOGI(TAG, "Connection failed, not starting esp_local_ctrl service");
144     }
145 }
146