文章目录

  • **04-ESP8226的WiFi通用库学习**
    • WiFi事件类:
    • WiFI睡眠模式:
    • WiFi配置相关:
  • 官方例程:
    • 中断事件回调函数的使用:

04-ESP8226的WiFi通用库学习

ESP8266通用库的头文件名,ESP8266WiFiGeneric.h

按照内容区分为下面几个类别:

WiFi事件类:

//STA模式下连接
WiFiEventHandler onStationModeConnected(std::function<void(const WiFiEventStationModeConnected&)>);
//STA模式下断开连接
WiFiEventHandler onStationModeDisconnected(std::function<void(const WiFiEventStationModeDisconnected&)>);
//STA模式下验证模式更改
WiFiEventHandler onStationModeAuthModeChanged(std::function<void(const WiFiEventStationModeAuthModeChanged&)>);
//STA模式下获取IP
WiFiEventHandler onStationModeGotIP(std::function<void(const WiFiEventStationModeGotIP&)>);
//STA模式DHCP超时
WiFiEventHandler onStationModeDHCPTimeout(std::function<void(void)>);
//AP模式下被连接
WiFiEventHandler onSoftAPModeStationConnected(std::function<void(const WiFiEventSoftAPModeStationConnected&)>);
//AP模式下被断开
WiFiEventHandler onSoftAPModeStationDisconnected(std::function<void(const WiFiEventSoftAPModeStationDisconnected&)>);
//AP模式下探针接收请求
WiFiEventHandler onSoftAPModeProbeRequestReceived(std::function<void(const WiFiEventSoftAPModeProbeRequestReceived&)>);
//WiFi模式改变
WiFiEventHandler onWiFiModeChange(std::function<void(const WiFiEventModeChange&)>);

上面函数返回的WiFi中断事件与下面枚举定义对应

typedef enum WiFiEvent
{WIFI_EVENT_STAMODE_CONNECTED = 0,WIFI_EVENT_STAMODE_DISCONNECTED,WIFI_EVENT_STAMODE_AUTHMODE_CHANGE,WIFI_EVENT_STAMODE_GOT_IP,WIFI_EVENT_STAMODE_DHCP_TIMEOUT,WIFI_EVENT_SOFTAPMODE_STACONNECTED,WIFI_EVENT_SOFTAPMODE_STADISCONNECTED,WIFI_EVENT_SOFTAPMODE_PROBEREQRECVED,WIFI_EVENT_MODE_CHANGE,WIFI_EVENT_SOFTAPMODE_DISTRIBUTE_STA_IP,WIFI_EVENT_MAX,WIFI_EVENT_ANY = WIFI_EVENT_MAX,
} WiFiEvent_t;

WiFI睡眠模式:

//设置睡眠模式
bool setSleepMode(WiFiSleepType_t type, uint8_t listenInterval = 0);//获取睡眠模式WiFiSleepType_t getSleepMode();//获得调制解调器睡眠和轻度睡眠的最大睡眠水平的收听间隔
uint8_t getListenInterval ();//延时关闭WiFi
bool forceSleepBegin(uint32 sleepUs = 0);//唤醒WiFi
bool forceSleepWake();

WiFi配置相关:

//获取WiFi的phy模式
WiFiPhyMode_t getPhyMode();//设置输出功率
void setOutputPower(float dBm);//WiFi配置信息是否保存在flash中
static void persistent(bool persistent);//配置WiFi模式
bool mode(WiFiMode_t);//获取WiFi模式
WiFiMode_t getMode();//使能STA模式
bool enableSTA(bool enable);//使能AP模式
bool enableAP(bool enable);

官方例程:

/*This sketch shows how to use WiFi event handlers.In this example, ESP8266 works in AP mode.Three event handlers are demonstrated:- station connects to the ESP8266 AP- station disconnects from the ESP8266 AP- ESP8266 AP receives a probe request from a stationWritten by Markus Sattler, 2015-12-29.Updated for new event handlers by Ivan Grokhotkov, 2017-02-02.This example is released into public domain,or, at your option, CC0 licensed.
*/#include <ESP8266WiFi.h>
#include <stdio.h>#ifndef APSSID
#define APSSID "esp8266"
#define APPSK  "88888888"
#endifconst char* ssid     = APSSID;
const char* password = APPSK;//定义WiFi中断事件的结构体变量
WiFiEventHandler stationConnectedHandler;
WiFiEventHandler stationDisconnectedHandler;
WiFiEventHandler probeRequestPrintHandler;
WiFiEventHandler probeRequestBlinkHandler;bool blinkFlag;void setup() {Serial.begin(115200);pinMode(LED_BUILTIN, OUTPUT);digitalWrite(LED_BUILTIN, HIGH);// Don't save WiFi configuration in flash - optionalWiFi.persistent(false);// Set up an access pointWiFi.mode(WIFI_AP);WiFi.softAP(ssid, password);// Register event handlers.// Callback functions will be called as long as these handler objects exist.// Call "onStationConnected" each time a station connectsstationConnectedHandler = WiFi.onSoftAPModeStationConnected(&onStationConnected);// Call "onStationDisconnected" each time a station disconnectsstationDisconnectedHandler = WiFi.onSoftAPModeStationDisconnected(&onStationDisconnected);// Call "onProbeRequestPrint" and "onProbeRequestBlink" each time// a probe request is received.// Former will print MAC address of the station and RSSI to Serial,// latter will blink an LED.probeRequestPrintHandler = WiFi.onSoftAPModeProbeRequestReceived(&onProbeRequestPrint);probeRequestBlinkHandler = WiFi.onSoftAPModeProbeRequestReceived(&onProbeRequestBlink);
}void onStationConnected(const WiFiEventSoftAPModeStationConnected& evt) {Serial.print("Station connected: ");Serial.println(macToString(evt.mac));
}void onStationDisconnected(const WiFiEventSoftAPModeStationDisconnected& evt) {Serial.print("Station disconnected: ");Serial.println(macToString(evt.mac));
}void onProbeRequestPrint(const WiFiEventSoftAPModeProbeRequestReceived& evt) {Serial.print("Probe request from: ");Serial.print(macToString(evt.mac));Serial.print(" RSSI: ");Serial.println(evt.rssi);
}void onProbeRequestBlink(const WiFiEventSoftAPModeProbeRequestReceived&) {// We can't use "delay" or other blocking functions in the event handler.// Therefore we set a flag here and then check it inside "loop" function.blinkFlag = true;
}void loop() {if (millis() > 10000 && probeRequestPrintHandler) {// After 10 seconds, disable the probe request event handler which prints.// Other three event handlers remain active.Serial.println("Not printing probe requests any more (LED should still blink)");probeRequestPrintHandler = WiFiEventHandler();}if (blinkFlag) {blinkFlag = false;digitalWrite(LED_BUILTIN, LOW);delay(100);digitalWrite(LED_BUILTIN, HIGH);}delay(10);
}String macToString(const unsigned char* mac) {char buf[20];snprintf(buf, sizeof(buf), "%02x:%02x:%02x:%02x:%02x:%02x",mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);return String(buf);
}

中断事件回调函数的使用:

将中断事件的返回值给定义的结构体变量stationConnectedHandler,作为自定义中断标志位使用。

onStationConnected)是链接中断处理函数

stationConnectedHandler = WiFi.onSoftAPModeStationConnected(&onStationConnected);

中断标志位可以在loop逻辑中判断使用,记得使用后要清除自定义中断标志位。一并的中断函数也被清除

probeRequestPrintHandler = WiFiEventHandler();

onStationConnected是链接中断处理函数

stationConnectedHandler = WiFi.onSoftAPModeStationConnected(&onStationConnected);

中断标志位可以在loop函数逻辑中判断使用,记得使用后要清除自定义中断标志位。一并的中断函数也被清除

probeRequestPrintHandler = WiFiEventHandler();

04-ESP8226的WiFi通用库学习相关推荐

  1. Linux驱动学习--WPA扫描相关流程及WIFI通用接口NL80211/WEXT分析

    目录 1.引言 2.WIFI通用接口介绍 3.wifi协议接口层 4.Android wpa_supplicant源码分析–扫描scan过程 4.1 wpa_supplicant.conf配置文件的分 ...

  2. Ubuntu18.04 + 树莓派4B + wifi + 换源 +ssh + 防火墙相关 + mate桌面 + + vnc + ROS Melodic

    说在前面的话,这是一个系列文章,研究从零落地 slam 小车,以下内容的 markdown 形式上传Gitee / Github 了可以直接 down 下来用捏 Gitee 从零落地 slam 小车: ...

  3. POCO C++库学习和分析 -- 序

    POCO C++库学习和分析 -- 序 1. POCO库概述: POCO是一个C++的开源库集.同一般的C++库相比,POCO的特点是提供了整一个应用框架.如果要做C++程序应用框架的快速开发,我觉得 ...

  4. Arduino开发-TFT_eSPI库学习

    TFT_eSPI库学习 文章目录 TFT_eSPI库学习 TFT_eSPI库安装以及配置 TFT_eSPI库文件目录 配置文件 1.User_Setup_.h 2. User_Setup_Select ...

  5. 1 跨芯片方案的IPC通用库的架构设计

    专栏特色 1.所有源码严格遵守统一的编码规范. 2.纯C++接口,接口封装严谨,接口功能丰富,应用层调用简单便捷. 3.近二十年行业经验和技术积累打造的高质量商用级代码,架构清晰合理,便于扩展和维护. ...

  6. Golang常用库学习

    Golang常用库学习 标准库fmt 标准库log 标准库time 标准库strconv 标准库 testing 单元测试 简单测试 单元测试覆盖率统计 表格驱动测试 性能(基准)测试 标准库 os ...

  7. POCO C++库学习和分析 -- 异常、错误处理、调试

    POCO C++库学习和分析 -- 异常.错误处理.调试 1. 异常处理 C++同C语言相比,提供了异常机制.通过使用try,catch关键字可以捕获异常,这种机制使得程序员在程序异常发生时,可以通过 ...

  8. STM32 HAL库学习笔记1-HAL库简介

    STM32 HAL库学习笔记1-HAL库简介 HAL库 SPL 库 和 HAL 库两者相互独立,互不兼容.几种库的比较如下 目前几种库对不同芯片的支持情况如下 ST 中文官网上有一篇<关于ST库 ...

  9. colly爬虫库学习笔记

    colly爬虫库学习笔记 前言 稍微的学习了一下Go语言的基础知识(错误处理和协程通道这些还没看),想着能不能做点东西,突然想到自己当时学了python之后就是专门为了写爬虫(虽然后来也咕了,只会一个 ...

最新文章

  1. Node.js 全局对象
  2. 关于网管软件中的预警功能的发展
  3. boost::math模块使用正态分布的示例
  4. python的实验报告怎么写_学号:20191221,《python实验设计》实验报告三
  5. Google大数据三论文
  6. 电脑一直弹出传奇游戏网页弹窗怎么办
  7. JCreator使用技巧
  8. #转载汉化 用C++写出来的人工智能围棋游戏
  9. PMP干货教你一秒选对PMP考试答案!
  10. 宇视相机怎样连接拾音器
  11. Veeam Backup 9.5 恢复VMware虚拟机
  12. 从零搭建个人资讯系统1
  13. lua fadeOut
  14. java学习总结(16.06.03)java中数组的定义和初始化
  15. Ekl去记录nginx的日志
  16. 淘宝补单怎么防止降权?如何避免降权?
  17. 亚马逊运营怎么做广告?六大方法!
  18. win11右键,默认就是显示更多怎么调整 右键 默认右键 右 右
  19. 无人机利用视觉slam实现位置估计
  20. PK61键盘使用说明

热门文章

  1. Python-itchat之微信好友大曝光
  2. hadoop3.x支持LZO压缩配置
  3. 练习练习练习~不断的练习
  4. 蓝桥杯2021年第十二届省赛-杨辉三角形
  5. Stanford CS230吴恩达Reading Research Papers学习笔记
  6. 奇怪吸引子---Hadley
  7. 嵌入式硬盘录像机恢复软件 V2.0(支持大华、WFS所有版本)
  8. 2021周记12:理财、朋友与焦虑
  9. ISO三体系是指什么?
  10. Android中TrafficStats流量监控类