ESP8266 Smart Config配网

Smartconfig(一键配网)模式
Smartconfig原理
Smartconfig 简称一键配网,它可以将WIFI设备便捷快速的连接路由器。smartconfig原理: 智能设备进入初始化状态,处于混杂模式下,监听网络中的所有报文,手机端APP将WiFi名和密码编码到UDP报文中,通过广播包或者组播包发送,智能设备接收到UDP报文后解码,得到WiFi名称和密码,然后主动连接到指定的WiFi AP路由器上。

对于ESP8266使用微信公众号端进行一键配网简单操作步骤:
1.8266 端作为station,进入smartconfig, 等待手机端发出的用户名和密码;
2.手机端填写当前网络的密码通过UDP广播;
3.8266 获取到信息之后连接网络。

30s内完成smartconfig,LED闪烁;超时自动关闭,LED常亮
LED使用的开发板上的LED,LED闪烁,软件定时器,smartconfig;
代码如下:

// 声明smartconfig连接状态的LED为GPIO2口
#define HUMITURE_WIFI_LED_IO_MUX PERIPHS_IO_MUX_GPIO2_U//LEDGPIO2
#define HUMITURE_WIFI_LED_IO_NUM 2
#define HUMITURE_WIFI_LED_IO_FUNC FUNC_GPIO2
 os_timer_t os_timer;//LED灯定时器
 static int count = 0;//30s
 char ledStatus = 1;///wifi led 亮灭状态
 
void ICACHE_FLASH_ATTR
os_timer_callback()//定时器回调函数
{
 os_printf("hh");
 os_printf("this is %d\n",fun());
 fun();//自身调用次数
 GPIO_OUTPUT_SET(GPIO_ID_PIN(2), ledStatus);//led闪烁
 ledStatus = ledStatus?0:1;
 }
int fun()
{
 //static int count = 0;
 os_printf("this is %d\n",++count);
 if(count==30)
       {
        smartconfig_stop();//
        os_timer_disarm(&os_timer);//取消定时
        count=0;
       }//30秒连接停止,定时停止,count归零,
 //return ++count;
 }
 //在smartconfig LINK_OVER后count清0,led常亮
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:
       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_disconnect();
       wifi_station_connect();
       break;
      case SC_STATUS_LINK_OVER:
       os_printf("SC_STATUS_LINK_OVER\n");
       if (pdata != NULL)
       {
        uint8 phone_ip[4] = {0};
        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]);
       }
       smartconfig_stop();
       os_timer_disarm(&os_timer);
       ledStatus = 1;
       count=0;
      // user_udpclient_init(8686);
       break;
      }
}

void ICACHE_FLASH_ATTR
user_init(void)
{
 smartconfig_stop();
 wifi_set_opmode_current (0x01);//sta模式
 smartconfig_start(smartconfig_done);
 //ESP8266_PWM_Init();//呼吸灯
 os_timer_disarm( &os_timer );//取消定时器定时
 count=0;//调用打印从0开始
 os_timer_setfn(&os_timer,os_timer_callback,NULL );//定时器回调函数
 os_timer_arm( &os_timer, 1000, true );//使能毫秒级定时器
 }

效果图
30s未连接,停止Smartconfig

17s时连接成功。

————————————————
版权声明:本文为CSDN博主「一方沃土」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_40110248/article/details/91128747

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章