ESP8266之wifi模式设置

模块:esp-01s
SDK版本:ESP8266_RTOS_SDK-3.0
开发环境:wsl+vscode
功能模块:wifi
ESP8266⽀持wifi协议 802.11 b/g/n
有2个Wi-Fi接⼝,⽀持基础结构型⽹络 (Infrastructure BSS) ⼯作站 (Station) 模式/SoftAP
模式。
本文参考在RTOS-SDK下连接wifi

本来想使用SDK里提供的例程,但是发现有一些问题,在作预编译处理时无论“EXAMPLE_ESP_WIFI_MODE_AP ”是什么,总会只编译station模式。看样子只能将station模式和softAP模式分开编译,根据实际选择。
station模式注意要连接的wifi名称和密码是否正确。
以下是代码部分,经过烧录验证成功,代码每一步都有非常详细的注释

1.Station模式

#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "rom/ets_sys.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "driver/uart.h"
//wifi的回调事件注册
//static EventGroupHandle_t wifi_event_group;
static esp_err_t event_handler(void *ctx, system_event_t *event)
{
    switch(event->event_id) {
    //station模式下开始连接
    case SYSTEM_EVENT_STA_START:
        printf("开始连接wifi\n");
        esp_wifi_connect();
        break;
    //station模式下已完成连接并且获取到了IP
    case SYSTEM_EVENT_STA_GOT_IP: 
        //将其ip信息打印出来
        printf("获取到IP:%s\n",ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip));
        break;
    //AP模式下的其他设备连接上了本设备AP
    case SYSTEM_EVENT_AP_STACONNECTED:
    //将其连接上本设备的设备的信息打印出来
    printf("设备:"MACSTR" 加入, AID=%d\n",MAC2STR(event->event_info.sta_connected.mac),event- 
    >event_info.sta_connected.aid);
        break;
    //AP模式下,其他设备与本设备断开的连接
    case SYSTEM_EVENT_AP_STADISCONNECTED:
    printf("station:"MACSTR"断开, AID=%d\n",MAC2STR(event- 
  >event_info.sta_disconnected.mac),event->event_info.sta_disconnected.aid);
        break;
    //station模式下 本设备从AP断开/AP踢了本设备
    case SYSTEM_EVENT_STA_DISCONNECTED:
        esp_wifi_connect();//重新连接之前的AP
        break;
    default:
        break;
    }
    return ESP_OK;
}  
void wifi_init_sta(void)
{
   //开始wifi 连接
//wifi_event_group = xEventGroupCreate(); //创建一个事件组
tcpip_adapter_init();           //初始化TCP/IP相关,这个函数只需要调用一次
esp_event_loop_init(event_handler, NULL);   //注册事件
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
esp_wifi_init(&cfg); //初始化wifi底层相关
//构造需要连接的wifi信息
wifi_config_t wifi_config = {
    .sta = {
        .ssid = "esp8266_sta",  //填入wifi名称/ssid
        .password = "12345678" //填入wifi密码
    },
};
esp_wifi_set_mode(WIFI_MODE_STA); //设置wifi模式,设置为station模式
esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config); //将其wifi信息配置进去
esp_wifi_start(); //开启wifi状态机 ()
}
void app_main(void)
{
    // uart_set_baudrate(0,115200);//将串口设置成115200
     esp_err_t ret = nvs_flash_init();
     if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
      ESP_ERROR_CHECK(nvs_flash_erase());
      ret = nvs_flash_init();
    }
    ESP_ERROR_CHECK(ret);
    printf("SDK version:%s\n", esp_get_idf_version());
    printf("ESP_WIFI_MODE_STA");//  打印当前模式                                                                                     
    wifi_init_sta();//station模式初始化
}

2.SoftAP模式

#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "rom/ets_sys.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "driver/uart.h" 
//wifi的回调事件注册
//static EventGroupHandle_t wifi_event_group;
static esp_err_t event_handler(void *ctx, system_event_t *event)
{
    switch(event->event_id) {
    //station模式下开始连接
    case SYSTEM_EVENT_STA_START:
        printf("开始连接wifi\n");
        esp_wifi_connect();
        break;
    //station模式下已完成连接并且获取到了IP
    case SYSTEM_EVENT_STA_GOT_IP: 
        //将其ip信息打印出来
        printf("获取到IP:%s\n",ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip));
        break;
    //AP模式下的其他设备连接上了本设备AP
    case SYSTEM_EVENT_AP_STACONNECTED:
    //将其连接上本设备的设备的信息打印出来
    printf("设备:"MACSTR" 加入, AID=%d\n",MAC2STR(event->event_info.sta_connected.mac),event- 
  >event_info.sta_connected.aid);
        break;
    //AP模式下,其他设备与本设备断开的连接
    case SYSTEM_EVENT_AP_STADISCONNECTED:
    printf("station:"MACSTR"断开, AID=%d\n",MAC2STR(event- 
 >event_info.sta_disconnected.mac),event->event_info.sta_disconnected.aid);
        break;
    //station模式下 本设备从AP断开/AP踢了本设备
    case SYSTEM_EVENT_STA_DISCONNECTED:
        esp_wifi_connect();//重新连接之前的AP
        break;
    default:
        break;
    }
    return ESP_OK;
}
void wifi_init_softap(void)
{
    //开始wifi 连接
    //wifi_event_group = xEventGroupCreate(); //创建一个事件组
    tcpip_adapter_init();           //初始化TCP/IP相关,这个函数只需要调用一次
    esp_event_loop_init(event_handler, NULL);   //注册事件
    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    esp_wifi_init(&cfg); //初始化wifi底层相关

    //构造需要连接的wifi信息
     wifi_config_t wifi_config = {
        .ap = {
            .ssid = "esp8266_ap", //设置ap名字
            .password = "12345678",//设置ap密码
            .max_connection = 1,//设置ap最大连接数
            .authmode = WIFI_AUTH_WPA_WPA2_PSK //设置ap加密模式
        },
    };
    esp_wifi_set_mode(WIFI_MODE_AP); //设置wifi模式,设置为ap模式
    esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config); //将其wifi信息配置进去
    esp_wifi_start(); //开启wifi状态机 ()
}
void app_main(void)
{
    // uart_set_baudrate(0,115200);//将串口设置成115200
     
     esp_err_t ret = nvs_flash_init();
     if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
      ESP_ERROR_CHECK(nvs_flash_erase());
      ret = nvs_flash_init();
    }
    ESP_ERROR_CHECK(ret);
    printf("SDK version:%s\n", esp_get_idf_version());
    printf("ESP_WIFI_MODE_AP");//打印当前模式                                                                                       
    wifi_init_softap();//ap模式初始化
}

本文链接:

https://www.veryxs.com/index.php/archives/5/
1 + 9 =
快来做第一个评论的人吧~