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
|
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include "freertos/event_groups.h"
#include <esp_log.h>
#include <esp_wifi.h>
#include "net.h"
#define WIFI_CONNECTED_BIT BIT0
static EventGroupHandle_t wifi_evt_group;
static const char *tag = "wifi";
static void wifi_evt_handler(void *arg, esp_event_base_t eb, int32_t id, void *data)
{
ip_event_got_ip_t *event = (ip_event_got_ip_t *) data;
ESP_LOGI(tag, "ip: %s", ip4addr_ntoa(&event->ip_info.ip));
xEventGroupSetBits(wifi_evt_group, WIFI_CONNECTED_BIT);
}
void wifi_connect(void)
{
wifi_evt_group = xEventGroupCreate();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
esp_wifi_init(&cfg);
wifi_config_t wifi_config = {
.sta = {
.ssid = CONFIG_WIFI_SSID,
.password = CONFIG_WIFI_PASS,
.threshold.authmode = WIFI_AUTH_WPA2_PSK
},
};
esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config);
esp_wifi_set_mode(WIFI_MODE_STA);
esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &wifi_evt_handler, 0);
ESP_ERROR_CHECK(esp_wifi_start());
ESP_ERROR_CHECK(esp_wifi_connect());
xEventGroupWaitBits(wifi_evt_group,
WIFI_CONNECTED_BIT,
pdFALSE,
pdFALSE,
portMAX_DELAY);
}
|