user47312: 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.

user4143: I suspect the problem stems from the client side.  This line:
client.println(sense+"\r");  //Client's message to the server

doesn't do what you think it does.
sense is a number (200).  "\r" is a string constant located at an address in memory.  When you add the two together you are not concatenating two strings, but you are adding 200 to the address of the string constant - and then sending whatever it finds at that location.
The "\r" is actually redundant anyway, since you are using println which adds \r\n to the end of a print. However if you do want to do it manually you just need to split it into two prints:
client.print(sense);
clienr.print("\r");


47312: <p>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.</p>

<p>Here's my server code:</p>

<pre><code>#include &lt;ESP8266WiFi.h&gt;
#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&lt;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&lt;Max_Clients ; ++i){                           //For available clients
  if (NULL != clients[i] &amp;&amp; clients[i]-&gt;available()){    
    String request = clients[i]-&gt;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]-&gt;println("To client 1.\r");
    }
    if (i == 1){
      analogWrite(D2, duty[i]);
      clients[i]-&gt;println("To client 2.\r");
    }
    if (i == 2){
      analogWrite(D3, duty[i]);
      clients[i]-&gt;println("To client 3.\r");
    }

    clients[i]-&gt;flush();                                    //Waiting for transmission to complete
    clients[i]-&gt;stop();
   }   
  } 
 }                                                                 //void loop closure
</code></pre>

<p>Here's my client code (which will be same for all clients):</p>

<pre><code>#include &lt;ESP8266WiFi.h&gt;
#include &lt;ESP8266HTTPClient.h&gt;

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");   
    }
  }
</code></pre>

<p>On uploading this code, the Serial Monitor at the server shows:</p>

<blockquote>
  <p>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</p>
</blockquote>

<p>While the Serial Monitor at the client shows:</p>

<blockquote>
  <p>Connecting...
  Connecting...
  Connecting...
  Connecting...
  Connecting...
  Connecting...
  Connecting...
  Connected.
  from server: To client 1.</p>
</blockquote>

<p>I have no clue of the source of this error. Any help is appreciated.</p>

<p>Thanks in advance.</p>
 4143: <p>I suspect the problem stems from the client side.  This line:</p>

<pre><code>client.println(sense+"\r");  //Client's message to the server
</code></pre>

<p>doesn't do what you think it does.</p>

<p><code>sense</code> is a number (200).  <code>"\r"</code> is a string constant located at an address in memory.  When you add the two together you are not concatenating two strings, but you are adding 200 to the address of the string constant - and then sending whatever it finds at that location.</p>

<p>The "\r" is actually redundant anyway, since you are using <code>println</code> which adds <code>\r\n</code> to the end of a print. However if you do want to do it manually you just need to split it into two prints:</p>

<pre><code>client.print(sense);
clienr.print("\r");
</code></pre>
