Bluetooth Communication Questions

Currently, I get a value every second from the gps sensor. The received value is being transmitted to the mobile phone through Bluetooth communication. At the same time, I want to send a certain price to Arduino from my cell phone. But this doesn't work. I thought the reason was that Bluetooth communication could not be sent and received at the same time. I want to find out if what I thought is right and how to solve it.

Two way Bluetooth traffic is common enough, so it is probably just a code problem, and likely a simple one, but who would ever know?

#include <TinyGPS.h>
TinyGPS gps;

double current_latitude, current_longitude;
double destination_latitude, destination_longitude;
string line="";

void setup() {
  Serial.begin(9600);
  Serial2.begin(9600); //bluetooth(HC-06)
  Serial1.begin(9600); // gps sensor(Neo-7M)
 
}

void loop() {
  gps_out(); 
  bt_in(); 
  
}

void gps_out(){ 
  bool newData = false;
  unsigned long chars;
  unsigned short sentences, failed;

   for (unsigned long start = millis(); millis() - start < 1000;)
  {
    while (Serial1.available())
    {
      char c = Serial1.read();
       //Serial.write(c); 
      if (gps.encode(c)) 
        newData = true;
    }
  }

  if (newData)
  {
    float flat, flon;
    unsigned long age;
    gps.f_get_position(&flat, &flon, &age);
    Serial.print("LAT=");
    Serial.print(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat, 5);
    Serial.print(" LON=");
    Serial.println(flon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon, 5); 
    Serial2.print(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat, 5);
    Serial2.print(",");
    Serial2.print(flon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon, 5); 
    delay(1000);
  }
}

void bt_in(){
  if(Serial2.available()) { 
   line=Serial2.readStringUntil('\n');
    
    String destination_latitude = line.substring(0, 8);
    String destination_longitude = line.substring(8);
      
      
    Serial.print("lat:");    
    Serial.print(destination_latitude);
    Serial.print(",");
    Serial.print("long:");
    Serial.println(destination_longitude);
      
}

Currently, I get a value every second from the gps sensor. The received value is being transmitted to the mobile phone through Bluetooth communication. At the same time, I want to send a certain price to Arduino from my cell phone. But this doesn't work. I thought the reason was that Bluetooth communication could not be sent and received at the same time. I want to find out if what I thought is right and how to solve it. I use arduino mega

You do not need to start a new thread all the time, stick with this one now and delete the first one.
Please refrain from all that unnecessary whitespace, it is just a bloody insult.
Clearly, there is nothing wrong with your transmission code, or the wiring. Your bt_in subrtn is not too familiar but may be quite kosher - for a while. Note that you are using capital S strings therein but, in line 5, you are defining line as string. Using capital S Strings is a bad idea as it clogs up memory, even on a Mega, but I don't think it is the root cause of your problem.

this may be of assistance.

Topics merged

Cross-posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting will result in a timeout from the forum.

In the future, please take some time to pick the forum board that best suits the topic of your question and then only post once to that forum board. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum board. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

I want to send a certain price to Arduino from my cell phone. But this doesn't work

What is the sending app on the phone? Is it configured to send a message terminated with '\n'?

Have you confirmed that without sending any gps data out, you can read the destination lat/long values with your bt_in()?

If you send the outgoing message every 10 seconds, can you read the incoming message between transmissions?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.