English
English
简体中文
日本語
pdf-icon

Arduino Quick Start

2. Devices & Examples

5. Extensions

6. Applications

Stamp-C5 Wi-Fi

Example Programs

Compilation Requirements

  • M5Stack Board Manager version >= 3.3.8
  • Development board option = M5StampC5
  • M5Unified library version >= 0.2.19
Note
In actual applications, replace ssid in the code below with the actual Wi-Fi name and password with the actual Wi-Fi password.

Wi-Fi STA / AP

cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#include <M5Unified.h>
#include <WiFi.h>

const char *ssid     = "ssid";
const char *password = "password";

void setup()
{
    Serial.begin(115200);
    delay(1000);

    Serial.println();
    Serial.println("Stamp-C5 Wi-Fi demo");

    // STA MODE
    WiFi.mode(WIFI_STA);
    Serial.println("WiFi mode set to STA");
    Serial.print("Connecting to ");
    Serial.println(ssid);

    WiFi.begin(ssid, password);

    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }

    Serial.println();
    Serial.print("Connected to ");
    Serial.println(ssid);
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
    Serial.print("RSSI: ");
    Serial.print(WiFi.RSSI());
    Serial.println(" dBm");

    // AP MODE
    // WiFi.mode(WIFI_AP);
    // Serial.println("WiFi mode set to AP");
    // WiFi.softAP(ssid, password);
    // Serial.println("AP started");
    // Serial.print("IP address: ");
    // Serial.println(WiFi.softAPIP());
}

void loop()
{
}

Example serial output:

Stamp-C5 Wi-Fi demo
WiFi mode set to STA
Connecting to M5Stack
....
Connected to M5Stack
IP address: 192.168.51.135
RSSI: -59 dBm

Wi-Fi Scan

cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
#include <M5Unified.h>
#include <WiFi.h>

void setup()
{
    Serial.begin(115200);
    delay(1000);

    WiFi.mode(WIFI_STA);
    WiFi.disconnect();
    delay(100);
}

void loop()
{
    Serial.println("Scan start");

    // WiFi.scanNetworks will return the number of networks found.
    int n = WiFi.scanNetworks();
    Serial.println("Scan done");

    if (n == 0) {
        Serial.println("no networks found");
    } else {
        Serial.print(n);
        Serial.println(" networks found");
        Serial.println("Nr | SSID                             | RSSI | CH | Encryption");

        for (int i = 0; i < n; ++i) {
            Serial.printf("%2d", i + 1);
            Serial.print(" | ");
            Serial.printf("%-32.32s", WiFi.SSID(i).c_str());
            Serial.print(" | ");
            Serial.printf("%4ld", static_cast<long>(WiFi.RSSI(i)));
            Serial.print(" | ");
            Serial.printf("%2ld", static_cast<long>(WiFi.channel(i)));
            Serial.print(" | ");

            switch (WiFi.encryptionType(i)) {
                case WIFI_AUTH_OPEN:
                    Serial.print("open");
                    break;
                case WIFI_AUTH_WEP:
                    Serial.print("WEP");
                    break;
                case WIFI_AUTH_WPA_PSK:
                    Serial.print("WPA");
                    break;
                case WIFI_AUTH_WPA2_PSK:
                    Serial.print("WPA2");
                    break;
                case WIFI_AUTH_WPA_WPA2_PSK:
                    Serial.print("WPA+WPA2");
                    break;
                case WIFI_AUTH_WPA2_ENTERPRISE:
                    Serial.print("WPA2-EAP");
                    break;
                case WIFI_AUTH_WPA3_PSK:
                    Serial.print("WPA3");
                    break;
                case WIFI_AUTH_WPA2_WPA3_PSK:
                    Serial.print("WPA2+WPA3");
                    break;
                case WIFI_AUTH_WAPI_PSK:
                    Serial.print("WAPI");
                    break;
                default:
                    Serial.print("unknown");
                    break;
            }
            Serial.println();
            delay(10);
        }
    }

    Serial.println();
    // Delete the scan result to free memory for code below.
    WiFi.scanDelete();

    // Wait a bit before scanning again.
    delay(5000);
}

Example serial output:

Scan start
Scan done
5 networks found
Nr | SSID                             | RSSI | CH | Encryption
 1 | M5Stack                          |  -45 |  6 | WPA2+WPA3
 2 | M5Stack-Guest                    |  -58 |  6 | WPA2
 3 | Office-WiFi                      |  -63 | 11 | WPA+WPA2
 4 | IoT-Test                         |  -72 |  1 | WPA2
 5 | Open-Network                     |  -80 |  1 | open
On This Page