pdf-icon

WIFI

Please load the library #include <WiFi.h> before use.

WiFi.softAP()

Description:

Enable AP mode, returns true if successful.

Syntax:

bool softAP(const char* ssid, const char* passphrase = NULL, int channel = 1, int ssid_hidden = 0, int max_connection = 4)

Parameter Description Type
ssid AP name (0~32 bytes) const char*
passphrase Password (8~63 bytes) or NULL const char*
channel Channel int
ssid_hidden Whether to hide SSID (1: hidden, 0: not hidden) int
max_connection Maximum number of connections (1~4) int

Example:

#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);
}

WiFi.softAPConfig()

Description:

Set the IP address of the AP.

Syntax:

bool softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet)

Parameter Description Type
local_ip Default local IP (192.168.4.1) IPAddress
gateway Default gateway (192.168.4.1) IPAddress
subnet Default subnet mask (255.255.255.0) IPAddress

Example:

#include <M5Stack.h>
#include <WiFi.h>

IPAddress local_IP(192, 168, 4,22);  // Manually set the IP address of the open network.
IPAddress gateway(192, 168, 4,9);    // Manually set gateway IP address.
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);
}

WiFi.softAPdisconnect()

Description:

Close the current AP, if wifioff is true, restore network settings.

Syntax:

bool softAPdisconnect(bool wifioff = false)

Parameter Description Type
wifioff Restore settings bool

Example:

#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()) {
            M5.Lcd.println("WiFi AP is closed");
            delay(5000);
        }
    } else {
        M5.Lcd.println("WiFi AP is down");
    }
    delay(1000);
}

WiFi.softAPgetStationNum()

Description:

Returns the number of clients connected to the AP.

Syntax:

uint8_t softAPgetStationNum()

Example:

#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);
}

WiFi.softAPIP()

Description:

Returns the IP address of the current AP.

Syntax:

IPAddress softAPIP()

Example:

#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);
}

WiFi.softAPsetHostname()

Description:

Set the hostname, returns true if successful.

Syntax:

bool softAPsetHostname(const char * hostname)

Parameter Description Type
hostname Hostname const char*

Example:

#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);
}

WiFi.softAPgetHostname()

Description:

Get the hostname.

Syntax:

const char * softAPgetHostname()

Example:

#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());
    } else {
        M5.Lcd.println("WiFi AP is down");
    }
    delay(1000);
}

WiFi.softAPmacAddress()

Description:

Returns the MAC address of the host.

Syntax:

String softAPmacAddress(void)

Example:

#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);
}

WiFi.begin()

Description:

Connects to the specified network (if the connect parameter is true) or just saves the settings if false, returning the WiFi status.

Syntax:

wl_status_t begin(const char* ssid, const char *passphrase = NULL, int32_t channel = 0, const uint8_t* bssid = NULL, bool connect = true)

Parameter Description Type
ssid Network hotspot name const char*
passphrase Password (optional) const char*
channel Hotspot channel number (optional) int32_t
bssid Hotspot MAC address (optional) const uint8_t*
connect Establish connection (optional) bool

Example:

#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.config()

Description:

Configures network address.

Syntax:

bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = (uint32_t)0x00000000, IPAddress dns2 = (uint32_t)0x00000000)

Parameter Description Type
local_ip Set IP address IPAddress
gateway Set gateway IPAddress
subnet Set subnet mask IPAddress
dns1 Set DNS1 address IPAddress
dns2 Set DNS2 address IPAddress

WiFi.disconnect()

Description:

Disconnects from the network, optionally restoring settings and erasing saved AP settings from flash memory.

Syntax:

bool disconnect(bool wifioff = false, bool eraseap = false)

Parameter Description Type
wifioff Restore settings bool
eraseap Clear AP settings from flash memory bool

Example:

#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() {}

WiFi.isConnected()

Description:

Returns the network connection status.

Syntax:

bool isConnected()

Example:

#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() {}

WiFi.setAutoReconnect()

Description:

Sets automatic reconnection.

Syntax:

bool setAutoReconnect(bool autoReconnect)

Example:

#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() {}

WiFi.getAutoReconnect()

Description:

Returns whether to reconnect automatically.

Syntax:

bool getAutoReconnect()

WiFi.localIP()

Description:

Returns the local IP address.

Syntax:

IPAddress localIP()

Example:

#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() {}

WiFi.subnetMask()

Description:

Returns the subnet mask.

Syntax:

IPAddress subnetMask()

Example:

#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() {}

WiFi.gatewayIP()

Description:

Returns the gateway address.

Syntax:

IPAddress gatewayIP()

Example:

#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() {}

WiFi.dnsIP()

Description:

Returns the DNS address.

Syntax:

IPAddress dnsIP(uint8_t dns_no = 0)

Example:

#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() {}

WiFi.macAddress()

Description:

Returns the MAC address.

Syntax:

String macAddress()

Example:

#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() {}

WiFi.getHostname()

Description:

Returns the host name.

Syntax:

const char * getHostname()

Example:

#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.status()

Description:

Returns the WiFi status.

Syntax:

wl_status_t status()

Return Values

Value Description
255 WL_NO_SHIELD Check WiFi Shield
0 WL_IDLE_STATUS Switching between WiFi modes
1 WL_NO_SSID_AVAIL Unable to access the set SSID network
2 WL_SCAN_COMPLETED Scan completed
3 WL_CONNECTED Connection successful
4 WL_CONNECT_FAILED Connection failed
5 WL_CONNECTION_LOST Connection lost
6 WL_DISCONNECTED Disconnected

Example:

#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.scanNetworks()

Description:

Scans for WiFi networks (by default in blocking mode).

Syntax:

int16_t scanNetworks(bool async = false, bool show_hidden = false, bool passive = false, uint32_t max_ms_per_chan = 300)

Parameter Type Description
async bool Asynchronous scan WiFi numbers (non-blocking), start with true
show_hidden bool Whether to scan hidden networks
passive bool Quick scan
max_ms_per_chan uint32_t Channel scan time

Example:

#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() {}

WiFi.scanComplete()

Description:

In asynchronous mode, retrieves the scanned networks. Returns -1 if the scan is incomplete, and -2 if the scan fails.

Syntax:

int16_t scanComplete()

Example:

#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() {}

WiFi.SSID()

Description:

Returns the scanned network SSID.

Syntax:

String SSID(uint8_t networkItem)

Parameter Type Description
networkItem uint8_t SSID name

Example:

#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() {}

WiFi.scanDelete()

Description:

Deletes the scan result from memory.

Syntax:

void scanDelete()

Example:

#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.encryptionType()

Description:

Returns the scanned encrypted network.

Syntax:

wifi_auth_mode_t encryptionType(uint8_t networkItem)

Function Parameter Type Description
networkItem uint8_t SSID name

Example:

#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() {}

WiFi.RSSI()

Description:

Returns the RSSI strength of the scanned networks.

Syntax:

int32_t RSSI(uint8_t networkItem)

Parameter Type Description
networkItem uint8_t RSSI item

Example:

#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() {}

WiFi.channel()

Description:

Returns the channel number of the scanned networks.

Syntax:

int32_t channel(uint8_t networkItem)

Parameter Type Description
networkItem uint8_t Channel item

Example:

#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() {}
On This Page