# ESP8266 - One client sending same data to multiple servers

**URL:** <https://forum.arduino.cc/t/esp8266-one-client-sending-same-data-to-multiple-servers/703934>\
**Category:** General Guidance\
**Created:** [April 8, 2021, 11:10am UTC](https://forum.arduino.cc/t/esp8266-one-client-sending-same-data-to-multiple-servers/703934 "2021-04-08T11:10:53Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![elcrni](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/elcrni/32/1013044_2.png) [@elcrni](https://forum.arduino.cc/u/elcrni)\
**Post date:** [April 8, 2021, 11:10am UTC](https://forum.arduino.cc/t/esp8266-one-client-sending-same-data-to-multiple-servers/703934/1 "2021-04-08T11:10:53Z")

</div>

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:

```cpp
#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

---

<div class="post-metadata">

**Author:** ![Juraj](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/juraj/32/238785_2.png) [@Juraj](https://forum.arduino.cc/u/Juraj)\
**Post date:** [April 8, 2021, 11:19am UTC](https://forum.arduino.cc/t/esp8266-one-client-sending-same-data-to-multiple-servers/703934/2 "2021-04-08T11:19:24Z")

</div>

add client.stop() after client.print

you didn't show your server's sketch

---

<div class="post-metadata">

**Author:** ![elcrni](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/elcrni/32/1013044_2.png) [@elcrni](https://forum.arduino.cc/u/elcrni)\
**Post date:** [April 8, 2021, 11:42am UTC](https://forum.arduino.cc/t/esp8266-one-client-sending-same-data-to-multiple-servers/703934/3 "2021-04-08T11:42:05Z")

</div>

Thanks Juraj.

Client code below:

```cpp
#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

---

<div class="post-metadata">

**Author:** ![Juraj](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/juraj/32/238785_2.png) [@Juraj](https://forum.arduino.cc/u/Juraj)\
**Post date:** [April 8, 2021, 12:08pm UTC](https://forum.arduino.cc/t/esp8266-one-client-sending-same-data-to-multiple-servers/703934/4 "2021-04-08T12:08:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![elcrni](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/elcrni/32/1013044_2.png) [@elcrni](https://forum.arduino.cc/u/elcrni)\
**Post date:** [April 8, 2021, 1:10pm UTC](https://forum.arduino.cc/t/esp8266-one-client-sending-same-data-to-multiple-servers/703934/5 "2021-04-08T13:10:50Z")

</div>

Thanks Juraj,

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

many thanks,  
Alek

---

<div class="post-metadata">

**Author:** ![Juraj](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/juraj/32/238785_2.png) [@Juraj](https://forum.arduino.cc/u/Juraj)\
**Post date:** [April 8, 2021, 4:21pm UTC](https://forum.arduino.cc/t/esp8266-one-client-sending-same-data-to-multiple-servers/703934/6 "2021-04-08T16:21:28Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![elcrni](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/elcrni/32/1013044_2.png) [@elcrni](https://forum.arduino.cc/u/elcrni)\
**Post date:** [April 8, 2021, 4:48pm UTC](https://forum.arduino.cc/t/esp8266-one-client-sending-same-data-to-multiple-servers/703934/7 "2021-04-08T16:48:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Juraj](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/juraj/32/238785_2.png) [@Juraj](https://forum.arduino.cc/u/Juraj)\
**Post date:** [April 8, 2021, 5:11pm UTC](https://forum.arduino.cc/t/esp8266-one-client-sending-same-data-to-multiple-servers/703934/8 "2021-04-08T17:11:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![elcrni](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/elcrni/32/1013044_2.png) [@elcrni](https://forum.arduino.cc/u/elcrni)\
**Post date:** [April 8, 2021, 5:17pm UTC](https://forum.arduino.cc/t/esp8266-one-client-sending-same-data-to-multiple-servers/703934/9 "2021-04-08T17:17:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Juraj](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/juraj/32/238785_2.png) [@Juraj](https://forum.arduino.cc/u/Juraj)\
**Post date:** [April 8, 2021, 5:23pm UTC](https://forum.arduino.cc/t/esp8266-one-client-sending-same-data-to-multiple-servers/703934/10 "2021-04-08T17:23:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![elcrni](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/elcrni/32/1013044_2.png) [@elcrni](https://forum.arduino.cc/u/elcrni)\
**Post date:** [April 8, 2021, 5:30pm UTC](https://forum.arduino.cc/t/esp8266-one-client-sending-same-data-to-multiple-servers/703934/11 "2021-04-08T17:30:23Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![Juraj](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/juraj/32/238785_2.png) [@Juraj](https://forum.arduino.cc/u/Juraj)\
**Post date:** [April 8, 2021, 5:32pm UTC](https://forum.arduino.cc/t/esp8266-one-client-sending-same-data-to-multiple-servers/703934/12 "2021-04-08T17:32:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![elcrni](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/elcrni/32/1013044_2.png) [@elcrni](https://forum.arduino.cc/u/elcrni)\
**Post date:** [April 8, 2021, 5:39pm UTC](https://forum.arduino.cc/t/esp8266-one-client-sending-same-data-to-multiple-servers/703934/13 "2021-04-08T17:39:20Z")

</div>

on client, if i add this:

```cpp
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

---

<div class="post-metadata">

**Author:** ![elcrni](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/elcrni/32/1013044_2.png) [@elcrni](https://forum.arduino.cc/u/elcrni)\
**Post date:** [April 8, 2021, 5:57pm UTC](https://forum.arduino.cc/t/esp8266-one-client-sending-same-data-to-multiple-servers/703934/14 "2021-04-08T17:57:00Z")

</div>

if i replace this:

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

```

with this:

```cpp
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"...

---

<div class="post-metadata">

**Author:** ![Juraj](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/juraj/32/238785_2.png) [@Juraj](https://forum.arduino.cc/u/Juraj)\
**Post date:** [April 8, 2021, 6:05pm UTC](https://forum.arduino.cc/t/esp8266-one-client-sending-same-data-to-multiple-servers/703934/15 "2021-04-08T18:05:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![elcrni](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/elcrni/32/1013044_2.png) [@elcrni](https://forum.arduino.cc/u/elcrni)\
**Post date:** [April 8, 2021, 10:14pm UTC](https://forum.arduino.cc/t/esp8266-one-client-sending-same-data-to-multiple-servers/703934/16 "2021-04-08T22:14:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![elcrni](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/elcrni/32/1013044_2.png) [@elcrni](https://forum.arduino.cc/u/elcrni)\
**Post date:** [April 9, 2021, 7:11am UTC](https://forum.arduino.cc/t/esp8266-one-client-sending-same-data-to-multiple-servers/703934/17 "2021-04-09T07:11:14Z")

</div>

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

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

```

It does not work with this either.

Many thanks,  
Alek

---

<div class="post-metadata">

**Author:** ![elcrni](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/elcrni/32/1013044_2.png) [@elcrni](https://forum.arduino.cc/u/elcrni)\
**Post date:** [April 10, 2021, 3:24pm UTC](https://forum.arduino.cc/t/esp8266-one-client-sending-same-data-to-multiple-servers/703934/18 "2021-04-10T15:24:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [August 8, 2021, 3:25pm UTC](https://forum.arduino.cc/t/esp8266-one-client-sending-same-data-to-multiple-servers/703934/19 "2021-08-08T15:25:03Z")

</div>

Closed on August 8, 2021, 3:25pm UTC

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.
