Layer: L5 Session ยท Build flags: PC_ENABLE_TELNET
What this example teaches
This opens a Telnet console (RFC 854) on port 23 alongside the HTTP server. The library negotiates echo + character mode, edits the input line for you (backspace works), and hands each completed line to a command callback - so you write a tiny command interpreter, not a byte pump.
Plaintext warning. Telnet has no auth and no encryption. Use it only on a trusted LAN; prefer SSH or the WebSocket terminal for anything exposed.
Opening the port. As with SSH, you add a non-HTTP listener and then begin() (which activates all listeners, here HTTP on 80 too):
pc_telnet_on_command(on_command);
server.begin(80);
@ PROTO_TELNET
Telnet (RFC 854).
A line-at-a-time command handler. The callback receives a fully-edited line and the connection id; reply with pc_telnet_print / pc_telnet_println / pc_telnet_frame. This example is a minimal shell with help, heap, uptime, and an echo fallback:
void on_command(const char *line, uint8_t conn_id) {
if (strcmp(line, "heap") == 0)
pc_telnet_frame(REPLY_HEAP, (uint32_t)ESP.getFreeHeap());
else if (line[0])
pc_telnet_frame(REPLY_ECHO, line);
}
Build-flag note. PC_ENABLE_TELNET must be set for the whole build; an in-sketch #define does not reach the separately compiled library, so pass it as a build_flag (see the build_flags gotcha).
WiFi.setSleep(false) is called so the radio stays responsive for an interactive session.
Build and run
pio ci --board=esp32dev --project-option="framework=arduino" \
--project-option="build_flags=-DPC_ENABLE_TELNET=1" \
--lib="." examples/L5-Session/Telnet/Telnet.ino
Flash, read the IP on Serial, then:
telnet <ip> 23 # type "help"
Annotated source
The complete sketch (Telnet.ino), reproduced verbatim with added explanatory comments:
#define PC_ENABLE_TELNET 1
static const char *SSID = "YOUR_SSID";
static const char *PASSWORD = "YOUR_PASSWORD";
void on_command(const char *line, uint8_t conn_id)
{
(void)conn_id;
if (strcmp(line, "help") == 0)
pc_telnet_println("commands: help, heap, uptime, <echo>");
else if (strcmp(line, "heap") == 0)
pc_telnet_frame(REPLY_HEAP, (uint32_t)ESP.getFreeHeap());
else if (strcmp(line, "uptime") == 0)
pc_telnet_frame(REPLY_UPTIME, (uint32_t)millis());
else if (line[0])
pc_telnet_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));
pc_telnet_on_command(on_command);
Serial.println("Telnet on port 23 (try: telnet <ip>)");
}
void loop()
{
}
Single-port HTTP server with deterministic, zero-allocation execution.
int32_t listen(uint16_t port, ConnProto proto=ConnProto::PROTO_HTTP)
Register a port to listen on when begin() is called.
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.
Layer 6/7 - minimal RFC 854 Telnet server (PC_ENABLE_TELNET).