学习日记——如何将ESP8266设置成AP模式(2020.7.1)

一、WIFI接口

  • wifi_station_set_config
    功能:设置 Wi-Fi Station 接口的配置参数,并保存到 Flash
    注意:
    • 请在 ESP8266 Station 使能的情况下,调用本接口。
    • 如果 wifi_station_set_config 在 user_init 中调用,则 ESP8266 Station 接口会在系统初始化完成后,⾃动连接 AP(路由),无需再调用 wifi_station_connect
    • 否则,需要调⽤ wifi_station_connect 连接 AP(路由)。
    • station_config.bssid_set ⼀般设置为 0 ,仅当需要检查 AP 的 MAC 地址时(多用于有重名 AP 的情况下)设置为 1。
    • 本设置如果与原设置不同,会更新保存到 Flash 系统参数区。
    函数定义:bool wifi_station_set_config (struct station_config *config)
    参数:struct station_config *config:Wi-Fi Station 接⼝配置参数指针
    返回:
    true:成功
    false:失败
    示例:
void	ICACHE_FLASH_ATTR
user_set_station_config(void)
{
 	 char	ssid[32]	=	SSID;	
 	 char	password[64]	=	PASSWORD;	
 	 struct	station_config	stationConf;	
 	 stationConf.bssid_set	=	0;		//need	not	check	MAC	address	of	AP
 	 

 	 os_memcpy(&stationConf.ssid,	ssid,	32);	
 	 os_memcpy(&stationConf.password,	password,	64);	
 	 wifi_station_set_config(&stationConf);	

}

void	user_init(void)
{
 	 wifi_set_opmode(STATIONAP_MODE);	//Set	softAP	+	station	mode

 	 	user_set_station_config();

}
  • wifi_station_set_config_current
    功能:设置 Wi-Fi Station 接口的配置参数,不保存到 Flash
    注意:
    • 请在 ESP8266 Station 使能的情况下,调用本接口。
    • 如果 wifi_station_set_configuser_init 中调用,则 ESP8266 Station 接口会在系统初始化完成后,自动连接 AP(路由),无需再调用 wifi_station_connect
    • 否则,需要调用 wifi_station_connect 连接 AP(路由)。
    station_config.bssid_set ⼀般设置为 0 ,仅当需要检查 AP 的 MAC 地址时(多用于有重名 AP 的情况下)设置为 1。
    • 本设置如果与原设置不同,会更新保存到 Flash 系统参数区。
    函数定义 bool wifi_station_set_config_current (struct station_config *config)
    参数 struct station_config *config:Wi-Fi Station 接⼝配置参数指针
    返回
    true:成功
    false:失败
  • wifi_station_connect
    功能:ESP8266 Wi-Fi Station 接口连接 AP
    注意:请勿在 user_init 中调⽤本接口,请在 ESP8266 Station 使能并初始化完成后调用;如果 ESP8266 已经连接某个 AP,请先调用 wifi_station_disconnect 断开上⼀次连接。
    函数定义:bool wifi_station_connect (void)
    参数:无
    返回:
    true:成功
    false:失败
  • wifi_station_disconnect
    功能:ESP8266 Wi-Fi Station 接口从 AP 断开连接
    注意:请勿在 user_init 中调⽤本接口,本接口必须在系统初始化完成后,并且 ESP8266 Station 接口使能的情况下调用。
    函数定义:bool wifi_station_disconnect (void)
    参数: 无
    返回:
    true:成功
    false:失败
  • wifi_station_get_connect_status
    功能:查询 ESP8266 Wi-Fi Station 接⼝连接 AP 的状态。
    注意:若为特殊应⽤场景:调用wifi_station_set_reconnect_policy 关闭重连功能,且未调用wifi_set_event_handler_cb 注册 Wi-Fi 事件回调,则本接口失效,无法准确获得连接状态。
    函数定义:uint8 wifi_station_get_connect_status (void)
    参数:无
    返回
enum{
				STATION_IDLE	=	0,
				STATION_CONNECTING,
				STATION_WRONG_PASSWORD,
				STATION_NO_AP_FOUND,
				STATION_CONNECT_FAIL,
				STATION_GOT_IP
};
  • wifi_station_scan
    功能:获取 AP 的信息
    注意:请勿在 user_init 中调⽤本接口,本接口必须在系统初始化完成后,并且 ESP8266 Station 接口使能的情况下调用。
    函数定义: bool wifi_station_scan (struct scan_config *config, scan_done_cb_t cb);
    结构体:
struct	scan_config	{
				uint8	*ssid;							//	AP’s	ssid
				uint8	*bssid;						//	AP’s	bssid
				uint8	channel;					//scan	a	specific	channel
				uint8	show_hidden;	//scan	APs	of	which	ssid	is	hidden.
				wifi_scan_type_t	scan_type;	//	scan	type,	active	or	passive
				wifi_scan_time_t	scan_time;	//	scan	time	per	channel

};

参数:
• struct scan_config *config:扫描 AP 的配置参数

  • 若 config==null:扫描获取所有可⽤ AP 的信息
  • 若 config.ssidnull && config.bssidnull && config.channel!=null:ESP8266
    Station 接口扫描获取特定信道上的 AP 信息。
  • 若 config.ssid!=null && config.bssidnull && config.channelnull:ESP8266
    Station 接口扫描获取所有信道上的某特定名称 AP 的信息。
    • scan_done_cb_t cb:扫描完成的 callback
    返回
    true:成功
    false:失败
  • scan_done_cb_t
    功能: wifi_station_scan 的回调函数
    注意: 请勿在 user_init 中调⽤本接口,本接口必须在系统初始化完成后,并且 ESP8266 Station 接口使能的情况下调用。
    函数定义 void scan_done_cb_t (void *arg, STATUS status)
    参数:
    void *arg:扫描获取到的 AP 信息指针,以链表形式存储,数据结构 struct bss_info
    STATUS status:扫描结果
    返回 无
    示例:
wifi_station_scan(&config,	scan_done);
static	void	ICACHE_FLASH_ATTR	scan_done(void	*arg,	STATUS	status)	{
				if	(status	==	OK)	{
								struct	bss_info	*bss_link	=	(struct	bss_info	*)arg;
								...
				}
}
  • wifi_station_ap_number_set
    功能:设置ESP8266 Station 最多可记录⼏个 AP 的信息。
    注意:ESP8266 Station 成功连⼊⼀个 AP 时,可以保存 AP 的 SSID 和 password 记录。本设置如果与原设置不同,会更新保存到 Flash 系统参数区。
    函数定义:bool wifi_station_ap_number_set (uint8 ap_number)
    参数 :uint8 ap_number:记录 AP 信息的最⼤数⽬(最⼤值为 5)
    返回:
    true:成功
    false:失败
  • wifi_station_get_ap_info
    功能: 获取 ESP8266 Station 保存的 AP 信息,最多记录 5 个。
    函数定义:uint8 wifi_station_get_ap_info(struct station_config config[])
    参数:struct station_config config[]:AP 的信息,数组大小必须为 5
    返回:记录 AP 的数⽬
    示例:
 struct	station_config	config[5];
int	i	=	wifi_station_get_ap_info(config);
  • wifi_station_get_current_ap_id
    功能:获取当前连接的 AP 保存记录 ID 值。ESP8266 可记录每⼀个配置连接的 AP,从 0 开始计数。
    函数定义:uint8 wifi_station_get_current_ap_id ();
    参数:无
    返回:当前连接的 AP 保存记录的 ID 值。
  • wifi_station_get_auto_connect
    功能:查询 ESP8266 Station 上电是否会⾃动连接已记录的 AP(路由)。
    函数定义:uint8 wifi_station_get_auto_connect(void)
    参数:无
    返回:
    0:不⾃动连接 AP
    ⾮ 0:⾃动连接 AP
  • wifi_station_set_auto_connect
    功能:设置 ESP8266 Station 上电是否⾃动连接已记录的 AP(路由),默认为⾃动连接。
    注意:
    • 本接口如果在 user_init 中调⽤,则当前这次上电就生效;如果在其他地方调⽤,则下⼀次上电生效。
    • 本设置如果与原设置不同,会更新保存到 Flash 系统参数区。
    函数定义:bool wifi_station_set_auto_connect(uint8 set)
    参数
    uint8 set:上电是否⾃动连接 AP
    • 0:不⾃动连接 AP
    • 1:⾃动连接 AP
    返回
    true:成功
    false:失败

二、例程

  • 在 user init中首先执行初始化操作
// OLED初始化
OLED_ Init();
OLED_ ShowStr ing(0, 0, "ESP8266 = AP") ;
OLED_ ShowString(0, 2, "IP:") ;
  • 调用ESP8266_ AP_ Init_ JX()函数设置ESP8266AP模式得相关参数
void ICACHE_ FLASH_ ATTR ESP8266 AP Init JX()
{
struct softap_ config AP_ Config;// AP参数结构体
wifi_ set_ opmode (0x02) ;//设置为AP模式,并保存到Flash
//结构体赋值(注意: [ 服务集标识符/密码]须设为字符串形式)
os_ memset (&AP_ Config, 0sizeof (struct softap_ config)); // AP参数结构体= 0
os_ strcpy (AP_ Config. ssid, ESP8266_ AP_ SSID); // 设置SSID (将字符串复制到ssid数组)
os_ strcpy (AP_ Conf ig. password, ESP8266_ AP_ PASS); // 设置密码(将字符串复制到passwor d数组)
AP_ Config. ssid_ len=os_ str len (ESP8266_ AP_ SSID); // 设置ss id长度(和SS ID的长度一致)
AP_ Config. channel=1;//通道号1~13
AP_ Config. authmode=AUTH WPA2_ PSK;//通道号1~13
AP_ Config. ssid_ bidden=0;//不隐藏SSID
AP_ Conf ig. max_ cmnnection=4;//最大连接数
AP_ Conf ig. beacon_ interval=100;//信标间隔时槽100~60000 ms
wifi_ softap_ set_ config (&AP_ Config) ;//设置soft-AP,并保存到Flash
}
  • 注意&AP_ Config这个参数是AP参数结构体指针,在函数得最开始得地方需要定义一个参数结构体。

结构体得成员:

struct softap config
{
uint8 ssid[32]//存放wifi名
uint8 password [64] ;//wifi密码
uint8 ssid_len;//wifi名长度
uint8 channel ;//通道号
AUTH_ MODE authmode ;//加密方式
uint8 ssid_ hidden;//是否隐藏wifi名
uint8 max_ connection;//最大连接数
uint16 beacon_ interval ;//信标间隔时槽
}

注意:
wifi名和wifi密码使用宏定义
sizeof(…):返回参数所占空间的大小(以字节为单位)
os memset(void *s,int ch, size_ t n)在一段内存块中填充某个指定值
void *s 内存块指针
int ch 填充值
size_t n.填充大小
char os_ strcpy(char *s1, const char *s2)将参数2指向的”字符串”复制到参数1指向的地址处

  • 设置AP相关参数后,执行定时回调函数,如何结束user init函数
    定时的回调函数
void ICACHE_ FLASH_ ATTR 0S_ Timer_ 1_ cb(void) 
{
   struct ip_ info ST_ ESP8266_ IP; // IP信息结构体
   u8 ESP8266_ IP[4]; //点分十进制形式保存IP 
   //查询并打印ESP8266的工作模式
   switch (wifi_ get_ _opmode()) // 输出工作模式
   {
        case 0x01: os_ pr intf ("\nESP8266_ Mode = Stat ion\n") ;
        case 0x02: os_ _pr intf ("\nESP8266_ Mode = SoftAP\n") ;
        case 0x03: os_ pr intf ("\nESP8266_ Mode = Stat i on+SoftAP\n") ; break;
     }
//获取ESP8266_ AP模式下的IP地址
 // [AP模式下,如果开启DHCP (默认),并且未设置IP相关参数,ESP8266的 IP地址=192.168.4.1]
wifi_ get_ ip_ info (SOFTAP_ IF, &ST_ ESP8266_ IP); //参数2: IP信息结构体指针
 // ESP8266_ AP_ IP. ip. addr==32位二进制IP地址,将它转换为点分十进制的形式。
 ESP8266_ IP[0] = ST_ ESP8266_ IP. ip. addr ;//点分十进制IP的第- -个数<==> addr低八位
 ESP8266_ IP[1] = ST_ ESP8266_ IP. ip. addr>>8;//点分十进制IP的第二个数<==> addr 次低八位
 ESP8266_ IP[2] = ST_ ESP8266_ IP. ip. addr>>16;//点分十进制IP的第三个数<==> addr次高八位
 ESP8266_ IP[3] = ST_ ESP8266_ IP. ip. addr>>24;//点分十进制IP的第四个数<==> addr高八位
 //打印ESP8266的IP地址
 os_ pr intf ("ESP8266_ IP = %d. %d. %d. %d\n", ESP8266_ IP[0], ESP8266_ IP[1], ESP8266_ IP[2], ESP8266_ IP[3]);
 OLED_ ShowIP (24, 2, ESP8266_ IP);//显示ESP8266的IP地址
 //查询并打印接入此WIFI的设备数量
 os_ pr intf ("Number of devices connected to this WIFI = %d\n" ,wifi_ softap_ get_ stat ion_ num()) ;

现象

打开串口,复位ESP8266
在这里插入图片描述
由串口打印出得数据可以看到,ESP8266当前模式是AP模式。而且可以看到ESP8266得、的IP地址是192.168.4.1.当前工作模式是AP模式。当前接入此wifi的设备数量是0
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
当我使用链接此wifi时,串口打印显示wifi连接数量为1个。
在这里插入图片描述

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

热爱生活的fuyao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值