Sending debug data by TCP instead of Serial using ESP8266?

I am working with an Adafruit Huzzah breakout board based on an ESP-12 device.
With it I want to build an electricity meter data collector and so far I have been using Serial in order to debug. In operation the serial port is dedicated for incoming data to the electricity meter but the outgoing is available so I used that for debugging.
Now I am soon done with this application and I plan on deploying it on location (in my summer home 95 km away).

But once the device is hooked up to the meter there I cannot see the debug messages...

So I wonder if there is some simple TCP/IP socket function I can add to the ESP code to do this:

  • Allow connection from a TCP client on my PC to the ESP
  • While the client is connected arrange for all debug messages now going to Serial to instead be sent to the TCP client.
  • Accept simple commands from the socket client to control the device (like sending the most recent data or doing some action like resetting the ESP8266 etc.

I now realize that I should have made a "debugreport()" function where the target is the Serial port so that could be redirected to a connected TCP socket. But I probably now need to restructure that part and cut away all the Serial.printx() calls to a new function which can be switched between serial and TCP/IP...

So two questions:

  1. Is there already a functional demo code that can be used as a base for the above?
  2. Is there a "simple" way to "redirect" the Serial.print() and Serial.println() calls such that they will wind up in a TCP client outgoing channel instead? Note that I have used the Serial.print() functionality to send strings out with the data I supply so I can print floats and integers and strings and char[] arrays without converting all this into text strings first...

run a web server on the ESP
your PC connects to the web server and views your data

Well, the data are debug messages that are sent by the ESP on events that happen now and then. To see these I need a socket server to connect to and whenever the debug message is going to be sent it will be sent by this socket if a client is connected, otherwise by serial.
There are no data to see on a webpage in-between, because they are not saved anywhere....
So I want to get these messages into my Windows application and show them in the scrolling log window as they arrive.

It is not like a changing value like the example you pointed to.
That is a good way to for instance show the electricity meter total value and the instantaneous power usage and local temperature on a webpage, so the suggestion is valid for such things! :slight_smile:
Thanks!

Maybe this would work (found it on the webpage you pointed to by going to an index for ESP8266).

implement your own TCP or UDP server to run on the PC
when an event occurs the Arduino

  1. connects to the server2
  2. sends a packet which is displayed
  3. disconnects from the server

probably sending a UDP datagram is simplest

or send yourself an email

my TelnetStream library does the network part.
my StreamLib library has a TeePrint to print to two outputs. it is simple so see how it works and you can add switching between the outpuys

If you're using an ESP, why not simply use HTTP, and create a web page that shows the current status. It is trivial to send a single page of the latest status, and only slightly more complex to have it periodically update itself, using AJAX, servert-sent events, or any one of many other asynchronous update methods. You can then view the status from any device with a browser.

Thanks for replies!
I really do not want to involve browsers like FireFox etc in this, instead I want to receive the data on the PC side just like it was coming from a serial port, but now over a TCP socket. The receiving program will be my existing simulator.

In my simulator program, which I have written for this project, I have various other functions and there is a logger which displays all the incoming data from the debug output of the ESP8266 in a "Debug messages" panel and also sends test data to the ESP according to settings in the simulator via the serial port.
The latter function will not be used when the device is deployed because then the serial data will be streaming from the electricity meter.
But when the TCP socket is used then the outgoing direction will be used to send commands to the ESP itself for controlling its operation.

Here is a screenshot of the simulator in operation as it is right now with only serial comm available:

Then use sockets or UDP to send the debug data to a dedicated TCP port. It doesn't get much simpler.

And, using HTTP does not REQUIRE using a browser. The basic protocol is VERY simple, text-based, and easy to parse on the client side. So you could easily use HTTP to transfer the debug data with only minor modifications to the Arduino Web Client example code.

I am now looking at an old RS232- WiFi converter I once upon a time included in another project where it was used to replace the physical wires for RS232 with a WiFi connection to the other end where it was again converted to RS232.
The converter acts as an AP so to open the connection one connects by WiFi to its SSID and then opens a socket connection on port 2101...
Then serial data are just channeled through in both directions in raw format.

I had to dig back to 2013-14 or so to find it.
I don't know yet if the code is portable or if libraries have changed too much since then. Or if it can be incorporated as a new feature into what I have already now...

did you read my comment?

Is this the one you mean?

And a google search brought me here, is this the one?

Seems to be!
By what I can understand it has a "command line 'API' too:

void loop() {
  switch (TelnetStream.read()) {
    case 'R': //<== one-letter command to reset the ESP?
    TelnetStream.stop();
    delay(100);
    ESP.reset();
      break;
    case 'C': <== one-letter command to disconnect?
      TelnetStream.println("bye bye");
      TelnetStream.flush();
      TelnetStream.stop();
      break;
  }

So if you send 'C' you disconnect and if you send 'R' you tell it to reset the ESP device?
What caused you to choose C for disconnect? D seems to be more logical.

In the other direction it seems to send whatever you give it to the connected client, right? So this has to implement a "Telnet" connection, what does tyhat entail?
And can print and println accept all of the strange (to me) formats as Serial.Print can?

Another question regarding the example:
How can this loop in setup() ever exit?

  if (WiFi.waitForConnectResult() != WL_CONNECTED) {
    Serial.println("Failed to connect.");
    while (1) {
      delay(10);
    }
  }

Seems like a physical reset or power cycle is the only way to get out of this...
Not so good for remote operations.

the library is in Library Manager

Ctrl+C. C is Shift+C

it is an example, so if the conditions are not there, there is no point to do something

@Juraj

In the documenation of your telnetStream-library you wrote

The library creates a TelnetStream object, which can be used the same way as Serial, but the output is sent to all connected telnet clients. It enables remote logging or debugging.

What does it mean if you write "all connected telnet clients" ?

Does this mean as soon as the microcontroller that is running the telnetStream-library is online in the local WiFi-network I can connect with (how many telnet-clients?) to this microcontroller ?

I'm not familiar with telnet at all. Can you post a link where an actual available telnet-client for windows is explained in a step-by-step / screenshot for screenhot manner how to configure the telnet-client to make it work?

I have tried this earlier but failed.

best regards Stefan

@StefanL38 yes to all currently connected clients.

for Windows Telnet client google. in older windows it needed to be installed from Windows installation. it was not installed by default

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.