2018-08-30 02:11:21 +00:00
|
|
|
/* WiFi softAP Example
|
2018-03-13 02:44:49 +00:00
|
|
|
|
|
|
|
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, this
|
|
|
|
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
|
|
CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
*/
|
|
|
|
#include <string.h>
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
|
|
|
#include "freertos/event_groups.h"
|
|
|
|
#include "esp_system.h"
|
|
|
|
#include "esp_wifi.h"
|
2018-11-20 10:26:53 +00:00
|
|
|
#include "esp_event.h"
|
2018-03-13 02:44:49 +00:00
|
|
|
#include "esp_log.h"
|
|
|
|
#include "nvs_flash.h"
|
|
|
|
|
|
|
|
#include "lwip/err.h"
|
|
|
|
#include "lwip/sys.h"
|
|
|
|
|
2018-08-30 02:11:21 +00:00
|
|
|
/* The examples use WiFi configuration that you can set via 'make menuconfig'.
|
2018-03-13 02:44:49 +00:00
|
|
|
|
|
|
|
If you'd rather not, just change the below entries to strings with
|
|
|
|
the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
|
|
|
|
*/
|
|
|
|
#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
|
|
|
|
|
|
|
|
/* FreeRTOS event group to signal when we are connected*/
|
2018-08-30 02:11:21 +00:00
|
|
|
static EventGroupHandle_t s_wifi_event_group;
|
2018-03-13 02:44:49 +00:00
|
|
|
|
2018-08-30 02:11:21 +00:00
|
|
|
static const char *TAG = "wifi softAP";
|
2018-03-13 02:44:49 +00:00
|
|
|
|
2018-11-20 10:26:53 +00:00
|
|
|
static void wifi_event_handler(void* arg, esp_event_base_t event_base,
|
|
|
|
int32_t event_id, void* event_data)
|
2018-03-13 02:44:49 +00:00
|
|
|
{
|
2018-11-20 10:26:53 +00:00
|
|
|
if (event_id == WIFI_EVENT_AP_STACONNECTED) {
|
|
|
|
wifi_event_ap_staconnected_t* event = (wifi_event_ap_staconnected_t*) event_data;
|
|
|
|
ESP_LOGI(TAG, "station "MACSTR" join, AID=%d",
|
|
|
|
MAC2STR(event->mac), event->aid);
|
|
|
|
} else if (event_id == WIFI_EVENT_AP_STADISCONNECTED) {
|
|
|
|
wifi_event_ap_stadisconnected_t* event = (wifi_event_ap_stadisconnected_t*) event_data;
|
|
|
|
ESP_LOGI(TAG, "station "MACSTR" leave, AID=%d",
|
|
|
|
MAC2STR(event->mac), event->aid);
|
2018-03-13 02:44:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void wifi_init_softap()
|
|
|
|
{
|
2018-08-30 02:11:21 +00:00
|
|
|
s_wifi_event_group = xEventGroupCreate();
|
2018-03-13 02:44:49 +00:00
|
|
|
|
|
|
|
tcpip_adapter_init();
|
2018-11-20 10:26:53 +00:00
|
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
2018-03-13 02:44:49 +00:00
|
|
|
|
|
|
|
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
|
|
|
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
2018-11-20 10:26:53 +00:00
|
|
|
|
|
|
|
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL));
|
|
|
|
|
2018-03-13 02:44:49 +00:00
|
|
|
wifi_config_t wifi_config = {
|
|
|
|
.ap = {
|
|
|
|
.ssid = EXAMPLE_ESP_WIFI_SSID,
|
|
|
|
.ssid_len = strlen(EXAMPLE_ESP_WIFI_SSID),
|
|
|
|
.password = EXAMPLE_ESP_WIFI_PASS,
|
|
|
|
.max_connection = EXAMPLE_MAX_STA_CONN,
|
|
|
|
.authmode = WIFI_AUTH_WPA_WPA2_PSK
|
|
|
|
},
|
|
|
|
};
|
|
|
|
if (strlen(EXAMPLE_ESP_WIFI_PASS) == 0) {
|
|
|
|
wifi_config.ap.authmode = WIFI_AUTH_OPEN;
|
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
2018-11-20 10:26:53 +00:00
|
|
|
ESP_LOGI(TAG, "wifi_init_softap finished. SSID:%s password:%s",
|
2018-03-13 02:44:49 +00:00
|
|
|
EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void app_main()
|
|
|
|
{
|
|
|
|
//Initialize NVS
|
|
|
|
esp_err_t ret = nvs_flash_init();
|
2018-07-25 15:11:09 +00:00
|
|
|
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
2018-03-13 02:44:49 +00:00
|
|
|
ESP_ERROR_CHECK(nvs_flash_erase());
|
|
|
|
ret = nvs_flash_init();
|
|
|
|
}
|
|
|
|
ESP_ERROR_CHECK(ret);
|
|
|
|
|
|
|
|
ESP_LOGI(TAG, "ESP_WIFI_MODE_AP");
|
|
|
|
wifi_init_softap();
|
|
|
|
}
|