Layer: L6 Presentation ยท Build flags: PC_ENABLE_WEB_TERMINAL (requires PC_ENABLE_WEBSOCKET)
What this example teaches
This serves a self-contained green-phosphor terminal page (the docs-site CRT theme) plus a WebSocket endpoint, giving you a remote console in the browser: device output streams to every open page, and each line you type is delivered to a command callback. It rides the library's existing WebSocket layer, so it is zero-heap, and the page auto-selects ws:// or wss:// (works unchanged once TLS is on).
One call mounts the whole thing. pc_web_terminal_begin(server, path) serves the page and the WebSocket; ..._on_command registers your handler:
pc_web_terminal_begin(server, "/terminal");
pc_web_terminal_on_command(on_command);
Browser to device. The command callback receives a typed line; respond with pc_web_terminal_println / ..._printf, which broadcast to all open terminals:
void on_command(const char *line, uint8_t client_id) {
if (strcmp(line, "heap") == 0)
pc_web_terminal_frame(REPLY_HEAP, (uint32_t)ESP.getFreeHeap());
else
pc_web_terminal_frame(REPLY_ECHO, line);
}
Device to browser. From loop() you can push output any time; gate it on pc_web_terminal_client_count() so you only format when someone is watching:
if (pc_web_terminal_client_count() > 0)
pc_web_terminal_frame(HEARTBEAT, (uint32_t)millis(), (uint32_t)ESP.getFreeHeap());
A plaintext alternative is Telnet; the raw WebSocket primitive is WebSocket.
Build and run
pio ci --board=esp32dev --project-option="framework=arduino" \
--project-option="build_flags=-DPC_ENABLE_WEB_TERMINAL=1" \
--lib="." examples/L6-Presentation/WebTerminal/WebTerminal.ino
Flash, then browse to http://<ip>/terminal and type help.
Annotated source
The complete sketch (WebTerminal.ino), reproduced verbatim with added explanatory comments:
#define PC_ENABLE_WEB_TERMINAL 1
#include "services/web_terminal.h"
static const char *SSID = "YOUR_SSID";
static const char *PASSWORD = "YOUR_PASSWORD";
void on_command(const char *line, uint8_t client_id)
{
(void)client_id;
if (strcmp(line, "help") == 0)
pc_web_terminal_println("commands: help, heap, uptime, <echo>");
else if (strcmp(line, "heap") == 0)
pc_web_terminal_frame(REPLY_HEAP, (uint32_t)ESP.getFreeHeap());
else if (strcmp(line, "uptime") == 0)
pc_web_terminal_frame(REPLY_UPTIME, (uint32_t)millis());
else
pc_web_terminal_frame(REPLY_ECHO, line);
}
void setup()
{
Serial.begin(115200);
Serial.print("Connecting to WiFi");
{
delay(250);
Serial.print('.');
}
Serial.printf("IP: %u.%u.%u.%u\n", (unsigned)(ip & 0xFF), (unsigned)((ip >> 8) & 0xFF),
(unsigned)((ip >> 16) & 0xFF), (unsigned)((ip >> 24) & 0xFF));
Serial.println("Open http://<ip>/terminal in a browser");
pc_web_terminal_begin(server, "/terminal");
pc_web_terminal_on_command(on_command);
int32_t result = server.
begin(80);
if (result < 0)
{
Serial.printf("begin() failed (error %d)\n", result);
return;
}
Serial.println("Server started on port 80");
}
void loop()
{
static unsigned long last = 0;
if (millis() - last >= 3000)
{
last = millis();
if (pc_web_terminal_client_count() > 0)
pc_web_terminal_frame(HEARTBEAT, (uint32_t)millis(), (uint32_t)ESP.getFreeHeap());
}
}
Single-port HTTP server with deterministic, zero-allocation execution.
int32_t begin(const WebServerConfig *cfg=nullptr)
Initialize all connection slots and open all registered listeners.
void handle()
Drive the server - call every Arduino loop() iteration.
bool init_wifi_physical(const char *, const char *)
Connect to a WiFi access point.
uint32_t pc_net_egress_ip(void)
IPv4 (network byte order) of the current egress interface, or 0 if none.
bool wifi_ready()
True if the WiFi station link is up (associated + an IP is assigned).
Layer 1 (Physical) - link bring-up and live egress-interface reporting.
Layer 7 (Application) - public HTTP routing API.