How to Fix Bluetooth Master to receive the data being sent by the slave

And we don't read Your mind, Your thoughts.

You can simply use SerialTransfer.h to automatically packetize and parse your data for inter-Arduino communication without the headace. The library is installable through the Arduino IDE and includes many examples.

Here are the library’s features:

This library:

  • can be downloaded via the Arduino IDE’s Libraries Manager (search “SerialTransfer.h”)
  • works with “software-serial” libraries
  • is non blocking
  • uses packet delimiters
  • uses consistent overhead byte stuffing
  • uses CRC-8 (Polynomial 0x9B with lookup table)
  • allows the use of dynamically sized packets (packets can have payload lengths anywhere from 1 to 255 bytes)
  • can transfer bytes, ints, floats, and even structs!!

Example TX Arduino Sketch:

#include "SerialTransfer.h"

SerialTransfer myTransfer;

void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);
  myTransfer.begin(Serial1);
}

void loop()
{
  myTransfer.txBuff[0] = 'h';
  myTransfer.txBuff[1] = 'i';
  myTransfer.txBuff[2] = '\n';
  
  myTransfer.sendData(3);
  delay(100);
}

Example RX Arduino Sketch:

#include "SerialTransfer.h"

SerialTransfer myTransfer;

void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);
  myTransfer.begin(Serial1);
}

void loop()
{
  if(myTransfer.available())
  {
    Serial.println("New Data");
    for(byte i = 0; i < myTransfer.bytesRead; i++)
      Serial.write(myTransfer.rxBuff[i]);
    Serial.println();
  }
  else if(myTransfer.status < 0)
  {
    Serial.print("ERROR: ");
    Serial.println(myTransfer.status);
  }
}

Robin2:
At this stage I am very confused about what is happening where.

Is the code in Reply #18 for the slave (I presume that is what you are calling the robot) or is it for the master? When you have master and slave codes you really need to post both of them every time.

Is the GPS attached to the slave? What serial port is it connected to? How do you know its data is not being received? And do you mean the slave can’t see the GPS data or the master can’t see the GPS data?

A List showing what everything is connected to on both master and slave would be very useful.

Keep in mind that you are thinking about this and working on it for hours. We only drop in occasionally for a few minutes at a time. We only know what you tell us. We can’t see your workbench.

…R

Sorry for late reply…was in class during day.

I have two systems communicating.
System 1: BLUETOOTH SLAVE
This system has two GPS modules separated 10 meters apart. The two GPS modules get coordinates of the points they’re placed.
A configured Bluetooth slave sends these coordinates to the Bluetooth master which is System 2.

System 2: BLUETOOTH MASTER.
The system has a single GPS module with a compass. The GPS module on the system is used to determine the location of the autonomous car. The compass controls the servo for the heading.
System 2 also has a line follower system that controls the start and stops. The GPS has n input on start and stop and does not interfere with driving the motor.

So the Issue yesterday was the GPS module on system 2(master) wasn’t showing any data on the serial monitor despite getting a fix.

Here is my code for the GPS module and the compass combined.

// Imports
#include <Servo.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_HMC5883_U.h>
#include <Servo.h>
#include <TinyGPS++.h>
//#include <TinyGPS.h>
// You must then add your 'Declination Angle' to the compass, which is the 'Error' of the magnetic field in your location.
// Find yours here: http://www.magnetic-declination.com/
// Mine is: 23° 4' E (Positive),
#define DECLINATION_ANGLE +0.34f


#define COMPASS_OFFSET 0.35f


#define GPS_UPDATE_INTERVAL 1000


#define GPS_WAYPOINT_TIMEOUT 25

// Struct to combine our coordinates into one struct for ease of use
struct GeoLoc {
  float lat;
  float lon;
};
boolean newData = true;
//double latitude, longitude;
//double lat, lng;
const byte maxDataLength = 100;
const byte numChars = 32;
char receivedChars[numChars];

boolean newdata = false;

float lat;
float  lon;
// GPS
//TinyGPS gps;
TinyGPSPlus gps;


// servomotor
Servo myservo;

bool enabled = true;


/* Compass */
Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);
long data;
GeoLoc checkGPS() {
  Serial.println("Reading onboard GPS: ");
  Serial2.begin(9600); // connect gps sensor
 
 while (Serial2.available())
  {
    data = Serial2.read();
    if (gps.encode(data) > 0)
    {
      lat = (gps.location.lat());
      lon = (gps.location.lng());
      
      GeoLoc aircraftLoc;
  aircraftLoc.lat = lat;
  aircraftLoc.lon = lon;
  
      Serial.print("latitude:");
      Serial.println(lat, 7);
      Serial.print("longitude:");
      Serial.println(lon, 7);
    
    }
  } 
}

void displayCompassDetails(void)
{
  sensor_t sensor;
  mag.getSensor(&sensor);
  Serial.println("------------------------------------");
  Serial.print  ("Sensor:       "); Serial.println(sensor.name);
  Serial.print  ("Driver Ver:   "); Serial.println(sensor.version);
  Serial.print  ("Unique ID:    "); Serial.println(sensor.sensor_id);
  Serial.print  ("Max Value:    "); Serial.print(sensor.max_value); Serial.println(" uT");
  Serial.print  ("Min Value:    "); Serial.print(sensor.min_value); Serial.println(" uT");
  Serial.print  ("Resolution:   "); Serial.print(sensor.resolution); Serial.println(" uT");
  Serial.println("------------------------------------");
  Serial.println("");
  delay(500);
}

#ifndef DEGTORAD
#define DEGTORAD 0.0174532925199432957f
#define RADTODEG 57.295779513082320876f
#endif

float geoBearing(struct GeoLoc &a, struct GeoLoc &b) {
  float y = sin(b.lon - a.lon) * cos(b.lat);
  float x = cos(a.lat) * sin(b.lat) - sin(a.lat) * cos(b.lat) * cos(b.lon - a.lon);
  return atan2(y, x) * RADTODEG;
}

float geoDistance(struct GeoLoc &a, struct GeoLoc &b) {
  const float R = 6371000; // radius of earth in metres
  float p1 = a.lat * DEGTORAD;
  float p2 = b.lat * DEGTORAD;
  float dp = (b.lat - a.lat) * DEGTORAD;
  float dl = (b.lon - a.lon) * DEGTORAD;

  float x = sin(dp / 2) * sin(dp / 2) + cos(p1) * cos(p2) * sin(dl / 2) * sin(dl / 2);
  float y = 2 * atan2(sqrt(x), sqrt(1 - x));

  // returns distance in meters
  return R * y;
}

float geoHeading()
{
  /* Get a new sensor event */
  sensors_event_t event;
  mag.getEvent(&event);


  float heading = atan2(event.magnetic.y, event.magnetic.x);

  // Offset
  heading -= DECLINATION_ANGLE;
  heading -= COMPASS_OFFSET;


  if (heading < 0)
    heading += 2 * PI;


  if (heading > 2 * PI)
    heading -= 2 * PI;


  float headingDegrees = heading * 180 / M_PI;

  // Map to -180 - 180
  while (headingDegrees < -180) headingDegrees += 360;
  while (headingDegrees >  180) headingDegrees -= 360;

  return headingDegrees;
}



void controlservo(int direction)
{
  int headingAngle;
  float headingDegrees;
  headingAngle = headingDegrees;
  if (headingAngle == 0) {
    myservo.write(87);
  }
  else if (headingAngle == headingDegrees) {
    myservo.write(headingDegrees);
  }
}



void driveTo(struct GeoLoc &loc, int timeout)
{
  Serial2.read();
  GeoLoc aircraftLoc = checkGPS();
  Serial1.read();

  if (aircraftLoc.lat != 0 && aircraftLoc.lon != 0 && enabled) {
    float distance = 0;
    //Start move loop here
    do {
      Serial2.read();
      aircraftLoc = checkGPS();
      Serial1.read();

      distance = geoDistance(aircraftLoc, loc);
      float bearing = geoBearing(aircraftLoc, loc) - geoHeading();

      Serial.print("Distance: ");
      Serial.println(distance);

      Serial.print("Bearing: ");
      Serial.println(geoBearing(aircraftLoc, loc));

      Serial.print("Heading: ");
      Serial.println(geoHeading());


    }
    while (distance > 1.0 && enabled && timeout > 0);


  }
}

void setupCompass()
{
  /* Initialise the compass */
  if (!mag.begin())
  {
    /* There was a problem detecting the HMC5883 ... check your connections */
    Serial.println("Ooops, no HMC5883 detected ... Check your wiring!");
    while (1);
  }

  /* Display some basic information on this sensor */
  displayCompassDetails();
}

void setup()
{
  // Attaching servo
  myservo.attach(13);



  //Debugging via serial
  Serial.begin(115200);

  // Compass
  setupCompass();

  //GPS
  Serial2.begin(9600);
  
  //Bluetooth
 
  Serial1.begin(9600);
  Serial.println("ATcommand");  //ATcommand Start



}
void loop (){
   recvWithStartEndMarkers();
    showNewData();
   
}
void showNewData() {
    if (newData) {
      Serial.println(receivedChars);
   delay(100);
         }
   
        newdata = false;
}

void recvWithStartEndMarkers()
{
     static boolean recvInProgress = false;
     static byte ndx = 0;
     char startMarker = '<';
     char endMarker = '>';
     char rc;
 
     if (Serial1.available() > 0)
     {
          rc = Serial1.read();
          if (recvInProgress == true)
          {
               if (rc != endMarker)
               {
                    receivedChars[ndx] = rc;
                    ndx++;
                    if (ndx > maxDataLength) { ndx = maxDataLength; }
               }
               else
               {
                     receivedChars[ndx] = '\0'; // terminate the string
                     recvInProgress = false;
                     ndx = 0;
                     newData = true;
               }
          }
          else if (rc == startMarker) { recvInProgress = true; }
     }
 
}

Here is the Slave code

#include <TinyGPS++.h>
TinyGPSPlus gps1;
TinyGPSPlus gps2;
double latitude, longitude;
double lat, lng;

int potValue;
#define trigPin  11
#define echoPin  12
#define buzzer  7
void setup() {
 Serial.begin(115200);
  Serial.println("The GPS Received Signal:");
  Serial1.begin(9600); // connect gps sensor
  Serial2.begin(9600); // connect gps sensor
Serial3.begin(9600); //Bluetooth slave

pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzer, OUTPUT);

}
long data;
void loop() {
   
  while (Serial1.available())
  {
    data = Serial1.read();
    if (gps1.encode(data) > 0)
    {
      latitude = (gps1.location.lat());
      longitude = (gps1.location.lng());
      Serial.print("latitude:");
      Serial.println(latitude, 7);
      Serial.print("longitude:");
      Serial.println(longitude, 7);
   
    }
  }

  while (Serial2.available())
  {
    data = Serial2.read();
    if (gps2.encode(data) > 0)
    {
      lat = (gps2.location.lat());
      lng = (gps2.location.lng());
      Serial.print("lat:");
      Serial.println(lat, 7);
      Serial.print("long:");
      Serial.println(lng, 7);
     
    }
   
  }
 long duration, distance;
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
  duration=pulseIn(echoPin, HIGH);
  distance =(duration/2)/29.1;
  Serial.print(distance);
  Serial.println("CM");
  delay(10);
  potValue =distance;
potValue =map(potValue, 0, 1023, 0, 180);

 
   if((distance>=20) && (distance<=100))
 {
   digitalWrite(buzzer, HIGH);
  delay(100);
  Serial.println('distance');
 }

 else if((distance>200))
 {

   digitalWrite(buzzer,LOW);
     Serial3.print('<');
     Serial3.print("latitude:");
   Serial3.print(latitude, 7);
   Serial3.print(','); // a comma
   Serial3.print("longitude:");
   Serial3.print(longitude, 7);
   Serial3.print('>');
    delay(1000);
   
  Serial3.print('<');
  Serial3.print("lat:");
  Serial3.print(lat, 7);
  Serial3.print(','); // a comma
  Serial3.print("lng");
  Serial3.print(lng, 7);
  Serial3.print('>');
   
 
}
else if((distance<=20))
{
  digitalWrite(buzzer, LOW);
  delay(100);
  Serial.println('distance');
}
}

So the Issue yesterday was the GPS module on system 2(master) wasn't showing any data on the serial monitor despite getting a fix.

In the master code you have a function checkGPS()

  1. I do not see where that function is actually called anywhere in the program you posted.

2). Have you tested this gps function in a small stand alone program and can demonstrate that it works properly if it is actually called?

Have you tested this gps function in a small stand alone program and can demonstrate that it works properly if it is actually called?

Yes, i did a standalone to check if the single program works and it did work....but adding it to this it didnt.

alexmunala:
Yes, i did a standalone to check if the single program works and it did work....but adding it to this it didnt.

That suggests that you have a version of the program (perhaps a more complete version) that you have not posted.

If, as @cattledog suggests, you have not been calling the relevant function it is hard to see the point of posting that version of the program.

...R