-
Notifications
You must be signed in to change notification settings - Fork 0
/
ESP32-WIFI.c
218 lines (183 loc) · 6.39 KB
/
ESP32-WIFI.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include <string.h>
/** DEFINES **/
#define WIFI_SUCCESS 1 << 0
#define WIFI_FAILURE 1 << 1
#define TCP_SUCCESS 1 << 0
#define TCP_FAILURE 1 << 1
#define MAX_FAILURES 10
/** GLOBALS **/
// event group to contain status information
static EventGroupHandle_t wifi_event_group;
// retry tracker
static int s_retry_num = 0;
// task tag
static const char *TAG = "WIFI";
/** FUNCTIONS **/
// event handler for wifi events
static void wifi_event_handler(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data)
{
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START)
{
ESP_LOGI(TAG, "Connecting to AP...");
esp_wifi_connect();
}
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED)
{
if (s_retry_num < MAX_FAILURES)
{
ESP_LOGI(TAG, "Reconnecting to AP...");
esp_wifi_connect();
s_retry_num++;
}
else
{
xEventGroupSetBits(wifi_event_group, WIFI_FAILURE);
}
}
}
// event handler for ip events
static void ip_event_handler(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data)
{
if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP)
{
ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
ESP_LOGI(TAG, "STA IP: " IPSTR, IP2STR(&event->ip_info.ip));
s_retry_num = 0;
xEventGroupSetBits(wifi_event_group, WIFI_SUCCESS);
}
}
// connect to wifi and return the result
esp_err_t connect_wifi()
{
int status = WIFI_FAILURE;
/** INITIALIZE ALL THE THINGS **/
// initialize the esp network interface
ESP_ERROR_CHECK(esp_netif_init());
// initialize default esp event loop
ESP_ERROR_CHECK(esp_event_loop_create_default());
// create wifi station in the wifi driver
esp_netif_create_default_wifi_sta();
// setup wifi station with the default wifi configuration
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
/** EVENT LOOP CRAZINESS **/
wifi_event_group = xEventGroupCreate();
esp_event_handler_instance_t wifi_handler_event_instance;
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
ESP_EVENT_ANY_ID,
&wifi_event_handler,
NULL,
&wifi_handler_event_instance));
esp_event_handler_instance_t got_ip_event_instance;
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
IP_EVENT_STA_GOT_IP,
&ip_event_handler,
NULL,
&got_ip_event_instance));
/** START THE WIFI DRIVER **/
wifi_config_t wifi_config = {
.sta = {
.ssid = "ssid-for-me",
.password = "super-secure-password",
.threshold.authmode = WIFI_AUTH_WPA2_PSK,
.pmf_cfg = {
.capable = true,
.required = false},
},
};
// set the wifi controller to be a station
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
// set the wifi config
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
// start the wifi driver
ESP_ERROR_CHECK(esp_wifi_start());
ESP_LOGI(TAG, "STA initialization complete");
/** NOW WE WAIT **/
EventBits_t bits = xEventGroupWaitBits(wifi_event_group,
WIFI_SUCCESS | WIFI_FAILURE,
pdFALSE,
pdFALSE,
portMAX_DELAY);
/* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
* happened. */
if (bits & WIFI_SUCCESS)
{
ESP_LOGI(TAG, "Connected to ap");
status = WIFI_SUCCESS;
}
else if (bits & WIFI_FAILURE)
{
ESP_LOGI(TAG, "Failed to connect to ap");
status = WIFI_FAILURE;
}
else
{
ESP_LOGE(TAG, "UNEXPECTED EVENT");
status = WIFI_FAILURE;
}
/* The event will not be processed after unregister */
ESP_ERROR_CHECK(esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, got_ip_event_instance));
ESP_ERROR_CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, wifi_handler_event_instance));
vEventGroupDelete(wifi_event_group);
return status;
}
// connect to the server and return the result
esp_err_t connect_tcp_server(void)
{
struct sockaddr_in serverInfo = {0};
char readBuffer[1024] = {0};
serverInfo.sin_family = AF_INET;
serverInfo.sin_addr.s_addr = 0x0100007f;
serverInfo.sin_port = htons(12345);
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0)
{
ESP_LOGE(TAG, "Failed to create a socket..?");
return TCP_FAILURE;
}
if (connect(sock, (struct sockaddr *)&serverInfo, sizeof(serverInfo)) != 0)
{
ESP_LOGE(TAG, "Failed to connect to %s!", inet_ntoa(serverInfo.sin_addr.s_addr));
close(sock);
return TCP_FAILURE;
}
ESP_LOGI(TAG, "Connected to TCP server.");
bzero(readBuffer, sizeof(readBuffer));
int r = read(sock, readBuffer, sizeof(readBuffer) - 1);
for (int i = 0; i < r; i++)
{
putchar(readBuffer[i]);
}
if (strcmp(readBuffer, "HELLO") == 0)
{
ESP_LOGI(TAG, "WE DID IT!");
}
return TCP_SUCCESS;
}
void app_main(void)
{
esp_err_t status = WIFI_FAILURE;
// initialize storage
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
{
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);
// connect to wireless AP
status = connect_wifi();
if (WIFI_SUCCESS != status)
{
ESP_LOGI(TAG, "Failed to associate to AP, dying...");
return;
}
status = connect_tcp_server();
if (TCP_SUCCESS != status)
{
ESP_LOGI(TAG, "Failed to connect to remote server, dying...");
return;
}
}