前言
SmartConfig配网是在手机APP连接路由器的情况下.
APP发送无线信息在空中.
Wi-Fi模块通过监测空中的无线信息实现的获取路由器名称和密码
1.包含以下头文件
#include "smartconfig.h"
2.编写配网程序
void smartconfig_callback(sc_status status, void *pdata){ switch(status) { case SC_STATUS_WAIT: os_printf("SC_STATUS_WAIT "); break; case SC_STATUS_FIND_CHANNEL: os_printf("SC_STATUS_FIND_CHANNEL "); break; case SC_STATUS_GETTING_SSID_PSWD: os_printf("SC_STATUS_GETTING_SSID_PSWD "); sc_type *type = pdata; if (*type == SC_TYPE_ESPTOUCH) { os_printf("SC_TYPE:SC_TYPE_ESPTOUCH "); } else { os_printf("SC_TYPE:SC_TYPE_AIRKISS "); } break; case SC_STATUS_LINK://获取到手机发来的无线信息 os_printf("SC_STATUS_LINK "); struct station_config *sta_conf = pdata; //连接路由器 WIFI_Connect(sta_conf->ssid, sta_conf->password, wifiConnectCb); break; case SC_STATUS_LINK_OVER: os_printf("SC_STATUS_LINK_OVER "); if (pdata != NULL) { uint8 phone_ip[4] = {0}; memcpy(phone_ip, (uint8*)pdata, 4); os_printf("Phone ip: %d.%d.%d.%d ",phone_ip[0],phone_ip[1],phone_ip[2],phone_ip[3]); } smartconfig_stop(); break; } }
void wifiConnectCb(uint8_t status) { if(status == STATION_GOT_IP){ os_printf(" Connect AP Success "); } else { os_printf(" DisConnect AP "); } }
3.测试
请使用第一节里面的APP进行测试
https://www.cnblogs.com/yangfengwu/p/11961168.html
如果想自己做SmartConfig的APP,请看这一节文章
https://www.cnblogs.com/yangfengwu/p/11911992.html
3.1,安装手机APP
3.2,点击APP右上角菜单
3.3,连接家里的路由器,输入路由器密码
3.4 下载好Wi-Fi模块程序
3.5,点击APP上面的搜索设备按钮
3.6 使用串口调试助手查看Wi-Fi打印的数据
4.优化:长按按钮3S,指示灯快闪,进入配网模式
int KeyDelay=0;//检测按键延时 int SmartConfigDelay=0;//控制LED闪耀时间 int SmartConfigFlag=0;//开始配网标志
//定时器初始化 hw_timer_init(0,1);//1:循环 //设置定时器回调函数 hw_timer_set_func(hw_test_timer_cb);//hw_test_timer_cb:硬件定时器中断回调函数 hw_timer_arm(1000);//1000:1000us定时进入中断函数
//按钮按下 if(GPIO_INPUT_GET(0) == 0){ KeyDelay++; if(KeyDelay>=3000 && SmartConfigFlag==0){ KeyDelay = 0; SmartConfigFlag = 1; wifi_set_opmode_current(STATION_MODE);//STATION模式下才能配网 smartconfig_set_type(SC_TYPE_ESPTOUCH_AIRKISS);//smartconfig + airkiss(APP+微信) esptouch_set_timeout(60);//超时时间60S smartconfig_start(smartconfig_callback);//设置配网回调函数 } } else{ KeyDelay=0; } //控制指示灯快闪 if(SmartConfigFlag == 1){ SmartConfigDelay++; if(SmartConfigDelay>100){ SmartConfigDelay=0; GPIO_OUTPUT_SET(2, 1-GPIO_INPUT_GET(2)); } } else{ SmartConfigDelay=0; }
void wifiConnectCb(uint8_t status) { if(status == STATION_GOT_IP){ os_printf(" Connect AP Success "); } else { os_printf(" DisConnect AP "); } } void smartconfig_callback(sc_status status, void *pdata){ switch(status) { case SC_STATUS_WAIT: os_printf("SC_STATUS_WAIT "); break; case SC_STATUS_FIND_CHANNEL: os_printf("SC_STATUS_FIND_CHANNEL "); break; case SC_STATUS_GETTING_SSID_PSWD: os_printf("SC_STATUS_GETTING_SSID_PSWD "); sc_type *type = pdata; if (*type == SC_TYPE_ESPTOUCH) { os_printf("SC_TYPE:SC_TYPE_ESPTOUCH "); } else { os_printf("SC_TYPE:SC_TYPE_AIRKISS "); } break; case SC_STATUS_LINK://获取到手机发来的无线信息 os_printf("SC_STATUS_LINK "); struct station_config *sta_conf = pdata; //连接路由器 WIFI_Connect(sta_conf->ssid, sta_conf->password, wifiConnectCb); break; case SC_STATUS_LINK_OVER: os_printf("SC_STATUS_LINK_OVER "); if (pdata != NULL) { uint8 phone_ip[4] = {0}; memcpy(phone_ip, (uint8*)pdata, 4); os_printf("Phone ip: %d.%d.%d.%d ",phone_ip[0],phone_ip[1],phone_ip[2],phone_ip[3]); } SmartConfigFlag = 0; smartconfig_stop(); break; } }
5.测试
长按开发板的固件引脚大约3S,等待指示灯快闪,之后松开按钮