1

I am trying to achieve communication between two ESP8266, ie one acts as a server and the other acts as a client, where i am passing the string from the server's serial monitor to the client. But the client is not receiving the string.

I have uploaded the server's and client's 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();
  byte numBytesAvailable= Serial.available();
  if(numBytesAvailable > 0) {
    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);
  }
  String line = client.readStringUntil('\r');
  Serial.println(line);
  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";
int ledPin1 = 14;
int ledPin2 =  12;
int ledPin3 = 13;
int ledPin4 = 15;
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: ");
}

void loop() {
  delay(200);
  // 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");
  while(client.available()) {
    String Data = client.readStringUntil('\r');
    Serial.println(Data);
    client.flush();
    Readled(Data);
  }
}

This is the logic. Please suggest me where I am going wrong.

  • 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 '16 at 6:07
  • exatly...here the problem is client.print is not working. – navya Jun 15 '16 at 6:18
  • Do you have any example code for this. it would be helpfull – navya Jun 15 '16 at 6:18
  • whatever i m sending through server is not reflecting in client – navya Jun 15 '16 at 6:20
  • Please reformat your code so it's readable. – JayEye Jun 15 '16 at 20:27
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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