上一次在程序中指定了路由器的SSID和密码,然后连接WiFi。
使用ESP8266的smartconfig接口,可以从手机的APP端给ESP8266传路由器的SSID和密码。
在系统初始化之后回调smartconfig接口即可:
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();
break;
}
}
void ICACHE_FLASH_ATTR to_scan(void){
wifi_station_disconnect();
smartconfig_start(smartconfig_done);
}
void ICACHE_FLASH_ATTR user_init(){
uart_init(BIT_RATE_115200, BIT_RATE_115200);
wifi_set_opmode(0x01);
system_init_done_cb(to_scan);
}
调用结果如图: