Skip to main content
Bumped by Community user
edited title
Link
Juraj
  • 18.3k
  • 4
  • 32
  • 50

ESP8266 - One server sending data to multiple clients.write() doesn't work

deleted 326 characters in body
Source Link
ocrdu
  • 1.8k
  • 3
  • 12
  • 24

I have a few arduinosArduinos with ESPs, one of which is set up to measure temperature, and I'd like to have it send commands to other connected arduinosArduinos to perform various functions (open/close valves, etc.)

My server ESP code looks a little something like this.:

    #include <ESP8266WiFi.h>

    const byte ON = 2;
    const byte OFF = 1;
    int message;
    
    WiFiServer server(80);
    
    void setup()
    {
      Serial.begin(9600);
      WiFi.softAP("access", "point");
      server.begin();
    }
    
    void loop()
    {
        server.write(ON);
        delay(1000);
        server.write(OFF);
        delay(1000);
      }
    }

My client code looks a little something like this:

    #include <ESP8266WiFi.h>
    
    const byte ON = 2;
    const byte OFF = 1;
    
    byte ip[] = {192, 168, 4, 1};
    WiFiClient client;
    
    int message;
    
    void setup() {
      Serial.begin(9600);
      // connect to wifi.
      WiFi.mode(WIFI_STA);
      WiFi.begin("access", "point");
    
      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
      }
    
      // Now connect to server object
      if (client.connect(ip, 80) == 1) {
        Serial.println("Connected");
      } else {
        Serial.println("Not Connected");
      }
      delay(10000);
    
    }   
    
    void loop() {
      if (client.connected()) {
        Serial.println("Waiting for Data");
        while(!client.available()) {}
        String message = client.read();
        Serial.println(message);
      }
    }

The client code does connect to the server, and getgets stuck at the "Waiting for Data" line, never getting out of the while (!client.available)while (!client.available) loop, so it clearly isn't receiving anything.

I'm sure I must look super dumb here, can someone help me understand the proper way to do what I want to do? Stuff I have seen online is primarily client -> server communication, do. Do I HAVE to ping the server with something in order to receive something back?

Any help is appreciated. This is for a big project and I hope that I can make it all work.

I have a few arduinos with ESPs, one of which is set up to measure temperature, and I'd like to have it send commands to other connected arduinos to perform various functions (open/close valves etc.)

My server ESP code looks a little something like this.

    #include <ESP8266WiFi.h>

    const byte ON = 2;
    const byte OFF = 1;
    int message;
    
    WiFiServer server(80);
    
    void setup()
    {
      Serial.begin(9600);
      WiFi.softAP("access", "point");
      server.begin();
    }
    
    void loop()
    {
        server.write(ON);
        delay(1000);
        server.write(OFF);
        delay(1000);
      }
    }

My client code looks a little something like this:

    #include <ESP8266WiFi.h>
    
    const byte ON = 2;
    const byte OFF = 1;
    
    byte ip[] = {192, 168, 4, 1};
    WiFiClient client;
    
    int message;
    
    void setup() {
      Serial.begin(9600);
      // connect to wifi.
      WiFi.mode(WIFI_STA);
      WiFi.begin("access", "point");
    
      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
      }
    
      //Now connect to server object
      if (client.connect(ip, 80) == 1) {
        Serial.println("Connected");
      } else {
        Serial.println("Not Connected");
      }
      delay(10000);
    
    }   
    
    void loop() {
      if (client.connected()) {
        Serial.println("Waiting for Data");
        while(!client.available()){}
        String message = client.read();
        Serial.println(message);
      }
    }

The client code does connect to the server, and get stuck at the "Waiting for Data" line, never getting out of the while (!client.available) loop, so it clearly isn't receiving anything.

I'm sure I must look super dumb here, can someone help me understand the proper way to do what I want to do? Stuff I have seen online is primarily client -> server communication, do I HAVE to ping the server with something in order to receive something back?

Any help is appreciated. This is for a big project and I hope that I can make it all work.

I have a few Arduinos with ESPs, one of which is set up to measure temperature, and I'd like to have it send commands to other connected Arduinos to perform various functions (open/close valves, etc.)

My server ESP code looks something like this:

#include <ESP8266WiFi.h>

const byte ON = 2;
const byte OFF = 1;
int message;
    
WiFiServer server(80);
    
void setup()
{
  Serial.begin(9600);
  WiFi.softAP("access", "point");
  server.begin();
}
    
void loop()
{
  server.write(ON);
  delay(1000);
  server.write(OFF);
  delay(1000);
}

My client code looks something like this:

#include <ESP8266WiFi.h>
    
const byte ON = 2;
const byte OFF = 1;
byte ip[] = {192, 168, 4, 1};
WiFiClient client;
int message;
    
void setup() {
  Serial.begin(9600);
  // connect to wifi.
  WiFi.mode(WIFI_STA);
  WiFi.begin("access", "point");
    
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }
    
  // Now connect to server object
  if (client.connect(ip, 80) == 1) {
    Serial.println("Connected");
  } else {
    Serial.println("Not Connected");
  }
  delay(10000);
}   
    
void loop() {
  if (client.connected()) {
    Serial.println("Waiting for Data");
    while(!client.available()) {}
    String message = client.read();
    Serial.println(message);
  }
}

The client code does connect to the server, and gets stuck at the "Waiting for Data" line, never getting out of the while (!client.available) loop, so it clearly isn't receiving anything.

I'm sure I must look super dumb here, can someone help me understand the proper way to do what I want to do? Stuff I have seen online is primarily client -> server communication. Do I HAVE to ping the server with something in order to receive something back?

Bumped by Community user
Added language specifier for syntax highlighting and removed thanks
Source Link
#include <ESP8266WiFi.h>

const byte ON = 2;
const byte OFF = 1;
int message;

WiFiServer server(80);

void setup()
{
  Serial.begin(9600);
  WiFi.softAP("access", "point");
  server.begin();
}

void loop()
{
    server.write(ON);
    delay(1000);
    server.write(OFF);
    delay(1000);
  }
}
    #include <ESP8266WiFi.h>

    const byte ON = 2;
    const byte OFF = 1;
    int message;
    
    WiFiServer server(80);
    
    void setup()
    {
      Serial.begin(9600);
      WiFi.softAP("access", "point");
      server.begin();
    }
    
    void loop()
    {
        server.write(ON);
        delay(1000);
        server.write(OFF);
        delay(1000);
      }
    }
#include <ESP8266WiFi.h>

const byte ON = 2;
const byte OFF = 1;

byte ip[] = {192, 168, 4, 1};
WiFiClient client;

int message;

void setup() {
  Serial.begin(9600);
  // connect to wifi.
  WiFi.mode(WIFI_STA);
  WiFi.begin("access", "point");

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }

  //Now connect to server object
  if (client.connect(ip, 80) == 1) {
    Serial.println("Connected");
  } else {
    Serial.println("Not Connected");
  }
  delay(10000);

}   

void loop() {
  if (client.connected()) {
    Serial.println("Waiting for Data");
    while(!client.available()){}
    String message = client.read();
    Serial.println(message);
  }
}
    #include <ESP8266WiFi.h>
    
    const byte ON = 2;
    const byte OFF = 1;
    
    byte ip[] = {192, 168, 4, 1};
    WiFiClient client;
    
    int message;
    
    void setup() {
      Serial.begin(9600);
      // connect to wifi.
      WiFi.mode(WIFI_STA);
      WiFi.begin("access", "point");
    
      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
      }
    
      //Now connect to server object
      if (client.connect(ip, 80) == 1) {
        Serial.println("Connected");
      } else {
        Serial.println("Not Connected");
      }
      delay(10000);
    
    }   
    
    void loop() {
      if (client.connected()) {
        Serial.println("Waiting for Data");
        while(!client.available()){}
        String message = client.read();
        Serial.println(message);
      }
    }

Any help is appreciated. This is for a big project and I hope that I can make it all work. Thank you all very much!

#include <ESP8266WiFi.h>

const byte ON = 2;
const byte OFF = 1;
int message;

WiFiServer server(80);

void setup()
{
  Serial.begin(9600);
  WiFi.softAP("access", "point");
  server.begin();
}

void loop()
{
    server.write(ON);
    delay(1000);
    server.write(OFF);
    delay(1000);
  }
}
#include <ESP8266WiFi.h>

const byte ON = 2;
const byte OFF = 1;

byte ip[] = {192, 168, 4, 1};
WiFiClient client;

int message;

void setup() {
  Serial.begin(9600);
  // connect to wifi.
  WiFi.mode(WIFI_STA);
  WiFi.begin("access", "point");

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }

  //Now connect to server object
  if (client.connect(ip, 80) == 1) {
    Serial.println("Connected");
  } else {
    Serial.println("Not Connected");
  }
  delay(10000);

}   

void loop() {
  if (client.connected()) {
    Serial.println("Waiting for Data");
    while(!client.available()){}
    String message = client.read();
    Serial.println(message);
  }
}

Any help is appreciated. This is for a big project and I hope that I can make it all work. Thank you all very much!

    #include <ESP8266WiFi.h>

    const byte ON = 2;
    const byte OFF = 1;
    int message;
    
    WiFiServer server(80);
    
    void setup()
    {
      Serial.begin(9600);
      WiFi.softAP("access", "point");
      server.begin();
    }
    
    void loop()
    {
        server.write(ON);
        delay(1000);
        server.write(OFF);
        delay(1000);
      }
    }
    #include <ESP8266WiFi.h>
    
    const byte ON = 2;
    const byte OFF = 1;
    
    byte ip[] = {192, 168, 4, 1};
    WiFiClient client;
    
    int message;
    
    void setup() {
      Serial.begin(9600);
      // connect to wifi.
      WiFi.mode(WIFI_STA);
      WiFi.begin("access", "point");
    
      while (WiFi.status() != WL_CONNECTED) {
        delay(500);
      }
    
      //Now connect to server object
      if (client.connect(ip, 80) == 1) {
        Serial.println("Connected");
      } else {
        Serial.println("Not Connected");
      }
      delay(10000);
    
    }   
    
    void loop() {
      if (client.connected()) {
        Serial.println("Waiting for Data");
        while(!client.available()){}
        String message = client.read();
        Serial.println(message);
      }
    }

Any help is appreciated. This is for a big project and I hope that I can make it all work.

Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Source Link
Loading