-->
Page 1 of 1

finally i did it : two esp8266 communication

PostPosted: Mon Oct 17, 2016 4:38 pm
by islam
i want to thank every member who guided me to solve my problem
here is the result:
i wanted to make two esp8266 communication
each one can send data to the other at any time
each esp acts as a client and a server at the same time


here is the code for the first esp:
Code: Select all#include <ESP8266WiFi.h>
const char *ssid = "ESPap";
boolean toggle_pin5=0;
boolean toggle_pin4=0;
IPAddress host(192,168,4,2);
const int httpPort = 81;
unsigned long currentMillis;
unsigned long previousMillis=0;
boolean ledstate=0;
WiFiServer server(80);
WiFiClient client1;
String req="";

void setup() {
  pinMode(5, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(14, INPUT_PULLUP);
  pinMode(12, INPUT_PULLUP);
 
  Serial.begin(115200);
  delay(10);
  Serial.print("Configuring access point...");
  WiFi.mode(WIFI_AP);
  boolean result = WiFi.softAP(ssid);
 
  if(result == true) Serial.println("Ready");
  else  Serial.println("Failed!");
 
  IPAddress myIP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(myIP);
  server.begin();
  Serial.println("HTTP server started");

  }
//========================================================================================================
void loop() {

 WiFiClient client2 = server.available();  // Check if a client has connected

 currentMillis= millis();
  if((currentMillis-previousMillis)>50) { ledstate ^=1; previousMillis=currentMillis; }
  if(digitalRead(14)==LOW){
    digitalWrite(5,LOW);
    digitalWrite(4,LOW);
    toggle_pin5=0;
    toggle_pin4=0;
  }
  if(digitalRead(12)==LOW){
    client1.connect(host, httpPort);
    client1.print("pilotc\r\n");
    Serial.println("pilotc");
    delay(250);
    }
   // Read the first line of HTTP request
   if(client2.connected()){
      req = client2.readStringUntil('\r');
      Serial.println(req);
   }
  if(req=="pin5") { 
    client2.print("ok pin5\r\n");
    client2.stop();
    toggle_pin5=1;
    req="";
  }
  if(toggle_pin5){digitalWrite(5, ledstate);}
  if(req=="pin4") {
    client2.print("ok pin4\r\n");
    client2.stop();
    toggle_pin4=1;
    req="";
  } 
  if(toggle_pin4) {digitalWrite(4, ledstate);}
}



here is the code for the second esp:


Code: Select all#include <ESP8266WiFi.h>

const char* ssid = "ESPap";
IPAddress host(192,168,4,1);
const int httpPort = 80;
String req="";
WiFiServer server(81);
WiFiClient client1;

void setup() {
  pinMode(5,INPUT_PULLUP);
  pinMode(4,INPUT_PULLUP);
  pinMode(12, OUTPUT);
  digitalWrite(12,HIGH);
 
  Serial.begin(115200);
  delay(10);
  Serial.println("");
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.mode(WIFI_STA);
  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());
  server.begin();
  Serial.println("HTTP server started");


while(!client1.connect(host, httpPort)){
  Serial.println("coonection failed");
  delay(50);
}
Serial.println("connection successed");
}
//===========================================================================================================
void loop() {
  WiFiClient client2 = server.available();  // Check if a client has connected

  if(WiFi.status() != WL_CONNECTED){
    Serial.println("reconnecting");
    WiFi.begin(ssid);
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
  }
 }

   if(digitalRead(5)==LOW){
    client1.connect(host, httpPort);
    client1.print("pin5\r\n");
    Serial.println("pin5 is on");
    delay(250);
   }
   if(digitalRead(4)==LOW){
    client1.connect(host, httpPort);
    client1.print("pin4\r\n");
    Serial.println("pin4 is on");
    delay(250);
    }
   if(client1.available()){
     String line = client2.readStringUntil('\r');
     Serial.println(line);
     if(line=="ok pin5"||line=="ok pin4") {Serial.println("order done");}
   } 
   //here is the code as a server
   if(client2.connected()){
    req = client2.readStringUntil('\r');
    Serial.println(req);
    if(req=="pin12")digitalWrite(12,LOW);
  }
}

Re: finally i did it : two esp8266 communication

PostPosted: Sun Oct 30, 2016 3:44 pm
by buchacho
Thanks for sharing. Can you explain how it is doing this?

Re: finally i did it : two esp8266 communication

PostPosted: Wed Dec 14, 2016 12:20 am
by Ebad
Great! I have been searching o it, can you share how to do it ?