softap_example_main.c


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_MAX_STA_CONN       CONFIG_ESP_MAX_STA_CONN
29
30
static const char *TAG = "wifi softAP";
31
32
static void wifi_event_handler(void* arg, esp_event_base_t event_base,
33
                                    int32_t event_id, void* event_data)
34
{
35
    if (event_id == WIFI_EVENT_AP_STACONNECTED) {
36
        wifi_event_ap_staconnected_t* event = (wifi_event_ap_staconnected_t*) event_data;
37
        ESP_LOGI(TAG, "station "MACSTR" join, AID=%d",
38
                 MAC2STR(event->mac), event->aid);
39
    } else if (event_id == WIFI_EVENT_AP_STADISCONNECTED) {
40
        wifi_event_ap_stadisconnected_t* event = (wifi_event_ap_stadisconnected_t*) event_data;
41
        ESP_LOGI(TAG, "station "MACSTR" leave, AID=%d",
42
                 MAC2STR(event->mac), event->aid);
43
    }
44
}
45
46
void wifi_init_softap(void)
47
{
48
    tcpip_adapter_init();
49
    ESP_ERROR_CHECK(esp_event_loop_create_default());
50
51
    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
52
    ESP_ERROR_CHECK(esp_wifi_init(&cfg));
53
54
    ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL));
55
56
   uint8_t mac_adress[6] = { 0 };
57
    char example_ssid[32] = { 0 };
58
//
59
    esp_efuse_mac_get_default(mac_adress);
60
    sprintf(example_ssid, EXAMPLE_ESP_WIFI_SSID"-%02X-%02X-%02X", mac_adress[3], mac_adress[4], mac_adress[5]);
61
62
    wifi_config_t wifi_config = {
63
        .ap = {
64
            .ssid = example_ssid,
65
            .ssid_len = strlen(example_ssid),
66
            .password = EXAMPLE_ESP_WIFI_PASS,
67
            .max_connection = EXAMPLE_MAX_STA_CONN,
68
            .authmode = WIFI_AUTH_WPA_WPA2_PSK
69
        },
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(ESP_IF_WIFI_AP, &wifi_config));
78
    ESP_ERROR_CHECK(esp_wifi_start());
79
80
    ESP_LOGI(TAG, "wifi_init_softap finished. SSID:%s password:%s",
81
             EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
82
}
83
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
}