请在使用前加载库 #include <WiFi.h>
功能说明:
开启AP模式,开启成功返回真
函数原型:
bool softAP(const char* ssid, const char* passphrase = NULL, int channel = 1, int ssid_hidden = 0, int max_connection = 4)
参数 | 描述 | 类型 |
---|---|---|
ssid | AP名称 0~32个字节 | const char* |
passphrase | 密码 8~63个字节 或 NULL | const char* |
channel | 信道 | int |
ssid_hidden | 是否隐藏SSID 1:隐藏 0:不隐藏 | int |
max_conncetion | 最大连接数量1~4 | int |
案例程序:
#include <M5Stack.h>
#include <WiFi.h>
void setup() {
M5.begin();
M5.Power.begin();
M5.Lcd.setTextSize(2);
}
void loop() {
if (WiFi.softAP("M5Stack_AP", "12345678", 1, 0, 4)) {
M5.Lcd.println("WiFi AP is set up");
M5.Lcd.println(WiFi.softAPIP());
} else {
M5.Lcd.println("WiFi AP is down");
}
delay(1000);
}
功能说明:
设置AP的IP地址
函数原型:
bool softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet)
参数 | 描述 | 类型 |
---|---|---|
local_ip | 默认本地IP 192.168.4.1 | IPAddress |
gateway | 默认网关 192.168.4.1 | IPAddress |
subnet | 默认子网掩码 255.255.255.0 | IPAddress |
案例程序:
#include <M5Stack.h>
#include <WiFi.h>
IPAddress local_IP(192, 168, 4,22); // Manually set the ip address of the open network. 手动设置的开启的网络的ip地址
IPAddress gateway(192, 168, 4,9); // Manually set gateway IP address. 手动设置的网关IP地址
IPAddress subnet(255, 255, 255,0); // Manually set subnet mask. 手动设置的子网掩码
void setup() {
M5.begin();
M5.Power.begin();
M5.Lcd.setTextSize(2);
}
void loop() {
if (WiFi.softAP("M5Stack_AP", "12345678", 1, 0, 4)) {
M5.Lcd.println("WiFi AP is set up");
M5.Lcd.println(WiFi.softAPIP());
delay(300);
WiFi.softAPConfig(local_IP, gateway, subnet);
} else {
M5.Lcd.println("WiFi AP is down");
}
delay(1000);
}
功能说明:
关闭当前AP,wifioff设置为true则还原网络设置
函数原型:
bool softAPdisconnect(bool wifioff = false)
参数 | 描述 | 类型 |
---|---|---|
wifioff | 还原设置 | bool |
案例程序:
#include <M5Stack.h>
#include <WiFi.h>
void setup() {
M5.begin();
M5.Power.begin();
M5.Lcd.setTextSize(2);
}
void loop() {
if (WiFi.softAP("M5Stack_AP", "12345678", 1, 0, 4)) {
M5.Lcd.println("WiFi AP is set up");
M5.Lcd.println(WiFi.softAPIP());
delay(1000);
if (WiFi.softAPdisconnect()) { //Turn off the current AP and restore the network settings. 关闭当前AP,并还原网络设置
M5.Lcd.println("WiFi AP is closed");
delay(5000);
}
} else {
M5.Lcd.println("WiFi AP is down");
}
delay(1000);
}
功能说明:
返回连接到AP的客户端数量
函数原型:
uint8_t softAPgetStationNum()
案例程序:
#include <M5Stack.h>
#include <WiFi.h>
void setup() {
M5.begin();
M5.Power.begin();
M5.Lcd.setTextSize(2);
}
void loop() {
if (WiFi.softAP("M5Stack_AP", "12345678", 1, 0, 4)) {
M5.Lcd.println("WiFi AP is set up");
M5.Lcd.printf("Number of AP connections:%d",WiFi.softAPgetStationNum());
} else {
M5.Lcd.println("WiFi AP is down");
}
delay(1000);
}
功能说明:
返回当前AP的IP地址
函数原型:
IPAddress softAPIP()
案例程序:
#include <M5Stack.h>
#include <WiFi.h>
void setup() {
M5.begin();
M5.Power.begin();
M5.Lcd.setTextSize(2);
}
void loop() {
if (WiFi.softAP("M5Stack_AP", "12345678", 1, 0, 4)) {
M5.Lcd.println("WiFi AP is set up");
M5.Lcd.println(WiFi.softAPIP());
} else {
M5.Lcd.println("WiFi AP is down");
}
delay(1000);
}
功能说明:
设置主机名称,成功返回真
函数原型:
bool softAPsetHostname(const char * hostname)
参数 | 描述 | 类型 |
---|---|---|
hostname | 主机名称 | const char* |
案例程序:
#include <M5Stack.h>
#include <WiFi.h>
void setup() {
M5.begin();
M5.Power.begin();
M5.Lcd.setTextSize(2);
}
void loop() {
if (WiFi.softAP("M5Stack_AP", "12345678", 1, 0, 4)) {
M5.Lcd.println("WiFi AP is set up");
M5.Lcd.printf("Host name:%s\n", WiFi.softAPgetHostname());
delay(1000);
WiFi.softAPsetHostname("M5_AP");
M5.Lcd.printf("After Host name:%s\n", WiFi.softAPgetHostname());
} else {
M5.Lcd.println("WiFi AP is down");
}
delay(1000);
}
功能说明:
获得主机名称
函数原型:
const char * softAPgetHostname()
案例程序:
#include <M5Stack.h>
#include <WiFi.h>
void setup() {
M5.begin();
M5.Power.begin();
M5.Lcd.setTextSize(2);
}
void loop() {
if (WiFi.softAP("M5Stack_AP", "12345678", 1, 0, 4)) {
M5.Lcd.println("WiFi AP is set up");
M5.Lcd.printf("Host name:%s", WiFi.softAPgetHostname());
} else {
M5.Lcd.println("WiFi AP is down");
}
delay(1000);
}
功能说明:
返回主机 MAC 地址
函数原型:
String softAPmacAddress(void)
案例程序:
#include <M5Stack.h>
#include <WiFi.h>
void setup() {
M5.begin();
M5.Power.begin();
M5.Lcd.setTextSize(2);
}
void loop() {
if (WiFi.softAP("M5Stack_AP", "12345678", 1, 0, 4)) {
M5.Lcd.println("WiFi AP is set up");
M5.Lcd.print("MAC:");
M5.Lcd.println(WiFi.softAPmacAddress());
} else {
M5.Lcd.println("WiFi AP is down");
}
delay(1000);
}
功能说明:
接入指定网络(如果connect参数为真则接入网络;如果参数为假则只保存设置,返回wifi状态)
函数原型:
wl_status_t begin(const char* ssid, const char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL, bool connect = true)
参数 | 描述 | 类型 |
---|---|---|
ssid | 网络热点名称 | const char* |
passphrase | 密码(可选) | const char* |
channel | 热点通道号(可选) | int32_t |
bssid | 热点的mac地址(可选) | const uint8_t* |
connect | 建立连接(可选) | bool |
案例程序:
#include <M5Stack.h>
#include <WiFi.h>
// Fill in the hotspot name and password you are connected to.
// 填写你所连接的热点名称和密码
const char *ssid = "yourNetwork";
const char *password = "'secretPassword";
void setup() {
M5.begin();
M5.Power.begin();
M5.Lcd.setTextSize(2);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
M5.Lcd.print(".");
}
M5.Lcd.println("\nWiFi connected");
}
void loop() {}
功能说明:
配置网络地址
函数原型:
bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = (uint32_t)0x00000000, IPAddress dns2 = (uint32_t)0x00000000)
参数 | 描述 | 类型 |
---|---|---|
local_ip | 设置IP | IPAddress |
gateway | 设置网关 | IPAddress |
subnet | 设置子网掩码 | IPAddress |
dns1 | 设置DNS1 | IPAddress |
dns2 | 设置DNS2 | IPAddress |
功能说明:
断开网络连接,若wifioff为true则还将还原网络设置,若eraseap为true则将清除保存于flash中的网络参数.
函数原型:
bool disconnect(bool wifioff = false, bool eraseap = false)
参数 | 描述 | 类型 |
---|---|---|
wifioff | 还原设置 | bool |
eraseap | flash清除AP设置 | bool |
案例程序:
#include <M5Stack.h>
#include <WiFi.h>
// Fill in the hotspot name and password you are connected to.
// 填写你所连接的热点名称和密码
const char *ssid = "yourNetwork";
const char *password = "'secretPassword";
void setup() {
M5.begin();
M5.Power.begin();
M5.Lcd.setTextSize(2);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
M5.Lcd.print(".");
}
M5.Lcd.println("\nWiFi connected");
if(WiFi.disconnect(true, true)) {
M5.Lcd.println("WiFi disconnected");
}
}
void loop() {}
功能说明:
返回网络连接状态
函数原型:
bool isConnected()
案例程序:
#include <M5Stack.h>
#include <WiFi.h>
// Fill in the hotspot name and password you are connected to.
// 填写你所连接的热点名称和密码
const char *ssid = "yourNetwork";
const char *password = "'secretPassword";
void setup() {
M5.begin();
M5.Power.begin();
M5.Lcd.setTextSize(2);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
M5.Lcd.print(".");
}
M5.Lcd.println("\nWiFi connected");
M5.Lcd.print(WiFi.isConnected());
}
void loop() {}
功能说明:
设置自动重连.
函数原型:
bool setAutoReconnect(bool autoReconnect)
案例程序:
#include <M5Stack.h>
#include <WiFi.h>
// Fill in the hotspot name and password you are connected to.
// 填写你所连接的热点名称和密码
const char *ssid = "yourNetwork";
const char *password = "'secretPassword";
void setup() {
M5.begin();
M5.Power.begin();
M5.Lcd.setTextSize(2);
if(WiFi.setAutoReconnect(true)) {
M5.Lcd.println("Whether it has been set successfully to automatically reconnect");
}
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
M5.Lcd.print(".");
}
M5.Lcd.println("\nWiFi connected");
}
void loop() {}
功能说明:
返回是否重新连接
函数原型:
bool getAutoReconnect()
功能说明:
返回本地IP地址
函数原型:
IPAddress localIP()
案例程序:
#include <M5Stack.h>
#include <WiFi.h>
// Fill in the hotspot name and password you are connected to.
// 填写你所连接的热点名称和密码
const char *ssid = "yourNetwork";
const char *password = "'secretPassword";
void setup() {
M5.begin();
M5.Power.begin();
M5.Lcd.setTextSize(2);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
M5.Lcd.print(".");
}
M5.Lcd.println("\nWiFi connected");
M5.Lcd.print("IP:");
M5.Lcd.print(WiFi.localIP());
}
void loop() {}
功能说明:
返回子网掩码
函数原型:
IPAddress subnetMask()
案例程序:
#include <M5Stack.h>
#include <WiFi.h>
// Fill in the hotspot name and password you are connected to.
// 填写你所连接的热点名称和密码
const char *ssid = "yourNetwork";
const char *password = "'secretPassword";
void setup() {
M5.begin();
M5.Power.begin();
M5.Lcd.setTextSize(2);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
M5.Lcd.print(".");
}
M5.Lcd.println("\nWiFi connected");
M5.Lcd.print("subnetMask:");
M5.Lcd.print(WiFi.subnetMask());
}
void loop() {}
功能说明:
返回网关地址
函数原型:
IPAddress gatewayIP()
案例程序:
#include <M5Stack.h>
#include <WiFi.h>
// Fill in the hotspot name and password you are connected to.
// 填写你所连接的热点名称和密码
const char *ssid = "yourNetwork";
const char *password = "'secretPassword";
void setup() {
M5.begin();
M5.Power.begin();
M5.Lcd.setTextSize(2);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
M5.Lcd.print(".");
}
M5.Lcd.println("\nWiFi connected");
M5.Lcd.print("gatewayIP:");
M5.Lcd.print(WiFi.gatewayIP());
}
void loop() {}
功能说明:
返回DNS地址
函数原型:
IPAddress dnsIP(uint8_t dns_no = 0)
案例程序:
#include <M5Stack.h>
#include <WiFi.h>
// Fill in the hotspot name and password you are connected to.
// 填写你所连接的热点名称和密码
const char *ssid = "yourNetwork";
const char *password = "'secretPassword";
void setup() {
M5.begin();
M5.Power.begin();
M5.Lcd.setTextSize(2);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
M5.Lcd.print(".");
}
M5.Lcd.println("\nWiFi connected");
M5.Lcd.print("dnsIP:");
M5.Lcd.print(WiFi.dnsIP());
}
void loop() {}
功能说明:
返回mac地址
函数原型:
String macAddress()
案例程序:
#include <M5Stack.h>
#include <WiFi.h>
// Fill in the hotspot name and password you are connected to.
// 填写你所连接的热点名称和密码
const char *ssid = "yourNetwork";
const char *password = "'secretPassword";
void setup() {
M5.begin();
M5.Power.begin();
M5.Lcd.setTextSize(2);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
M5.Lcd.print(".");
}
M5.Lcd.println("\nWiFi connected");
M5.Lcd.print("macAddress:");
M5.Lcd.print(WiFi.macAddress());
}
void loop() {}
功能说明:
主机名称
函数原型:
const char * getHostname()
案例程序:
#include <M5Stack.h>
#include <WiFi.h>
// Fill in the hotspot name and password you are connected to.
// 填写你所连接的热点名称和密码
const char *ssid = "yourNetwork";
const char *password = "'secretPassword";
void setup() {
M5.begin();
M5.Power.begin();
M5.Lcd.setTextSize(2);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
M5.Lcd.print(".");
}
M5.Lcd.println("\nWiFi connected");
M5.Lcd.print("getHostname:");
M5.Lcd.print(WiFi.getHostname());
}
void loop() {}
功能说明:
返回WiFi状态
函数原型:
wl_status_t status()
返回值
值 | 描述 |
---|---|
255 | WL_NO_SHIELD 检查WiFi Shield |
0 | WL_IDLE_STATUS 正在WiFi工作模式间切换 |
1 | WL_NO_SSID_AVAIL 无法访问设置的SSID网络 |
2 | WL_SCAN_COMPLETED 扫描完成 |
3 | WL_CONNECTED 连接成功 |
4 | WL_CONNECT_FAILED 连接失败 |
5 | WL_CONNECTION_LOST 丢失连接 |
6 | WL_DISCONNECTED 断开连接 |
案例程序:
#include <M5Stack.h>
#include <WiFi.h>
// Fill in the hotspot name and password you are connected to.
// 填写你所连接的热点名称和密码
const char *ssid = "yourNetwork";
const char *password = "'secretPassword";
void setup() {
M5.begin();
M5.Power.begin();
M5.Lcd.setTextSize(2);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
M5.Lcd.print(".");
}
M5.Lcd.println("\nWiFi connected");
}
void loop() {}
功能说明:
扫描WiFi网络(默认状态下为阻塞模式)
函数原型:
int16_t scanNetworks(bool async = false, bool show_hidden = false, bool passive = false, uint32_t max_ms_per_chan = 300)
参数 | 类型 | 描述 |
---|---|---|
async | bool | 异步扫描 Wifi数量(非阻塞),值为true时启动 |
show_hidden | bool | 是否扫描隐藏网络 |
passive | bool | 快速扫描 |
max_ms_per_chan | uint32_t | 通道扫描时间 |
案例程序:
#include <M5Stack.h>
#include <WiFi.h>
void setup() {
M5.begin();
M5.Power.begin();
M5.Lcd.setTextSize(2);
M5.Lcd.println("Scanning...");
WiFi.scanNetworks();
M5.Lcd.printf("wifi number:%d\n", WiFi.scanComplete());
}
void loop() {}
功能说明:
异步模式下获取扫描网络.返回值-1表示扫描未完成.返回值为-2表示扫描失败.
函数原型:
int16_t scanComplete()
案例程序:
#include <M5Stack.h>
#include <WiFi.h>
void setup() {
M5.begin();
M5.Power.begin();
M5.Lcd.setTextSize(2);
WiFi.scanNetworks(true);
M5.Lcd.printf("Scanning...");
while (WiFi.scanComplete() == WIFI_SCAN_RUNNING ||
WiFi.scanComplete() == WIFI_SCAN_FAILED) {
M5.Lcd.printf(".");
delay(500);
}
M5.Lcd.printf("\nWiFi number:%d\n", WiFi.scanComplete());
}
void loop() {}
功能说明:
返回扫描到的网络.
函数原型:
String SSID(uint8_t networkItem)
参数 | 类型 | 描述 |
---|---|---|
networkItem | uint8_t | SSID名称 |
案例程序:
#include <M5Stack.h>
#include <WiFi.h>
void setup() {
M5.begin();
M5.Power.begin();
M5.Lcd.setTextSize(2);
M5.Lcd.println("Scanning...");
WiFi.scanNetworks();
M5.Lcd.printf("wifi number:%d\n", WiFi.scanComplete());
for (int i = 1; i < WiFi.scanComplete(); i++) {
M5.Lcd.printf("%d:%s\n", i, WiFi.SSID(i).c_str());
}
}
void loop() {}
功能说明:
从内存中删除扫描结果
函数原型:
void scanDelete()
案例程序:
#include <M5Stack.h>
#include <WiFi.h>
void setup() {
M5.begin();
M5.Power.begin();
M5.Lcd.setTextSize(2);
M5.Lcd.println("Scanning...");
WiFi.scanNetworks();
M5.Lcd.println("Scan complete");
WiFi.scanDelete();
M5.Lcd.println("Scan result deleted from memory");
}
void loop() {}
功能说明:
返回扫描到的加密网络
函数原型:
wifi_auth_mode_t encryptionType(uint8_t networkItem)
函数参数 | 类型 | 描述 |
---|---|---|
networkItem | uint8_t | SSID名称 |
案例程序:
#include <M5Stack.h>
#include <WiFi.h>
void setup() {
M5.begin();
M5.Power.begin();
M5.Lcd.setTextSize(2);
M5.Lcd.println("Scanning...");
WiFi.scanNetworks();
M5.Lcd.printf("wifi number:%d\n", WiFi.scanComplete());
for (int i = 1; i < WiFi.scanComplete(); i++) { //开始逐个打印扫描到的
M5.Lcd.printf("%d:", i);
M5.Lcd.print(WiFi.SSID(i));
M5.Lcd.print(",\t");
M5.Lcd.println(
(WiFi.encryptionType(i) == WIFI_AUTH_OPEN) ? "open" : "encryption");
delay(10);
}
}
void loop() {}
功能说明:
返回扫描到的RSSI强度
函数原型:
int32_t RSSI(uint8_t networkItem)
参数 | 类型 | 描述 |
---|---|---|
networkItem | uint8_t | RSSI item |
案例程序:
#include <M5Stack.h>
#include <WiFi.h>
void setup() {
M5.begin();
M5.Power.begin();
M5.Lcd.setTextSize(2);
M5.Lcd.println("Scanning...");
WiFi.scanNetworks();
M5.Lcd.printf("wifi number:%d\n", WiFi.scanComplete());
for (int i = 1; i < WiFi.scanComplete(); i++) {
M5.Lcd.printf("%d:%s", i, WiFi.SSID(i).c_str());
M5.Lcd.printf(" , %d\n", WiFi.RSSI(i));
}
}
void loop() {}
功能说明:
返回扫描到的网络通道号
函数原型:
int32_t channel(uint8_t networkItem)
参数 | 类型 | 描述 |
---|---|---|
networkItem | uint8_t | channel item |
案例程序:
#include <M5Stack.h>
#include <WiFi.h>
void setup() {
M5.begin();
M5.Power.begin();
M5.Lcd.setTextSize(2);
M5.Lcd.println("Scanning...");
WiFi.scanNetworks();
M5.Lcd.printf("wifi number:%d\n", WiFi.scanComplete());
for (int i = 1; i < WiFi.scanComplete(); i++) {
M5.Lcd.printf("%d:%s", i, WiFi.SSID(i).c_str());
M5.Lcd.printf(" , %d\n", WiFi.channel(i));
}
}
void loop() {}