1 /*  WiFi softAP Example
2 
3    This example code is in the Public Domain (or CC0 licensed, at your option.)
4 
5    Unless required by applicable law or agreed to in writing, this
6    software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
7    CONDITIONS OF ANY KIND, either express or implied.
8 */
9 #include <string.h>
10 #include "freertos/FreeRTOS.h"
11 #include "freertos/task.h"
12 #include "esp_system.h"
13 #include "esp_wifi.h"
14 #include "esp_event.h"
15 #include "esp_log.h"
16 #include "nvs_flash.h"
17 
18 #include "lwip/err.h"
19 #include "lwip/sys.h"
20 
21 /* The examples use WiFi configuration that you can set via project configuration menu.
22 
23    If you'd rather not, just change the below entries to strings with
24    the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
25 */
26 #define EXAMPLE_ESP_WIFI_SSID      CONFIG_ESP_WIFI_SSID
27 #define EXAMPLE_ESP_WIFI_PASS      CONFIG_ESP_WIFI_PASSWORD
28 #define EXAMPLE_ESP_WIFI_CHANNEL   CONFIG_ESP_WIFI_CHANNEL
29 #define EXAMPLE_MAX_STA_CONN       CONFIG_ESP_MAX_STA_CONN
30 
31 static const char *TAG = "wifi softAP";
32 
wifi_event_handler(void * arg,esp_event_base_t event_base,int32_t event_id,void * event_data)33 static void wifi_event_handler(void* arg, esp_event_base_t event_base,
34                                     int32_t event_id, void* event_data)
35 {
36     if (event_id == WIFI_EVENT_AP_STACONNECTED) {
37         wifi_event_ap_staconnected_t* event = (wifi_event_ap_staconnected_t*) event_data;
38         ESP_LOGI(TAG, "station "MACSTR" join, AID=%d",
39                  MAC2STR(event->mac), event->aid);
40     } else if (event_id == WIFI_EVENT_AP_STADISCONNECTED) {
41         wifi_event_ap_stadisconnected_t* event = (wifi_event_ap_stadisconnected_t*) event_data;
42         ESP_LOGI(TAG, "station "MACSTR" leave, AID=%d",
43                  MAC2STR(event->mac), event->aid);
44     }
45 }
46 
wifi_init_softap(void)47 void wifi_init_softap(void)
48 {
49     ESP_ERROR_CHECK(esp_netif_init());
50     ESP_ERROR_CHECK(esp_event_loop_create_default());
51     esp_netif_create_default_wifi_ap();
52 
53     wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
54     ESP_ERROR_CHECK(esp_wifi_init(&cfg));
55 
56     ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
57                                                         ESP_EVENT_ANY_ID,
58                                                         &wifi_event_handler,
59                                                         NULL,
60                                                         NULL));
61 
62     wifi_config_t wifi_config = {
63         .ap = {
64             .ssid = EXAMPLE_ESP_WIFI_SSID,
65             .ssid_len = strlen(EXAMPLE_ESP_WIFI_SSID),
66             .channel = EXAMPLE_ESP_WIFI_CHANNEL,
67             .password = EXAMPLE_ESP_WIFI_PASS,
68             .max_connection = EXAMPLE_MAX_STA_CONN,
69             .authmode = WIFI_AUTH_WPA_WPA2_PSK
70         },
71     };
72     if (strlen(EXAMPLE_ESP_WIFI_PASS) == 0) {
73         wifi_config.ap.authmode = WIFI_AUTH_OPEN;
74     }
75 
76     ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
77     ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_config));
78     ESP_ERROR_CHECK(esp_wifi_start());
79 
80     ESP_LOGI(TAG, "wifi_init_softap finished. SSID:%s password:%s channel:%d",
81              EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS, EXAMPLE_ESP_WIFI_CHANNEL);
82 }
83 
app_main(void)84 void app_main(void)
85 {
86     //Initialize NVS
87     esp_err_t ret = nvs_flash_init();
88     if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
89       ESP_ERROR_CHECK(nvs_flash_erase());
90       ret = nvs_flash_init();
91     }
92     ESP_ERROR_CHECK(ret);
93 
94     ESP_LOGI(TAG, "ESP_WIFI_MODE_AP");
95     wifi_init_softap();
96 }
97