ESP8266的三种工作方式
- AP模式
 - Station模式
 - AP+Station模式
 
esp8266模组在Station模式下是一个网络节点,可以通过路由器接入到互联网;esp8266模组在AP模式下是一个WIFI热点,可以使用手机或者电脑直接进行局域网控制;AP+Station模式是两种模式共存的方式。
基于SDK的开发流程
设置Station模式
流程图
代码
#define SSID "你的路由器WIFI名称"
#define PASSWORD "你的路由器密码"
/******************************************************************************
 * FunctionName : user_init
 * Description  : entry of user application, init user function here
 * Parameters   : none
 * Returns      : none
*******************************************************************************/
void ICACHE_FLASH_ATTR
user_init(void)
{
    printf("SDK version:%s\n", system_get_sdk_version());
	wifi_set_opmode(STATION_MODE);		//设置模式
	struct station_config * config = \
		(struct station_config *)malloc( sizeof(struct station_config) );	//给结构体分配空间
	sprintf(config->ssid, SSID);	//填充参数
	sprintf(config->password, PASSWORD);
	
	wifi_station_set_config(config);	//设置参数
	free(config);	//释放空间
	
	wifi_station_connect();	//连接
	
}  
串口打印结果
设置AP模式
流程图
代码
#define SSID "你要设置的热点名"
#define PASSWORD "你要设置的接入密码"
/******************************************************************************
 * FunctionName : user_init
 * Description  : entry of user application, init user function here
 * Parameters   : none
 * Returns      : none
*******************************************************************************/
void ICACHE_FLASH_ATTR
user_init(void)
{
    printf("SDK version:%s\n", system_get_sdk_version());
	
	wifi_set_opmode(SOFTAP_MODE);		//设置模式
	struct softap_config * config = \
		(struct softap_config *)malloc( sizeof(struct softap_config) );	//给结构体分配空间
		
	wifi_softap_get_config(config);	//获取参数到内存
	sprintf(config->ssid, SSID);	//填充参数
	sprintf(config->password, PASSWORD);
	config->authmode = AUTH_WPA_WPA2_PSK;	//加密方式
	config->max_connection = 5;				//最大连接数量
	config->ssid_len = 0;					//设置长度为0
	wifi_softap_set_config(config);	//设置参数
	free(config);	//释放空间
}  
串口打印结果
Smartconfig 智能连接
使用之前station模式,就必需在项目代码中配置接入路由器的名称-密码,很不方便。使用智能连接的方式,可以让wifi模块通过手机进而接入路由器了,无需在代码中配置密码。
 手机通过UDP广播,将AP的相关信息组帧发出。而WiFi模块一直处于UDP监听状态。获取到AP信息之后,WiFi模块便可以接入AP了。
流程图
代码配置
/******************************************************************************
 * FunctionName : user_init
 * Description  : entry of user application, init user function here
 * Parameters   : none
 * Returns      : none
*******************************************************************************/
void ICACHE_FLASH_ATTR
user_init(void)
{
    printf("SDK version:%s\n", system_get_sdk_version());
    /* need to set opmode before you set config */
    wifi_set_opmode(STATION_MODE);
    xTaskCreate(smartconfig_task, "smartconfig_task", 256, NULL, 2, NULL);
}  
软件
Airkiss是微信针对物联网开发的,底层是对Smartconfig技术封装。扫码使用
串口打印
PS:至于源码的编译与烧写,我这边使用的是乐鑫提供的编译环境,有兴趣可以get一下。

京公网安备 11010502036488号