0

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.)

In other words, the only communication I need is for one server to hopefully broadcast to a few clients. I don't really need to discern which client is which.

I am struggling to make sense of some of the documentation, and this is new to me.

For starters, I'm just trying to get any communication at all functional before I do the next parts.

My server device is set up as a soft access point and a WiFiServer.

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. Thank you all very much!

1

Your server doesn't handle clients. It is only a listening TCP socket. You must handle incoming TCP connections by checking if the server has a client available.

This is from ChatServer example. A telnet server to connect to with telnet terminal.

void loop() {
  // wait for a new client:
  WiFiClient client = server.available();

  // when the client sends the first byte, say hello:
  if (client) {
    if (client.connected()) {
      if (!alreadyConnected) {
        Serial.println("We have a new client");
        client.println("Hello, client!");
        alreadyConnected = true;
      }

      if (client.available() > 0) {
        // read the byte incoming from the client:
        char c = client.read();
        // echo the byte back to the client:
        client.write(c);
        // echo the byte to the server as well:
        Serial.write(c);
      }
    } else {
      alreadyConnected = false;
      client.disconnect();
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.