Je peux me tromper complètement en mettant en place cette Instructables, mais j’ai eu du mal à trouver un moyen facile de corriger les données de date et heure GPS à mon fuseau ici en Australie. J’ai fait quelques projets Arduino qui ont inclus un module GPS avec une certaine forme d’enregistrement de données. Chaque fois que j’ai eu des difficultés de conversion heure UTC capturé de la chaîne de données NMEA en heure locale ou ont simplement abandonné et utilisé un CCF dans mon projet.
Il y a que quelques solutions sur le web incluent un couple ici sur Instructables, mais comme un codeur amateur, j’ai lutté pour intégrer le code dans mes propres croquis.
J’espère que cette annonce va donner ceux tout aussi mal, une solution facile. J’ai essayé de garder le code aussi simple que possible avec beaucoup d’options en fonction de votre configuration, Uno ou Mega, logiciels et matériels des ports série. J’ai aussi mis le calcul de fuseau horaire dans une fonction simple qui peut être copiée et collée directement dans vos propres croquis.
Vous devez installer la Bibliothèque TinyGPS ++ et la Bibliothèque AltSoftSerial pour le croquis pour compiler correctement. J’ai eu la meilleure compatibilité avec les modules GPS, que j’ai en utilisant la librairie TinyGPS ++. La bibliothèque AltSoftSerial le rend plus facile d’avoir plusieurs équipement série relié à votre Uno. Ce n’est pas un problème lorsque vous utilisez un méga, mais si vous ne souhaitez pas fixer vos équipements série sur broches numériques différents, c’est la bibliothèque plus simple que j’ai trouvé.
Voici le schéma ci-dessous :
#include <Time.h> // Time Library #include <TinyGPS++.h> // GPS Library #include <AltSoftSerial.h> // Allows two Serial connections // Set GPS RX and TX pins if using software serial connections. // See below to use hardware serial connections //static const int RXPin = 4, TXPin = 3; // Example Uno static const int RXPin = 48, TXPin = 46; // Example Mega static const uint32_t GPSBaud = 4800; //static const uint32_t GPSBaud = 9600; // The TinyGPS++ object TinyGPSPlus gps; // Serial connection to the GPS device AltSoftSerial Serial_GPS; //#define Serial_GPS Serial1 // Uncomment this line & comment // above line to use a hardware // Serial Port // Change this value to suit your Time Zone const int UTC_offset = 10; // Eastern Australia Time time_t prevDisplay = 0; // Count for when time last displayed void setup() { Serial.begin(9600); Serial_GPS.begin(GPSBaud); // Start GPS Serial Connection Serial.println("Waiting for GPS time ... "); } void loop() { GPS_Timezone_Adjust(); // Call Time Adjust Function } void GPS_Timezone_Adjust(){ while (Serial_GPS.available()) { if (gps.encode(Serial_GPS.read())) { int Year = gps.date.year(); byte Month = gps.date.month(); byte Day = gps.date.day(); byte Hour = gps.time.hour(); byte Minute = gps.time.minute(); byte Second = gps.time.second(); // Set Time from GPS data string setTime(Hour, Minute, Second, Day, Month, Year); // Calc current Time Zone time by offset value adjustTime(UTC_offset * SECS_PER_HOUR); } } // -- Delete this section if not displaying time ------- // if (timeStatus()!= timeNotSet) { if (now() != prevDisplay) { prevDisplay = now(); SerialClockDisplay(); } } // -- Also delete the SerialClockDisplay()function ---- // } // Keep void SerialClockDisplay(){ // Serial Monitor display of new calculated time - // once adjusted GPS time stored in now() Time Library // calculations or displays can be made. if (hour() < 10) Serial.print(F("0")); Serial.print(hour()); Serial.print(F(":")); if (minute() < 10) Serial.print(F("0")); Serial.print(minute()); Serial.print(F(":")); if (second() < 10) Serial.print(F("0")); Serial.print(second()); Serial.print(" "); if (day() < 10) Serial.print(F("0")); Serial.print(day()); Serial.print(F("/")); if (month() < 10) Serial.print(F("0")); Serial.print(month()); Serial.print(F("/")); Serial.println(year()); }
J’espère que vous avez trouvé cela utile et il facilite beaucoup votre prochain projet d’enregistreur de données GPS.