TCP ESP32 two-way communication

munand6
Posts: 1
Joined: Fri May 12, 2023 9:00 am

TCP ESP32 two-way communication

Postby munand6 » Fri May 12, 2023 9:57 am

Hello everyone, I found on the net a code for a two-way communication between two Wifi devices, via TCP. It's quite simple and works well, the only problem is that if one of the two switches off, when you switch it on again it doesn't connect, you have to turn them both off to re-establish the connection.
I made various tests but I can't, I ask, can someone help me? for me and for other people, because this sketch could be very offensive to others.
Thank you

  1. #include <WiFi.h>
  2.  
  3. const char* ssid     = "ESP32-AP";
  4. const uint16_t portNumber = 50000; WiFiServer server(portNumber);
  5. WiFiClient client;
  6. bool connected = false;
  7.  
  8. void setup() {
  9.   Serial.begin(115200); Serial.println();
  10.   Serial.print("Setting AP (Access Point)…");
  11.   WiFi.softAP(ssid);
  12.   IPAddress IP = WiFi.softAPIP();
  13.   Serial.print(" -> IP address: "); Serial.println(IP);
  14.   server.begin();
  15. }
  16.  
  17. void loop() {
  18.   if (!connected) {
  19.     delay(5000);
  20.     client = server.available();
  21.     if (client) {
  22.       Serial.println("Got a client !");
  23.       if (client.connected()) {
  24.         Serial.println("and it's connected!");
  25.         connected = true;
  26.       } else {
  27.         Serial.println("but it's not connected!");
  28.         client.stop();  
  29.       }
  30.     }
  31.   } else {
  32.     if (client.connected()) {
  33.       while (client.available()) Serial.write(client.read());
  34.       while (Serial.available()) {
  35.         char r = Serial.read();
  36.         Serial.write(r);
  37.         client.write(r);
  38.       }
  39.     } else {
  40.       Serial.println("Client is gone");
  41.       client.stop();  
  42.       connected = false;
  43.     }
  44.   }
  45. }
  46. ]
  47.  
  48.  
  49. [
  50. #include <WiFi.h>
  51. const char* ssid     = "ESP32-AP";
  52. const uint16_t portNumber = 50000;
  53. IPAddress serverIP;
  54. WiFiClient client;
  55. bool connected = false;
  56.  
  57. void setup() {
  58.   Serial.begin(115200); Serial.println();
  59.   Serial.print("Connecting to SSID:"); Serial.println(ssid);
  60.   WiFi.mode(WIFI_STA);
  61.   WiFi.disconnect();
  62.   WiFi.begin(ssid);
  63.   while (WiFi.status() != WL_CONNECTED) {
  64.     delay(500);
  65.     Serial.write('.');
  66.   }
  67.   Serial.println(".\nConnected");
  68.   Serial.print("Local IP = "); Serial.println(WiFi.localIP());
  69.   serverIP = WiFi.gatewayIP();
  70.   Serial.flush();
  71. }
  72.  
  73. void loop() {
  74.   if (! connected) {
  75.     if (client.connect(serverIP, portNumber)) {
  76.       Serial.print("Connected to Gateway IP = "); Serial.println(serverIP);
  77.       connected = true;
  78.     } else {
  79.       Serial.print("Could NOT connect to Gateway IP = "); Serial.println(serverIP);
  80.       delay(500);
  81.     }
  82.   } else {
  83.    
  84.     while (client.available()) Serial.write(client.read());
  85.     while (Serial.available()) {
  86.       char r = Serial.read();
  87.       Serial.write(r);
  88.       client.write(r);}
  89.     }
  90.   }]

Who is online

Users browsing this forum: No registered users and 93 guests