0

I have vn100 (VectorNav 100 IMU), 2 esp32 wroom with wifi/bt. At the end, I want to get the data from vn100 and transfer it to esp32 that is not connected to the vn100.So what is needed:

  1. Vn100 connected with wires to esp32 wroom with wifi/bt, lets name it esp1.
  2. get the data from vn100 to esp1.
  3. transfer the data from esp1 to another esp32 wroom with wifi/bt, lets name it esp2. The connection between esp1 and esp2 is wireless. Using wifi(One esp as access point and the other connects to it, further in code). I have connected the vn100 to esp1 and got the data correctly and all good. Now im trying to transfer it to esp2 but its not working.

Each packet that comes from vn100 starts with byte 0xFA that indicates start of a packet from vn100. when i make esp1 as a Access point station, the packets that come from vn100 stops coming to esp1 (no more bytee 0xFA so no more packets coming from vn100).As a result the data from vn100 stops its stream. I need help with this part.

*I tried to connect esp1 and esp2 with wifi and transfer static data such as string/random numbers (without taking the data from vn100) and it worked fine.

Code for esp1:

#include <HardwareSerial.h>
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
const char *ssid = "ssid";
const char *password = "password";


// Function declarations
void read_imu_data(void);
void check_sync_byte(void);
unsigned short calculate_imu_crc(byte data[], unsigned int length);


// Union functions for byte to float conversions
// IMU sends data as bytes, the union functions are used to convert
// these data into other data types

//first 4 bytes are the header of the output binary message packets on the serial interface.
// next 36 bytes are the payload( the data we want )
// last 2 bytes are the crc for the checksum

// 1st byte is sync(page 35 in vn manual)
// 2nd byte is Groups(page 35 in vn manual), we choose group 01 
//3rd and 4th byte is group fields (page 35 in vn manual)

// Attitude data // 12 bytes
union {float f; byte b[4];} yaw; // 4 bytes
union {float f; byte b[4];} pitch;// 4 bytes
union {float f; byte b[4];} roll;// 4 bytes

// Angular rates // 12 bytes
union {float f; byte b[4];} W_x;// 4 bytes
union {float f; byte b[4];} W_y;// 4 bytes
union {float f; byte b[4];} W_z;// 4 bytes

// Acceleration // 12 bytes
union {float f; byte b[4];} a_x;// 4 bytes
union {float f; byte b[4];} a_y;// 4 bytes
union {float f; byte b[4];} a_z;// 4 bytes


float prev_y = 0.0; 
float prev_v_y = 0.0; 
float y_acc = 0.0; 
// new update
int distance_to_board = 50;//1m
int pos_y = 0.0;//in cm
int pos_z = 0.0;//in cm
// Checksum // 2 bytes
union {unsigned short s; byte b[2];} checksum;



// Parameters
bool imu_sync_detected = false;  // check if the sync byte (0xFA) is detected
byte in[100];  // array to save data sent from the IMU


HardwareSerial EspResevier(2);
AsyncWebServer server(80);

String readlocation() {

  return (String(pos_y) + "," + String(pos_z));
}


void setup() {

  // Start Serial for printing data to the Serial Monitor
  Serial.begin(115200);
  EspResevier.begin(115200, SERIAL_8N1, 21, 22 );//RX ,TX
  EspResevier.setRxBufferSize(1024);
  //delay(10);

  EspResevier.println("$VNASY,0*XX\r\n");//Pause Async Outputs
  EspResevier.print("$VNWRG,06,0*XX\r\n"); //write register command
  EspResevier.print("$VNWRG,75,2,8,01,0128*XX\r\n");
  EspResevier.print("$VNTAR*5F\r\n");
//  EspResevier.println("$VNCMD*XX\r\n"); //open cn cmd
//  EspResevier.println("system save\r\n"); 
//  EspResevier.println("exit\r\n");
  EspResevier.println("$VNASY,1*XX\r\n");// Resume Async Outputs

//  delay (1000);
    // Setting the ESP as an access point
   Serial.println("Setting AP (Access Point)…");
  WiFi.softAP(ssid,password);
  Serial.println(WiFi.softAP(ssid, password) ? "Ready" : "Failed!");
  IPAddress IP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(IP);

    server.on("/location", HTTP_GET, [](AsyncWebServerRequest *request){
   request->send_P(200, "text/plain", readlocation().c_str());
  });
  
    // Start server
  server.begin();
  delay (1000);
  Serial.println("FINISHED SETUP");
  
}


unsigned long timer = 0;


void loop() {
    imu_sync_detected = false;
   // Check if new IMU data is available
  if (EspResevier.available()){
    //Serial.println(" THERE IS AVAILABE!");
    check_sync_byte();
  }

  // If sync byte is detected, read the rest of the data
  if (imu_sync_detected) {
    //Serial.println(" READING!");
    read_imu_data();
  }
  delay(100);
}



// Check for the sync byte (0xFA)
void check_sync_byte(void) {
  for (int i = 0; i < 6; i++) {
    if (EspResevier.available()==0){ 
      break;
    }
    EspResevier.readBytes(in, 1); // reads 1 byte from EspResevier (IMU) and puts it in "in" array.
    if (in[0] == 0xFA) {
      //Serial.println("FOUND SYNC BYTE");
      imu_sync_detected = true;
      break;
    }
  }
}


// Read the IMU bytes
void read_imu_data(void) {
  if (EspResevier.available()>=41){
  EspResevier.readBytes(in, 41);
  checksum.b[0] = in[40];
  checksum.b[1] = in[39];

  if (calculate_imu_crc(in, 39) == checksum.s) {
    for (int i = 0; i < 4; i++) {
      yaw.b[i] = in[3 + i];
      pitch.b[i] = in[7 + i];
      roll.b[i] = in[11 + i];
      W_x.b[i] = in[15 + i];
      W_y.b[i] = in[19 + i];
      W_z.b[i] = in[23 + i];
      a_x.b[i] = in[27 + i];
      a_y.b[i] = in[31 + i];
      a_z.b[i] = in[35 + i];
    }

    
    // calculating y axe movement
    int delta_y = 0;
    delta_y = (tan(0.0174533*yaw.f) * distance_to_board) - pos_y;
    pos_y += delta_y;


    // calculating z axe movement
    int delta_z = 0;
    delta_z = (tan(0.0174533*pitch.f) * distance_to_board) - pos_z;
    pos_z += delta_z;
    Serial.println(String(pos_y) + "," + String(pos_z));//z
    }
  }
}


// Calculate the 16-bit CRC for the given ASCII or binary message.
unsigned short calculate_imu_crc(byte data[], unsigned int length)
{
  unsigned int i;
  unsigned short crc = 0;
  for(i=0; i<length; i++){
    crc = (byte)(crc >> 8) | (crc << 8);
    crc ^= data[i];
    crc ^= (byte)(crc & 0xff) >> 4;
    crc ^= crc << 12;
    crc ^= (crc & 0x00ff) << 5;
  }
//  Serial.print("crc : ");
//  Serial.println(crc);
  return crc;
}

code for esp2:

#include <WiFi.h>
#include <HTTPClient.h>
const char *ssid = "ssid";
const char *password = "password";

//Your IP address or domain name with URL path
const char* serverNamePres = "http://192.168.4.1/location";

#include <Wire.h>

String location;


void setup() {
  Serial.begin(115200);
  
  WiFi.begin(ssid, password);
  Serial.println("Connecting");
  while(WiFi.status() != WL_CONNECTED) { 
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
     // Check WiFi connection status
    if(WiFi.status()== WL_CONNECTED ){
      location = httpGETRequest(serverNamePres);
      Serial.println(" Location: " + location );
    }
    else {
      Serial.println("WiFi Disconnected");
    }
  delay(100);
}

String httpGETRequest(const char* serverName) {
  WiFiClient client;
  HTTPClient http;
    
  // Your Domain name with URL path or IP address with path
  http.begin(client, serverName);
  
  // Send HTTP POST request
  int httpResponseCode = http.GET();
  
  String payload = "--"; 
  
  if (httpResponseCode>0) {
    Serial.print("HTTP Response code: ");
    Serial.println(httpResponseCode);
    payload = http.getString();
  }
  else {
    Serial.print("Error code: ");
    Serial.println(httpResponseCode);
  }
  // Free resources
  http.end();

  return payload;
}

in the code you can see 2 files :

  1. File for esp1 which is connected with wires to the vn100. Esp1 receives the data and save it in 2 variables pos_y and pos_z which i want to send to esp2. Esp1 is set as a access point so that esp2 can connect to it.

  2. File for esp2 which is connected wirelessly to esp1.

I need help with fixing this data transfer problem.Or if anyone can point to a problematic point in the connection between these 3 devices. Thanks

0 Answers0