ESP8266 - One client sending same data to multiple servers

hi all,

I have a small project going on, got the code working but have a problem. I would like to have one ESP8266 as server and then just add clinets along the way, to receive the same transmission.
My code works for one client, and when i connect another client, that one does not work, and as soon as i disconnect the first one, the second one starts working.

I would like to be able to just add clients and they all connect automatically... perhaps up to 10 or 20, if possible.

My server code below:

#include <ESP8266WiFi.h>

const char *ssid = "ssid";
const char *password = "password";

int sensorValue1;        
int btn1 = 4;
static int sensorValueLast ;

void setup() {
  pinMode(btn1, INPUT_PULLUP);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
  } 
}

void loop() {
  if(digitalRead(btn1) == LOW) {
    sensorValue1 = 1;
  } else  {
    sensorValue1 = 0;
  }
  
  if ( sensorValueLast == sensorValue1 ) return ;  // leave
  sensorValueLast = sensorValue1 ;
  WiFiClient client;
  const char * host = "192.168.4.1";
  const int httpPort = 80;
  if (!client.connect(host, httpPort)) {
    return;
  }
  String url = "/data/?sensor_reading=sensor1_value";
  url.replace("sensor1_value", String(sensorValue1));
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");
}

Many thanks for any help,
Alek

add client.stop() after client.print

you didn't show your server's sketch

Thanks Juraj.

Client code below:

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>


const char *ssid      = "ssid";
const char *password  = "password";
int sensorValue1 = 0;
ESP8266WebServer server(80);

void handleSentVar() {
  if (server.hasArg("sensor_reading"))
  {
    sensorValue1 = server.arg("sensor_reading").toInt() ;
    if (sensorValue1 == 0)  {
      digitalWrite(2, HIGH);
    }
    if (sensorValue1 == 1)  {
      digitalWrite(2, LOW);
    }
  }
}

void setup() {
  WiFi.softAP(ssid, password);
  IPAddress myIP = WiFi.softAPIP();
  pinMode(2, OUTPUT);
  server.on("/data/", HTTP_GET, handleSentVar); // when the server receives a request with /data/ in the string then run the handleSentVar function
  server.begin();
}

void loop() {
  server.handleClient();
}

Many thanks,
Alek

SoftAP supports only 5 devices connected to its WiFi network. uses a WiFi Access Point to create a larger WiFi network

Thanks Juraj,

Even 5 would be of huge help, but not sure how to add those five.

many thanks,
Alek

there is nothing you should do. it should work.
what is happening?
a next client can't join the WiFi network or it can't send the HTTP request?

Well, i have 3 D1 Mini's, one is transmitter and 2 receivers.
When i plug in one receiver, it works. When i plug in the second one, it does not work. If i unplug the first receiver, the second one starts working.
If i connect both receivers and restart transmitter, it randomly connects one or the other receiver, but never both.

Not sure what the problem is...

Many thanks,
Alek

how "doesn't work"?
there are no transmitters and receivers with WiFi and TCP. all devices can transmit and receive

ok, perhaps to explain the setup a bit better.

So, when my signal generator generates a signal it gets picked up by the client (transmitter) and transmits it to the server (receiver). When the signal generator outputs HIGH, a LED on the server lights up, if the generated signal is low, LED is OFF...

As i said, once i plug in one server, it works as it should. When i connect the 2nd server, the LED is constantly ON, that is the output is constantly HIGH.

i do not know if its HTTP request problem or joining the network problem

Many thanks,
Alek

why would the LED go off if you connect a second 'client'?

you don't send 0 or 1, but "sensor1_value". toInt returns 0 and the LED is on inverting circuit

Juraj:
why would the LED go off if you connect a second 'client'?

you don't send 0 or 1, but "sensor1_value"

No, once i connect the second ESP8266 the LED on that 2nd ESP is constantly ON, the first ESP works as its supposed to work. Then, if i unplug the 1st ESP, the 2nd one picks up and works as it should.
My signal is 1Hz, pulses every second so the LEDs on all ESPs connected should blink, and not be always ON or OFF
Many thanks,
Alek

so then find out what stops the sketch. add some prints to Serial

on client, if i add this:

if (!client.connect(host, httpPort)) {
     Serial.println("connection failed");
    return;
  }

i get "connection failed" for the 2nd ESP... not sure how to debug further...

Thanks,
Alek

if i replace this:

client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");

with this:

client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Connection: close\r\n\r\n");
  unsigned long timeout = millis();
  while (client.available() == 0) {
    if (millis() - timeout > 5000) {
      Serial.println(">>> Client Timeout !");
      client.stop();
      return;
    }
  }

then no clients connect and i get a message "Client Timeout"...

I don't know what does the WebServer if you don't send a response in the request handler function.
it is possible it is stuck

i have no idea what could be the issue. It works perfectly with one connected server, but no chance to get it to work with more than one.

Any tips would be highly appreciated.

many thanks,
Alek

is this how the response should look like, in the "handleSentVar"?

server.send(200, "text/html", "Data received");

It does not work with this either.

Many thanks,
Alek

hi all,

After further research, some things i understand better, keep in mind i am very new to ESP and WIFI communication between them.

It's now clear that i would be able to send data from multiple clients to one server, BUT what i need here is to send the SAME data from one client to multiple servers, perhaps that's why my rig does not work...

Any ideas or tips would be appreciated.

Many thanks,
Alek