02-ESP8266的AP模式学习

上次学习了ESP8266的SAT模式,今天学习Soft-AP模式,也叫热点模式

02-ESP8266的Soft-AP模式学习

AP模式

​ AP模式全称:Access Pointer可接入点,通俗的说叫热点。常见的路由器就是AP模式工作,手机和电脑也可以作为热点使用。作为热点使用时,站点(STA)加入到AP模式的网络中。下图是ESP8266处于AP模式的示意图。

image

​ Soft-AP是指软件上实现AP功能,功能性与硬件AP差不多,但是Soft-AP覆盖面小,接入点少。

ESP8266WiFIAP库

主要是两个部分,相比STA模式要少一些

第一部分:Soft-AP模式参数配置

        bool softAP(const char* ssid, const char* psk = NULL, int channel = 1, int ssid_hidden = 0, int max_connection = 4);
        bool softAP(const String& ssid,const String& psk = emptyString,int channel = 1,int ssid_hidden = 0,int max_connection = 4);
        bool softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet);
        bool softAPdisconnect(bool wifioff = false);

第二部分:Soft-AP模式的信息管理

        uint8_t softAPgetStationNum();

        IPAddress softAPIP();

        uint8_t* softAPmacAddress(uint8_t* mac);
        String softAPmacAddress(void);

	String softAPSSID() const;
	String softAPPSK() const;

一般配置流程:

1.设置为AP模式

WiFi.enableAP(ENABLEAP);

2.配置soft-AP模式信息

WiFi.softAP(SSID , PSK)

例程:

/*
内容:学习ESP8266的Soft-AP模式
时间:2022.4.24
作者:bobo
邮件:980181362@qq.com
*/

//头文件
#include <ESP8266WiFi.h>    //里面包含AP头文件:ESP8266WiFiAP.h

//宏定义
#define ENABLEAP 1 //AP模式使能

//常量定义
char SSID[] = "ESP8266";
char PSK[] = "123456789";

//启动函数
void setup()
{
    //串口调试配置
    Serial.begin(9600);  
    //延时为了区别启动过程和后面答应的内容
    delay(2000);
    //设置WiFi的工作模式
    WiFi.mode(WIFI_AP);
    //配置AP模式
    if(WiFi.softAP(SSID , PSK))//设置WiFi名字和密码
    {
        Serial.println("Soft-AP is config successful");
    }
    else
    {
        Serial.println("Soft-AP is config failed");
    }
    //使能Soft-AP模式
    WiFi.enableAP(ENABLEAP);

    //打印当前模式
    Serial.print("WiFi is mode :");
    Serial.println(WiFi.getMode());
    //打印当前MAC地址  
    Serial.print("MACIP is :");
    Serial.println(WiFi.softAPmacAddress());
    //打印当前IP地址
    Serial.print("APIP is :");
    Serial.println(WiFi.softAPIP());
}

//无效循环函数
void loop()
{
    //打印STA数量
    Serial.print("The number of STA is :");
    Serial.println(WiFi.softAPgetStationNum());
    delay(500);
}

串口调试效果:

Soft-AP is config successful
WiFi is mode :2
MACIP is :A6:E5:7C:BC:9A:87
APIP is :192.168.4.1
The number of STA is :0
The number of STA is :0
The number of STA is :0
The number of STA is :0
The number of STA is :0
The number of STA is :0
The number of STA is :0
The number of STA is :0
he number of STA is :0
The number of STA is :0
The number of STA is :0
The number of STA is :0
The number of STA is :0


版权声明:本文为TB980181原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。