Bidirectional data-exchange similar as ESP-NOW or UDP but with (sorry not HTTP) but with TCP

I have successfully programmed data-exchange using ESP-NOW and UDP.
With data-exchange I mean send a rather small number of bytes 10-200 in both directions. Very similiar to a serial connection.
This required to learn some details but I had it it up and running pretty quick.
As a simple example:

two ESP32 called ESP1 ESP2

ESP1 sends "time" ESP2 receives "time" and sends back

ESP2 sends "12:34" ESP1 recives "12:34"

Data-exchange done. Exchange a few bytes

By googling I tried to find something similar based on HTTP TCP.

As HTTP is used 99% for more complex things like webrequests based on HTTP-GET or HTTP-POST with complex data-structures including JSON I did not find anything that would be similar to the super-simple "send a few bytes or characters back and forth-example.

If somebody knows a link that has a demo-code that shows how to to bi-directional exchange of a few bytes back and forth
by consequent avoiding using html this would be great.

As you can see by my questions I have almost no knowledge about HTTP TCP. It might even possible that bi-directional data-exchange of a few bytes back and forth

by consequent avoiding using html is not possible.

If this is the case what would be the bare minimum = the shortest ever possible html-code that does nothing more than this example:
not based on http
but based on TCP

ESP1 sends "time" ---->based on HTTP TCP------>ESP2 receives "time" and sends back

ESP2 sends "12:34" ---->based on HTTP TCP------>ESP1 receives "12:34"

EDIT: I'm very sorry I confused http with TCP. This means all the above written is meant with TCP
best regards Stefan

Is the http session open for any other reason?

Yes or no, a simultaneous ‘telnet’ session could be used like a serial port to push some simple ascii data between two hosts.

Hi lcn,

thank you for answering.

I don't understand what you mean by that.
Does "http session" mean If I'm focused on http as the protocol = meaning I want to use http and nothing else?

or does this mean some kind of http-communication-mode?
keep server/clients websockets opened or closed?

is telnet "on top" of http?

By this questions you can again see: I know almost nothing about how http works.

explaining more of the very basics about http or links to real basic explanations much appreciated.

best regards Stefan

a very big

oooops !!!

Beeing a total noob regarding this subject I confused http with TCP
I aplogise for that.

Now what would be an appropriate way to correct this thread without making the postings look silly?

best regards Stefan

i’ve never renamed a thread ?!
Anyway, it sounds like you’re getting closer to your solution. :duck:

to summarize:

Hi Juraj,

I understand the basic principle of the lines of code that where posted on the link.

But it is not a full demo-code. Do I understand right that the only things needed additionally is

  1. connect to a WiFi-network
  2. create an instance of the WiFiClient-object
  3. adding this code mentioned in the answer you linked to?

client socket

if (client.connect(serverIP, PORT)) {
  client.print("request\n");
  String response = client.readStringUntil('\n');
  Serial.println(response);
  client.stop();
}

The second code-snippet is called "server side"

WiFiClient client = server.available();
if (client && client.connected()) {
  String request = client.readStringUntil('\n');
  Serial.println(request);
  client.print("response\n");
  client.stop();
}

In this answer the "server-side" uses a "client" too.
As I only know so few about this TCP, cleint, server, sockets I'm confused now.

does this mean that two clients can communicate with each other without a server involved?

best regards Stefan

the WiFi library you use has examples with complete client and server sketch.

server

All client examples show HTTP, but all the http protocol strings are only data. you can send any data to your server.

OK I will lookup it
but as I want to stay below http I have doubts that the examples are as simple as plain simple send /receive some characters.

I edited the previous comment

HTTP is a request/response protocol so the examples show the client connecting sending receiving the response and disconnecting. And the HTTP server examples get a client reads the request, writes a response and closes.

For your communication, you can let the client connected all the time and only reconnect if the connection disconnects for some reason.

The WiFiTelnetToSerial example shows a server which doesn't disconnect clients.

Hi Juraj,

I apreciate your time and effort to answer and to explain. Thank you very much.
In this case I insist on getting an direct answer to my question or get no answer.

The first question below is not meant as an inplicit request for writing a complete demo-code. It is meant as what the letters of the question say. This means the answer to this first question is: "nnnn minutes"
where "nnnn" is the number of minutes.

  1. As a rough estimation: How many minutes would it take you to write a demo-code that demonstrates a very simple but BI-directional TCP-connection between two ESP32's that does

exactly

this:

ESP1 sends once every second "Hello ESP2 " + ESP1count++

ESP2 receive this message sending back "I'm here " + ESP1count * 10

in parallel
ESP2 sends once every 5 seconds "How are you ESP1?" + ESP2Count++
ESP1 receives this message sending back "Oh I'm fine" + ESP2Count * 1000

where ESP1count and ESP2count are variables that count up with every cycle

not more not less.

I want to emphasise again: I would like to know the rough estimated real number of minutes it takes an experienced user to write this code. So I could estimate how much time it will take me as a

total noob

to code this.

only in case the time to write such a demo-code is less than 10 minutes:
would you mind writing it and post it here?

best regards Stefan

sorry I didn't read all the original post because I thought it is solved with lastchancename's comment.

it is very easy. I am just lazy to search for both my esp32 and USB cables :slight_smile:

finally user @J-M-L posted in another thread a demo-code that comes close to what I have described above.

You can find the demo-code here:

best regards Stefan

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