2018-08-29 22:11:21 -04:00
|
|
|
/* WiFi softAP Example
|
2018-03-12 22:44:49 -04: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 "esp_system.h"
|
|
|
|
#include "esp_wifi.h"
|
2018-11-20 05:26:53 -05:00
|
|
|
#include "esp_event.h"
|
2018-03-12 22:44:49 -04:00
|
|
|
#include "esp_log.h"
|
|
|
|
#include "nvs_flash.h"
|
|
|
|
|
|
|
|
#include "lwip/err.h"
|
|
|
|
#include "lwip/sys.h"
|
|
|
|
|
2019-06-22 21:54:31 -04:00
|
|
|
/* The examples use WiFi configuration that you can set via project configuration menu.
|
2018-03-12 22:44:49 -04: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
|
2019-05-09 10:43:06 -04:00
|
|
|
#define EXAMPLE_MAX_STA_CONN CONFIG_ESP_MAX_STA_CONN
|
2018-03-12 22:44:49 -04:00
|
|
|
|
2018-08-29 22:11:21 -04:00
|
|
|
static const char *TAG = "wifi softAP";
|
2018-03-12 22:44:49 -04:00
|
|
|
|
2019-05-22 05:56:10 -04:00
|
|
|
static void wifi_event_handler(void* arg, esp_event_base_t event_base,
|
2018-11-20 05:26:53 -05:00
|
|
|
int32_t event_id, void* event_data)
|
2018-03-12 22:44:49 -04:00
|
|
|
{
|
2018-11-20 05:26:53 -05: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-12 22:44:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
void wifi_init_softap(void)
|
2018-03-12 22:44:49 -04:00
|
|
|
{
|
2019-11-29 04:54:02 -05:00
|
|
|
ESP_ERROR_CHECK(esp_netif_init());
|
2018-11-20 05:26:53 -05:00
|
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
2019-08-31 10:21:31 -04:00
|
|
|
esp_netif_create_default_wifi_ap();
|
2018-03-12 22:44:49 -04:00
|
|
|
|
|
|
|
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
|
|
|
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
2018-11-20 05:26:53 -05:00
|
|
|
|
|
|
|
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL));
|
|
|
|
|
2018-03-12 22:44:49 -04: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 05:26:53 -05:00
|
|
|
ESP_LOGI(TAG, "wifi_init_softap finished. SSID:%s password:%s",
|
2018-03-12 22:44:49 -04:00
|
|
|
EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
|
|
|
|
}
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
void app_main(void)
|
2018-03-12 22:44:49 -04:00
|
|
|
{
|
|
|
|
//Initialize NVS
|
|
|
|
esp_err_t ret = nvs_flash_init();
|
2018-07-25 11:11:09 -04:00
|
|
|
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
2018-03-12 22:44:49 -04:00
|
|
|
ESP_ERROR_CHECK(nvs_flash_erase());
|
|
|
|
ret = nvs_flash_init();
|
|
|
|
}
|
|
|
|
ESP_ERROR_CHECK(ret);
|
2019-05-22 05:56:10 -04:00
|
|
|
|
2018-03-12 22:44:49 -04:00
|
|
|
ESP_LOGI(TAG, "ESP_WIFI_MODE_AP");
|
|
|
|
wifi_init_softap();
|
|
|
|
}
|