ESP32 wifi server with multiple clients
Posted: Tue Sep 13, 2022 7:24 pm
Hello everyone
I have a program where I configure my ESP32 as a server and it sent randomly generated data, the problem is that I can only connect 1 client at a time, what I want is that multiple clients can connect and they all receive the same information,
I put the code that I have in my ESP32 as a server
I am managing the clients with python
the client code is as follows
the idea is that all clients have the same code and receive the same data
but it only allows me to connect 1 client at a time
I have a program where I configure my ESP32 as a server and it sent randomly generated data, the problem is that I can only connect 1 client at a time, what I want is that multiple clients can connect and they all receive the same information,
I put the code that I have in my ESP32 as a server
Code: Select all
#include <WiFi.h>
const char* ssid = "SSID";
const char* password = "Password";
char data[9];
WiFiServer wifiServer(80);
void setup() {
Serial.begin(115200);
delay(1000);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Conectando a WiFi..");
}
Serial.println("Conectado a la red WiFi network");
Serial.println(WiFi.localIP());
wifiServer.begin();
}
void loop() {
WiFiClient client = wifiServer.available();
if (client) {
while(client.connected()) {
//Serial.println("Client Connected");
data[0] = random(30,50);
data[1] = random(30,50);
data[2] = random(30,50);
data[3] = random(30,50);
data[4] = random(30,50);
data[5] = random(30,50);
data[6] = random(30,50);
data[7] = random(30,50);
data[8] = random(30,50);
client.write(data,9);
Serial.println(int(data[0]));
Serial.println(int(data[1]));
Serial.println(int(data[2]));
Serial.println(int(data[3]));
Serial.println(int(data[4]));
Serial.println(int(data[5]));
Serial.println(int(data[6]));
Serial.println(int(data[7]));
Serial.println(int(data[8]));
delay(1000);
}
}
}
the client code is as follows
Code: Select all
import socket
sock = socket.socket()
host = "192.168.1.68" #ESP32 IP in local network
port = 80 #ESP32 Server Port
sock.connect((host, port))
data = ""
lista = [0,0,0,0,0,0,0,0,0]
x = 0
while True:
data = sock.recv(1)
#x = data.decode()
#y = x.split("p")
buffer = int.from_bytes(data,"big")
lista[x] = buffer
x = x + 1
if x >=9:
print(lista)
x=0
but it only allows me to connect 1 client at a time