Simultaionus bidirectional TCP communication

Cool Javelin
Posts: 12
Joined: Wed Oct 20, 2021 3:58 am

Simultaionus bidirectional TCP communication

Postby Cool Javelin » Tue Oct 18, 2022 6:02 am

Hello:

I am connecting one ESP32 and a few ESP8266 boards in a WiFi network, and I need bidirectional communication between the 32 and each of the 8266's.

I can find several examples on creating a client on the 32, and servers on the 8266's. I have only tried one pair so far, but it seems straight forward.

As I understand it, the client can only send data to the server.

However, I require the reverse as well. I need to send data from the client to the server, AND get a response back from the server. (I need to set a relay, and verify the relay got set before moving on to the next steps of the program.)

Must I set up a client and a server on both boards using different ports, or is there something I am missing to get a response from the server back to the client?

Thanks, Mark.

Craige Hales
Posts: 94
Joined: Tue Sep 07, 2021 12:07 pm

Re: Simultaionus bidirectional TCP communication

Postby Craige Hales » Tue Oct 18, 2022 1:57 pm

I suspect you are using a bidirectional protocol. Can you show what you do to set up and use the connection? What sort of message do you send to close the relay? What message to open? How do you receive the message on the far end?

If you are building this with http then you could test by using a browser to visit the 8266 web pages. The /status web page shows the current state. The /open and /close pages do the action and redirect to the /status page. The HTLM can be as simple as "1" for closed and "0" for open, which will make the esp32's job really simple...

Http is built on top of sockets, and that's where the bidirectional support lives. Possibly you are using a socket and just closing it too soon.

Maybe there is a pre-packaged way to do this that someone will point out.
Craige

Cool Javelin
Posts: 12
Joined: Wed Oct 20, 2021 3:58 am

Re: Simultaionus bidirectional TCP communication

Postby Cool Javelin » Sat Oct 22, 2022 6:23 pm

Hello Craige:

Thanks for taking the time to chat with me about this.

While I had not thought about making an HTTP server (on the 8266 server side,) I think it might be a bit much.

Rather, on the Arduino forum, someone said I can make a "WiFiClient" on both machines, then after "server.available," use the client on the server to send stuff.

It is all very confusing to keep my head wrapped around the concepts, especially when the instance on both machines is called "client."

See this... https://arduino.stackexchange.com/quest ... 6899#66899

Just some info on my project...

In my case, I am making an irrigation system with a single long pipe, valves at each sprinkler head, and a pump that takes water from my well. Each valve will have a small solar panel, a battery, and an 8266 controlling the valve via a relay. (yes, maybe a bit overkill, but wire is expensive.)

I can only run one head at a time, and I want to make sure one valve is open to prevent the pump from over pressuring so reliable bidirectional communication is imperative.

I am just sending the word "openvalve" or "closevalve" from the client to the server, then I want the server to respond with "valveisopen" or "valveisclosed"

Also, I want the server to send "valveisopen" once a second so the client can turn off the pump if a problem occurs.

Their is another aspect that I have not dealt with yet, what happens if communication is simply lost. It may take quite some time before the client notices the server is dead and turns off the pump. I may have a pressure sensor on the pump to handle over pressure in hardware.

Of course I could do all this using traditional methods (store bought timers, valves, and wire) but what is the fun in that?

I am sure this will be 5 times the cost, less reliable and take a long time but it is infinitely expandable. When the 8266's get too far from the router, I can implement a mesh network to water the "back 2"

Thanks, Mark.

Craige Hales
Posts: 94
Joined: Tue Sep 07, 2021 12:07 pm

Re: Simultaionus bidirectional TCP communication

Postby Craige Hales » Tue Oct 25, 2022 10:39 pm

the stackexchange you refer to looks bidirectional.

Code: Select all

client socket

if (client.connect(serverIP, PORT)) {
  client.print("request\n");   <<<<<<< this is where you put "openvalve" or "closevalve"
  String response = client.readStringUntil('\n');
  Serial.println(response); <<<<<<<< this is where you find out the status, "isopen" or "isclosed"
  client.stop();<<<<< closing the connection 
}

server side

WiFiClient client = server.available();
if (client && client.connected()) {
  String request = client.readStringUntil('\n'); <<<<<<<<< this is where you get a command, "openvalve" or "closevalve"
  Serial.println(request);
  client.print("response\n"); <<<< and this is where you send confirmation, "isopen" or "isclosed"
  client.stop();<<<<<< closing the connection
}

You'll want a "valve_status" command that will confirm the current status. You might call it a lot, round-robin through all the servers.

I've not actually used it. My quick take on it: the wificlient represents an open socket, bidirectional, until something causes it to close. You can bounce messages back and forth until something breaks. A simple web server could be built on top of it.

I'd still suggest the http approach because it will be easier to debug the servers from a browser. See https://espressif-docs.readthedocs-host ... l#examples for pretty much your use case with just enough more code to make it browser friendly.

You are right, you probably don't need or want a full capability web server, but I think you'll find it useful to craft your messages to be compatible with a browser...a mini-server. Later you may actually want a status page with history, soil moisture sensor, temp sensor, hourly data, ... at each location.


Thanks for pointing out the wificlient, I might take a deeper look later.
Craige

Who is online

Users browsing this forum: No registered users and 139 guests