168 lines
5.5 KiB
C
168 lines
5.5 KiB
C
|
/* ESPRESSIF MIT License
|
||
|
*
|
||
|
* Copyright (c) 2018 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
|
||
|
*
|
||
|
* Permission is hereby granted for use on all ESPRESSIF SYSTEMS products, in which case,
|
||
|
* it is free of charge, to any person obtaining a copy of this software and associated
|
||
|
* documentation files (the "Software"), to deal in the Software without restriction, including
|
||
|
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||
|
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished
|
||
|
* to do so, subject to the following conditions:
|
||
|
*
|
||
|
* The above copyright notice and this permission notice shall be included in all copies or
|
||
|
* substantial portions of the Software.
|
||
|
*
|
||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||
|
*/
|
||
|
#include <string.h>
|
||
|
#include "freertos/FreeRTOS.h"
|
||
|
#include "freertos/task.h"
|
||
|
#include "freertos/event_groups.h"
|
||
|
#include "esp_system.h"
|
||
|
#include "esp_wifi.h"
|
||
|
#include "esp_event_loop.h"
|
||
|
#include "esp_log.h"
|
||
|
#include "nvs_flash.h"
|
||
|
#include "sdkconfig.h"
|
||
|
|
||
|
#include "lwip/err.h"
|
||
|
#include "lwip/sys.h"
|
||
|
#include "app_wifi.h"
|
||
|
|
||
|
static const char *TAG = "app_wifi";
|
||
|
|
||
|
#define EXAMPLE_ESP_WIFI_MODE_AP 1 //TRUE:AP FALSE:STA
|
||
|
#define EXAMPLE_ESP_WIFI_SSID CONFIG_ESP_WIFI_SSID
|
||
|
#define EXAMPLE_ESP_WIFI_PASS CONFIG_ESP_WIFI_PASSWORD
|
||
|
#define EXAMPLE_MAX_STA_CONN CONFIG_MAX_STA_CONN
|
||
|
|
||
|
static esp_err_t event_handler(void *ctx, system_event_t *event)
|
||
|
{/*{{{*/
|
||
|
switch(event->event_id) {
|
||
|
case SYSTEM_EVENT_STA_START:
|
||
|
esp_wifi_connect();
|
||
|
break;
|
||
|
case SYSTEM_EVENT_STA_GOT_IP:
|
||
|
ESP_LOGI(TAG, "got ip:%s",
|
||
|
ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip));
|
||
|
break;
|
||
|
case SYSTEM_EVENT_AP_STACONNECTED:
|
||
|
ESP_LOGI(TAG, "station:" MACSTR " join, AID=%d",
|
||
|
MAC2STR(event->event_info.sta_connected.mac),
|
||
|
event->event_info.sta_connected.aid);
|
||
|
break;
|
||
|
case SYSTEM_EVENT_AP_STADISCONNECTED:
|
||
|
ESP_LOGI(TAG, "station:" MACSTR " leave, AID=%d",
|
||
|
MAC2STR(event->event_info.sta_disconnected.mac),
|
||
|
event->event_info.sta_disconnected.aid);
|
||
|
|
||
|
break;
|
||
|
case SYSTEM_EVENT_STA_DISCONNECTED:
|
||
|
esp_wifi_connect();
|
||
|
break;
|
||
|
default:
|
||
|
break;
|
||
|
}
|
||
|
return ESP_OK;
|
||
|
}/*}}}*/
|
||
|
|
||
|
#if EXAMPLE_ESP_WIFI_MODE_AP
|
||
|
static void wifi_init_softap()
|
||
|
{
|
||
|
tcpip_adapter_init();
|
||
|
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
|
||
|
|
||
|
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||
|
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
||
|
|
||
|
uint8_t mac[6];
|
||
|
ESP_ERROR_CHECK(esp_wifi_get_mac(ESP_IF_WIFI_AP, mac));
|
||
|
|
||
|
wifi_config_t wifi_config;
|
||
|
memset(&wifi_config, 0, sizeof(wifi_config_t));
|
||
|
if (strlen(EXAMPLE_ESP_WIFI_SSID) == 0)
|
||
|
{
|
||
|
snprintf((char *)wifi_config.ap.ssid, 32, "esp-eye-%x%x", mac[4], mac[5]);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
memcpy(wifi_config.ap.ssid, EXAMPLE_ESP_WIFI_SSID, sizeof(EXAMPLE_ESP_WIFI_SSID));
|
||
|
}
|
||
|
memcpy(wifi_config.ap.password, EXAMPLE_ESP_WIFI_PASS, sizeof(EXAMPLE_ESP_WIFI_PASS));
|
||
|
wifi_config.ap.ssid_len = strlen(EXAMPLE_ESP_WIFI_SSID);
|
||
|
wifi_config.ap.max_connection = EXAMPLE_MAX_STA_CONN;
|
||
|
wifi_config.ap.authmode = WIFI_AUTH_WPA_WPA2_PSK;
|
||
|
if (strlen(EXAMPLE_ESP_WIFI_PASS) == 0) {
|
||
|
wifi_config.ap.authmode = WIFI_AUTH_OPEN;
|
||
|
}
|
||
|
|
||
|
esp_wifi_set_ps(WIFI_PS_NONE);
|
||
|
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
|
||
|
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config));
|
||
|
|
||
|
ESP_ERROR_CHECK(esp_wifi_start());
|
||
|
|
||
|
ESP_LOGI(TAG, "wifi_init_softap finished.SSID:%s password:%s",
|
||
|
wifi_config.ap.ssid, EXAMPLE_ESP_WIFI_PASS);
|
||
|
|
||
|
char buf[80];
|
||
|
sprintf(buf, "SSID:%s", wifi_config.ap.ssid);
|
||
|
sprintf(buf, "PASSWORD:%s", wifi_config.ap.password);
|
||
|
|
||
|
}
|
||
|
|
||
|
#else
|
||
|
|
||
|
static void wifi_init_sta()
|
||
|
{
|
||
|
tcpip_adapter_init();
|
||
|
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
|
||
|
|
||
|
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||
|
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
||
|
|
||
|
wifi_config_t wifi_config = {0};
|
||
|
memset(&wifi_config, 0, sizeof(wifi_config_t));
|
||
|
memcpy(wifi_config.sta.ssid, EXAMPLE_ESP_WIFI_SSID, sizeof(EXAMPLE_ESP_WIFI_SSID));
|
||
|
memcpy(wifi_config.sta.password, EXAMPLE_ESP_WIFI_PASS, sizeof(EXAMPLE_ESP_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, "connect to ap SSID:%s password:%s", wifi_config.sta.ssid, wifi_config.sta.password);
|
||
|
|
||
|
char buf[80];
|
||
|
sprintf(buf, "SSID:%s", wifi_config.sta.ssid);
|
||
|
sprintf(buf, "PASSWORD:%s", wifi_config.sta.password);
|
||
|
|
||
|
}
|
||
|
#endif
|
||
|
|
||
|
void app_wifi_init ()
|
||
|
{
|
||
|
//Initialize NVS
|
||
|
esp_err_t ret = nvs_flash_init();
|
||
|
if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
|
||
|
ESP_ERROR_CHECK(nvs_flash_erase());
|
||
|
ret = nvs_flash_init();
|
||
|
}
|
||
|
ESP_ERROR_CHECK(ret);
|
||
|
|
||
|
#if EXAMPLE_ESP_WIFI_MODE_AP
|
||
|
ESP_LOGI(TAG, "ESP_WIFI_MODE_AP");
|
||
|
wifi_init_softap();
|
||
|
#else
|
||
|
ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
|
||
|
wifi_init_sta();
|
||
|
#endif /*EXAMPLE_ESP_WIFI_MODE_AP*/
|
||
|
|
||
|
}
|
||
|
|