Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to achieve communication between esp to esp,i,e one acts as a serer and other acts as a client, where i am passing the string from server serial monitor to client. but client is not receiving the string.

I hv uploaded the server and client code below:

#include <ESP8266WiFi.h>
const char* ssid = "xxx";
const char* password = "xx";

WiFiServer server(80);
char inData[64];
char inChar=-1;

void setup() {
  Serial.begin(115200);
  delay(10);

  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);


  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");

  // Start the server
  server.begin();
  Serial.println("Server started");

  // Print the IP address
 Serial.println("ip address..");
 IPAddress ip(192,168,1,2);
 IPAddress gateway(192,168,1,1);
 Serial.print(F("selecting static ip to:"));
  IPAddress subnet(255,255,255,0);
  WiFi.config(ip,gateway,subnet);
  Serial.print(WiFi.localIP());
}

void loop() {  

 // Check if a client has connected
 WiFiClient client = server.available();
 // if (client.connected()) {
   //  Serial.println("client");
    //return;
  //} 
  byte numBytesAvailable= Serial.available();
   //Serial.print(numBytesAvailable);

   if(numBytesAvailable > 0)
   {client.print("hi");
    //client.flush();
      //String received = Serial.readString();
   //   client.println(received);
       //delay(100);
      // Serial.println(received);
    // client.print("hi");
      int i;
        for (i=0;i<numBytesAvailable;i++){
            inChar= Serial.read();

            inData[i] = inChar;   
        }
        inData[i] = '\0';    

    client.print(inData);

     delay(500);
  client.flush();

    Serial.println(inData);
   } 
   //Serial.println(WiFi.localIP());
  //client.print(received);
    // Read all the lines of the reply from server and print them to Serial
  /*while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.println(line);
  }*/

  //Serial.println("");

  delay(100);
}

client code:

/*
 *  This sketch sends data via HTTP GET requests to data.sparkfun.com service.
 *
 *  You need to get streamId and privateKey at data.sparkfun.com and paste them
 *  below. Or just customize this script to talk to other HTTP servers.
 *
 */

#include <ESP8266WiFi.h>
void Readled(String line);
const char* ssid     = "xx";
//const char* password = "xx";
int ledPin1 = 14;
int ledPin2 =  12;
int ledPin3 = 13;
int ledPin4 = 15;
//const char* host = "data.sparkfun.com";
//const char* streamId   = "....................";
//const char* privateKey = "....................";
String ON=String("ON");
String OFF=String("OFF");
char inData[64];
char inChar=-1;
void setup()
{
  Serial.begin(115200);
  delay(10);

  // We start by connecting to a WiFi network


 Serial.println();
 Serial.println();
 Serial.print("Connecting to ");
 Serial.println(ssid);

 WiFi.begin(ssid);

 while (WiFi.status() != WL_CONNECTED)
 {
    delay(500);
    Serial.print(".");
 }

 Serial.println("");
 Serial.println("WiFi connected");  
 Serial.println("IP address: ");

 /*Serial.println(WiFi.localIP());
 IPAddress ip(192, 168, 1, 3);
 IPAddress gateway(192, 168, 1, 1);
 Serial.print(F("Setting static ip to : ")); 
 Serial.println(ip); 
 IPAddress subnet(255, 255, 255, 0); 
 WiFi.config(ip, gateway, subnet);*/
}


void loop() 
{
  delay(200);
  //++value;

 // Serial.print("connecting to ");
  //Serial.println(WiFi.localIP());

  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  const int httpPort = 80;
  if (!client.connect("192.168.1.2", httpPort)) {
    Serial.println("connection failed");
    return;
  }


  // This will send the request to the server
client.print("hi");
 // byte num=client.available();
  // Read all the lines of the reply from server and print them to Serial
 while(client.available()){

 // client.print("hi");
    String Data = client.readStringUntil('\r');

     // delay(100);

    Serial.println(Data);
     client.flush();

    Readled(Data);
//    Serial.println(line.length());


  } 


}

}

this is the logic. please suggest me where i am going wrong

share|improve this question
1  
yes it is error free. please suggest the logic for esp-esp communication. i should get the string which i give from the server esp serial monitor in client esp serial monitor – navya Jun 15 at 6:07
    
exatly...here the problem is client.print is not working. – navya Jun 15 at 6:18
    
Do you have any example code for this. it would be helpfull – navya Jun 15 at 6:18
    
whatever i m sending through server is not reflecting in client – navya Jun 15 at 6:20
    
Please reformat your code so it's readable. – JayEye Jun 15 at 20:27

I don't like the order between two statements:

server.begin();
...
WiFi.config(ip,gateway,subnet);

Since you start the server and then modify the IP address, server may be "connected" to the wrong address. Try swapping these two statement groups - start the server after setting the IP address.

I don't know your WiFi router, but it might not like you arbitrarily changing your IP address like that.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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