diff --git a/Code/gps_logger_yb b/Code/gps_logger_yb new file mode 100644 index 0000000000000000000000000000000000000000..8a2e17698903a8f2708c5f4cf76540026d80a6c5 --- /dev/null +++ b/Code/gps_logger_yb @@ -0,0 +1,176 @@ +#include <TinyGPS++.h> +#include <SoftwareSerial.h> +#include "FS.h" +#include "SD.h" +#include <SPI.h> + +static const int RXPin = 16, TXPin = 17; +static const uint32_t GPSBaud = 9600; + +#define SD_CS 5 +RTC_DATA_ATTR int readingID = 0; + +String dataMessage; +String fileName = "leer"; +String monat,tag,jahr; +String stunde,minute1,sekunde; +float flat, flong, kmh; +int sat; + +// The TinyGPS++ object +TinyGPSPlus gps; + +// The serial connection to the GPS device +SoftwareSerial ss(RXPin, TXPin); + +// ----------------------------------------------------- + +void setup() { + + Serial.begin(115200); + ss.begin(GPSBaud); + + // Initialize SD card + SD.begin(SD_CS); + if(!SD.begin(SD_CS)) { + Serial.println("Card Mount Failed"); + return; + } + uint8_t cardType = SD.cardType(); + if(cardType == CARD_NONE) { + Serial.println("No SD card attached"); + return; + } + Serial.println("Initializing SD card..."); + if (!SD.begin(SD_CS)) { + Serial.println("ERROR - SD card initialization failed!"); + return; // init failed + } + +} + +void loop() { + // put your main code here, to run repeatedly: + smartDelay(1000); + Serial.print(gps.date.value()); + if(gps.date.value() > 0 && fileName!="leer"){ + //Serial.print("File Name wurde geschrieben und warte auf loggen: "); + Serial.println(fileName.c_str()); + loggeDaten(); + }else{ + erstelleFileName(); + erstelleFile(); + } + +} + +static void smartDelay(unsigned long ms) +{ + unsigned long start = millis(); + do + { + while (ss.available()) + gps.encode(ss.read()); + } while (millis() - start < ms); +} + +void erstelleFileName() { + if(gps.date.value() > 0) + { + fileName = "/" + String(gps.date.value()) + ".txt"; + Serial.print("File Name war erfolgreich: "); + Serial.println(fileName.c_str()); + Serial.print("Der Zeitstempel: "); + Serial.println(gps.time.value()); + } + Serial.print("Noch kein Zeitstempel!!! "); + Serial.println(gps.date.value()); +} + +void erstelleFile(){ + if(fileName != "leer"){ + File file = SD.open(fileName.c_str()); + if(!file) { + Serial.println("File doens't exist"); + Serial.println("Creating file..."); + writeFile(SD, fileName.c_str(), "Reading ID, Tag, Monat, Jahr, Stunde, Minute, Sekunde, Latitude, Longitude, Speed, AnzahlSat \r\n"); + } + else { + Serial.println("File already exists"); + } + file.close(); + } +} + +void leseGPS(){ + + while (ss.available() > 0) + if (gps.encode(ss.read())) + + if (millis() > 5000 && gps.charsProcessed() < 10) + { + Serial.println(F("No GPS detected: check wiring.")); + while(true); + } + + tag = gps.date.day(); + monat = gps.date.month(); + jahr = gps.date.year(); + stunde = gps.time.hour(); + minute1 = gps.time.minute(); + sekunde = gps.time.second(); + flat = gps.location.lat(); + flong = gps.location.lng(); + kmh = gps.speed.kmph(); + sat = gps.satellites.value(); + + Serial.print("HIER KOMMT LATITUDE: "); + Serial.println(gps.location.lat()); +} + +void loggeDaten() { + if (gps.date.isValid()){ + leseGPS(); + dataMessage = String(readingID) + "," + String(tag) + "," + String(monat) + "," + + String(jahr) + "," + String(stunde) + "," + String(minute1) + "," + + String(sekunde) + "," + String(flat, 6) + "," + String(flong, 6) + "," + String(kmh) + "," + String(sat) + "\r\n"; + Serial.print("Save data: "); + Serial.println(dataMessage); + appendFile(SD, fileName.c_str(), dataMessage.c_str()); + readingID++; + } +} + +// Write to the SD card (DON'T MODIFY THIS FUNCTION) +void writeFile(fs::FS &fs, const char * path, const char * message) { + Serial.printf("Writing file: %s\n", path); + + File file = fs.open(path, FILE_WRITE); + if(!file) { + Serial.println("Failed to open file for writing"); + return; + } + if(file.print(message)) { + Serial.println("File written"); + } else { + Serial.println("Write failed"); + } + file.close(); +} + +// Append data to the SD card (DON'T MODIFY THIS FUNCTION) +void appendFile(fs::FS &fs, const char * path, const char * message) { + Serial.printf("Appending to file: %s\n", path); + + File file = fs.open(path, FILE_APPEND); + if(!file) { + Serial.println("Failed to open file for appending"); + return; + } + if(file.print(message)) { + Serial.println("Message appended"); + } else { + Serial.println("Append failed"); + } + file.close(); +}