I am trying to communicate multiple clients to a server, all using nodeMCUs. I am getting a weird run time error at the Server Serial Monitor of Arduino IDE when I upload the code.
Here's my server code:
#include <ESP8266WiFi.h>
#define Max_Clients 5
WiFiServer server(80);
WiFiClient *clients[Max_Clients] = {NULL};
String inputs [Max_Clients] = {""}; //Initializing to store the string received from clients
void setup() {
pinMode(D1,OUTPUT); //Intializing pins for showing output and input
pinMode(D2,OUTPUT);
pinMode(D3,OUTPUT);
pinMode(A0,INPUT);
Serial.begin(9600); //Start communication between the ESP8266-12E and the monitor window
WiFi.mode(WIFI_AP); //This ESP8266-12E is an AccessPoint
WiFi.softAP("NodeMCU_js", "12345678"); //Provide the (SSID, password)
IPAddress myIP = WiFi.softAPIP(); //Obtain the IP of the Server
Serial.println("Server IP is: "); //Print the IP to the monitor window
Serial.println(myIP);
server.begin(); //Start the HTTP Server
}
void loop() {
WiFiClient client = server.available(); //Check if a new Client has connected to the server
if (client) {
Serial.println("A new Client has connected :)");
for (int i=0 ; i<Max_Clients ; ++i){ //Acknowledges and sets up new clients
if (NULL == clients[i]){
clients[i] = new WiFiClient(client); //nothing in clients[i] means it is a new client
break;
}
}
}
for(int i=0 ; i<Max_Clients ; ++i){ //For available clients
if (NULL != clients[i] && clients[i]->available()){
String request = clients[i]->readStringUntil('\r');
Serial.println("The string of client "+String(i+1)); //Print client's message
inputs[i] = request;
Serial.println(inputs[i]);
int duty[4];
duty[i] = inputs[i].toInt();
if (i == 0){
analogWrite(D1, duty[i]);
clients[i]->println("To client 1.\r");
}
if (i == 1){
analogWrite(D2, duty[i]);
clients[i]->println("To client 2.\r");
}
if (i == 2){
analogWrite(D3, duty[i]);
clients[i]->println("To client 3.\r");
}
clients[i]->flush(); //Waiting for transmission to complete
clients[i]->stop();
}
}
} //void loop closure
Here's my client code (which will be same for all clients):
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
char ssid[] = "NodeMCU_js";
char password[] = "12345678";
WiFiClient client; // Creates a client that can connect to to a specified internet IP address and port as defined in client.connect()
IPAddress server(192,168,4,1);
void setup() {
pinMode(A0,INPUT);
pinMode(D6,OUTPUT);
WiFi.persistent(false);
WiFi.mode(WIFI_STA);
Serial.begin(9600); //Serial connection
WiFi.begin(ssid,password); //WiFi connection
while (WiFi.status() != WL_CONNECTED) { //Wait for the WiFI connection completion
delay(500);
Serial.println("Connecting...");
}
Serial.println("Connected.");
client.connect(server, 80); // Connection to the server
}
void loop() {
if(WiFi.status() == WL_CONNECTED){ //Check WiFi connection status
int sense = 200;
client.println(sense+"\r"); //Client's message to the server
String answer = client.readStringUntil('\r'); //Reading response from the server
Serial.println("from server: " + answer); //Printing the response
client.flush();
delay(3600000);
}
else{
Serial.println("Error in WiFi connection");
}
}
On uploading this code, the Serial Monitor at the server shows:
A new Client has connected :) The string of client 1 nt8_t* BufferedStreamDataSource::get_buffer(size_t) [with TStream = >ProgmemStream; uint8_t = unsigned char; size_t = unsigned int] The string of client 1
While the Serial Monitor at the client shows:
Connecting... Connecting... Connecting... Connecting... Connecting... Connecting... Connecting... Connected. from server: To client 1.
I have no clue of the source of this error. Any help is appreciated.
Thanks in advance.