1

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

#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);
    } 
  }
}

I am managing the clients with python the client code is as follows

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

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

2
  • research multicast
    – jsotola
    Sep 13, 2022 at 20:15
  • the esp32 Arduino WiFi library doesn't implement the print-to-all-clients functionality as other Arduino WiFi libraries do. maybe my TelnetStream library can help you.
    – Juraj
    Sep 14, 2022 at 4:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.