BlogBeast
ESP8266

Creating a Telnet Server on ESP8266

By BlogBeast · · 4 min read

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

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

  1. Fill in your Wi-Fi name and password and upload the sketch.
  2. Open the Serial Monitor at 115200 baud and note the IP address.
  3. From your computer, connect:
telnet 192.168.1.60

If 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

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

Troubleshooting

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.