• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

ESP32 – How to Telnet or Serial Monitor over Wifi?

Q&A Forum › Category: ESP32 › ESP32 – How to Telnet or Serial Monitor over Wifi?
0 Vote Up Vote Down
Bernie asked 3 years ago

Hi — This is a very basic question.  I have an ESP32 (Espduino) running a web server example.

But I don’t want a web server and a browser client.  I need a serial telnet server on the ESP32 – that is, to connect a serial monitor over Wifi to the ESP32.  This is the most basic thing one may want to do if not connecting to a web server.

  1. I could not find a previous question in this forum about this matter.  Is there a library for a serial telnet server over Wifi?
  2. Can I use the Serial Monitor of the Arduino IDE for the client?
  3. What Telnet client utility would you recommend for Windows for this purpose?

Appreciate all replies and thanks to all.

Question Tags: ESP32 wifi
9 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 3 years ago

Hi Bernie.
I’m not familiar with that subject. I found this video about that subject that might help: https://youtu.be/j9yW10OcahI
We have this tutorial that builds a “serial monitor” using a web server: https://randomnerdtutorials.com/esp32-webserial-library/ but I don’t think this is what you are looking for.
Regards,
Sara

0 Vote Up Vote Down
Bernie answered 3 years ago

I am looking at the Example WiFiTelnetToSerial that comes with ESP32.  Amazing how poorly it is written and half of it is bloatware.  The author must have loved nested code, which is bad practice.  The essence of the code can be as little as ten lines.  Once I reduce it and make it work, I will publish it here.

0 Vote Up Vote Down
Sara Santos Staff answered 3 years ago

Ok.
Thanks. Keep us updated.
Regards,
Sara

0 Vote Up Vote Down
Bernie answered 3 years ago

This code lets you do a WiFi OTA Serial Monitor to let’s say a Mega.

Wire connect UART RX/TX 2 of the Mega (pins D16, D17) to the UART TX/RX 2 of the ESP32.  Make sure the connections are RX to TX and TX to RX. Set the speeds on both ends to 9600.  I have seen data dropped when set to faster speeds.

You can then start Putty on Windows, or any other terminal emulator, connect to the ESP32 OTA on port 23, and you will have access to Serial2 on the Mega.  Serial2.print() on the Mega will show up on Putty, through ESP32 and WiFi.  Ketboard input on Putty will be available to Mega on Serial2.

Here is the code for the Telnet server after some massaging.  Install Putty.exe on Windows to telnet to the ESP32.  The core of the code is the last 2 paragraphs.  Everything else is unfortunate but necessary exception handling.

#include <WiFi.h>
WiFiServer Server(23);
WiFiClient Client; // one client should be able to telnet to this ESP32
const char *ssid = "WIFI";
const char *password = "PASSWORD";

void setup() {
int8_t i;
Serial.begin(115200);
Serial.print("\nAttaching to WiFi '" + String(ssid) + String("' ..."));

WiFi.begin(ssid, password);
Serial.print(" attached to WiFi.\nConnecting to network ... ");
for (i = 60; i != 0; i--) {
if (WiFi.status() == WL_CONNECTED) break;
delay(333);
}
if (i == 0) {
Serial.println("Network connection failed!\nRestarting ESP32!");
delay(1000);
ESP.restart();
}

Serial.print(" network connected.\nLocal IP address: ");
Serial.println(WiFi.localIP());
Server.begin();
Server.setNoDelay(true);
Serial.print("Ready! Use port 23 to connect.");
}

void loop() {
delay(200);
if (WiFi.status() != WL_CONNECTED) {
Serial.println("WiFi not connected! Retrying ...");
if (Client) Client.stop();
return;
}
if (Server.hasClient()) { //check if there are any new clients
Client = Server.available();
if (!Client) Serial.println("available broken");
Serial.print("New client: ");
Serial.println(Client.remoteIP());
}

if (Client && Client.connected()) { //check clients for data
if (Client.available()) {
while (Client.available())
Serial.write(Client.read()); //get data from the telnet client and push it to the UART
}
}
else if (Client) Client.stop();

if (Serial.available()){ //check UART for data
size_t len = Serial.available();
char sbuf[len];
Serial.readBytes(sbuf, len);
if (Client && Client.connected()) Client.write(sbuf, len);
}
}
1 Vote Up Vote Down
Sara Santos Staff answered 3 years ago

Thanks for sharing this.
Regards,
Sara

0 Vote Up Vote Down
Bernie answered 3 years ago

The Telnet server just echos serial traffic to a WiFi client terminal such as Putty.  The following few lines.  Everything else is to handle exceptions to make sure Client exists.

  • while (Client.available()) Serial2.write(Client.read());
    size_t len = Serial2.available();
    if (len == 0) return;
    char sbuf[len];
    Serial2.readBytes(sbuf, len);
    Client.write(sbuf, len);
1 Vote Up Vote Down
Sara Santos Staff answered 3 years ago

Great!
Thanks for the explanation.
Regards,
Sara

0 Vote Up Vote Down
Steen Spaten answered 3 years ago

Fantastic !! Thank you – thank you – thank you. This is a precise, clean, no nonsense code – spot on the subject.
I am using this to communicate with my project over WiFi and serial communication. In my case I had to discard checking for new clients.
I combined the “Websocket Web Server” in your book “Build WEB Servers” with this TCP-UART bridge code so I can upload a configuration to my project board.
Again: Thank you – from an retired old guy (NOT a programmer). And thank you for this great community – it makes my otium a lot more fun….

0 Vote Up Vote Down
Sara Santos Staff answered 3 years ago

That’s great!
I’m glad this thread was useful.
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025
  • [eBook Updated] Learn ESP32 with Arduino IDE eBook – Version 3.2 April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.