esp32-warm-water
esp32 based project for the control of a heating element based on temperature
wifi_sta.c
Go to the documentation of this file.
1 /*
2  * MIT License
3  *
4  * Copyright (c) 2021 wolffshots
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  *
24  * A note on the copyright line: https://www.copyright.gov/title17/92chap4.html#408
25  */
26 
32 #include "wifi_sta.h"
33 
34 /* FreeRTOS event group to signal when we are connected*/
35 EventGroupHandle_t s_wifi_event_group;
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 const char *TAG = CONFIG_WIFI_SAP_LOG_TAG;
43 int s_retry_num = 0;
44 void event_handler(void *arg, esp_event_base_t event_base,
45  int32_t event_id, void *event_data);
46 void wifi_init_sta(void);
47 
48 void event_handler(void *arg, esp_event_base_t event_base,
49  int32_t event_id, void *event_data)
50 {
51 #ifdef CONFIG_ESP_MAXIMUM_RETRY_STA
52  if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START)
53  {
54  esp_wifi_connect();
55  }
56  else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED)
57  {
58  if (s_retry_num < CONFIG_ESP_MAXIMUM_RETRY_STA)
59  {
60  esp_wifi_connect();
61  s_retry_num++;
62  ESP_LOGI(TAG, "retry to connect to the AP");
63  }
64  else
65  {
66  xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
67  }
68  ESP_LOGI(TAG, "connect to the AP fail");
69  }
70  else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP)
71  {
72  ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
73  ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
74  sprintf(ip_address, IPSTR, IP2STR(&event->ip_info.ip));
75  s_retry_num = 0;
76  xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
77  }
78 #endif
79 }
81 {
82 #if defined(CONFIG_ESP_WIFI_SSID_STA) && defined(CONFIG_ESP_WIFI_PASSWORD_STA)
83  s_wifi_event_group = xEventGroupCreate();
84  ESP_ERROR_CHECK(esp_netif_init());
85  ESP_ERROR_CHECK(esp_event_loop_create_default());
86  esp_netif_create_default_wifi_sta();
87  wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
88  ESP_ERROR_CHECK(esp_wifi_init(&cfg));
89  esp_event_handler_instance_t instance_any_id;
90  esp_event_handler_instance_t instance_got_ip;
91  ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
92  ESP_EVENT_ANY_ID,
94  NULL,
95  &instance_any_id));
96  ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
97  IP_EVENT_STA_GOT_IP,
99  NULL,
100  &instance_got_ip));
101 
102  wifi_config_t wifi_config = {
103  .sta = {
104  .ssid = CONFIG_ESP_WIFI_SSID_STA,
105  .password = CONFIG_ESP_WIFI_PASSWORD_STA,
106  /* Setting a password implies station will connect to all security modes including WEP/WPA.
107  * However these modes are deprecated and not advisable to be used. Incase your Access point
108  * doesn't support WPA2, these mode can be enabled by commenting below line */
109  .threshold.authmode = WIFI_AUTH_WPA2_PSK,
110  .pmf_cfg = {
111  .capable = true,
112  .required = false},
113  },
114  };
115  ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
116  ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
117  ESP_ERROR_CHECK(esp_wifi_start());
118  ESP_LOGI(TAG, "wifi_init_sta finished.");
119  /* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum
120  * number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */
121  EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
123  pdFALSE,
124  pdFALSE,
125  portMAX_DELAY);
126 
127  // hide after first 2 chars
128  char pass[6];
129  if (strlen(CONFIG_ESP_WIFI_PASSWORD_STA) > 5)
130  {
131  char str2[40];
132  strncpy(str2, CONFIG_ESP_WIFI_PASSWORD_STA, sizeof(str2));
133  strncpy(pass, str2, 2);
134  }
135  else
136  {
137  pass[0] = '*';
138  pass[1] = '*';
139  }
140  pass[2] = '*';
141  pass[3] = '*';
142  pass[4] = '*';
143  pass[5] = '\0';
144  /* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
145  * happened. */
146  if (bits & WIFI_CONNECTED_BIT)
147  {
148  ESP_LOGI(TAG, "connected to ap SSID: %s, password: %s",
149  CONFIG_ESP_WIFI_SSID_STA, pass);
150  }
151  else if (bits & WIFI_FAIL_BIT)
152  {
153  ESP_LOGI(TAG, "Failed to connect to SSID: %s, password: %s",
154  CONFIG_ESP_WIFI_SSID_STA, pass);
155  }
156  else
157  {
158  ESP_LOGE(TAG, "UNEXPECTED EVENT");
159  }
160  /* The event will not be processed after unregister */
161  ESP_ERROR_CHECK(esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, instance_got_ip));
162  ESP_ERROR_CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, instance_any_id));
163  vEventGroupDelete(s_wifi_event_group);
164 #endif
165 }
167 {
168  ESP_LOGI(TAG, "wifi station!");
169  esp_err_t ret = nvs_flash_init();
170  if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
171  {
172  ESP_ERROR_CHECK(nvs_flash_erase());
173  ret = nvs_flash_init();
174  }
175  ESP_ERROR_CHECK(ret);
177 }
char ip_address[16]
Definition: main.c:54
void wifi_init_station(void)
Definition: wifi_sta.c:80
const char * TAG
Definition: wifi_sta.c:42
EventGroupHandle_t s_wifi_event_group
Definition: wifi_sta.c:35
void wifi_init_sta(void)
Definition: wifi_sta.c:166
void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
Definition: wifi_sta.c:48
#define WIFI_FAIL_BIT
Definition: wifi_sta.c:40
#define WIFI_CONNECTED_BIT
Definition: wifi_sta.c:39
int s_retry_num
Definition: wifi_sta.c:43
defininitions for wrapper component to set up wifi in station mode