TestEepromGPS.ino 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //code arduino qui recupere la position gps toute les 5 sec et qui enregistre dans l'eeprom les 500 derniere position. ou l'on peut couper l'allimentation
  2. #include <Adafruit_GPS.h>
  3. #include <EEPROM.h>
  4. // what's the name of the hardware serial port?
  5. #define GPSSerial Serial1
  6. // Connect to the GPS on the hardware port
  7. Adafruit_GPS GPS(&GPSSerial);
  8. // Set GPSECHO to 'false' to turn off echoing the GPS data to the Serial console
  9. // Set to 'true' if you want to debug and listen to the raw GPS sentences
  10. #define GPSECHO false
  11. uint32_t timer = millis();
  12. #define RX_PIN 18
  13. #define TX_PIN 19
  14. #define GPS_BAUD 9600
  15. SoftwareSerial ss(RX_PIN, TX_PIN);
  16. const int max_positions = 500;
  17. const int eeprom_start_address = 0;
  18. int eeprom_current_address = eeprom_start_address;
  19. void setup() {
  20. Serial.begin(115200);
  21. Serial.println("Adafruit GPS library basic parsing test!");
  22. // 9600 NMEA is the default baud rate for Adafruit MTK GPS's- some use 4800
  23. GPS.begin(9600);
  24. // uncomment this line to turn on RMC (recommended minimum) and GGA (fix data) including altitude
  25. GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
  26. // uncomment this line to turn on only the "minimum recommended" data
  27. //GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY);
  28. // For parsing data, we don't suggest using anything but either RMC only or RMC+GGA since
  29. // the parser doesn't care about other sentences at this time
  30. // Set the update rate
  31. GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); // 1 Hz update rate
  32. // For the parsing code to work nicely and have time to sort thru the data, and
  33. // print it out we don't suggest using anything higher than 1 Hz
  34. // Request updates on antenna status, comment out to keep quiet
  35. GPS.sendCommand(PGCMD_ANTENNA);
  36. delay(1000);
  37. // Ask for firmware version
  38. GPSSerial.println(PMTK_Q_RELEASE);
  39. }
  40. void loop(){
  41. //si char on envoie la memoire
  42. if(Serial.available()){
  43. Serial.read();
  44. AfficheEeprom();
  45. }
  46. // read data from the GPS in the 'main loop'
  47. char c = GPS.read();
  48. // if you want to debug, this is a good time to do it!
  49. // if a sentence is received, we can check the checksum, parse it...
  50. if (GPS.newNMEAreceived()) {
  51. // a tricky thing here is if we print the NMEA sentence, or data
  52. // we end up not listening and catching other sentences!
  53. // so be very wary if using OUTPUT_ALLDATA and trying to print out data
  54. //Serial.print(GPS.lastNMEA()); // this also sets the newNMEAreceived() flag to false
  55. if (!GPS.parse(GPS.lastNMEA())) // this also sets the newNMEAreceived() flag to false
  56. return; // we can fail to parse a sentence in which case we should just wait for another
  57. }
  58. // approximately every 2 seconds or so, print out the current stats
  59. if (millis() - timer > 5000) {
  60. timer=millis();
  61. /*Serial.print("Lat/Long: ");
  62. Serial.print(GPS.latitude);
  63. Serial.print(", ");
  64. Serial.println(GPS.longitude);
  65. Serial.print(", ");
  66. Serial.print(GPS.angle);
  67. Serial.print(", ");
  68. Serial.println(GPS.speed);*/
  69. // Enregistrer la position dans l'EEPROM
  70. EEPROM.put(eeprom_current_address, GPS.latitude);
  71. eeprom_current_address += sizeof(float);
  72. EEPROM.put(eeprom_current_address + sizeof(float), GPS.longitude);
  73. eeprom_current_address += sizeof(float);
  74. EEPROM.put(eeprom_current_address + sizeof(float), GPS.angle);
  75. eeprom_current_address += sizeof(float);
  76. EEPROM.put(eeprom_current_address + sizeof(float), GPS.speed);
  77. eeprom_current_address += sizeof(float);
  78. }
  79. }
  80. void AfficheEeprom() {
  81. int address = 0;
  82. while (address < EEPROM.length()) {
  83. float latitude, longitude,speed, angle;
  84. EEPROM.get(address, latitude); // Lecture de la latitude
  85. address += sizeof(float); // Ajout de la taille de la latitude à l'adresse de départ pour obtenir l'adresse de la longitude
  86. EEPROM.get(address, longitude); // Lecture de la longitude
  87. address += sizeof(float); // Ajout de la taille de la latitude à l'adresse de départ pour obtenir l'adresse de la longitude
  88. EEPROM.get(address, angle); // Lecture de la longitude
  89. address += sizeof(float);
  90. EEPROM.get(address, speed); // Lecture de la longitude
  91. address += sizeof(float);
  92. Serial.print("Latitude: ");
  93. Serial.print((latitude), 6);
  94. Serial.print(", Longitude: ");
  95. Serial.print((longitude), 6);
  96. Serial.print(", angle: ");
  97. Serial.print((longitude), 6);
  98. Serial.print(", speed: ");
  99. Serial.println(speed, 6);
  100. }
  101. }
  102. float gpsToDecimal(float gps_coord) {
  103. int degrees = int(gps_coord / 100); // Calcul des degrés
  104. float minutes = gps_coord - (degrees * 100); // Calcul des minutes
  105. float decimal_degrees = degrees + (minutes / 60); // Conversion en degrés décimaux
  106. return decimal_degrees;
  107. }