Logging new file every hour on Ultimate gps logger shield

Hello,

So I downloaded an example from the GPS Adafruit library for my Ultimate GPS logger, it works great and with some modification I am able to use to save the NMEA sentences to my SD. The thing is I want to create a new file every hour or so because I don’t want one massive file. I provided my sketch below. I am not the best coder so if you could please tell me where I am missing something that would be greatly appreciated. In the sketch I tried a code to make a new file every minute but it didn’t work.

I appreciate the input!

shield_sdlog-1.ino (5.15 KB)

See the guidelines in the post at the top of the forum for details on how to post code, so forum members dont have to download it,

'How to get the best out of this forum' ;

And perhaps explain what 'it didn't work' means, what happens, what errors do you see etc.

There should be code to close the file and open a new one in loop. There isn't, so everything goes to the original file you opened in setup.

This

might help. You use the same sort of code, but check for change of hour rather than the day.

#include <SPI.h>
#include <Adafruit_GPS.h>

#include <SD.h>
#include <avr/sleep.h>
#include "RTClib.h"

#include <Time.h>
RTC_Millis rtc;
Adafruit_GPS GPS(&Serial3);  //Connect RX-port 14 and TX to port 15

// Set GPSECHO to 'false' to turn off echoing the GPS data to the Serial console
// Set to 'true' if you want to debug and listen to the raw GPS sentences
#define GPSECHO  true
/* set to true to only log to SD when GPS has a fix, for debugging, keep it false */
#define LOG_FIXONLY false
#ifndef ESP8266 // Sadly not on ESP8266
bool usingInterrupt = false;
#endif

// Set the pins used
//#define chipSelect 10
#define chipSelect 53 //for MEGA

#define logPin 9
#define ledPin 13

File logfile;
uint8_t parseHex(char c) {
  if (c < '0')
    return 0;
  if (c <= '9')
    return c - '0';
  if (c < 'A')
    return 0;
  if (c <= 'F')
    return (c - 'A')+10;
}

// blink out an error code
void error(uint8_t errno) {

  while(1) {
    uint8_t i;
    for (i=0; i<errno; i++) {}
    for (i=errno; i<10; i++) {}
  }}
 String NMEA1;  //We will use this variable to hold our first NMEA sentence
 String NMEA2;  //We will use this variable to hold our second NMEA sentence
 char c;       //Used to read the characters spewing from the GPS module
 char timestamp[30];

 DateTime now;

void dateTime(uint16_t* date, uint16_t* time) {
 DateTime now = rtc.now();
 sprintf(timestamp, "%02d:%02d:%02d %2d/%2d/%2d \n", now.hour(),now.minute(),now.second(),now.month(),now.day(),now.year()-2000);
 // return date using FAT_DATE macro to format fields
 *date = FAT_DATE(now.year(), now.month(), now.day());
 *time = FAT_TIME(now.hour(), now.minute(), now.second());
}

 void setup() { 
#ifndef ESP8266
    while (!Serial); // wait for serial port to connect. Needed for native USB
#endif

  rtc.begin(DateTime(F(__DATE__), F(__TIME__)));
  SdFile::dateTimeCallback(dateTime);

  Serial.begin(115200);
  Serial.println("\r\nUltimate GPSlogger Shield");
  pinMode(logPin, OUTPUT);
  pinMode(10, OUTPUT);
  if (!SD.begin(chipSelect)) {
    Serial.println("Card init. failed!");
    error(2);
  }
  char filename[15];
  strcpy(filename, "GPSLOG00.CSV");
  for (uint8_t i = 0; i < 100; i++) {
    filename[6] = '0' + i/10;
    filename[7] = '0' + i%10;
    // create if does not exist, do not open existing, write, sync after write
    if (! SD.exists(filename)) {
      break;
    }}

  logfile = SD.open(filename, FILE_WRITE);
  if( ! logfile ) {
    Serial.print("Couldnt create ");
    Serial.println(filename);
    error(3);
  }
  Serial.print("Writing to ");
  Serial.println(filename);



  
  now = rtc.now();
  
  // connect to the GPS at the desired rate
  GPS.begin(9600);
  Serial.begin(115200);
  GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
  GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);   // 100 millihertz (once every 10 seconds), 1Hz or 5Hz update rate
  GPS.sendCommand(PGCMD_NOANTENNA);
  delay(1000);
  Serial3.println(PMTK_Q_RELEASE);
}

uint32_t timer = millis();
void loop() 
{
  DateTime now = rtc.now();
  
  readGPS();  //This is a function we define below which reads two NMEA sentences from GPS

 logfile.print(now.year(), DEC);logfile.print('/');
 logfile.print(now.month(), DEC); logfile.print("/");
 logfile.print(now.day(), DEC);
 logfile.print(F(", "));
 logfile.print(now.hour(), DEC);logfile.print(':');
 logfile.print(now.minute(), DEC);logfile.print(':');
 logfile.print(now.second(), DEC);
 logfile.print(F(", "));
 logfile.print(NMEA1); //Write first NMEA to SD card
 logfile.flush();  //Close the file



}
void readGPS(){  //This function will read and remember two NMEA sentences from GPS
  while(!GPS.newNMEAreceived()) { //Keep reading characters in this loop until a good NMEA sentence is received
  c=GPS.read(); //read a character from the GPS
  }
GPS.parse(GPS.lastNMEA());  //Once you get a good NMEA, parse it
NMEA1=GPS.lastNMEA();      //Once parsed, save NMEA sentence into NMEA1
while(!GPS.newNMEAreceived()) {  //Go out and get the second NMEA sentence, should be different type than the first one read above.
  c=GPS.read();  

  }  
    DateTime now = rtc.now();
    Serial.print(now.year(), DEC); Serial.print('/'); 
    Serial.print(now.month(), DEC); Serial.print("/");
    Serial.print(now.day(), DEC);
    Serial.print(F(", "));
    Serial.print(now.hour(), DEC); Serial.print(':');
    Serial.print(now.minute(), DEC); Serial.print(':');
    Serial.print(now.second(), DEC); //Serial.print('.');
    Serial.print(NMEA1);
    Serial.println();
    delay(200);
}

Here is my code.

Wildbill would you be able to write something that works for that? ( I don’t understand how to write code that well yet )
Any links that could lead me in that right direction?

I followed your link Nick but I don’t know how I could use it with mine. I have seen other posts where you suggest to add a Getclock function but it doesn’t exist for SD.h

You might also have seen where I apologised for the confusion I caused.
Getclock is simply my routine for checking the RTC, which is needed to determine the change of day or, in your case, change of hour. Any means of reading the RTC will do.

I appreciate the help Nick I just don't know how to incorporate it into my sketch. Could you possibly edit the sketch and post it with your edits? Or possibly show me one of yours so that I can reference it? I know it's asking a lot but I don't know how to go about it any other way.

Here is a stripped down version that makes a new file every day.

#include <OneWire.h>
#include <DallasTemperature.h>
#include <PCD8544.h>             // Nokia 5110
#include "Wire.h"                
#include <SD.h>
#include <SPI.h>                 // SD 

#define DS1307_ADDRESS 0x68

char filename[] = "00000000.CSV";
File myFile;
char dumpName[] = "00000000.CSV";
File dumpFile;

static PCD8544 lcd;

// Green group Dee Why (red or amber LED shields)
byte InThermo[8] =  {0x28, 0x69, 0xC2, 0xB0, 0x03, 0x00, 0x00, 0x9F };
byte OutThermo[8] = {0x28, 0x7A, 0x8B, 0xC0, 0x03, 0x00, 0x00, 0x2F};
byte DrainThermo[8] = {0x28, 0x54, 0xF7, 0x2D, 0x04, 0x00, 0x00, 0x68}; 
  
#define ONE_WIRE_BUS 3
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
 
byte  second, minute, hour, weekDay, day, month, year;
int k=0;

const int chipSelect = 4;

float tempC, InTemp, OutTemp, DrainTemp, diff; 

// Define the strings for our datastream IDs
char sensorId0[] = "InThermo";
char sensorId1[] = "OutThermo";
char sensorId2[] = "DrainThermo";
char strIn[8];   // export to BT Graphic
char strOut[8];  // ditto
char strDrain[8];// ditto 
char strdiff[8]; // ditto
char charBuf [13];    // assemble read file name

String readString;
String stringOne, stringTwo, stringThree, stringFour;
String stringFive, stringSix;

//_________________________________________

void setup() {
  Serial3.begin(115200);
  lcd.begin(84, 48);
     // Register the custom symbols...
  lcd.createChar(DEGREES_CHAR, degrees_glyph);
  lcd.createChar(SLASH_CHAR, slash_glyph);
  pinMode(0, INPUT_PULLUP);   // just a precaution for bluetooth  
  Serial.begin(115200);
  
  delay(300);//Wait for newly restarted system to stabilize
 
  lcd.setCursor (0,0);
  lcd.println("Init SD CARD");
  // make sure that the default chip select pin 53 is set to
  // output, even if you don't use it:
  pinMode(53, OUTPUT);//MEGA
  
    myFile = SD.open(filename, FILE_WRITE);//<<<<<<<<<<<<< OPEN
  delay(2000);
      GetClock();
      getFileName();
      lcd.setCursor(0,3); 
      lcd.println(filename);
        delay(2000);
  
    lcd.clear();
        
  //Sequence for bluetooth
  // "Evalue1,value2,value3\n" 
  //so insert three variable between four strings,E,,\n two are the same twice, 
  //to make a fourth string 
  
  stringOne = "E";
  stringTwo = ",";
  stringThree = "\n";

    running();
  }
//________________________________________________________________

void loop() {
  GetClock();
  if (today != day)
  {
   today = day;
    getFileName();
  }
  
    while (available()) 
  {
    delay(3);  
    char c = read();
    readString += c; 
  }// end while

  if (readString.length() >0) 
  {  
    getDump();   
   readString="";  
   } // end if
  
  //get the values from the DS8B20's 
  sensors.requestTemperatures();

  InTemp = sensorValue(InThermo);
  OutTemp = sensorValue(OutThermo);  
  DrainTemp = sensorValue(DrainThermo); 

  diff = OutTemp - InTemp;
  
dtostrf(InTemp,4, 2, strIn);
dtostrf(OutTemp,4, 2, strOut);
dtostrf(diff,4, 2, strdiff);

stringFour = stringOne + strIn + stringTwo + strOut + stringTwo + strdiff + stringThree;

  Serial3.println(stringFour);

  (print temps and time to lcd)

  k=k+1;  

  if (k>9 )
  {       
  myFile.print(hour);
  myFile.print(":");
  myFile.print(minute);
  myFile.print(":");
  myFile.print(second);
  myFile.print(",");

  myFile.print(InTemp);
  myFile.print(",");
  myFile.print(OutTemp);
  myFile.print(",");
  myFile.print(DrainTemp);
  myFile.print(",");
  myFile.println(diff);
   
      k=0;
  }
  delay(850);
}  // loop ends here

//sensorValue function
float sensorValue (byte deviceAddress[])
{
  tempC = sensors.getTempC (deviceAddress);
  return tempC;
}

byte bcdToDec(byte val)  {
  // Convert binary coded decimal to normal decimal bers
  return ( (val/16*10) + (val%16) );
}

void running(){
(lcd print stuff)  
  }

void getFileName(){
   myFile.close();//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>CLOSE  
sprintf(filename, "%02d%02d%02d.csv", year, month, day);
   myFile = SD.open(filename, FILE_WRITE);//<<<<<<<<<<<<< OPEN
}

void GetClock(){
  // Reset the register pointer
  Wire.beginTransmission(DS1307_ADDRESS);
  byte zero = 0x00;
  Wire.write(zero);
  Wire.endTransmission();
  Wire.requestFrom(DS1307_ADDRESS, 7);

  second = bcdToDec(Wire.read());
  minute = bcdToDec(Wire.read());
  hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
  weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
  day = bcdToDec(Wire.read());
  month = bcdToDec(Wire.read());
  year = bcdToDec(Wire.read());
}

void getDump() {
   stringSix = "2015" + readString + ".csv";
   stringSix.toCharArray(charBuf, 15);  
        myFile.close();//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>CLOSE  
  File dumpFile = SD.open(charBuf);
  if (dumpFile) 
  {
    lcd.clear();
      lcd.setCursor (0,1);
      lcd.println("DUMP FROM ");
      lcd.setCursor (0,2);
      lcd.println(charBuf);
    while (dumpFile.available())
    {
      write(dumpFile.read());
    }
    dumpFile.close();
       myFile = SD.open(filename, FILE_WRITE);//<<<<<<<<<<<<< OPEN
      lcd.clear();
      running();
  }  
  else {
    println("error opening file");
       }
}
#include "Wire.h"               
#include <SD.h>
#include <SPI.h>                 // SD
#include <Adafruit_GPS.h>
#include "RTClib.h"
#define DS1307_ADDRESS 0x68

char filename[] = "00000000.CSV";
File logfile;
char dumpName[] = "00000000.CSV";
File dumpFile;

#define ONE_WIRE_BUS 3
byte  second, minute, hour, weekDay, day, month, year;
int k=0;

const int chipSelect = 10;
char charBuf [13];    // assemble read file name

String readString;
String stringOne, stringTwo, stringThree, stringFour;
String stringFive, stringSix;

int today = 0;
 
//_________________________________________

void setup() {
  
  Serial.begin(115200);
  Serial.println("\r\nUltimate GPSlogger Shield");
  //pinMode(logPin, OUTPUT);
  pinMode(10, OUTPUT);
  delay(300);//Wait for newly restarted system to stabilize
 
  Serial.println("Init SD CARD");
  logfile = SD.open(filename, FILE_WRITE);//<<<<<<<<<<<<< OPEN
  delay(2000);
      GetClock();
      getFileName();
    
      Serial.println(filename);
        delay(2000);
 
  }
//________________________________________________________________

void loop() {
  GetClock();
  if (today != day)
  {
   today = day;
    getFileName();
  }
    while (available())
  {
    delay(3); 
    char c = read();
    readString += c;
  }// end while

  if (readString.length() >0)
  { 
    getDump();   
   readString=""; 
   } // end if 
   k=k+1; 

  if (k>9 )
  {       
  logfile.print(hour);
  logfile.print(":");
  logfile.print(minute);
  logfile.print(":");
  logfile.print(second);
  logfile.print(",");

  logfile.print(NMEA1);
  logfile.print(",");
      k=0;
  }
  delay(850);
}  // loop ends here
byte bcdToDec(byte val)  {
  // Convert binary coded decimal to normal decimal bers
  return ( (val/16*10) + (val%16) );
}

void getFileName(){
   logfile.close();//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>CLOSE 
sprintf(filename, "%02d%02d%02d.csv", year, month, day);
   logfile = SD.open(filename, FILE_WRITE);//<<<<<<<<<<<<< OPEN
}
void GetClock(){
  // Reset the register pointer
  Wire.beginTransmission(DS1307_ADDRESS);
  byte zero = 0x00;
  Wire.write(zero);
  Wire.endTransmission();
  Wire.requestFrom(DS1307_ADDRESS, 7);

  second = bcdToDec(Wire.read());
  minute = bcdToDec(Wire.read());
  hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
  weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
  day = bcdToDec(Wire.read());
  month = bcdToDec(Wire.read());
  year = bcdToDec(Wire.read());
}

void getDump() {
   stringSix = "2015" + readString + ".csv";
   stringSix.toCharArray(charBuf, 15); 
        logfile.close();//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>CLOSE 
  File dumpFile = SD.open(charBuf);
  if (dumpFile)
  {
    
      Serial.println(charBuf);
    while (dumpFile.available())
    {
      write(dumpFile.read());
    }
    dumpFile.close();
       logfile = SD.open(filename, FILE_WRITE);//<<<<<<<<<<<<< OPEN
  } 
  else {
    Serial.println("error opening file");
       }
}

I took a look at your code Nick and I took out some of the stuff that I couldn’t use in my project. My goal was to get it working with the bare minimum of the sketch but then it gave me an error message of “today” was not declared. I added an “int today=0” function just to see if it would compile and then it said “available” was not declared. How did you get over this error?

Serial3.available
?
It was just some rags lying about, and may not be relevant. Don’t expect it to compile.