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.
- I could not find a previous question in this forum about this matter. Is there a library for a serial telnet server over Wifi?
- Can I use the Serial Monitor of the Arduino IDE for the client?
- What Telnet client utility would you recommend for Windows for this purpose?
Appreciate all replies and thanks to all.
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
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.
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); } }
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);
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….