151 lines
5.3 KiB
C
151 lines
5.3 KiB
C
/* WiFi softAP Example
|
|
|
|
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 <stdio.h>
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/task.h"
|
|
#include "esp_system.h"
|
|
#include "../../../components/esp_wifi/include/esp_wifi.h"
|
|
#include "esp_event.h"
|
|
#include "esp_log.h"
|
|
#include "nvs_flash.h"
|
|
|
|
#include "lwip/err.h"
|
|
#include "lwip/sys.h"
|
|
#include "driver/ledc.h"
|
|
#include "esp_err.h"
|
|
#include "../../../components/esp_hw_support/include/esp_mac.h"
|
|
/* The examples use WiFi configuration that you can set via project configuration menu.
|
|
|
|
If you'd rather not, just change the below entries to strings with
|
|
the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
|
|
*/
|
|
#define ESP_WIFI_SSID "T2N_056455"
|
|
#define ESP_WIFI_PASS "01010101"
|
|
#define ESP_WIFI_CHANNEL 1
|
|
#define MAX_STA_CONN 8
|
|
|
|
static const char *TAG = "wifi softAP";
|
|
|
|
#define LEDC_TIMER LEDC_TIMER_0
|
|
#define LEDC_MODE LEDC_LOW_SPEED_MODE
|
|
#define LEDC_OUTPUT_IO (21) // Define the output GPIO
|
|
#define LEDC_CHANNEL LEDC_CHANNEL_0
|
|
#define LEDC_DUTY_RES LEDC_TIMER_13_BIT // Set duty resolution to 13 bits
|
|
#define LEDC_DUTY (4095) // Set duty to 50%. ((2 ** 13) - 1) * 50% = 4095
|
|
#define LEDC_FREQUENCY (3840) // Frequency in Hertz. Set frequency at 5 kHz
|
|
|
|
static void wifi_event_handler(void *arg, esp_event_base_t event_base,
|
|
int32_t event_id, void *event_data)
|
|
{
|
|
// 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);
|
|
// }
|
|
}
|
|
|
|
void wifi_init_softap(void)
|
|
{
|
|
ESP_ERROR_CHECK(esp_netif_init());
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
|
esp_netif_create_default_wifi_ap();
|
|
|
|
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
|
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
|
|
|
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
|
|
ESP_EVENT_ANY_ID,
|
|
&wifi_event_handler,
|
|
NULL,
|
|
NULL));
|
|
|
|
wifi_config_t wifi_config = {
|
|
.ap = {
|
|
.ssid = ESP_WIFI_SSID,
|
|
.ssid_len = strlen(ESP_WIFI_SSID),
|
|
.channel = ESP_WIFI_CHANNEL,
|
|
.password = ESP_WIFI_PASS,
|
|
.max_connection = MAX_STA_CONN,
|
|
.authmode = WIFI_AUTH_WPA_WPA2_PSK},
|
|
};
|
|
|
|
nvs_handle_t my_handle;
|
|
esp_err_t err = nvs_open("wifi", NVS_READWRITE, &my_handle);
|
|
if (err == ESP_OK)
|
|
{
|
|
size_t len;
|
|
char temp_str[64];
|
|
uint8_t channel;
|
|
if (nvs_get_str(my_handle, "ap_ssid", temp_str, &len) == ESP_OK)
|
|
{
|
|
strncpy((char*)wifi_config.ap.ssid, temp_str, len);
|
|
wifi_config.ap.ssid_len = len;
|
|
}
|
|
if (nvs_get_str(my_handle, "ap_psk", temp_str, &len) == ESP_OK)
|
|
{
|
|
strncpy((char*)wifi_config.ap.password, temp_str, len);
|
|
}
|
|
if (nvs_get_u8(my_handle, "ap_ch", &channel) == ESP_OK)
|
|
{
|
|
wifi_config.ap.channel = channel;
|
|
}
|
|
|
|
nvs_close(my_handle);
|
|
}
|
|
if (strlen(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(WIFI_IF_AP, &wifi_config));
|
|
ESP_ERROR_CHECK(esp_wifi_start());
|
|
|
|
ESP_LOGI(TAG, "wifi_init_softap finished. SSID:%s password:%s channel:%d",
|
|
ESP_WIFI_SSID, ESP_WIFI_PASS, ESP_WIFI_CHANNEL);
|
|
}
|
|
|
|
// void app_main2(void)
|
|
// {
|
|
// // Initialize NVS
|
|
// esp_err_t ret = nvs_flash_init();
|
|
// if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
|
|
// {
|
|
// ESP_ERROR_CHECK(nvs_flash_erase());
|
|
// ret = nvs_flash_init();
|
|
// }
|
|
// ESP_ERROR_CHECK(ret);
|
|
|
|
// ESP_LOGI(TAG, "ESP_WIFI_MODE_AP");
|
|
// wifi_init_softap();
|
|
// // ledc_init();
|
|
// // ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, LEDC_CHANNEL, LEDC_DUTY));
|
|
// // // Update duty to apply the new value
|
|
// // ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LEDC_CHANNEL));
|
|
// // gpio_config_t io_conf = {};
|
|
// // // disable interrupt
|
|
// // io_conf.intr_type = GPIO_INTR_DISABLE;
|
|
// // // set as output mode
|
|
// // io_conf.mode = GPIO_MODE_OUTPUT;
|
|
// // // bit mask of the pins that you want to set,e.g.GPIO18/19
|
|
// // io_conf.pin_bit_mask = 1 << 9;
|
|
// // // disable pull-down mode
|
|
// // io_conf.pull_down_en = 0;
|
|
// // // disable pull-up mode
|
|
// // io_conf.pull_up_en = 0;
|
|
// // // configure GPIO with the given settings
|
|
// // gpio_config(&io_conf);
|
|
// // gpio_set_level(9, 1);
|
|
// }
|