English
English
简体中文
日本語

Arduino Quick Start

2. Devices & Examples

5. Extensions

6. Applications

CoreP4X Wi-Fi

The CoreP4X controller is based on the StampP4X, and Wi-Fi is provided through the Stamp-AddOn C6 For P4 module, supporting 2.4 GHz Wi-Fi.

Example Program

Note
The Wi-Fi function requires the Stamp-AddOn C6 For P4 module. The CoreP4X device does not include this module; make sure the module is installed correctly.

Installation Diagram

Compilation Requirements

  • M5Stack Board Manager version >= 3.3.10-a
  • Board selection = M5CoreP4X
  • M5Unified library version >= 0.3.1
  • M5GFX library version >= 0.3.1
Note
The M5CoreP4X board variant already defines the ESP-Hosted SDIO pins of CoreP4X in pins_arduino.h.

CoreP4X connects to the C6 Wi-Fi module through the ESP-Hosted SDIO interface of the StampP4X. The SDIO signals are connected as follows:

Signal ESP32-P4 Pin
SDIO CLK G43
SDIO CMD G44
SDIO D0 G45
SDIO D1 G46
SDIO D2 G47
SDIO D3 G48
SDIO RESET G42
Note
Replace WIFI_SSID and WIFI_PASSWORD with the actual network information.
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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
#define WIFI_SSID     "your-ssid"
#define WIFI_PASSWORD "your-password"

#include <WiFi.h>
#include <M5Unified.h>

static constexpr uint32_t RETRY_INTERVAL_MS = 15000;

M5Canvas canvas(&M5.Display);

static uint32_t lastRetryMs = 0;
static wl_status_t lastStatus = WL_IDLE_STATUS;

static const char* statusText(wl_status_t status)
{
  switch (status) {
    case WL_CONNECTED:       return "Connected";
    case WL_NO_SSID_AVAIL:   return "SSID not found";
    case WL_CONNECT_FAILED:  return "Connect failed";
    case WL_CONNECTION_LOST: return "Connection lost";
    case WL_DISCONNECTED:    return "Disconnected";
    case WL_IDLE_STATUS:     return "Connecting...";
    default:                 return "Connecting...";
  }
}

static void drawWiFi(const char* status, const char* detail, uint16_t color)
{
  canvas.fillSprite(TFT_BLACK);
  canvas.setTextDatum(middle_center);

  canvas.setTextColor(TFT_WHITE, TFT_BLACK);
  canvas.setFont(&fonts::FreeSansBold24pt7b);
  canvas.drawString("CoreP4X WiFi", 240, 56);

  canvas.setTextColor(color, TFT_BLACK);
  canvas.setFont(&fonts::FreeSansBold18pt7b);
  canvas.drawString(status, 240, 190);

  canvas.setTextColor(TFT_WHITE, TFT_BLACK);
  canvas.setFont(&fonts::FreeSans12pt7b);
  canvas.drawString(detail, 240, 270);

  canvas.setTextColor(TFT_DARKGREY, TFT_BLACK);
  canvas.setFont(&fonts::FreeSans9pt7b);
  canvas.drawString("Retry every 15 seconds", 240, 420);
  canvas.pushSprite(0, 0);
}

static void beginWiFi()
{
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  lastRetryMs = millis();
  lastStatus = WL_IDLE_STATUS;

  drawWiFi("Connecting...", WIFI_SSID, TFT_YELLOW);
  Serial.printf("Connecting to %s\n", WIFI_SSID);
}

void setup()
{
  auto cfg = M5.config();
  cfg.serial_baudrate = 115200;
  M5.begin(cfg);

  M5.Display.setBrightness(128);
  canvas.createSprite(M5.Display.width(), M5.Display.height());

  WiFi.mode(WIFI_STA);
  beginWiFi();
}

void loop()
{
  const wl_status_t status = WiFi.status();

  if (status != lastStatus) {
    lastStatus = status;

    if (status == WL_CONNECTED) {
      char detail[64];
      snprintf(detail, sizeof(detail), "IP: %s",
               WiFi.localIP().toString().c_str());
      drawWiFi("WiFi connected", detail, TFT_GREEN);
      Serial.printf("WiFi connected: %s\n", detail);
      Serial.printf("RSSI: %d dBm\n", WiFi.RSSI());
    } else {
      drawWiFi(statusText(status), WIFI_SSID, TFT_YELLOW);
      Serial.printf("WiFi: %s\n", statusText(status));
    }
  }

  if (status != WL_CONNECTED &&
      millis() - lastRetryMs >= RETRY_INTERVAL_MS) {
    beginWiFi();
  }

  delay(200);
}

After uploading, the device connects to the configured 2.4 GHz Wi-Fi. During the connection, the screen displays the current status and the SSID; after a successful connection, it displays WiFi connected and the obtained IP address, and the serial port also outputs the IP address and RSSI.

Serial output example:

Connecting to M5Stack
WiFi connected: IP: 192.168.51.94
RSSI: -52 dBm
Page Tools
PDF
On This Page