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.