作者:得不到的最美丽 | 来源:互联网 | 2023-10-15 15:14
文章目录目的WiFi其他方法WiFi事件响应事件列表注册事件使用示例总结目的WiFi在使用过程中并非会一直如希望般稳定运行的,为了应对这些情况就需要能够了解WiFi
目的 WiFi在使用过程中并非会一直如希望般稳定运行的,为了应对这些情况就需要能够了解WiFi状态,并对WiFi突发事件作出响应。
WiFi其他方法 WiFi.status()
返回STA模式
下WiFi状态,返回值如下: 0 : WL_IDLE_STATUS
正在WiFi工作模式间切换; 1 : WL_NO_SSID_AVAILin
无法访问设置的SSID网络; 3 : WL_CONNECTED
连接成功; 4 : WL_CONNECT_FAILED
密码不正确; 6 : WL_DISCONNECTED
if module is not configured in station mode(实际测试下来有点看不懂);WiFi.persistent(bool persistent)
该方法设置是否将WiFi参数保存于Flash中,默认为true
,即在每次调用WiFi.begin()
、WiFi.softAP()
、WiFi.disconnect
、WiFi.softAPdisconnect
方法时都会将相关数据写入到Flash中; 如果使用该方法设置为false
时,以上动作将不会把数据写入Flash中,仅仅改变内存中的WiFi设置;WiFi.mode(m)
设置WiFi工作模式,参数可填写WIFI_AP
、WIFI_STA
、WIFI_AP_STA
、WIFI_OFF
;WiFi.getMode()
返回当前WiFi工作模式;bool setSleepMode (WiFiSleepType_t type, int listenInterval=0)
设置WiFi节电模式,该方法在esp8266-arduino core v2.5.0中有改动,详细可以参考下文链接; WiFi事件响应 如目的所述WiFi在使用过程中并不是一直稳定的,在出现变化时会触发WiFi事件,我们就可以针对不同情况作出处理。不同的WiFi变化有不同的事件(WiFiEventHandler
),所有事件都需要在注册后使用。
事件列表 onStationModeConnected ( std: : function< void ( const WiFiEventStationModeConnected & ) > ) onStationModeDisconnected ( std: : function< void ( const WiFiEventStationModeDisconnected & ) > ) onStationModeAuthModeChanged ( std: : function< void ( const WiFiEventStationModeAuthModeChanged & ) > ) onStationModeGotIP ( std: : function< void ( const WiFiEventStationModeGotIP & ) > ) onStationModeDHCPTimeout ( std: : function< void ( void ) > ) onSoftAPModeStationConnected ( std: : function< void ( const WiFiEventSoftAPModeStationConnected & ) > ) onSoftAPModeStationDisconnected ( std: : function< void ( const WiFiEventSoftAPModeStationDisconnected & ) > )
注册事件 使用WiFiEventHandler
需要先声明&#xff0c;然后注册对应事件并加入事件触发时执行的操作&#xff1a;
WiFiEventHandler myEvent; void myHandler ( const WiFiEventStationModeConnected& event) { } myEvent &#61; WiFi. onStationModeConnected ( myHandler) ;
使用示例 使用下面代码上传至模块中&#xff1a;
#include const char * ssid &#61; "********" ; const char * password &#61; "********" ; WiFiEventHandler STAConnected; WiFiEventHandler STADisconnected; WiFiEventHandler STAGotIP; void ConnectedHandler ( const WiFiEventStationModeConnected & event) { Serial. println ( WiFi. status ( ) ) ; Serial. println ( "模块连接到网络" ) ; } void DisconnectedHandler ( const WiFiEventStationModeDisconnected & event) { Serial. println ( WiFi. status ( ) ) ; Serial. println ( "模块从网络断开" ) ; } void setup ( ) { Serial. begin ( 115200 ) ; Serial. println ( ) ; STAConnected &#61; WiFi. onStationModeConnected ( ConnectedHandler) ; STADisconnected &#61; WiFi. onStationModeDisconnected ( DisconnectedHandler) ; STAGotIP &#61; WiFi. onStationModeGotIP ( [ ] ( const WiFiEventStationModeGotIP & event) { Serial. println ( WiFi. status ( ) ) ; Serial. println ( "模块获得IP" ) ; } ) ; WiFi. mode ( WIFI_STA) ; WiFi. begin ( ssid, password) ; Serial. println ( WiFi. status ( ) ) ; } void loop ( ) { delay ( 5000 ) ; WiFi. disconnect ( ) ; }
代码运行后可以看到下面效果&#xff1a;
总结 上面介绍的内容可以用来增加开发者对于设备的掌控&#xff0c;这对于开发稳定的产品是比较有帮助的。 参考链接&#xff1a; https://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/readme.html#diagnostics https://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/generic-examples.html https://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/generic-class.html https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.h https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp