The antenna first
The XIAO ESP32S3 ships with a small Wi-Fi antenna to clip onto its connector. Without it, the range drops to a few centimetres and the network may not even appear.
ESP32, Wi-Fi and WebSocket
Create a Wi-Fi access point on an ESP32, serve a web page, then exchange messages in both directions with the browser using a WebSocket.
Prerequisites:
Software:
By the end of this tutorial, the XIAO creates its own Wi-Fi network. A phone that connects to it opens a web page served by the board, with two buttons that turn the XIAO’s LED on and off. In the other direction, the board continuously sends the joystick position, which the page displays live.
This is the network skeleton of the Snake project: a page served by the console, and a line open in both directions for the game messages. The notions (access point, HTTP, WebSocket, protocol) are explained in the concept Wi-Fi, HTTP and WebSocket.
This tutorial starts from a fresh PlatformIO project: the Pong code is not needed. The joystick stays wired as in the workshop (X axis on D0).
Wi-Fi (WiFi.h) and the web server (WebServer.h) are part of the Arduino-ESP32 core: nothing to install. The WebSocket server comes from the WebSockets library by Links2004, to be declared in platformio.ini.
[env:seeed_xiao_esp32s3]
platform = espressif32
board = seeed_xiao_esp32s3
framework = arduino
monitor_speed = 115200
lib_deps =
links2004/WebSockets@^2.4.0
WiFi.softAP() turns the board into an access point: it creates a network with the given name (SSID) and password, and takes the address 192.168.4.1. Choose a name that is unique in the room, otherwise the other teams’ networks will be confused with yours. The password must be at least 8 characters long, otherwise the access point does not start.
#include <Arduino.h>
#include <WiFi.h>
constexpr char WIFI_SSID[] = "Console-Team1"; // unique in the room
constexpr char WIFI_PASSWORD[] = "snake2026"; // 8 characters minimum
void setup() {
Serial.begin(115200);
delay(1500); // gives the serial monitor time to reopen after the reset
WiFi.softAP(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Access point ready, address: ");
Serial.println(WiFi.softAPIP());
}
void loop() {}
The web server listens on port 80. You tell it what to answer for each address requested: here, for the root /, the player’s page.
The page is written directly in the code, in a raw string literal: everything between the two rawliteral markers is copied as is, quotation marks and line breaks included. For now, the page only has a title and a status text; the JavaScript comes in the next step.
server.handleClient() must be called on every pass of loop(): that is where the server checks whether a request has arrived and answers it.
#include <WebServer.h>
WebServer server(80);
const char PAGE[] = R"rawliteral(
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>XIAO console</title>
</head>
<body>
<h1>XIAO console</h1>
<p>This page is served by the board.</p>
</body>
</html>
)rawliteral";
void handleRoot() {
server.send(200, "text/html", PAGE); // 200 = "OK" in HTTP
}
In setup(), after the access point:
server.on("/", handleRoot);
server.begin();
And in loop():
void loop() {
server.handleClient();
}
The WebSocket server listens on another port, 81. Like the web server, it must be serviced on every pass of loop() with webSocket.loop().
Events (a browser connects, disconnects, sends a message) arrive through a callback function: onWebSocketEvent(), which the library calls by itself. Each connected browser gets a client number (0, 1, 2…).
On the page side, a few lines of JavaScript open the connection to ws://192.168.4.1:81/ and show its state. If the connection drops, the page retries by itself one second later.
#include <WebSocketsServer.h>
WebSocketsServer webSocket(81);
void onWebSocketEvent(uint8_t client, WStype_t type, uint8_t* payload, size_t length) {
switch (type) {
case WStype_CONNECTED:
Serial.printf("Client %u connected\n", client);
break;
case WStype_DISCONNECTED:
Serial.printf("Client %u disconnected\n", client);
break;
case WStype_TEXT:
Serial.printf("Client %u: %.*s\n", client, (int)length, (const char*)payload);
break;
default:
break;
}
}
In setup():
webSocket.begin();
webSocket.onEvent(onWebSocketEvent);
In loop(), next to server.handleClient():
webSocket.loop();
Replace the <body> of the page with this one:
<body>
<h1>XIAO console</h1>
<p>Link: <strong id="status">connecting...</strong></p>
<script>
let ws;
function connect() {
ws = new WebSocket("ws://" + location.hostname + ":81/");
ws.onopen = () => {
document.getElementById("status").textContent = "connected";
};
ws.onclose = () => {
document.getElementById("status").textContent = "lost, retrying...";
setTimeout(connect, 1000); // automatic reconnection
};
}
connect();
</script>
</body>
location.hostname takes the address the page was loaded from (192.168.4.1): the page does not need to know it in advance.
Let us add two buttons that turn the XIAO’s LED on and off. First the protocol must be fixed: the browser sends the text LED:1 to turn it on and LED:0 to turn it off. Any other message is ignored.
In the callback function, do the minimum: check the message and store the command. loop() then applies it, as for an interrupt. That way, all the logic stays in one place, in the main loop.
int ledCommand = -1; // -1: no command pending
// in onWebSocketEvent(), replace the WStype_TEXT case:
case WStype_TEXT:
// Expected message: "LED:0" or "LED:1", nothing else
if (length == 5 && memcmp(payload, "LED:", 4) == 0) {
if (payload[4] == '1') ledCommand = 1;
if (payload[4] == '0') ledCommand = 0;
}
break;
In setup(), pinMode(LED_BUILTIN, OUTPUT);, then in loop():
if (ledCommand != -1) {
digitalWrite(LED_BUILTIN, ledCommand == 1 ? LOW : HIGH); // XIAO LED: LOW turns it on
ledCommand = -1;
}
On the page side, two buttons and a function that only sends if the link is open:
<button onclick="sendMessage('LED:1')">On</button>
<button onclick="sendMessage('LED:0')">Off</button>
function sendMessage(message) {
if (ws && ws.readyState === WebSocket.OPEN) {
ws.send(message);
}
}
(The sendMessage() function goes in the <script>, next to connect().)
In the other direction, the board sends the joystick position in the form JOY:2048. Two precautions avoid saturating the link:
millis() (never by delay());broadcastTXT() sends the message to all connected browsers.
constexpr int PIN_JOYSTICK_X = D0;
constexpr uint32_t SEND_PERIOD_MS = 100; // 10 messages per second at most
void sendJoystick() {
static uint32_t lastSendMs = 0;
static int lastSentValue = -1000;
uint32_t nowMs = millis();
if (nowMs - lastSendMs < SEND_PERIOD_MS) {
return;
}
lastSendMs = nowMs;
int x = analogRead(PIN_JOYSTICK_X);
if (abs(x - lastSentValue) <= 40) {
return; // no real change
}
lastSentValue = x;
char message[16];
snprintf(message, sizeof(message), "JOY:%d", x);
webSocket.broadcastTXT(message);
}
Call sendJoystick() in loop(). On the page side, a paragraph for the value, and the handling of received messages in connect():
<p>Joystick: <strong id="joy">?</strong></p>
ws.onmessage = (event) => {
const [type, value] = event.data.split(":");
if (type === "JOY") {
document.getElementById("joy").textContent = value;
}
};
In a game, the network is added to the game loop without modifying it:
server.handleClient() and webSocket.loop() are called on every pass of loop(), before the frame pacing test;void loop() {
// Network: on every pass, without waiting
server.handleClient();
webSocket.loop();
// Game loop: one frame every FRAME_TIME_MS
static uint32_t lastFrameMs = 0;
uint32_t nowMs = millis();
if (nowMs - lastFrameMs < FRAME_TIME_MS) {
return;
}
lastFrameMs = nowMs;
float joystickX = readJoystick(); // controls from the console...
readNetworkMessages(game); // ...and requests received from the browser
gameUpdate(game, joystickX, nowMs); // rules, validation of requests
rendererDraw(game); // console display
networkSendUpdates(game); // what has changed, to the browser
}
The number of connected browsers is given by webSocket.connectedClients(): enough to wait for a second player before starting the game, or to pause the game if they disappear.
#include <Arduino.h>
#include <WiFi.h>
#include <WebServer.h>
#include <WebSocketsServer.h>
constexpr char WIFI_SSID[] = "Console-Team1";
constexpr char WIFI_PASSWORD[] = "snake2026";
constexpr int PIN_JOYSTICK_X = D0;
constexpr uint32_t SEND_PERIOD_MS = 100;
WebServer server(80);
WebSocketsServer webSocket(81);
int ledCommand = -1; // -1: no command pending
const char PAGE[] = R"rawliteral(
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>XIAO console</title>
</head>
<body>
<h1>XIAO console</h1>
<p>Link: <strong id="status">connecting...</strong></p>
<p>Joystick: <strong id="joy">?</strong></p>
<button onclick="sendMessage('LED:1')">On</button>
<button onclick="sendMessage('LED:0')">Off</button>
<script>
let ws;
function connect() {
ws = new WebSocket("ws://" + location.hostname + ":81/");
ws.onopen = () => {
document.getElementById("status").textContent = "connected";
};
ws.onclose = () => {
document.getElementById("status").textContent = "lost, retrying...";
setTimeout(connect, 1000);
};
ws.onmessage = (event) => {
const [type, value] = event.data.split(":");
if (type === "JOY") {
document.getElementById("joy").textContent = value;
}
};
}
function sendMessage(message) {
if (ws && ws.readyState === WebSocket.OPEN) {
ws.send(message);
}
}
connect();
</script>
</body>
</html>
)rawliteral";
void handleRoot() {
server.send(200, "text/html", PAGE);
}
void onWebSocketEvent(uint8_t client, WStype_t type, uint8_t* payload, size_t length) {
switch (type) {
case WStype_CONNECTED:
Serial.printf("Client %u connected\n", client);
break;
case WStype_DISCONNECTED:
Serial.printf("Client %u disconnected\n", client);
break;
case WStype_TEXT:
if (length == 5 && memcmp(payload, "LED:", 4) == 0) {
if (payload[4] == '1') ledCommand = 1;
if (payload[4] == '0') ledCommand = 0;
}
break;
default:
break;
}
}
void sendJoystick() {
static uint32_t lastSendMs = 0;
static int lastSentValue = -1000;
uint32_t nowMs = millis();
if (nowMs - lastSendMs < SEND_PERIOD_MS) {
return;
}
lastSendMs = nowMs;
int x = analogRead(PIN_JOYSTICK_X);
if (abs(x - lastSentValue) <= 40) {
return;
}
lastSentValue = x;
char message[16];
snprintf(message, sizeof(message), "JOY:%d", x);
webSocket.broadcastTXT(message);
}
void setup() {
Serial.begin(115200);
delay(1500);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH); // LED off (inverted logic)
analogReadResolution(12);
WiFi.softAP(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Access point ready, address: ");
Serial.println(WiFi.softAPIP());
server.on("/", handleRoot);
server.begin();
webSocket.begin();
webSocket.onEvent(onWebSocketEvent);
}
void loop() {
server.handleClient();
webSocket.loop();
if (ledCommand != -1) {
digitalWrite(LED_BUILTIN, ledCommand == 1 ? LOW : HIGH);
ledCommand = -1;
}
sendJoystick();
}
The console creates its Wi-Fi network and serves its own page. The phone controls the board’s LED, and the board sends the joystick position to the phone, in real time and in both directions. The page reconnects by itself if the board restarts, and the board ignores invalid messages.
include/page.h file to lighten main.cpp, or in the board’s file system (LittleFS) when it grows.<canvas> element, and update it from the messages received.JSON.parse() on the page side).