GPS Logger - Error saving data

Hello, to everyone i'm writing the code of a little datalogger for a school project, i can't figure out how to save the gps data to the sd card the error that the ide gives me is 'No matching function to call to 'SDLib::File::print(TinyGPSDate&, TinyGPSTime&)'


#include <Arduino_LSM9DS1.h>
#include <Arduino_LPS22HB.h>
#include <Arduino_HTS221.h>

#include <TinyGPSPlus.h>

#include <Wire.h>
#include <SparkFun_MS5803_I2C.h>

#include <SPI.h>
#include <SD.h>

#define watersensor 9
#define podled 8

TinyGPSPlus gps;

MS5803 sensor(ADDRESS_LOW);

int nread = 0;
float xa, ya, za;                                 //Accelerometer coodinates
float xg, yg, zg;                                 //Gyroscope coodinates
float a, t, h, oldt, oldh;

double p;

float tpod;
double pressure_abs, pressure_relative, altitude_delta, pressure_baseline;
double base_altitude = 50;

const int chipSelect = 10;
Sd2Card card;



//-------------------------//


void initialization() {

  pinMode(watersensor, INPUT);
  pinMode(podled, OUTPUT);

  Wire.begin();
  Serial.println("Starting Sensor Inizalization!");
  delay (5000);


  if (!IMU.begin()) {
    Serial.println("Failed to initialize IMU!");
    while (1);
  }

  Serial.println("LSM9DS1  Sensor Inizialized");

  if (!BARO.begin()) {
    Serial.println("Failed to initialize pressure sensor!");
    while (1);
  }

  Serial.println("LPS22HB  Sensor Inizialized");

  if (!HTS.begin()) {
    Serial.println("Failed to initialize humidity temperature sensor!");
    while (1);
  }

  Serial.println("HTS221   Sensor Inizialized");

  sensor.reset();
  sensor.begin();
  pressure_baseline = sensor.getPressure(ADC_4096);

  Serial.println("MS5803   Sensor Inizialized");

  Serial.print("Accelerometer sample rate = ");
  Serial.print(IMU.accelerationSampleRate());
  Serial.println(" Hz");

  Serial.print("Gyroscope sample rate = ");
  Serial.print(IMU.gyroscopeSampleRate());
  Serial.println(" Hz");
}

void initialization_sd() {

  Serial.print("Initializing SD card...  ");

  // see if the card is present and can be initialized:

  if (!SD.begin(chipSelect)) {
    Serial.println("SD initialization failed, or not present");
    // don't do anything more:
    while (1);
  }
  Serial.println("Card initialized.");
}

void pressureRead() {

  p = BARO.readPressure();
  a = 44330 * ( 1 - pow(p / 101.325, 1 / 5.255) );
  p = p * 10.00;
}

void IMURead() {

  if (IMU.accelerationAvailable())
  {
    IMU.readAcceleration(xa, ya, za);
    IMU.readGyroscope(xg, yg, zg);
  }

}

void tempRead() {

  t    = HTS.readTemperature();
  h    = HTS.readHumidity();

  if (abs(oldt - t) >= 0.5 || abs(oldh - h) >= 1 )
  {
    oldt = t;
    oldh = h;
  }

}

void datadockPrint() {

        Serial.println("DOCK DATA LOG");

        //READ NUMBER
        Serial.print("Read Nr      : ");
        Serial.println(nread);

        //ALTITUDE
        //Serial.print("Altitude     : ");
        //Serial.print(a);
        //Serial.println(" m");

        //PRESSURE
        Serial.print("Pressure     : ");
        Serial.print(p);
        Serial.println(" mbar");

        //ACCELERATION
        Serial.print("Acceleration : ");
        Serial.print(xa);
        Serial.print('\t');
        Serial.print(ya);
        Serial.print('\t');
        Serial.println(za);

        //GYRO
        Serial.print("GyroScope    : ");
        Serial.print(xg);
        Serial.print('\t');
        Serial.print(yg);
        Serial.print('\t');
        Serial.println(zg);

        //TEMP
        Serial.print("Temperature  : ");
        Serial.print(t);
        Serial.println(" °C");

        //HUM
        Serial.print("Humidity     : ");
        Serial.print(h);
        Serial.println(" %");

        Serial.println("--------------------------------------");
}

void podRead() {


  // Read temperature from the sensor in deg C. This operation takes about
  tpod = sensor.getTemperature(CELSIUSS, ADC_4096);

  // Read pressure from the sensor in mbar.
  pressure_abs = sensor.getPressure(ADC_4096) / 10;

  // Convert abs pressure with the help of altitude into relative pressure

  //pressure_relative = (pressure_abs / pow(1 - (base_altitude / 44330.0), 5.255));

  // Taking our baseline pressure at the beginning we can find an approximate
  // change in altitude based on the differences in pressure.

  //altitude_delta = (44330.0 * (1 - pow(pressure_abs / pressure_baseline, 1 / 5.255)));
}

void datapodPrint() {

  Serial.println("POD DATA LOG");

  //READ NUMBER
  Serial.print("Read Nr      : ");
  Serial.println(nread);

  Serial.print("Temperature  : ");
  Serial.print(tpod);
  Serial.println(" °C");

  Serial.print("Pressure rel : ");

  Serial.print(pressure_abs);
  Serial.println(" mbar");

  //Serial.print("Altitude     : ");
  //Serial.println(altitude_delta);

  Serial.println("--------------------------------------");

}

float logwrite() {

  File dataFile = SD.open("log.txt", FILE_WRITE);

  if (dataFile) {

    dataFile.print(nread);
    dataFile.print(" ");

    //GPS DATA
    dataFile.print(" GPS ");
    dataFile.print(gps.date, gps.time);
    dataFile.print(",");
    dataFile.print(gps.location.lat(), gps.location.isValid(), 11, 6);
    dataFile.print(",");
    dataFile.print(gps.location.lng(), gps.location.isValid(), 12, 6);
    dataFile.print(",");
    dataFile.print(ps.altitude.meters(), gps.altitude.isValid(), 7, 2);
    dataFile.print(",");
    dataFile.print(gps.speed.kmph(), gps.speed.isValid(), 6, 2);

    //DOCK DATA
    dataFile.print(" DOCK ");
    //dataFile.print(a);
    //dataFile.print(",");
    dataFile.print(p);
    dataFile.print(",");
    dataFile.print(xa);
    dataFile.print(",");
    dataFile.print(ya);
    dataFile.print(",");
    dataFile.print(za);
    dataFile.print(",");
    dataFile.print(xg);
    dataFile.print(",");
    dataFile.print(yg);
    dataFile.print(",");
    dataFile.print(zg);
    dataFile.print(",");
    dataFile.print(t);
    dataFile.print(",");
    dataFile.print(h);

    //POD DATA
    dataFile.print(" POD ");
    dataFile.print(tpod);
    dataFile.print(",");
    dataFile.println(pressure_abs);

    dataFile.close();
    
  }

  else {
    Serial.println("ERROR WHILE SAVING DATA TO SD!");
  }

}

void gpsposition() {

    Serial.println("GPS DATA LOG");

    //READ NUMBER
    Serial.print("Read Nr      : ");
    Serial.println(nread);

    //DATE & TIME
    Serial.print("Date/Time    : ");
    printDateTime(gps.date, gps.time);
    Serial.println();

    //LATITUDE
    Serial.print("Latitude     : ");
    printFloat(gps.location.lat(), gps.location.isValid(), 11, 6);
    Serial.println();

    //LONGITUDE
    Serial.print("Longitude    : ");
    printFloat(gps.location.lng(), gps.location.isValid(), 12, 6);
    Serial.println();

    //ALTITUDE
    Serial.print("Altitude     : ");
    printFloat(gps.altitude.meters(), gps.altitude.isValid(), 7, 2);
    Serial.println();

    //SPEED
    Serial.print("Speed        : ");
    printFloat(gps.speed.kmph(), gps.speed.isValid(), 6, 2);
    Serial.println();
  
    smartDelay(1000);

    if (millis() > 5000 && gps.charsProcessed() < 10)
    Serial.print(F("No GPS data received: check wiring"));

    Serial.println("--------------------------------------");

  }

static void smartDelay(unsigned long ms) {
  unsigned long start = millis();
  do 
  {
    while (Serial1.available())
      gps.encode(Serial1.read());
  } while (millis() - start < ms);
}

static void printFloat(float val, bool valid, int len, int prec) {
  if (!valid)
  {
    while (len-- > 1)
      Serial.print('*');
    Serial.print(' ');
  }
  else
  {
    Serial.print(val, prec);
    int vi = abs((int)val);
    int flen = prec + (val < 0.0 ? 2 : 1); // . and -
    flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
    for (int i=flen; i<len; ++i)
      Serial.print(' ');
  }
  smartDelay(0);
}

static void printDateTime(TinyGPSDate &d, TinyGPSTime &t) {
  if (!d.isValid())
  {
    Serial.print(F("********** "));
  }
  else
  {
    char sz[32];
    sprintf(sz, "%02d/%02d/%02d ", d.month(), d.day(), d.year());
    Serial.print(sz);
  }
  
  if (!t.isValid())
  {
    Serial.print(F("******** "));
  }
  else
  {
    char sz[32];
    sprintf(sz, "%02d:%02d:%02d ", t.hour(), t.minute(), t.second());
    Serial.print(sz);
  }

  printInt(d.age(), d.isValid(), 5);
  smartDelay(0);
}

static void printInt(unsigned long val, bool valid, int len) {
  char sz[32] = "*****************";
  if (valid)
    sprintf(sz, "%ld", val);
  sz[len] = 0;
  for (int i=strlen(sz); i<len; ++i)
    sz[i] = ' ';
  if (len > 0) 
    sz[len-1] = ' ';
  Serial.print(sz);
  smartDelay(0);
}

  
//-------------------------//

void setup() {

  Serial.begin(115200);
  Serial1.begin(9600);
  Serial.println("Serial communication started ! ");

  initialization();
  initialization_sd();

}

void loop() {

  //1 = POD FUORI ACQUA 0=POD DENTRO ACQUA
  if (digitalRead(watersensor) == 1) {

    digitalWrite(podled, HIGH);

    //GPS READING
    gpsposition();
    
    //SENSOR READING
    pressureRead();
    IMURead();
    tempRead();
    podRead();

    //LOG DATA PRINT
    datadockPrint();
    datapodPrint();


    //SAVE LOG TO SD
    logwrite();
    
    nread++;
  }
  
  else
  {
    digitalWrite(podled, LOW);
    Serial.println("Pod out of water! Please insert into the water!");
    
    File dataFile = SD.open("log.txt", FILE_WRITE);
    dataFile.println("Pod out of water! Please insert into the water!");

    dataFile.close();
  }

    delay(2000);
  
}

I hope someone can help me, because all the other data are correctly displayed in serial monitor an sd card, but gps data is correctly shown only in serial monitor, when i call the function to save the data to the sd gives me an error

the .print() function doesn't know what to do with a TinyGPSDate and TinyGPSTime object. Look at the examples that come with the library

It shows you have to convert these items (and others) to a char array that is suitable for printing.

Ok, thank you but practically what i have to write to let it works? because how you can see if i use that function in Serial.print it works but in dataFile.print it don't works, hope you can help me :slight_smile:

This is not complete working code, but in simple terms you do something like this, when the GPS has a fix ;

  GPSLat = gps.location.lat();
  GPSLon = gps.location.lng();

  logFile.print(F("Lat,"));
  logFile.print(GPSLat, 6);
  logFile.print(F(",Lon,"));
  logFile.print(GPSLon, 6);

GPSLat an GPSLong shuould be Int,float or string?

Check the documentation here;

http://arduiniana.org/libraries/tinygpsplus/

Ok , i found everything that i need, i make some changes and now it complies later i will try to upload the code and i will let you know

Your mistake was trying to replace "printDateTime"and "printFloat" , with "datafile.print". One solution would be to change the printing functions:

static void printFloat(float val, bool valid, int len, int prec) {
static void printDateTime(TinyGPSDate &d, TinyGPSTime &t) {
static void printInt(unsigned long val, bool valid, int len) {

If they took an argument of type "Stream *" you could pass them "Serial" to write to Serial Monitor or "datafile" to write to the log. For example:

static void printDateTime(Stream *stream, TinyGPSDate &d, TinyGPSTime &t) {
  if (!d.isValid())
  {
    stream->print(F("********** "));
  }
  else
  {
    char sz[32];
    sprintf(sz, "%02d/%02d/%02d ", d.month(), d.day(), d.year());
    stream->print(sz);
  }
  
  if (!t.isValid())
  {
    stream->print(F("******** "));
  }
  else
  {
    char sz[32];
    sprintf(sz, "%02d:%02d:%02d ", t.hour(), t.minute(), t.second());
    stream->print(sz);
  }

  printInt(stream, d.age(), d.isValid(), 5);
  smartDelay(0);
}

Then you could use:
printDateTime(Serial, gps.date, gps.time);
printDateTime(datafile, gps.date, gps.time);