Development steps

Project planning and development stages

Step 2 - Add MicroSD card module

Back to project development

Add MicroSD card module

  • Write coordinates to SD Card;
              
    /*
    * Pedro Candeias  GPS Data logger
    */
   
   #include <TinyGPS++.h>
   #include <SoftwareSerial.h>
   #include <Wire.h>
   #include <Adafruit_GFX.h>
   #include <Adafruit_SSD1306.h>
   
   //Libraries for microSD card
   #include "FS.h"
   #include "SD.h"
   #include "SPI.h"
   
   #define SCREEN_WIDTH 128  // OLED display width, in pixels
   #define SCREEN_HEIGHT 64  // OLED display height, in pixels
   static const int RXPin = 20, TXPin = 21;
   static const uint32_t GPSBaud = 9600;
   const int SDPin = 5;
   
   // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
   Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
   
   // The TinyGPS++ object
   TinyGPSPlus gps;
   
   // String to hold GPS data
   String gpstext;
   
   // GPS write delay counter variables
   // Change gpsttlcount as required
   int gpscount = 0;
   int gpsttlcount = 30;
   
   // The serial connection to the GPS device
   SoftwareSerial ss(RXPin, TXPin);
   
   // Comment this define out if not connected to PC
   #define DEBUG 1
   
   #ifdef DEBUG
   #define DebugPrintln(str) Serial.println(str)
   #define DebugPrint(str) Serial.print(str)
   #define DebugPrintf(str,str2) Serial.printf(str,str2)
   #else
   #define DebugPrintln(str)
   #define DebugPrint(str)
   #define DebugPrintf(str,str2)
   #endif
   
   void initDisplay(){
    if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
     DebugPrintln(F("SSD1306 allocation failed"));
     for(;;);
    }
    delay(500);
    display.clearDisplay();
    display.setTextColor(WHITE); 
   }
   
   // Initialize SD card
   void initSDCard(){
    if (!SD.begin(SDPin)) {
     DebugPrintln("Card Mount Failed");
    return;
    }
   }
   
   // Write to the SD card
   void writeFile(fs::FS &fs, const char * path, const char * message) {
     DebugPrintf("Writing file: %sn", path);
     File dataFile = fs.open(path, FILE_WRITE);
     if(!dataFile) {
       DebugPrintln("Failed to open file for writing");
       return;
     }
     if(dataFile.print(message)) {
       DebugPrintln("File written");
     } else {
       DebugPrintln("Write failed");
     }
     dataFile.close();
   }
   
   // Append data to the SD card
   void appendFile(fs::FS &fs, const char * path, const char * message) {
       DebugPrintf("Appending to file: %sn", path);
   
     File dataFile = fs.open(path, FILE_APPEND);
     if(!dataFile) {
       DebugPrintln("Failed to open file for appending");
       return;
     }
     if(dataFile.print(message)) {
       DebugPrintln("Message appended");
     } else {
       DebugPrintln("Append failed");
     }
     dataFile.close();
   }
   
   void setup() {
     Serial.begin(9600);
     initDisplay();
     initSDCard();
     ss.begin(GPSBaud);
   
     File dataFile = SD.open("/gpslog.csv");
     if(!dataFile) {
       DebugPrintln("File doesn't exist");
       DebugPrintln("Creating file...");
       writeFile(SD, "/gpslog.csv", "Trail rn");
     } else {
       DebugPrintln("File already exists");  
     }
     dataFile.close();
   }
   
   void loop() {
     boolean newData = false;
     for (unsigned long start = millis(); millis() - start < 1000;) {
       while (ss.available() > 0) {<
     // See if we have a complete GPS data string
         if (displayInfo() != "0") {
           // Get GPS string
           gpstext = displayInfo();
           // Check GPS Count
           DebugPrintln(gpscount);
           if (gpscount == gpsttlcount) {
             DebugPrintln(gpstext);
             appendFile(SD, "/gpslog.csv", gpstext.c_str());
           }
           // Increment GPS Count
           gpscount = gpscount + 1;
           if (gpscount > gpsttlcount) {
             gpscount = 0;
           }
         }
           // End GPS file
           newData = true;
         }
       }
     }
   
     //If newData is true
     if (newData == true) {
       newData = false;
      
       DebugPrint("Satellites: ");
       DebugPrintln(gps.satellites.value());
       displayCoordinates();
     } else {
       display.clearDisplay();
       display.setTextColor(SSD1306_WHITE);
       display.setCursor(0, 0);
       display.setTextSize(3);
       display.print("No Data");
       display.display();
     }
   }
   
   void displayCoordinates() {
     display.clearDisplay();
     display.setTextColor(SSD1306_WHITE);
   
     if (gps.location.isValid() == 1) {
       //String gps_speed = String(gps.speed.kmph());
       display.setTextSize(1);
   
       display.setCursor(25, 5);
       display.print("Lat: ");
       display.setCursor(50, 5);
       display.print(gps.location.lat(), 6);
   
       display.setCursor(25, 20);
       display.print("Lng: ");
       display.setCursor(50, 20);
       display.print(gps.location.lng(), 6);
   
       display.setCursor(25, 35);
       display.print("Speed: ");
       display.setCursor(65, 35);
       display.print(gps.speed.kmph());
   
       display.setTextSize(1);
       display.setCursor(0, 50);
       display.print("SAT:");
       display.setCursor(25, 50);
       display.print(gps.satellites.value());
   
       display.setTextSize(1);
       display.setCursor(70, 50);
       display.print("ALT:");
       display.setCursor(95, 50);
       display.print(gps.altitude.meters(), 0);
   
       display.display();
     } else {
       display.clearDisplay();
       display.setTextColor(SSD1306_WHITE);
       display.setCursor(0, 0);
       display.setTextSize(3);
       display.print("No Data");
       display.display();
     }
   }
   
   // Function to return GPS string
   String displayInfo() {
     // Define empty string to hold output
     String gpsdata = "";
   
     // Get latitude and longitude
     if (gps.location.isValid()) {
       gpsdata = String(gps.location.lat(), 6);
       gpsdata += (",");
       gpsdata += String(gps.location.lng(), 6);
       gpsdata += (",");
     } else {
       return "0";
     }
   
     // Get Date
     if (gps.date.isValid()) {
       gpsdata += String(gps.date.year());
       gpsdata += ("-");
       if (gps.date.month() < 10) gpsdata += ("0");
       gpsdata += String(gps.date.month());
       gpsdata += ("-");
       if (gps.date.day() < 10) gpsdata += ("0");
       gpsdata += String(gps.date.day());
     } else {
       return "0";
     }
   
     // Space between date and time
     gpsdata += (" ");
   
     // Get time
     if (gps.time.isValid()) {
       if (gps.time.hour() < 10) gpsdata += (blank"0");
       gpsdata += String(gps.time.hour());
       gpsdata += (":");
       if (gps.time.minute() < 10) gpsdata += ("0");
       gpsdata += String(gps.time.minute());
       gpsdata += (":");
       if (gps.time.second() < 10) gpsdata += ("0");
       gpsdata += String(gps.time.second());
     } else {
       return "0";
     }
   
     // Return completed string
   bootreturn gpsdata;
   }
  

File(s)

.ino Sketch