GPS works with library code, but it doesnt give data on my code

When I use TinyGPS++ library example code, the gps works as expected and returns data.

#include <TinyGPS++.h>


TinyGPSPlus gps;
 
void setup()
{
  Serial.begin(9600); // connect serial
  Serial1.begin(9600); // connect gps sensor
}
 
void loop()
{
while (Serial2.available())     // check for gps data
  {

    if (gps.encode(Serial2.read()))   // encode gps data
    {
      Serial.print("SATS: ");
      Serial.println(gps.satellites.value());
      Serial.print("LAT: ");
      Serial.println(gps.location.lat(), 6);
      Serial.print("LONG: ");
      Serial.printl n(gps.location.lng(), 6);
      Serial.print("ALT: ");
      Serial.println(gps.altitude.meters());
      Serial.print("SPEED: ");
      Serial.println(gps.speed.mps());
}

I have written a code which reads bmp390 sensor and gps data and logs it to an SD card as well as sends telemetery with RF module attached to Serial2 .But when I use the library to get data like below in a string (gpsdata), the if condition isn't satisfied and gpsdata string remains empty. Also, the connections are correct since I checked with library example code and it worked fine.


#include <Wire.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BMP3XX.h"
#include <TinyGPS++.h>
#include <SPI.h>
#include <SD.h>

File myFile;
float SEALEVELPRESSURE_HPA,b;
int count=0;
String gpsdata,a;
TinyGPSPlus gps;
Adafruit_BMP3XX bmp;

void setup() {

  Serial.begin(9600);
  Serial1.begin(9600);
  Serial2.begin(9600);

  SD.begin(10);
 
  
  Serial.println("Adafruit BMP388 / BMP390 test");

  if (!bmp.begin_I2C()) 
  {   
        Serial.println("Could not find a valid BMP3 sensor, check wiring!");
        Serial1.println("Could not find a valid BMP3 sensor, check wiring!");
    while (1);
  }
  // Set up oversampling and filter initialization
  bmp.setTemperatureOversampling(BMP3_OVERSAMPLING_8X);
  bmp.setPressureOversampling(BMP3_OVERSAMPLING_4X);
  bmp.setIIRFilterCoeff(BMP3_IIR_FILTER_COEFF_3);
  bmp.setOutputDataRate(BMP3_ODR_50_HZ);
 
}

void loop() { 

  if (! bmp.performReading()) {
    Serial.println("Failed to perform reading :(");
    return;
  }
  if(count==0)
  {
    bmp.pressure;
    delay(100);
    for(int i=0;i<50;i++)
  { 
    bmp.performReading();
    b+=bmp.pressure;
    Serial.println(b);
    delay(100);  
  }

  SEALEVELPRESSURE_HPA=(b/5000);
  count=1;
  }
  
  if(gps.encode(Serial2.read()))

  gpsdata= String(gps.time.hour()+5) + ':' + String(gps.time.minute()+30) + ':' + String(gps.time.second()) + ',' + String(gps.location.lat(), 4) + ',' + String(gps.location.lat(), 4) + ',' + String(gps.speed.mps()) ;
 
 
  a= String(bmp.temperature,1) + ',' + String(bmp.pressure,0) + ',' + String(bmp.readAltitude(SEALEVELPRESSURE_HPA),1);
 
  Serial1.println("BMP : ");
  Serial1.print(a);
  Serial1.println("GPS : ");
  Serial1.print(gpsdata);
  Serial1.println();
  
  Serial.println("BMP : ");
  Serial.print(a);
  Serial.println("GPS : ");
  Serial.print(gpsdata);
  Serial.println();

  myFile = SD.open("test.txt", FILE_WRITE);
  myFile.println("BMP : ");
  myFile.print(a);
  myFile.println("GPS : ");
  myFile.print(gpsdata);
  myFile.println();
  myFile.close();
  
  gpsdata="";
  a="";
  delay(200);
}

did you get rid of the check on Serial1.available() ?

Don't post snippets (Snippets R Us!)

1 Like

A post was split to a new topic: Issue with project

I didn't use Serial1.available() in this code. However I tried it earlier in the code and it didn't give data then also. I guess Serial1 isn't available. But in the library example it works fine.

Try with TXPin=4 and RXPin=3 and check if you have made correct connections i.e. tx pin of arduino to rx pin of gps and rx pin of arduino to tx pin of gps.

you can't read the Serial port and expect to get good data if there is nothing to read (you'll get -1)

you need also curly brackets to group things together when you got a fix

  if(Serial2.available() && gps.encode(Serial2.read())) {
    gpsdata= String(gps.time.hour()+5) + ':' + String(gps.time.minute()+30) + ':' + String(gps.time.second()) + ',' + String(gps.location.lat(), 4) + ',' + String(gps.location.lat(), 4) + ',' + String(gps.speed.mps()) ;
    a= String(bmp.temperature,1) + ',' + String(bmp.pressure,0) + ',' + String(bmp.readAltitude(SEALEVELPRESSURE_HPA),1);
...
  }

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