you could send UDP datagrams, e.g.
ESP32 code
// ESP32 UDP chat program
#include <WiFi.h>
#include <WiFiUdp.h>
// network SSID (network name). and network password.
char ssid[] = "xxxxxxx";
char pass[] = "zzzzzzz";
// UDP information
unsigned int localPort = 10000; // local port to listen on
unsigned int remotePort = 10000; // remote port to transmiit too
WiFiUDP udp;
IPAddress remoteIP(192, 168, 1, 68);; // IP will be entered as text
void setup() {
Serial.begin(115200);
while (!Serial) delay(1);
delay(1000);
Serial.println();
Serial.println("ESP32 WiFi UDP chat - send UDP datagrams ");
WiFi.mode(WIFI_STA); // Connect to Wifi network.
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) { // Wait for connection
delay(500);
Serial.print(".");
}
Serial.print("\nGateway IP address: ");
Serial.println(WiFi.gatewayIP());
Serial.print("Ethernet UDP started ");
displayIPaddress(WiFi.localIP(), localPort);
// Begin udp port
udp.begin(localPort);
Serial.print("Opening udp port ");
Serial.println(localPort);
// get remote IP address
//remoteIP = getIPaddress("\nenter remote IP address (e.g. 192.168.1.176)? ");
Serial.print("\nRemote ");
displayIPaddress(remoteIP, remotePort);
}
void loop() {
if (Serial.available()) { // if Serial text available send as a datagram
udp.begin(localPort);
char text[20] = { 0 };
Serial.readBytesUntil('\n', text, 24);
Serial.print("Transmitting to ");
displayIPaddress(remoteIP, remotePort);
Serial.println(text);
udp.beginPacket(remoteIP, remotePort); // transmit datagram
udp.print(text);
udp.endPacket();
}
int packetSize = udp.parsePacket(); // check if datagram received
if (packetSize) {
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
displayIPaddress(udp.remoteIP(), udp.remotePort());
// read the packet into packetBuffer
char packetBuffer[100] = { 0 }; // buffer to hold incoming packet,
udp.read(packetBuffer, packetSize); //UDP_TX_PACKET_MAX_SIZE); // receive datagram
Serial.print("Contents: ");
Serial.println(packetBuffer);
}
}
// read IP address from keyboard and check it
IPAddress getIPaddress(const char* prompt) {
IPAddress ip;
while (1) { // read IP (end with new line)
Serial.print(prompt);
while (Serial.available() == 0) delay(10);
char text[40] = { 0 };
Serial.readBytesUntil('\n', (char *)text, 40);
//Serial.println(text);
if (ip.fromString(text)) break; // if IP OK break while
Serial.println("\ninvalid IP try again");
}
return ip;
}
// print IPAdress and port
void displayIPaddress(const IPAddress address, unsigned int port) {
Serial.print(" IP ");
for (int i = 0; i < 4; i++) {
Serial.print(address[i], DEC);
if (i < 3) Serial.print(".");
}
Serial.print(" port ");
Serial.println(port);
}
python code
import socket
UDP_IP = "192.168.1.68"
UDP_PORT = 10000
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
print("received message: %s" % data)
ESP32 serial monitor transmittinf text
ESP32 WiFi UDP chat - send UDP datagrams
......................
Gateway IP address: 192.168.1.254
Ethernet UDP started IP 192.168.1.164 port 10000
Opening udp port 10000
Remote IP 192.168.1.68 port 10000
Transmitting to IP 192.168.1.68 port 10000
hello
Transmitting to IP 192.168.1.68 port 10000
test 1234567890
Transmitting to IP 192.168.1.68 port 10000
test abcdefghijklmnopqrs
Transmitting to IP 192.168.1.68 port 10000
Python console
Python 3.11.1 (tags/v3.11.1:a7a450f, Dec 6 2022, 19:58:39) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
====================== RESTART: C:/Python311/UP_server.py ======================
received message: b'hello\r'
received message: b'test 1234567890\r'
received message: b'test abcdefghijklmnopqrs'
received message: b't\r'