Wifi
This commit is contained in:
parent
183a35b645
commit
012fa54f4e
|
@ -1,3 +1,4 @@
|
|||
.vscode
|
||||
build
|
||||
sdkconfig.old
|
||||
sdkconfig.old
|
||||
main/wifi-credentials.h
|
|
@ -1,6 +1,7 @@
|
|||
set(SOURCES
|
||||
"main.c"
|
||||
"usb-cdc.c"
|
||||
"nvs.c"
|
||||
"wifi.c"
|
||||
)
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#include <stdint.h>
|
||||
#include <esp_log.h>
|
||||
#include "usb-cdc.h"
|
||||
#include "nvs.h"
|
||||
#include "wifi.h"
|
||||
|
||||
static const char* TAG = "main";
|
||||
|
@ -9,6 +10,7 @@ void app_main(void) {
|
|||
ESP_LOGI(TAG, "start");
|
||||
|
||||
usb_cdc_init();
|
||||
nvs_init();
|
||||
wifi_init();
|
||||
|
||||
ESP_LOGI(TAG, "end");
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
#include <nvs_flash.h>
|
||||
#include <esp_log.h>
|
||||
|
||||
void nvs_init(void) {
|
||||
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);
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
#pragma once
|
||||
|
||||
void nvs_init(void);
|
|
@ -1,3 +1,3 @@
|
|||
#pragma once
|
||||
|
||||
void usb_cdc_init(void);
|
||||
void usb_cdc_init(void);
|
||||
|
|
80
main/wifi.c
80
main/wifi.c
|
@ -1,7 +1,83 @@
|
|||
#include <esp_log.h>
|
||||
#include <esp_wifi.h>
|
||||
#include <freertos/event_groups.h>
|
||||
#include "wifi-credentials.h"
|
||||
|
||||
static const char* TAG = "wifi";
|
||||
|
||||
#if !defined(WIFI_SSID) || !defined(WIFI_PASS)
|
||||
#error Create wifi-credentials.h with defined WIFI_SSID and WIFI_PASS
|
||||
#endif
|
||||
|
||||
#define WIFI_MAXIMUM_RETRY 3
|
||||
|
||||
// TODO remove global vars
|
||||
static EventGroupHandle_t wifi_event_group;
|
||||
#define WIFI_CONNECTED_BIT BIT0
|
||||
#define WIFI_FAIL_BIT BIT1
|
||||
static int s_retry_num = 0;
|
||||
|
||||
static void
|
||||
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_wifi_connect();
|
||||
} else if(event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
|
||||
if(s_retry_num < WIFI_MAXIMUM_RETRY) {
|
||||
esp_wifi_connect();
|
||||
s_retry_num++;
|
||||
ESP_LOGI(TAG, "retry to connect to the AP");
|
||||
} else {
|
||||
xEventGroupSetBits(wifi_event_group, WIFI_FAIL_BIT);
|
||||
}
|
||||
ESP_LOGI(TAG, "connect to the AP fail");
|
||||
} else 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, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
|
||||
s_retry_num = 0;
|
||||
xEventGroupSetBits(wifi_event_group, WIFI_CONNECTED_BIT);
|
||||
}
|
||||
}
|
||||
|
||||
void wifi_init(void) {
|
||||
ESP_LOGI(TAG, "Not implemented");
|
||||
}
|
||||
wifi_event_group = xEventGroupCreate();
|
||||
|
||||
ESP_ERROR_CHECK(esp_netif_init());
|
||||
|
||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||
esp_netif_create_default_wifi_sta();
|
||||
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
||||
|
||||
ESP_ERROR_CHECK(
|
||||
esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
|
||||
ESP_ERROR_CHECK(
|
||||
esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL));
|
||||
|
||||
wifi_config_t wifi_config = {
|
||||
.sta = {.ssid = WIFI_SSID, .password = WIFI_PASS},
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
||||
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
|
||||
ESP_ERROR_CHECK(esp_wifi_start());
|
||||
|
||||
ESP_LOGI(TAG, "wifi_init_sta finished.");
|
||||
|
||||
/* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum
|
||||
* number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */
|
||||
EventBits_t bits = xEventGroupWaitBits(
|
||||
wifi_event_group, WIFI_CONNECTED_BIT | WIFI_FAIL_BIT, pdFALSE, pdFALSE, portMAX_DELAY);
|
||||
|
||||
/* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
|
||||
* happened. */
|
||||
if(bits & WIFI_CONNECTED_BIT) {
|
||||
ESP_LOGI(TAG, "connected to ap SSID:%s password:%s", WIFI_SSID, WIFI_PASS);
|
||||
} else if(bits & WIFI_FAIL_BIT) {
|
||||
ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s", WIFI_SSID, WIFI_PASS);
|
||||
} else {
|
||||
ESP_LOGE(TAG, "UNEXPECTED EVENT");
|
||||
}
|
||||
|
||||
ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler));
|
||||
ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler));
|
||||
}
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
#pragma once
|
||||
|
||||
void wifi_init(void);
|
||||
void wifi_init(void);
|
||||
|
|
Loading…
Reference in New Issue