Hi all,
I've implemented a program that involves 3 ESP8266, 2 of them as clients and the other as a server. The program below, which is for the server, creates an array of WiFiClients that will be filled as soon as any of the clients are connected to the server. Connected clients send the server a string to turn on a led. The issue that I've encountered is in the instruction
if (NULL != clients[i] && clients[i]->available() )
Even though the client is connected and has sent the string to the server, it seems that "clients*->available()" is not able to read any bite so the if statement is false.*
What am I doing wrong?
```
*#include <ESP8266WiFi.h>
IPAddress local_IP(192,168,4,22);
IPAddress gateway(192,168,4,9);
IPAddress subnet(255,255,255,0);
WiFiServer server(3000);
#define MAX_CLIENTS 2
WiFiClient *clients[MAX_CLIENTS] = { NULL };
const int ledPin =2; // the number of the LED pin
void setup()
{
Serial.begin(115200);
Serial.println();
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
Serial.print("Setting soft-AP configuration ... ");
Serial.println(WiFi.softAPConfig(local_IP, gateway, subnet) ? "Ready" : "Failed!");
Serial.print("Setting soft-AP ... ");
boolean result = WiFi.softAP("ESPsoftAP_01", "00000000");
if(result == true)
{
Serial.println("Ready");
}
else
{
Serial.println("Failed!");
}
// Start the server
server.begin();
Serial.println(F("Server started"));
}
void loop()
{
WiFiClient newClient = server.available();
// wait for a client (web browser) to connect
if (newClient)
{
Serial.println("\n[New client connected]");
for (int i=0 ; i<MAX_CLIENTS ; i++) {
if (NULL == clients[i]) {
clients[i] = new WiFiClient(newClient);
break;
}
}
for (int i=0 ; i<MAX_CLIENTS ; i++)
{
// read line by line what the client (web browser) is requesting
if (NULL != clients[i] && clients[i]->available() )
{
String line = clients[i]->readStringUntil('\r');
Serial.print(line);
if (line.equals("accendi")){
// turn LED on:
digitalWrite(ledPin, HIGH);
delay(2000);
digitalWrite(ledPin, LOW);
}
// If you want to disconnect the client here, then do this:
clients[i]->stop();
delete clients[i];
clients[i] = NULL;
Serial.println("[Client disconnected]");
}
}
delay(1);// give the web browser time to receive the data
}
}*
```