-->
Page 1 of 1

ESP To ESP Connection (server , client)

PostPosted: Sun May 01, 2016 11:25 am
by Rotaru Andrei
Hello guys,

I'm making a project for the final year at the university and I can't make a connection between 2 esp to go right. First ESP have a accelerometer connected and I want that the other esp to read the values from the first one. Can anyone help me with a client code that I can use to read the values. Or tell me a simple way to do the connection between ESPs?

Server:
Code: Select all#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include "Wire.h"
#include "I2Cdev.h"
#include "MPU9150.h"


const char* ssid     = "testserv";
const char* password = "12345678";

ESP8266WebServer server(80);
float p,y;
int16_t yaw, pitch, roll;
int16_t gx, gy, gz;
int16_t mx, my, mz;

String webString="";
MPU9150 accelGyroMag;
unsigned long previousMillis = 0;
unsigned long previousRate;
int sampleCount;
const long interval = 100;    // interval at which to read sensor

void handle_root() {
      getIMU();       // read sensor
    webString="yaw "+String((int)y);

  server.send(200, "text/plain", webString);
  delay(100);
}


void setup() {
 
    Serial.begin(115200);
    Wire.begin();
    Serial.println("Initializing I2C devices...");
    accelGyroMag.initialize();

    Serial.println("Testing device connections...");
    Serial.println(accelGyroMag.testConnection() ? "MPU9150 connection successful" : "MPU9150 connection failed");
   
    WiFi.begin(ssid, password);
  Serial.print("\n\r \n\rWorking to connect");
 
  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("IMU Server");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
 
server.on("/", handle_root);
 
  server.begin();
  Serial.println("HTTP server started");
}

void loop() {
  server.handleClient();

}

void getIMU()
{
   unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis >= interval) {
   
    previousMillis = currentMillis;   
 
    accelGyroMag.getMotion9(&yaw, &pitch, &roll, &gx, &gy, &gz, &mx, &my, &mz);
    y= yaw  ;       
   p = pitch  ;
    // Check if any reads failed and exit early (to try again).
    if (isnan(yaw) || isnan(pitch)) {
      Serial.println("Failed to read from sensor!");
      return;
    }
  }

}

Re: ESP To ESP Connection (server , client)

PostPosted: Sun May 01, 2016 3:27 pm
by martinayotte
You just provided the WebServer code, but didn't show what the Client is doing !
Also, beware that having ESPs talking each other, the Client need to be in STA-only mode, because if both are in STA+AP, there will be clash in for the 192.168.4.1 IP address which will exist on both AP.

Re: ESP To ESP Connection (server , client)

PostPosted: Tue May 03, 2016 2:47 pm
by Rotaru Andrei
I tried to make a code for the client but I failed.I don't know how to read the values from it, I don't know the method. Thanks for reply!