Chat freely about anything...

User avatar
By vanjo9800
#38599 Hello, I want to make two ESP8266 communicate via WiFi. One of them is the Access Point, the other one is the client. I am using Arduino IDE. I managed to do it, but I face a problem. Constantly the client is disconnected from the server. I can fix that by connecting to the server every time in the loop() function, however that slowed very much the process.

So in brief my question is how to make two ESP8266 communicate over WiFi (TCP socket)?

If you need code, or additional information, I can give some.

Thank you in advance!!!
User avatar
By RichardS
#38600 Your in luck, just posted this today!

http://bit.ly/1Rk1W2Q
Attachments
esp2esp.jpg
http://bit.ly/1Rk1W2Q
esp2esp.jpg (18.4 KiB) Viewed 12917 times
User avatar
By martinayotte
#38609 I've posted those last summer :

viewtopic.php?f=6&t=4806&p=27501#p27501
viewtopic.php?f=27&t=4080#p23577

It is HTTP based, but it can also be done with plain TCP.
If yours is disconnecting all the time, it needs to be investigate.
Maybe you should provide your code.
User avatar
By vanjo9800
#38625 Thank you very much for both your answers. I already use TCP instead of HTTP, but still I have to send information from an accelerometer from the first ESP8266 to the second. Here is my code, it looks very much like yours.

The AP code:
Code: Select all#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <WiFiServer.h>
#include <Wire.h>

const char *ssid = "SumoRobot";
const char *password = "ZumoShield";

WiFiServer server(80);

void setup() {
  Serial.begin(115200);
  pinMode(BUILTIN_LED, OUTPUT);
  digitalWrite(BUILTIN_LED,LOW);
  Wire.begin();
   WiFi.softAP(ssid, password);

   IPAddress myIP = WiFi.softAPIP();
   server.begin();
}

WiFiClient client;

void loop() {
  if(!client.connected())
  {
    WiFiClient client = server.available();
  }
  if(client)
  {
    while(!client.available())
    {
      delay(1);
    }
    String s=client.readStringUntil('\r');
    if(s.length()!=0)
    {
      Serial.println(s);
      Wire.beginTransmission(7);
      Wire.write(s.c_str());
      Wire.endTransmission();
    }
    client.flush();
  }
}


And the client code:
Code: Select all#include <ESP8266WiFi.h>
#include<Wire.h>

const int MPU_addr=0x68;
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;

const char* ssid     = "SumoRobot";
const char* password = "ZumoShield";

const char* host = "192.168.4.1";

WiFiClient client;

void setup() {
  Serial.begin(115200);
  delay(100);
  Serial.println("Serial begins");
  pinMode(BUILTIN_LED, OUTPUT);
  digitalWrite(BUILTIN_LED,LOW);
  Wire.begin();
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x6B);
  Wire.write(0);
  Wire.endTransmission(true);
  delay(10);
  Serial.println("Accelerometer OK");

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.println("WIFI OK");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }
  Serial.println("Connected to Wifi");
}

String message="";

long cntAccel=0,cntSend=0;

void loop() {
  cntSend++;
  cntAccel++;
  if(cntAccel==333)
  {
    Wire.beginTransmission(MPU_addr);
    Wire.write(0x3B);
    Wire.endTransmission(false);
    Wire.requestFrom(MPU_addr,14,true);
    AcX=Wire.read()<<8|Wire.read();
    AcY=Wire.read()<<8|Wire.read();
    AcZ=Wire.read()<<8|Wire.read();
    Tmp=Wire.read()<<8|Wire.read();
    GyX=Wire.read()<<8|Wire.read();
    GyY=Wire.read()<<8|Wire.read();
    GyZ=Wire.read()<<8|Wire.read();
    int16_t roll=atan2(AcY,AcZ)*180/PI;
    int16_t pitch=atan2(AcX,sqrt(AcY*AcY+AcZ*AcZ))*180/PI;
    if(roll>25&&pitch>-30&&pitch<30)
    {
      message+='F';
    }
    if(roll<-38&&pitch>-30&&pitch<30)
    {
      message+='B';
    }
    if(pitch>35)
    {
      message+='R';
    }
    if(pitch<-35)
    {
      message+='L';
    }
    cntAccel=0;
  }
  if(cntSend==800)
  {
      if(!client.connected())
      {
        client.connect(host,80);
      }
      if(message.length()>0)
      {
        Serial.println(message);
        client.println(message);
        message="";
      }
      cntSend=0;
  }
}



In this case, both ESP8266 remain connected because if one of them disconnects, it goes into the if-statement and connects again, however the transfer of information is very slow and of a delay of more than 3 seconds.

Thank you in advance!!!