Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

+ initial wifi interface #22

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
176 changes: 176 additions & 0 deletions Firmware/ESP32/Hardware_bringup/src/APP/app_wifi.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
/**
* @file app_wifi.c
* @author Dong-Jae (Alex) Park
* @date 17 Feb 2021
* @brief Wifi initialization with router
*
* This document contains wifi initialization with router
*/

#include "app_wifi.h"

// Std. Lib
#include <string.h>

// TableUV Lib

// External Library
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "lwip/err.h"
#include "lwip/sys.h"

/////////////////////////////////
/////// DEFINITION ////////
/////////////////////////////////
#define EXAMPLE_ESP_WIFI_SSID "TP-LINK_B2C7EE"
#define EXAMPLE_ESP_WIFI_PASS "EDB2C7EE"
#define EXAMPLE_ESP_MAXIMUM_RETRY 10

/* The event group allows multiple bits for each event, but we only care about two events:
* - we are connected to the AP with an IP
* - we failed to connect after the maximum amount of retries */
#define WIFI_CONNECTED_BIT BIT0
#define WIFI_FAIL_BIT BIT1

typedef struct {
EventGroupHandle_t wifi_event_group;
const char *TAG;
int wifi_reconnect_num ;
} app_wifi_data_S;

/////////////////////////////////////////
/////// PRIVATE PROTOTYPE /////////
/////////////////////////////////////////
static void app_wifi_event_handler(void*, esp_event_base_t, int32_t, void*);
static void app_wifi_init_sta(void);

///////////////////////////
/////// DATA ////////
///////////////////////////
static app_wifi_data_S wifi_data;

////////////////////////////////////////
/////// PRIVATE FUNCTION /////////
////////////////////////////////////////
static void app_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_wifi_connect();
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
if (wifi_data.wifi_reconnect_num < EXAMPLE_ESP_MAXIMUM_RETRY) {
esp_wifi_connect();
wifi_data.wifi_reconnect_num++;
ESP_LOGI(wifi_data.TAG, "retry to connect to the AP");
} else {
xEventGroupSetBits(wifi_data.wifi_event_group, WIFI_FAIL_BIT);
}
ESP_LOGI(wifi_data.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(wifi_data.TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
wifi_data.wifi_reconnect_num = 0;
xEventGroupSetBits(wifi_data.wifi_event_group, WIFI_CONNECTED_BIT);
}
}

static void app_wifi_init_sta(void)
{
wifi_data.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_event_handler_instance_t instance_any_id;
esp_event_handler_instance_t instance_got_ip;
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
ESP_EVENT_ANY_ID,
&app_wifi_event_handler,
NULL,
&instance_any_id));
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
IP_EVENT_STA_GOT_IP,
&app_wifi_event_handler,
NULL,
&instance_got_ip));

wifi_config_t wifi_config = {
.sta = {
.ssid = EXAMPLE_ESP_WIFI_SSID,
.password = EXAMPLE_ESP_WIFI_PASS,
/* Setting a password implies station will connect to all security modes including WEP/WPA.
* However these modes are deprecated and not advisable to be used. Incase your Access point
* doesn't support WPA2, these mode can be enabled by commenting below line */
.threshold.authmode = WIFI_AUTH_WPA2_PSK,

.pmf_cfg = {
.capable = true,
.required = false
},
},
};
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
ESP_ERROR_CHECK(esp_wifi_start() );

ESP_LOGI(wifi_data.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_data.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(wifi_data.TAG, "connected to ap SSID:%s password:%s",
EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
} else if (bits & WIFI_FAIL_BIT) {
ESP_LOGI(wifi_data.TAG, "Failed to connect to SSID:%s, password:%s",
EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
} else {
ESP_LOGE(wifi_data.TAG, "UNEXPECTED EVENT");
}

/* The event will not be processed after unregister */
ESP_ERROR_CHECK(esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, instance_got_ip));
ESP_ERROR_CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, instance_any_id));
vEventGroupDelete(wifi_data.wifi_event_group);
}


///////////////////////////////////////
/////// PUBLIC FUNCTION /////////
///////////////////////////////////////
void app_wifi_init(void)
{
memset(&wifi_data, 0x00, sizeof(app_wifi_data_S));

wifi_data.TAG = "WIFI";
wifi_data.wifi_reconnect_num = 0;

//Initialize NVS
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);

ESP_LOGI(wifi_data.TAG, "ESP_WIFI_MODE_STA");
app_wifi_init_sta();
}
15 changes: 15 additions & 0 deletions Firmware/ESP32/Hardware_bringup/src/APP/app_wifi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* @file app_wifi.h
* @author Dong-Jae (Alex) Park
* @date 17 Feb 2021
* @brief Wifi initialization with router
*
* Wifi initialization with router
*/

#ifndef APP_WIFI_H
#define APP_WIFI_H

void app_wifi_init(void);

#endif //APP_WIFI_H
2 changes: 2 additions & 0 deletions Firmware/ESP32/Hardware_bringup/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "io_ping_map.h"
#include "APP/app_slam.h"
#include "APP/app_supervisor.h"
#include "APP/app_wifi.h"

// SDK config
#include "sdkconfig.h"
Expand Down Expand Up @@ -189,6 +190,7 @@ void app_main()
esp32_task_init();

// app level init
app_wifi_init();
app_slam_init();
app_supervisor_init();

Expand Down