当前位置:网站首页>ESP8266_SmartConfig
ESP8266_SmartConfig
2022-06-11 09:37:00 【小老虎_IOT】
1、WiFi配網方式
物聯網Wi-Fi配網方式,你知道幾種?-阿裏雲開發者社區 (aliyun.com)
這篇文章介紹並分析了幾種WiFi配網的方式,推薦看一下,目前主流的WiFi配網有設備熱點配網、藍牙配網、一鍵配網(SmartConfig);
- 設備熱點配網:手機連設備創建的熱點,把能够上網的WiFi名和密碼發給設備;
- 藍牙配網:手機連設備藍牙,把能够上網的WiFi名和密碼發給設備;
- 一鍵配網:手機仍然連能上網的WiFi,廣播WiFi名和密碼,設備開啟sniffer模式監聽,得到WiFi名和密碼;
2、SmartConfig配網
- 設備進入sniffer模式(又叫混雜模式,即不過濾數據報文),監聽環境中所有的802.11報文;
- 設備需要在所支持的信道(通常1-13)上輪詢監聽(因為設備不知道用戶手機和目標路由器的信道);
- 捕獲到符合配網規則的數據報文後停止信道輪詢,嘗試在在此信道上接收完成所有報文;
- 當前信道接收失敗或超時重新回到步驟2;
- 數據報文接收完成則關閉sniffer模式,回到station模式並開始嘗試連接路由器,狹義的配網過程完成。
3、ESP8266的SmartConfig接口
設置SmartConfig模式,開啟SmartConfig,根據配網不同的階段有回調函數,隨後關閉SmartConfig;
3.1、smartconfig_set_type
因為使用的是微信的Airkiss功能,所以選擇SC_TYPE_AIRKISS;
ESPTOUCH是樂鑫開發的一個APP,用於配網的,這裏不用;
3.2、smartconfig_start
按照說明就好了,先設置STA模式再調用此API,回調函數的參數在配網的不同階段不同,所以就可以switch case根據狀態來執行相應的操作,而且官方API文檔下還有示例參考;
3.3、smartconfig_stop
關閉SmartConfig模式,釋放內存,在獲取到IP之後調用;

4、代碼
是根據上次MQTT的程序改的,我想加的功能是,Flash中的SSID和密碼連接AP失敗10次之後,進入SmartConfig配網,然後在“安信可科技”公眾號上配網,並把新的SSID和密碼更新到Flash,這樣下次上電就不需要配網了;當然,比如只是WiFi斷線你可以不配網重啟下(說起來要是能自動恢複才好呢,挖個坑)
實現上述功能只需要修改wifi.c的代碼,算是wifi功能加入了SmartConfig的擴展吧,要包含的頭文件#include "smartconfig.h" ,原有wifi_check_ip中要添加的內容如下,設置未獲取到IP 10次後進入一鍵配網;
// 定時函數:檢查IP獲取情况
static void ICACHE_FLASH_ATTR wifi_check_ip(void *arg)
{
static u8 fail_num = 0;
// 未獲取到IP地址-------------------------------------------------------------
else{
fail_num++;
if(fail_num >= 10){
fail_num = 0;
INFO("--------------START SmartConfig------------------\r\n");
//wifi_set_opmode(STATION_MODE); // 設為STA模式
smartconfig_set_type(SC_TYPE_AIRKISS); // ESP8266配網方式【AIRKISS】
smartconfig_start(smartconfig_done); // 進入【智能配網模式】,並設置回調函數
return;
}
}
}然後添加SmartConfig的回調函數,重點在如何更新SSID和密碼到Flash,還有調用user_main.c中WiFi連接後的回調函數,從而進入SNTP、TCP、MQTT等後續操作,別忘了重啟定時器,別忘了把默認的SSID信息改錯;
// SmartConfig狀態發生改變時,進入此回調函數
// 參數1:sc_status status / 參數2:無類型指針【在不同狀態下,[void *pdata]的傳入參數是不同的】
void ICACHE_FLASH_ATTR smartconfig_done(sc_status status, void *pdata)
{
switch(status)
{
// CmartConfig等待
case SC_STATUS_WAIT:
os_printf("\r\nSC_STATUS_WAIT\r\n");
break;
// 發現【WIFI信號】(8266在這種狀態下等待配網)
case SC_STATUS_FIND_CHANNEL:
os_printf("\r\nSC_STATUS_FIND_CHANNEL\r\n");
os_printf("\r\n---- Please Use WeChat to SmartConfig ------\r\n\r\n");
break;
// 正在獲取【SSID】【PSWD】(8266正在抓取並解密【SSID+PSWD】)
case SC_STATUS_GETTING_SSID_PSWD:
os_printf("\r\nSC_STATUS_GETTING_SSID_PSWD\r\n");
// 配網方式
sc_type *type = pdata;
if (*type == SC_TYPE_ESPTOUCH){
os_printf("\r\nSC_TYPE:SC_TYPE_ESPTOUCH\r\n");
}else{
os_printf("\r\nSC_TYPE:SC_TYPE_AIRKISS\r\n");
}
break;
// 成功獲取到【SSID】【PSWD】,保存STA參數,並連接WIFI
case SC_STATUS_LINK:
os_printf("\r\nSC_STATUS_LINK\r\n");
struct station_config *sta_conf = pdata; // 獲取【STA參數】指針
// 將【SSID】【PASS】保存到【外部Flash】中
os_strncpy(sysCfg.sta_ssid, sta_conf->ssid, 32); // 【STA_SSID】WIFI名稱
os_strncpy(sysCfg.sta_pwd, sta_conf->password, 64); // 【STA_PASS】WIFI密碼
CFG_Save(); // 將更新後的系統參數燒錄到Flash中
//連接WiFi
wifi_station_set_config(sta_conf); // 設置STA參數【Flash】
wifi_station_disconnect(); // 斷開STA連接
wifi_station_connect(); // ESP8266連接WIFI
break;
// ESP8266作為STA,成功連接到WIFI
case SC_STATUS_LINK_OVER:
os_printf("\r\nSC_STATUS_LINK_OVER\r\n");
smartconfig_stop(); // 停止SmartConfig,釋放內存
if(wifiCb){ // 判斷是否設置了[wifiConnectCb]函數
wifiStatus = wifi_station_get_connect_status(); // 獲取接入狀態
wifiCb(wifiStatus); // wifiCb(wifiStatus)=wifiConnectCb(wifiStatus)
}
// 再次開啟定時器-------------------------------------------------------
os_timer_setfn(&WiFiLinker, (os_timer_func_t *)wifi_check_ip, NULL);
os_timer_arm(&WiFiLinker, 1000, 0); // 1s定時
break;
}
}边栏推荐
- Version mismatch between installed deeply lib and the required one by the script
- Machine learning notes - the story of master kaggle Janio Martinez Bachmann
- 制作业信息化为什么难施行?
- [software] ERP model selection method for large enterprises
- Remote office related issues to be considered by enterprises
- Telecommuting best practices and Strategies
- Shandong University project training (IV) -- wechat applet scans web QR code to realize web login
- Error [error] input tesnor exceeded available data range [neuralnetwork (3)] [error] input tensor '0' (0)
- Output image is bigger (1228800b) than maximum frame size specified in properties (1048576b)
- [scheme design] scheme of household oximeter based on single chip microcomputer
猜你喜欢

Machine learning notes - convolutional neural network memo list

Bowen dry goods | Apache inlong uses Apache pulsar to create data warehousing

Talk about reading the source code

Shandong University project training (IV) -- wechat applet scans web QR code to realize web login

Concurrent programming

Day41 process pool and thread pool

Sed explanation of shell script (SED command, sed -e, sed s/ new / old /...)

Don't use redis list to implement message queue. Stream is designed for queues

Video review pulsar summit Asia 2021, cases, operation and maintenance, ecological dry goods

ESP8266_SmartConfig
随机推荐
Flask (I) - quick start
ESP8266_通过MQTT协议连接阿里云
考研数学 【数列极限证明题】题型方法总结
Why is it difficult to implement informatization in manufacturing industry?
Analysis of high frequency interview questions in massive data processing
Day39 process object and other method mutexes
[TiO websocket] v. TiO websocket server counts the number of online people
Exclusive interview with PMC member Liu Yu: female leadership in Apache pulsar community
【ROS】noedic-moveit安装与UR5模型导入
报错RuntimeError: BlobReader error: The version of imported blob doesn‘t match graph_transformer
制作业信息化为什么难施行?
Where is it safer to open an account for soda ash futures? How much do you need to buy at least?
Development of PCBA circuit board for small oxygen generator
企业需要考虑的远程办公相关问题
ESP8266_SmartConfig
【软件】大企业ERP选型的方法
Flask (VII) - static file
Error [detectionnetwork (1)][warning]network compiled for 6 shapes, maximum available 10, compiling for 5 S
Design of wrist sphygmomanometer based on sic32f911ret6
Openstack explanation (XXIII) -- other configurations, database initialization and service startup of neutron