#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include "esp_wifi.h"
#include "esp_log.h"
#include "esp_event.h"
#include "nvs_flash.h"
#include "esp_system.h"
#include "driver/uart.h"
#include "string.h"
#include "driver/gpio.h"
//#include "uart0.h"
// #include "esp_spi_flash.h"
/* Set the SSID and Password via project configuration, or can set directly here */
#define DEFAULT_SSID CONFIG_EXAMPLE_WIFI_SSID
#define DEFAULT_PWD CONFIG_EXAMPLE_WIFI_PASSWORD
#if CONFIG_EXAMPLE_WIFI_ALL_CHANNEL_SCAN
#define DEFAULT_SCAN_METHOD WIFI_ALL_CHANNEL_SCAN
#elif CONFIG_EXAMPLE_WIFI_FAST_SCAN
#define DEFAULT_SCAN_METHOD WIFI_FAST_SCAN
#else
#define DEFAULT_SCAN_METHOD WIFI_FAST_SCAN
#endif /*CONFIG_EXAMPLE_SCAN_METHOD*/
#if CONFIG_EXAMPLE_WIFI_CONNECT_AP_BY_SIGNAL
#define DEFAULT_SORT_METHOD WIFI_CONNECT_AP_BY_SIGNAL
#elif CONFIG_EXAMPLE_WIFI_CONNECT_AP_BY_SECURITY
#define DEFAULT_SORT_METHOD WIFI_CONNECT_AP_BY_SECURITY
#else
#define DEFAULT_SORT_METHOD WIFI_CONNECT_AP_BY_SIGNAL
#endif /*CONFIG_EXAMPLE_SORT_METHOD*/
#if CONFIG_EXAMPLE_FAST_SCAN_THRESHOLD
#define DEFAULT_RSSI CONFIG_EXAMPLE_FAST_SCAN_MINIMUM_SIGNAL
#if CONFIG_EXAMPLE_FAST_SCAN_WEAKEST_AUTHMODE_OPEN
#define DEFAULT_AUTHMODE WIFI_AUTH_OPEN
#elif CONFIG_EXAMPLE_FAST_SCAN_WEAKEST_AUTHMODE_WEP
#define DEFAULT_AUTHMODE WIFI_AUTH_WEP
#elif CONFIG_EXAMPLE_FAST_SCAN_WEAKEST_AUTHMODE_WPA
#define DEFAULT_AUTHMODE WIFI_AUTH_WPA_PSK
#elif CONFIG_EXAMPLE_FAST_SCAN_WEAKEST_AUTHMODE_WPA2
#define DEFAULT_AUTHMODE WIFI_AUTH_WPA2_PSK
#else
#define DEFAULT_AUTHMODE WIFI_AUTH_OPEN
#endif
#else
#define DEFAULT_RSSI -127
#define DEFAULT_AUTHMODE WIFI_AUTH_OPEN
#endif /*CONFIG_EXAMPLE_FAST_SCAN_THRESHOLD*/
const int SCAN_DONE_BIT = BIT0;
static const char *TAG = "scan";
static void event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{
static uint8_t i=0;
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
//if(i++==2) esp_wifi_connect();
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
printf("wifi disconnected
****************
*
!!!
***************
");
// esp_wifi_connect();
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_SCAN_DONE) {
printf("scan done!
****************
*
***************
");
printf("i value is %d
",i);
if(i++==2) esp_wifi_connect();
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
}
}
/* Initialize Wi-Fi as sta and set scan method */
void uart_config(void)
{
//串口配置结构体
uart_config_t uart0_config, uart1_config,uart2_config;
//串口参数配置->uart0
uart0_config.baud_rate = 115200; //波特率
uart0_config.data_bits = UART_DATA_8_BITS; //数据位
uart0_config.parity = UART_PARITY_DISABLE; //校验位
uart0_config.stop_bits = UART_STOP_BITS_1; //停止位
uart0_config.flow_ctrl = UART_HW_FLOWCTRL_DISABLE; //硬件流控
uart_param_config(UART_NUM_0, &uart0_config); //设置串口
//IO映射-> T:IO1 R:IO3
// uart_set_pin(UART_NUM_0, TXD0_PIN, RXD0_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
// 注册串口服务即使能+设置缓存区大小
uart_driver_install(UART_NUM_0, 1024 * 2, 1024 * 2, 0, NULL, 0);
}
/****************/
static void fast_scan(void)
{
uint16_t ap_number=0;
//uint16_t number = 20; // 默认扫描列表大小
// wifi_ap_record_t ap_records[20]; // AP信息结构体大小
uint8_t* get_cmd;
uint8_t recv_length=0;
uint8_t i=0;
uint8_t ssid[32];
wifi_ap_record_t *ap_records;
ESP_ERROR_CHECK(esp_netif_init()); // 创建LwIP核心任务并初始化与LwIP相关的工作
ESP_ERROR_CHECK(esp_event_loop_create_default());
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, &event_handler, NULL, NULL)); // esp_wifi/include/esp_wifi_types.h
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL, NULL)); // esp_netif/include/esp_netif_types.h
// Initialize default station as network interface instance (esp-netif)
esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta(); // 分配内网ip和掩码
assert(sta_netif);
// Initialize and start WiFi
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); // 设置接入模式为 sta
ESP_ERROR_CHECK(esp_wifi_start());
ESP_ERROR_CHECK(esp_wifi_scan_start(NULL,true)); // 如果要扫描到所有的wifi就必须配置为null
ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&ap_number));
ap_records=(wifi_ap_record_t *)malloc(ap_number*sizeof(wifi_ap_record_t));
ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&ap_number,ap_records));
for(;i<ap_number;i++)
{
printf("ssid=%s bssid=%s primary=%d authmode=%d
",ap_records[i].ssid,ap_records[i].bssid,ap_records[i].primary,ap_records[i].authmode);
}
get_cmd=(uint8_t*)malloc(20*sizeof(uint8_t));
printf("which ssid choose
");
while(recv_length==0)
{
recv_length = uart_read_bytes(UART_NUM_0, get_cmd,20, 1000 / portTICK_RATE_MS);
}
i=get_cmd[0]-48; // 字符转数字
printf("receive value is %d,strlen is %d
",get_cmd[i],strlen((const char *)get_cmd));
printf("choose ssid is %s
",ap_records[i].ssid);
recv_length = 0;
wifi_config_t wifi_config = {
.sta = {
// .ssid = ssid,
// .password = DEFAULT_PWD,
.scan_method = DEFAULT_SCAN_METHOD,
.sort_method = DEFAULT_SORT_METHOD,
.threshold.rssi = DEFAULT_RSSI,
.threshold.authmode = DEFAULT_AUTHMODE, //ap_records[i].authmode,
},
};
memcpy(wifi_config.sta.ssid,ap_records[i].ssid,strlen((const char *)ap_records[i].ssid));
printf("wifi_config.sta.ssid %s
",wifi_config.sta.ssid);
printf("
print password
");
while(recv_length==0)
{
recv_length = uart_read_bytes(UART_NUM_0, get_cmd, 20, 1000 / portTICK_RATE_MS);
}
printf("receive string is %s,strlen is %d
",get_cmd,strlen((const char *)get_cmd));
memcpy(wifi_config.sta.password,get_cmd,strlen((const char *)get_cmd));
printf("wifi_config.sta.password %s
",wifi_config.sta.password);
recv_length = 0;
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());
ESP_ERROR_CHECK(esp_wifi_connect());
printf("free memary
");
free(ap_records);
free(get_cmd);
}
void app_main(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 );
uart_config();
fast_scan();
}
命令行上位机