EPS - ESP communication via TCP/IP

Miksior
Posts: 5
Joined: Thu Jan 09, 2020 7:52 pm

EPS - ESP communication via TCP/IP

Postby Miksior » Thu Jan 09, 2020 7:55 pm

Hello everyone!

I am looking for an example/guide how to use TCP/IP protocol to set up connection between few ESP modules.

So far i prepared UDP communication between 3 esp modules and it works, now i am looking for TCP/IP example. I would be grateful if u could help me and show me the example/code of TCP/IP implementation!

Regards!

Thomas_01
Posts: 5
Joined: Tue Dec 31, 2019 9:44 am

Re: EPS - ESP communication via TCP/IP

Postby Thomas_01 » Fri Jan 10, 2020 3:16 pm

Hallo,
I'm interested in the UDP-example. Can you write the program (the UDP parts) here in the forum.

thank you
Thomas

Miksior
Posts: 5
Joined: Thu Jan 09, 2020 7:52 pm

Re: EPS - ESP communication via TCP/IP

Postby Miksior » Fri Jan 10, 2020 6:56 pm

Sure: here you have the code:

Server :
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiUdp.h>
  3.  
  4. WiFiUDP Udp;
  5. unsigned int localUdpPort = 4210;  // local port to listen on
  6. char incomingPacket[255];
  7. IPAddress ip(192, 168, 4, 1);
  8.  
  9. void setup()
  10. {
  11.   Serial.begin(115200);
  12.   Serial.println();
  13.  
  14.   WiFi.begin("ESP32", "123456789");
  15.  
  16.   Serial.print("Connecting");
  17.   while (WiFi.status() != WL_CONNECTED)
  18.   {
  19.     delay(500);
  20.     Serial.print(".");
  21.   }
  22.   Serial.println();
  23.  
  24.   Serial.print("Connected, IP address: ");
  25.   Serial.println(WiFi.localIP());
  26. }
  27.  
  28. void loop() {
  29.  
  30. while (WiFi.status() != WL_CONNECTED)
  31. {
  32.   WiFi.reconnect();
  33.   delay(10000);
  34.   Serial.print(".Reconnected");
  35.   Serial.println(WiFi.localIP());
  36. }
  37. Udp.beginPacket(ip, localUdpPort);
  38. Udp.write("45"); // wysyłamy jeden bajt
  39. Udp.endPacket();
  40. Serial.print("Wyslalem cos\n");
  41. delay(5000);
  42. }
  43. }
and the client:
  1. #include <WiFi.h>
  2. #include <WiFiClient.h>
  3. #include <WiFiAP.h>
  4. #include <WiFiUdp.h>
  5. #include <stdlib.h>
  6.  
  7. WiFiUDP Udp;
  8. unsigned int localUdpPort = 4210;  // local port to listen on
  9. char incomingPacket[255];  // buffer for incoming packets
  10.  
  11.  
  12. void setup()
  13. {
  14.   Serial.begin(115200);
  15.   Serial.println();
  16.  
  17.   Serial.print("Setting soft-AP ... ");
  18.   Serial.println(WiFi.softAP("ESP32", "123456789") ? "Ready" : "Failed!");
  19.   IPAddress myIP = WiFi.softAPIP();
  20.   Serial.print("AP IP address: ");
  21.   Serial.println(myIP);
  22.  
  23.  
  24.   Udp.begin(localUdpPort);
  25.   Serial.printf("Now listening at IP %s, UDP port %d\n", WiFi.localIP().toString().c_str(), localUdpPort);
  26. }
  27.  
  28. void loop()
  29. {
  30.   int packetSize = Udp.parsePacket();
  31.   if (packetSize)
  32.   {
  33.     // receive incoming UDP packets
  34.     Serial.printf("Received %d bytes from %s, port %d\n", packetSize, Udp.remoteIP().toString().c_str(), Udp.remotePort());
  35.     int len = Udp.read(incomingPacket, 255);
  36.     if (len > 0)
  37.     {
  38.       incomingPacket[len] = 0;
  39.     }
  40.     Serial.printf("UDP packet contents: %s\n", incomingPacket);
  41.   }
  42. }

Thomas_01
Posts: 5
Joined: Tue Dec 31, 2019 9:44 am

Re: EPS - ESP communication via TCP/IP

Postby Thomas_01 » Mon Jan 13, 2020 7:01 pm

Hallo,

thank you for your post.
I have never worked before with UDP directly. I see your code so: The server sends and the client receives one Byte.
This is a communication in one direction. For two-way communication I have to mix client and server without the long delay() at the end of the server-loop...

I will try it and talk about my results here.


bye
Thomas

Who is online

Users browsing this forum: No registered users and 82 guests