环境及工具与第一章相同,这里就不在重复!
本章通过代码示例,来实现wifi模块连接到路由器的功能,模块开机上电是自动进入快连模式,待30秒后退出快连模式,若连接成功则运行正常程序,若未连接成功,则尝试以保存在flash内的参数进行连接,若连接成功则运行正常程序,若未成功则继续尝试重连。
加入了状态指示功能,开机指示灯闪烁,用于提示进入快连模式,若连接失败则指示灯熄灭,若连接成功则指示灯常亮,并打印已连接字符到串口。
使用方法:上电进入快连模式后,可以用EspTouch或AirKiss工具进行配网测试
user_main.c的全部代码如下
#include "ets_sys.h"
#include "osapi.h"
#include "gpio.h"//io口
#include "user_interface.h"
#include "smartconfig.h"//配网
#include "user_devicefind.h"
#include "user_webserver.h"
#if ESP_PLATFORM
#include "user_esp_platform.h"
#endif
void user_rf_pre_init(void){}
/*******************************************************************************
* 完成快连模式
*******************************************************************************/
void ICACHE_FLASH_ATTR
smartconfig_done(sc_status status, void *pdata)
{
switch(status) {
case SC_STATUS_WAIT://连接未开始,请勿在此阶段开始连接
os_printf("SC_STATUS_WAIT\n");
break;
case SC_STATUS_FIND_CHANNEL://在此阶段进行配对连接
os_printf("SC_STATUS_FIND_CHANNEL\n");
break;
case SC_STATUS_GETTING_SSID_PSWD://得到wifi名字和密码
os_printf("SC_STATUS_GETTING_SSID_PSWD\n");
sc_type *type = pdata;
if (*type == SC_TYPE_ESPTOUCH) {
os_printf("SC_TYPE:SC_TYPE_ESPTOUCH\n");
} else {
os_printf("SC_TYPE:SC_TYPE_AIRKISS\n");
}
break;
case SC_STATUS_LINK://正在连接路由器
os_printf("SC_STATUS_LINK\n");
struct station_config *sta_conf = pdata;
wifi_station_set_config(sta_conf);//设置WiFi station接口的配置参数,并保存到flash
wifi_station_disconnect();//WiFi station接口从AP断开连接
wifi_station_connect();//WiFi station接口连接AP
break;
case SC_STATUS_LINK_OVER://获取到ip,连接路由完成
os_printf("SC_STATUS_LINK_OVER\n");
if (pdata != NULL) {
uint8 phone_ip[4] = {0};
os_memcpy(phone_ip, (uint8*)pdata, 4);
os_printf("Phone ip: %d.%d.%d.%d\n",phone_ip[0],phone_ip[1],phone_ip[2],phone_ip[3]);//打印发广播过来的设备IP地址
}
smartconfig_stop();//停止配置
break;
}
}
/*******************************************************************************
* 自定义引脚运行函数_定时器回调
*******************************************************************************/
void ICACHE_FLASH_ATTR
Task_Run(void){
static uint32 lag[10];
static bool io;//io引脚标记
static bool first;//使用一次标记
if(lag[0]<500){lag[0]++;}
if(lag[0]<300){//开机闪烁,提示配网模式
if(io==true){
io=false;
GPIO_OUTPUT_SET(GPIO_ID_PIN(12), 0);//GPIO12输出低电平
}else{
io=true;
GPIO_OUTPUT_SET(GPIO_ID_PIN(12), 1);//GPIO12输出高电平
}
}else{//结束配网
uint8 status=wifi_station_get_connect_status();//连接状态
if(!first&&status!=STATION_GOT_IP){//未进行快连配置,则尝试连接原AP
first=true;
smartconfig_stop();//停止配置
wifi_station_disconnect();//WiFi station接口从AP断开连接
wifi_station_connect();//WiFi station接口连接AP
}
if(status==STATION_GOT_IP){
GPIO_OUTPUT_SET(GPIO_ID_PIN(12), 0);//GPIO12输出低电平
os_printf("Already connected\r\n");
}else{
GPIO_OUTPUT_SET(GPIO_ID_PIN(12), 1);//GPIO12输出高电平
}
}
}
/******************************************************************************
* FunctionName : user_init
* Description : entry of user application, init user function here
* Parameters : none
* Returns : none
*******************************************************************************/
void user_init(void)
{
/*设置串口波特率*/
uart_init(115200,9600);
/*打印版本信息*/
os_printf("SDK version:%s\n", system_get_sdk_version());
/*配置SmartConfig模式*/
smartconfig_set_type(SC_TYPE_ESPTOUCH_AIRKISS);//设置快连模式的协议类型(esptouch与airkiss)
wifi_set_opmode(STATION_MODE);//配置为客户端模式,并保存到flash(若使用快连则必须使用station模式)
smartconfig_start(smartconfig_done);//开启快连模式
/*配置GPIO12*/
PIN_FUNC_SELECT(PERIPHS_IO_MUX_MTDI_U,FUNC_GPIO12);
/*配置 硬件定时器*/
hw_timer_init(0,1);//初始化ISR硬件定时器,自动填装
hw_timer_set_func(Task_Run);//设置定时器回调函数
hw_timer_arm(100000);//使能硬件中断定时器,定时100ms
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。