Page 1 of 1
EPS - ESP communication via TCP/IP
Posted: Thu Jan 09, 2020 7:55 pm
by Miksior
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!
Re: EPS - ESP communication via TCP/IP
Posted: Fri Jan 10, 2020 3:16 pm
by Thomas_01
Hallo,
I'm interested in the UDP-example. Can you write the program (the UDP parts) here in the forum.
thank you
Thomas
Re: EPS - ESP communication via TCP/IP
Posted: Fri Jan 10, 2020 6:56 pm
by Miksior
Sure: here you have the code:
Server :
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
WiFiUDP Udp;
unsigned int localUdpPort = 4210; // local port to listen on
char incomingPacket[255];
IPAddress ip(192, 168, 4, 1);
void setup()
{
Serial.begin(115200);
Serial.println();
WiFi.begin("ESP32", "123456789");
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("Connected, IP address: ");
Serial.println(WiFi.localIP());
}
void loop() {
while (WiFi.status() != WL_CONNECTED)
{
WiFi.reconnect();
delay(10000);
Serial.print(".Reconnected");
Serial.println(WiFi.localIP());
}
Udp.beginPacket(ip, localUdpPort);
Udp.write("45"); // wysyĆamy jeden bajt
Udp.endPacket();
Serial.print("Wyslalem cos\n");
delay(5000);
}
}
and the client:
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiAP.h>
#include <WiFiUdp.h>
#include <stdlib.h>
WiFiUDP Udp;
unsigned int localUdpPort = 4210; // local port to listen on
char incomingPacket[255]; // buffer for incoming packets
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.print("Setting soft-AP ... ");
Serial.println(WiFi.softAP("ESP32", "123456789") ? "Ready" : "Failed!");
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
Udp.begin(localUdpPort);
Serial.printf("Now listening at IP %s, UDP port %d\n", WiFi.localIP().toString().c_str(), localUdpPort);
}
void loop()
{
int packetSize = Udp.parsePacket();
if (packetSize)
{
// receive incoming UDP packets
Serial.printf("Received %d bytes from %s, port %d\n", packetSize, Udp.remoteIP().toString().c_str(), Udp.remotePort());
int len = Udp.read(incomingPacket, 255);
if (len > 0)
{
incomingPacket[len] = 0;
}
Serial.printf("UDP packet contents: %s\n", incomingPacket);
}
}
Re: EPS - ESP communication via TCP/IP
Posted: Mon Jan 13, 2020 7:01 pm
by Thomas_01
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