ESP8266_SDK基礎(4)WiFi快連smartConfig

環境及工具與第一章相同,這裏就不在重複!web

本章經過代碼示例,來實現wifi模塊鏈接到路由器的功能,模塊開機上電是自動進入快連模式,待30秒後退出快連模式,若鏈接成功則運行正常程序,若未鏈接成功,則嘗試以保存在flash內的參數進行鏈接,若鏈接成功則運行正常程序,若未成功則繼續嘗試重連。api

加入了狀態指示功能,開機指示燈閃爍,用於提示進入快連模式,若鏈接失敗則指示燈熄滅,若鏈接成功則指示燈常亮,並打印已鏈接字符到串口。app

使用方法:上電進入快連模式後,能夠用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
}
相關文章
相關標籤/搜索