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

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