ESP8266 wifi模块正常上电,处理过程

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhejfl/article/details/79101009

wifi模块正常上电,需要涉及ESP8266 NOS SDK的 wifi接口部分、TCP/UDP部分  还有用户参数区访问部分。


/********************user_init函数***********************************/

void ICACHE_FLASH_ATTR
user_init(void)
{
uint32 system_free_size = 0;
      uint8 opmode;

        /*上电设置wifi的配置*/
wifi_station_set_auto_connect(1); //设置ESP8266 Station上电自动连接已记录的AP,在此调用,本次就生效

//set none sleep mode  设置省电模式:无睡眠全电, 默认是MODEM_SLEEP_T
wifi_set_sleep_type(NONE_SLEEP_T);   


wifi_station_ap_number_set(1);  //设置ESP8266 最多可记录几个AP信息(最多为5个,这里是1个,考虑配网)


wifi_station_set_reconnect_policy(true);  //设置断开重连策略为ture,即断开会重连


espconn_tcp_set_max_con(10);  //允许的最多的TCP连接数,即同时有效的TCP client连接最多10个


uart_init_3(115200,115200);     //配置UART0 和UART1
//       uart_init(115200, 115200);
UART_SetPrintPort(1);     //设置打印UART为UART1


os_printf("SDK version:%s\n", system_get_sdk_version());
     /*自动连接*/


wifi_set_opmode(STATION_MODE); //设置wifi的工作模式,并保存到Flash,

//#define NULL_MODE       0x00
//#define STATION_MODE    0x01
//#define SOFTAP_MODE     0x02
//#define STATIONAP_MODE  0x03
opmode = wifi_get_opmode();//获取当前wifi的工作模式


os_printf("Current WorkMode:%d\r\n", opmode);
         system_init_done_cb(autoConnectAP);       //用于注册系统初始化完成的回调函数,在user_init中调用。

        //smart_Config_NET();

}




对于自动连接这部分:基本思路是先扫描下该环境下时候有给AP,有的话直接连接connect,进入等待配网/或按键进入配网

/*******************autoConnectAp 自动连接AP函数***********************/

void autoConnectAP()
{
struct scan_config    Scanconfig;
struct station_config APMSG[5];
int i = wifi_station_get_ap_info(APMSG);   //获取ESP8266 Station保存的AP信息,最多5个
if(i>=1)
{
APconfig = APMSG[0];   //取第一个
}
else
{
os_printf("Never Configure NetWork");
return;
}
Scanconfig.ssid = (char *)os_zalloc(sizeof(APconfig.ssid));
Scanconfig.bssid = NULL;
Scanconfig.channel = 0;
Scanconfig.show_hidden = 1;
//先扫描
Conected_AP_Flag = false;   //连接上AP的标识
Conected_Server_Flag = false;    //链接上服务器的标识


 //获取Ap的信息,实参Scanconfig是扫描AP的配置参数,null表示扫描所有可用的AP信息;

 //可以获取扫描特定信道上的AP信息,也可以获取所有信道上的某特定名称AP的信息

 //scan_done 扫描完成的回调函数void scan_done_cb_t (void *arg, STATUS status)

wifi_station_scan(&Scanconfig, scan_done);   
}

struct scan_config {
uint8 *ssid; // AP’s ssid
    服务集标识
uint8 *bssid; // AP’s bssid
  MAC地址
uint8 channel; //scan a specific channel
  特定的信道
uint8 show_hidden; //scan APs of which ssid is hidden.
   查找隐藏SSID 
};  //扫描配置的结构体


struct station_config {
    uint8 ssid[32];   //wifi服务器标识
    uint8 password[64];  //密码
    uint8 bssid_set; // Note: If bssid_set is 1, station will just connect to the router
                        // with both ssid[] and bssid[] matched. Please check about this.  当为1,连接上
                       //热点(ssid和bssid都复合)
    uint8 bssid[6];  //站点MAC地址
}; //wifi_station接口配置参数

bool wifi_station_set_config_current (struct station_config *config) ; 设置wifi_station接口的配置参数,不保存到Flash

bool wifi_station_get_config_default (struct station_config *config) ;设置wifi_station接口的配置参数,保存到Flash
bool wifi_station_get_config (struct station_config *config)   查询WIFI Station接口的当前配置参数。
bool wifi_station_get_config_default (struct station_config *config)
查询WIFI station接口保存在Flash中的配置参数。

在配置完wifi_station 后就可以调用bool wifi_station_connect (void) 函数连接指定的AP点。

/***********scan参数*************************/

scan_done函数 是wifi_station_scan完成的 的回调函数;

void scan_done_cb_t (void *arg, STATUS status)  *arg扫描到AP信息存放的指针,以链表形式存储bss_info

STATUS status:扫描结果
typedef enum {
    OK = 0,   //成功
    FAIL,     //失败
    PENDING,   //暂停
    BUSY,  //忙
    CANCEL,  //取消
} STATUS;
而存储扫描到AP信息的链表结构

struct bss_info {
STAILQ_ENTRY(bss_info) next;

uint8 ssid[32];

uint8 ssid_len;

uint8 channel;

AUTH_MODE authmode;

uint8 is_hidden;

sint16 freq_offset
sint16 freqcal_val;
uint8 *esp_mesh_ie;
uint8 simple_pair;
}; 



开启一个线程,周期性检测AP的连接状态,处理不同的错误状态。。。。。???


猜你喜欢

转载自blog.csdn.net/zhejfl/article/details/79101009
今日推荐