Created
June 17, 2020 13:00
-
-
Save ypelletier/695562968b8c31ebc584e6e2fb0981c4 to your computer and use it in GitHub Desktop.
Affichage des données du module GPS sur un écan OLED et enregistrement sur une carte SD (STM32 Blue Pill).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*********************************************************** | |
Affichage des données du module GPS sur un écan | |
OLED et enregistrement sur une carte SD (STM32 Blue Pill). | |
Plus d'infos: | |
http://electroniqueamateur.blogspot.com/2020/06/fabrication-dun-enregistreur-gps-gps.html | |
************************************************************/ | |
// bibliothèque pour le module GPS | |
#include <TinyGPS++.h> | |
// bibliothèques pour l'écran OLED | |
#include <U8g2lib.h> | |
#include <Wire.h> | |
// blibliothèques pour la carte SD | |
#include <SPI.h> | |
#include <SD.h> | |
// GPS | |
TinyGPSPlus gps; | |
HardwareSerial Serial2(USART2); | |
// OLED | |
U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE); | |
// Carte SD | |
File monFichier; | |
int compteur = 0; | |
const int maxcompteur = 20; // 20 mises à jour du GPS sont nécessaire pour produire un enregistrement sur la carte SD | |
void setup() | |
{ | |
// initialisation de l'écran OLED | |
u8g2.begin(); | |
u8g2.enableUTF8Print(); | |
u8g2.setFont(u8g2_font_ncenB10_tf); // choix de la police | |
u8g2.clearBuffer(); | |
u8g2.setCursor(10, 40); | |
u8g2.print("Console GPS"); | |
u8g2.sendBuffer(); | |
// initialisation de la communication série avec le module GPS | |
Serial2.begin(9600); // module GPS | |
// initialisation du lecteur de carte SD | |
if (!SD.begin(PA4)) { | |
u8g2.print("Echec SD!"); | |
while (1); | |
} | |
// on se laisse le temps de voir l'écran de bienvenue | |
// (le gps ne sera pas prêt, de toute façon) | |
delay(1000); | |
} | |
void loop() | |
{ | |
while (Serial2.available() > 0) | |
if (gps.encode(Serial2.read())) | |
displayInfo(); | |
if (millis() > 5000 && gps.charsProcessed() < 10) | |
{ | |
u8g2.clearBuffer(); | |
u8g2.setCursor(10, 40); // position du début du texte | |
u8g2.print("Pas de GPS!"); | |
u8g2.sendBuffer(); | |
while (true); | |
} | |
} | |
void displayInfo() | |
{ | |
u8g2.clearBuffer(); | |
u8g2.setCursor(1, 12); | |
if (gps.location.isValid()) | |
{ | |
u8g2.print(gps.location.lat(), 6); | |
u8g2.setCursor(1, 26); | |
u8g2.print(gps.location.lng(), 6); | |
} | |
else | |
{ | |
u8g2.print("..."); | |
} | |
u8g2.setCursor(1, 40); | |
if (gps.date.isValid()) | |
{ | |
u8g2.print(gps.date.month()); | |
u8g2.print("/"); | |
u8g2.print(gps.date.day()); | |
u8g2.print("/"); | |
u8g2.print(gps.date.year()); | |
} | |
else | |
{ | |
u8g2.print("..."); | |
} | |
u8g2.setCursor(1, 54); | |
if (gps.time.isValid()) | |
{ | |
if (gps.time.hour() < 10) u8g2.print("0"); | |
u8g2.print(gps.time.hour()); | |
u8g2.print(":"); | |
if (gps.time.minute() < 10) u8g2.print("0"); | |
u8g2.print(gps.time.minute()); | |
u8g2.print(":"); | |
if (gps.time.second() < 10) u8g2.print("0"); | |
u8g2.print(gps.time.second()); | |
u8g2.print("."); | |
if (gps.time.centisecond() < 10) u8g2.print("0"); | |
u8g2.print(gps.time.centisecond()); | |
} | |
else | |
{ | |
u8g2.print("..."); | |
} | |
u8g2.sendBuffer(); | |
// enregistrement sur carte SD | |
if ((gps.location.isValid()) && (compteur > maxcompteur)) { | |
compteur = 0; | |
// ouverture (ou création) du fichier "TRAJET.CSV") | |
monFichier = SD.open("TRAJET.CSV", FILE_WRITE); | |
// si l'ouverture a réussi, on écrit à l'intérieur | |
if (monFichier) { | |
monFichier.print(gps.location.lat(),6); | |
monFichier.print(","); | |
monFichier.print(gps.location.lng(),6); | |
monFichier.print(","); | |
monFichier.println(gps.time.value()); | |
// on referme le fichier | |
monFichier.close(); | |
} | |
} | |
compteur++; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment