Creating a Telnet Server on ESP8266
Telnet is old, simple and still handy for hobby projects. With a telnet server on an ESP8266 you can connect from any computer on your network, type a command and watch your board respond, with no cable plugged in. It is great for checking free memory, toggling a pin or debugging a device that lives on a shelf.
Important: telnet sends everything, including passwords, in plain text. Use it only on a network you trust and never expose it to the internet. For anything sensitive, use an encrypted protocol instead.
What you need
- An ESP8266 board such as a NodeMCU or Wemos D1 mini
- Arduino IDE with the ESP8266 core installed
- A Wi-Fi network and a computer with a telnet client (
telnet,ncor PuTTY in telnet mode)
How it works
The ESP8266 core provides WiFiServer, which listens on a TCP port. Telnet normally uses port 23. When someone connects, server.accept() hands you a WiFiClient object. You read characters from it, assemble them into a line, run the command, and write the answer back. In recent cores accept() is the right call and available() is deprecated.
The complete sketch
This version allows one client at a time, asks for a password, and supports a few commands.
#include <ESP8266WiFi.h>
const char* WIFI_SSID = "your-wifi-name";
const char* WIFI_PASS = "your-wifi-password";
const char* TELNET_PASSWORD = "change-me";
WiFiServer server(23); // 23 is the standard telnet port
WiFiClient client;
String line;
bool loggedIn = false;
uint8_t skipBytes = 0; // used to ignore telnet negotiation bytes
void prompt() { client.print(loggedIn ? "> " : "Password: "); }
void handle(String cmd) {
cmd.trim();
if (!loggedIn) {
if (cmd == TELNET_PASSWORD) { loggedIn = true; client.println("Welcome. Type help."); }
else { client.println("Wrong password."); client.stop(); return; }
} else if (cmd == "help") {
client.println("Commands: help, uptime, heap, led on, led off, quit");
} else if (cmd == "uptime") {
client.printf("Up %lu seconds\r\n", millis() / 1000);
} else if (cmd == "heap") {
client.printf("Free heap: %u bytes\r\n", ESP.getFreeHeap());
} else if (cmd == "led on") {
digitalWrite(LED_BUILTIN, LOW); // the built-in LED is active low
client.println("LED on");
} else if (cmd == "led off") {
digitalWrite(LED_BUILTIN, HIGH);
client.println("LED off");
} else if (cmd == "quit") {
client.println("Bye.");
client.stop();
return;
} else if (cmd.length()) {
client.println("Unknown command. Type help.");
}
if (client.connected()) prompt();
}
void setup() {
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); }
Serial.printf("\nConnected. Telnet to %s\n", WiFi.localIP().toString().c_str());
server.begin();
server.setNoDelay(true);
}
void loop() {
WiFiClient incoming = server.accept();
if (incoming) {
if (client && client.connected()) {
incoming.println("Busy: another session is active.");
incoming.stop();
} else {
client = incoming;
loggedIn = false;
line = "";
client.println("ESP8266 telnet server");
prompt();
}
}
while (client && client.connected() && client.available()) {
uint8_t c = client.read();
if (c == 255) { skipBytes = 2; continue; } // IAC: skip the next two bytes
if (skipBytes) { skipBytes--; continue; }
if (c == '\n') { handle(line); line = ""; }
else if (c != '\r' && line.length() < 64) { line += (char)c; }
}
}
Try it out
- Fill in your Wi-Fi name and password and upload the sketch.
- Open the Serial Monitor at 115200 baud and note the IP address.
- From your computer, connect:
telnet 192.168.1.60If you do not have a telnet client, nc 192.168.1.60 23 works too. Enter the password, then try help, heap and led on.
Why the code is written this way
- One client at a time. The ESP8266 has limited RAM. A second connection gets a polite “busy” message.
- Non-blocking loop. We never wait inside
loop(), so the board can still do other work and the watchdog stays happy. - Line buffering. We collect characters until Enter and cap the line at 64 characters to avoid memory problems.
- Ignoring negotiation bytes. Some telnet clients send option negotiation bytes when they connect, starting with 255. The small filter skips them so they do not appear in your commands.
setNoDelay(true). Sends small replies immediately, which makes the session feel responsive.
Add your own commands
Each command is just another else if in handle(). Read a sensor and print it, switch a relay, show the Wi-Fi signal strength with WiFi.RSSI(), or restart with ESP.restart(). Keep responses short and always end lines with println, which sends the carriage return and newline that telnet clients expect.
Making it safer
- Change the password and do not reuse one you use elsewhere. Remember it is sent in plain text.
- Keep it on a separate or guest network if you can, away from computers that matter.
- Add a timeout that disconnects idle sessions, so a forgotten connection does not block the board.
- Do not forward port 23 on your router.
Troubleshooting
- Connection refused: check the IP address and that
server.begin()ran after Wi-Fi connected. - Strange characters at the start of commands: your client is sending negotiation bytes. The filter in the sketch handles the common case.
- LED works backwards: on many ESP8266 boards the built-in LED turns on when the pin is LOW.
- Random resets: avoid long blocking loops and
delay()calls insideloop().
From here you can turn this into a tiny remote console for your whole project. Just remember what it is: a convenient tool for a trusted network, not a secure remote login.
Note: test the sketch on your own board and core version, and adjust pins and names for your hardware.