GPS Phone Location to Arduino using Bluetooth

Hi,
I am trying to send my phone location to arduino using bluetooth. I am able to connect to the app and I can see lat and long on the app but it displays as all zeros when receiving the output on arduino. Below is my code. I think it is because the coordinates are being sent as two separate texts instead of together. I am not sure how to fix this.

String a;

void loop()

{

while(bluetoothSerial.available()) {

a= Serial.readString();// read the incoming data as string

Serial.println(a);

}

It seems there is a mix-up in your code. You are using both bluetoothSerial and Serial interchangeably, which could be causing the issue. If you want to send data from your phone (via Bluetooth) to your Arduino and see the latitude and longitude, you need to do the following:

  1. Set up the Bluetooth connection between the phone and Arduino using a Bluetooth module (HC-05, HC-06, etc.).

  2. In your Arduino code, use the SoftwareSerial library to create a Bluetooth connection. Make sure to define the correct RX and TX pins for your Bluetooth module. For example:

#include <SoftwareSerial.h>

SoftwareSerial bluetoothSerial(RX_PIN, TX_PIN); // Define RX_PIN and TX_PIN based on your setup

void setup() {
  Serial.begin(9600); // For debugging, connect Arduino to PC via USB
  bluetoothSerial.begin(9600); // Set the baud rate to match your Bluetooth module
}

void loop() {
  if (bluetoothSerial.available()) {
    String a = bluetoothSerial.readString(); // Read the incoming data from Bluetooth
    Serial.println(a); // Print the received data for debugging
  }
}
  1. In your MIT App Inventor app, use the BluetoothLE1 extension to connect to the Arduino over Bluetooth.

  2. When you want to send the location data (latitude and longitude) from the app to the Arduino, format it as a single string with a delimiter (e.g., a comma , or a semicolon ;). For example:

// Assuming you have obtained the latitude and longitude as variables lat and lon
String locationData = lat + "," + lon;
  1. Use the WriteBytes block from the BluetoothLE1 extension in MIT App Inventor to send the formatted locationData to the Arduino.

  2. On the Arduino side, read the incoming data from Bluetooth as a single string and then split it into latitude and longitude values using the delimiter. For example:

void loop() {
  if (bluetoothSerial.available()) {
    String a = bluetoothSerial.readString(); // Read the incoming data from Bluetooth
    Serial.println(a); // Print the received data for debugging

    // Split the received data into latitude and longitude
    int commaIndex = a.indexOf(',');
    if (commaIndex >= 0) {
      String latitudeStr = a.substring(0, commaIndex);
      String longitudeStr = a.substring(commaIndex + 1);

      // Convert the latitude and longitude strings to float values
      float latitude = latitudeStr.toFloat();
      float longitude = longitudeStr.toFloat();

      // Now you have the latitude and longitude as separate variables (latitude and longitude)
      // You can use them as needed in your Arduino code
    }
  }
}

By following these steps and properly formatting the data, you should be able to send the location data from your phone to the Arduino via Bluetooth and parse it correctly on the Arduino side.

If you recommend hc05 or hc06 modules and arduino code also written for these modules, do not recommend the BLE extension, because these modules do not support LE.

Here an example:

Thanks for your response. I am starting to get somewhere. I can now see data coming in but no comma to separate lat and long. I have been staring at this code for weeks and my brain is just not fresh.

String a;
void loop()
{
while (bluetoothSerial.available()) {

String a = bluetoothSerial.readString(); // Read the incoming data from Bluetooth

Serial.println(a); // Print the received data for debugging

// Split the received data into latitude and longitude

int commaIndex = a.indexOf(',');

 if (commaIndex != -1) {

  String latitudeStr = a.substring(0, commaIndex);

  String longitudeStr = a.substring(commaIndex+1);

  // Convert the latitude and longitude strings to float values

  float lat = latitudeStr.toFloat();

  float lon = longitudeStr.toFloat();

  // Now you have the latitude and longitude as separate variables (latitude and longitude)

}

// Reset for the next packet

started = false;

ended = false;

index = 0;

inData[index] = '\0';

GeoLoc phoneLoc;

phoneLoc.lat = Lat;

phoneLoc.lon = Long;

Serial.print(phoneLoc.lat, 7); Serial.print(", "); Serial.println(phoneLoc.lon, 7);

driveTo(phoneLoc, GPS_WAYPOINT_TIMEOUT);

}
}

image

That's because you did not send the comma from your AI2 code.
Use a three way text JOIN, comma in the middle.

Hi, this code actually stops the arduino from being able to receive gps data from the module I have. So, I can either receive data from gps module or phone. Not both

Hi,

I need help sending phone gps coordinates to arduino. I can see the Lat and long on the app, just not able to send the coordinates to arduino using bluetooth. Here is my code and blocks:

void loop()
{

  // Read all serial data available, as fast as possible
  while (bluetoothSerial.available() > 0)

  {

    char inChar = ((byte)bluetoothSerial.read());
    if (inChar == SOP)

    {

      index = 0;
      inData[index] = '\0';
      started = true;
      ended = false;

    }

    else if (inChar == EOP)

    {

      ended = true;
      break;

    }

    else

    {

      if (index < 79)

      {

        inData[index] = inChar;

        index++;

        inData[index] = '\0';
      }
    }

  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?

  if (started && ended)

  {

    char *token = strtok(inData, ",");

    boolean first = true;

    while (token)

    {

      double val = atof(token);

      token = strtok(NULL, ",");    

      if (first){
        Lat = val;

      }else{
        Long = val;

      }

      first = false;
    }

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';

    GeoLoc phoneLoc;
    phoneLoc.lat = Lat;
    phoneLoc.lon = Long;

    Serial.print(phoneLoc.lat, 7); Serial.print(", "); Serial.println(phoneLoc.lon, 7);
    driveTo(phoneLoc, GPS_WAYPOINT_TIMEOUT);
  }
}

This discussion might help. It shows how to send two numbers from an Android to an arduino. Note that in your example the latitude and longitude are just two numbers separated by commas by convention and this discussion shows how to do that. Try the example; if it works as shown you should be able to also transmitting the lat and lon you capture in your labels by adapting the example code.Bluetooth HC-06. Arduino. Send. Receive. Send text file. Multitouch. Image - #29 by Juan_Antonio. Juan Antonio previously sent you this link. Did you try it? Something like:
latlon will give you a start.

Hi, I thought i could help you and maybe you could help me. I can send the data via bluetooth comma delimited. However my phone app is not continuously sending updated and i have not figured that out. Does anyone want to see if they can help me? It seems it should update on every changr , but i walked around the block and it did not send new data.
I know i have to post what i have but i do not want to junk up the board with junk if there is noone reading this.

Welcome!

It would really help if you provided a screenshot of your relevant blocks, so we can see what you are trying to do, and where the problem may be.

To get an image of your blocks, right click in the Blocks Editor and select "Download Blocks as Image". You might want to use an image editor to crop etc. if required. Then post it here in the community.

See also

Taifun


Trying to push the limits! Snippets, Tutorials and Extensions from Pura Vida Apps by icon24 Taifun.

Welcome Bill.

When you walked around the block you probably walked outside the range of your device to talk to your Arduino using Bluetooth. Bluetooth has a limited range.

wow Such quick responses. Thanks guys for taking an interest.
This is the whole shooting match.. I have some other blocks that turn the text red or black that I "appropriated" from somewhere ,. I can work on that later.
So my thoughts were that every sensor long/lat change would send new data, but that is not what I see. SO... here is where I am. It works one time but I really need updates like every 500 millis so I don't lose control of my device. The vehicle essentially follows my phone.