3

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 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?

1 Answer 1

1

In esp8266 Arduino before version 3, write was not implemented in WiFiServer.

In version 3 WiFiServer doesn't have write, but class ArduinoWiFiServer implements the print-to-all-clients functionality.

Documentation link: https://arduino-esp8266.readthedocs.io/en/3.1.2/esp8266wifi/server-class.html

I am the author of ArduinoWiFiServer class.

Your Answer

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

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