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