diff --git a/Code/.gitkeep b/Code/.gitkeep new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 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(); +} diff --git a/Code/tiny_gps_esp32 b/Code/tiny_gps_esp32 new file mode 100644 index 0000000000000000000000000000000000000000..c2ce403a13ebbf2c71885be1938c195fb9b6c471 --- /dev/null +++ b/Code/tiny_gps_esp32 @@ -0,0 +1,226 @@ +#include <TinyGPS++.h> +#include <SoftwareSerial.h> +/* + This sample sketch demonstrates the normal use of a TinyGPS++ (TinyGPSPlus) object. + It requires the use of SoftwareSerial, and assumes that you have a + 4800-baud serial GPS device hooked up on pins 4(rx) and 3(tx). +*/ +static const int RXPin = 16, TXPin = 17; +static const uint32_t GPSBaud = 9600; + +// neu +long timeToDisplay = 0; +String monat,tag,jahr; +String stunde,minute1,sekunde; +float flat, flong, kmh; + +// Libraries for SD card +#include "FS.h" +#include "SD.h" +#include <SPI.h> + +// Define CS pin for the SD card module +#define SD_CS 5 + +// Save reading number on RTC memory +RTC_DATA_ATTR int readingID = 0; + +String dataMessage; + +// 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 + } + + // If the data.txt file doesn't exist + // Create a file on the SD card and write the data labels + File file = SD.open("/data.txt"); + if(!file) { + Serial.println("File doens't exist"); + Serial.println("Creating file..."); + writeFile(SD, "/data.txt", "Reading ID, Tag, Monat, Jahr, Stunde, Minute, Sekunde, Latitude, Longitude, Speed \r\n"); + } + else { + Serial.println("File already exists"); + } + file.close(); + +// getReadings(); // from the NEO-6M GPS module + logSDCard(); + // Increment readingID on every new reading +} + +void loop() +{ + // This sketch displays information every time a new sentence is correctly encoded. + 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); + } +// neu + smartDelay(1000); + displayInfo(); + logSDCard(); + // Increment readingID on every new reading + // readingID++; +} + +static void smartDelay(unsigned long ms) +{ + unsigned long start = millis(); + do + { + while (ss.available()) + gps.encode(ss.read()); + } while (millis() - start < ms); +} + + + +void displayInfo() +{ + Serial.print(F("Location: ")); + if (gps.location.isValid()) + { + flat = gps.location.lat(); + flong = gps.location.lng(); + Serial.print(flat, 6); + Serial.print(F(",")); + Serial.print(flong, 6); + //Serial.print(gps.location.lat(), 6); + //Serial.print(F(",")); + //Serial.print(gps.location.lng(), 6); + + } + else + { + Serial.print(F("INVALID")); + } + + Serial.print(F(" Speed KMH: ")); + if (gps.speed.isValid()) + { + kmh = gps.speed.kmph(); + Serial.print(kmh); + Serial.print(gps.speed.kmph()); + } + else + { + Serial.print(F("INVALID")); + } + Serial.print(F(" Date/Time: ")); + if (gps.date.isValid()) + { + + monat = gps.date.month(); + tag = gps.date.day(); + jahr = gps.date.year(); + Serial.print(monat); + Serial.print(F("/")); + Serial.print(tag); + Serial.print(F("/")); + Serial.print(jahr); + } + else + { + Serial.print(F("INVALID")); + } + + Serial.print(F(" ")); + if (gps.time.isValid()) + { + if (gps.time.hour() < 10) Serial.print(F("0")); + stunde = gps.time.hour(); + Serial.print(stunde); + Serial.print(F(":")); + if (gps.time.minute() < 10) Serial.print(F("0")); + minute1 = gps.time.minute(); + Serial.print(minute1); + Serial.print(F(":")); + if (gps.time.second() < 10) Serial.print(F("0")); + sekunde = gps.time.second(); + Serial.print(sekunde); + Serial.print(F(".")); + } + else + { + Serial.print(F("INVALID")); + } + + Serial.println(); +} + + +// Write the sensor readings on the SD card +void logSDCard() { + if (gps.location.isValid()){ + dataMessage = String(readingID) + "," + String(tag) + "," + String(monat) + "," + + String(jahr) + "," + String(stunde) + "," + String(minute1) + "," + + String(sekunde) + "," + String(flat, 6) + "," + String(flong, 6) + "," + String(kmh) + "\r\n"; + Serial.print("Save data: "); + Serial.println(dataMessage); + appendFile(SD, "/data.txt", 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(); +} diff --git a/README.md b/README.md index b91a7aabd06fc7364f0372b9461f21a6236f00bb..36bfb75d480f34d19b60e15b9058bb685026a7be 100644 --- a/README.md +++ b/README.md @@ -14,38 +14,63 @@ Die benötigten Bauteile werden im Folgendem mit den dazugehörigen Links aufgel 2. NEO-6M: 3. SD Card Module: 4. Antenne: -5. 10 Kabel: +5. 10 Verbindungs-Kabel: -Zusätzlich benötigt ihr noch Kabel, um sie miteinander anzuschließen und eine SD Karte, die ihr in das SD Card Module reinstecken könnt. +Zusätzlich benötigt ihr eine SD Karte, die ihr in das SD Card Module reinstecken könnt. ## **2. Bauteile zusammenbauen** -In der unteren Abbildung könnt ihr dann sehen, wie ihr die Bauteile miteinander verbinden müsst, damit ihr einen funktionierenden GPS Logger besitzt. Die Antenne wird im NEO-6M verbaut. +In der unteren Abbildung und zusätzlich in den zwei Tabellen könnt ihr dann sehen, wie ihr die Bauteile miteinander verbinden müsst, damit ihr einen funktionierenden GPS Logger besitzt. Die Antenne wird im NEO-6M verbaut. - + -## **.3. Code über Arduino draufspielen** +ESP32 | SD Card Module +------------- | ------------- +Pin 5 | CS +Pin 18 | SCK +Pin 23 | MOSI +Pin 19 | MISO +5V | VCC +GND | GND + +ESP32 | NEO-6M +------------- | ------------- +3V3 | VCC +Pin 17 | RX +Pin 16 | TX +GND | GND + +## **3. Code über Arduino draufspielen** Nachdem alle Bauteile, wie im Bild angeschlossen sind, muss nur noch der Code auf den GPS Logger draufgespielt werden. Dazu benötigen wir das Programm Arduino, welches ihr euch unter folgendem Link runterladen könnt. Arduino: https://www.arduino.cc/en/software -Nun könnt ihr das Programm öffnen. Anchließend kopiert ihr den Code mit dem der GPS_Logger bespielt werden soll aus dem Ordner "Code" und fügt diesen in Arduino ein. +Nun könnt ihr das Programm öffnen. Anchließend kopiert ihr den Code mit dem der GPS_Logger bespielt werden soll aus dem Ordner [Code](https://gitlab.reutlingen-university.de/smart-city-living-lab-hhz/gps_logger/-/tree/main/Code) und fügt diesen in Arduino ein. Als nächstes müsst ihr die nötigen Bibliotheken in das Programm einbinden. Die folgende Abbildung zeigt welche Bibliotheken ihr dafür installieren müsst. + + Wollt ihr nun beispielsweiße die Bibliothek "SPI" installieren geht ihr wie folgt vor. -1: Klickt auf Sketch und anschließend auf Bibliothek einbinden. -2: Klickt nun auf Bibliotheken verwalten. Daraufhin öffnet sich der Bibliotheksverwalter. -3: Sucht nun in der Suchleiste nach der Bibliothek. Hier "SPI" -4: Wählt die gefundene Bibliothek aus und klickt auf installieren. +1. Klickt auf Sketch und anschließend auf Bibliothek einbinden. +2. Klickt nun auf Bibliotheken verwalten. Daraufhin öffnet sich der Bibliotheksverwalter. +3. Sucht nun in der Suchleiste nach der Bibliothek. Hier "SPI" +4. Wählt die gefundene Bibliothek aus und klickt auf installieren. + +Habt ihr alle benötigten Biliotheken installiert, solltet ihr den Code überprüfen. Dafür klickt ihr auf das Häkchen oben links. -Habt ihr alle benötigten Biliotheken installiert solltet ihr den Code überprüfen. Dafür klickt ihr auf das Häckchen oben links. + -Falls alles richtig installiert wurde läuft die Überprüfung durch den Code durch. +Falls alles richtig installiert wurde, läuft die Überprüfung durch den Code durch. -Bevor ihr den Code auf den GPS_Logger spielen könnt müsst ihr noch bei "Board" unter dem Reiter "Werkzeuge" das "ESP32 Dev Module" auswählen. Falls dieses Boeard nicht angezeigt wird müsst ihr die ESP32 Module über den Boardverwalter intsallieren. +Bevor ihr den Code auf den GPS_Logger spielen könnt, müsst ihr noch bei "Board" unter dem Reiter "Werkzeuge" das "ESP32 Dev Module" auswählen. Falls dieses Board nicht angezeigt wird, müsst ihr die ESP32 Module über den Boardverwalter intsallieren. + + Anschließend könnt ihr den GPS_Logger am PC anschließen und diesen über den Button hochladen mit dem Code bespielen. -Falls das hochladen erfolgreich war wird dies angezeigt. + + + +Falls das Hochladen erfolgreich war, wird dies angezeigt.