Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
- Committer:
- snec_student
- Date:
- 2022-06-20
- Revision:
- 29:fbc5f53d8d0f
- Parent:
- 28:6f0009bce093
- Child:
- 30:2457f9928392
File content as of revision 29:fbc5f53d8d0f:
#include "mbed.h"
/**************************************************************/
/* Déclaration des Sous fonctions */
/**************************************************************/
void ISR_read(); // lecture liaison serie
uint8_t gencrc2(uint8_t *data); // calcul crc NMEA
DigitalOut myled(PTB22);
DigitalOut CMD_200WX(PTA1,0);
// initialisation de la liaison serie vers le capteur WX200
static UnbufferedSerial capt(PTC4,PTC3,4800);
char c;
char Rx_buffer[100];
char trame[100];
char trame_cpy[100];
volatile bool flag_ISR_read=0;
volatile int index=0;
// variables associees au capteur WX200
static char type[20]; // type de la trame (GPGGA, RMC...)
const char * separators = ","; // separateurs pour une trame NMEA
char i;
size_t len;
uint8_t val_crc; // valeur du crc calcule a la reception de la trame
// variables associees a une trame de type GPGGA
bool flag_GPGGA=0;
static char horaire[20]; // heure UTC (champ 1)
static char lattitude[20]; // Lattitude (champ 2)
static char hemisphere[2]; // Hemisphere (N/S) (champ 3)
static char longitude[20]; // Longitude (champ 4)
static char dir[2]; // direction (E/W) (champ 5)
static char quality[2]; // GPS quality indicator (0 a 8) (champ 6)
static char nb_satellites[10]; // Number of satellites in use, 0-12 (champ 7)
static char HDOP[10]; // Horizontal dilution of precision (HDOP) (champ 8)
static char altitude[10]; // Altitude relative to mean-sea-level (geoid), meters (to the nearest whole meter) (champ 9)
static char M[2]; // M (champ 10)
static char altitude_cor[10]; // Geoidal separation, meters (to the nearest whole meter). (champ 11)
int n_sat; // nombre de satellites
// variables associees a une trame de type WIMDA
bool flag_WIMDA=0;
static char pres_inch[20]; // Barometric pressure, inches of mercury, to the nearest 0.01 inch (champ 1)
static char I[2]; // inches of mercury (champ 2)
static char pres_bar[20]; // Barometric pressure, bars, to the nearest .001 bar(champ 3)
static char B[2]; // B = bars (champ 4)
static char air_temp[10]; // Air temperature, degrees C, to the nearest 0.1 degree C (champ 5)
static char C1[2]; // C = degrees C (champ 6)
static char wat_temp[10]; // Water temperature, degrees C (champ 7) blank with WX200
static char C2[2]; // C = degrees C (champ 8)
static char rel_hum[10]; // Relative humidity, percent, to the nearest 0.1 percent (champ 9)
static char abs_hum[10]; // Absolute humidity, percent (champ 10) blank with WX200
static char dew_point[10]; // Dew point, degrees C, to the nearest 0.1 degree C (champ 11) blank with WX200
static char C3[2]; // C = degrees C (champ 12)
static char Wind_dir_T[10]; // Wind direction, degrees True, to the nearest 0.1 degree (champ 13)
static char T[2]; // T = True (champ 14)
static char Wind_dir_M[10]; // Wind direction, degrees Magnetic, to the nearest 0.1 degree (champ 15)
static char M2[2]; // M = magnetic (champ 16)
static char Wind_speed_knots[10]; // Wind speed, knots, to the nearest 0.1 knot (champ 17)
static char N[2]; // N = knots (champ 18)
static char Wind_speed_ms[10]; // Wind speed, meters per second, to the nearest 0.1 m/s (champ 19)
static char M3[2]; // M = meters per second(champ 20)
// variables associees a une trame de type GPRMC
static char vitesse[20];
static char cap[20];
static char date[20];
static char magn[20];
static char crc[10];
static char alerte[3];
int main()
{
// autorise departs en interruption liaison serie
capt.attach(&ISR_read,SerialBase::RxIrq);
// attente 3 secondes puis alimentation du capteur
thread_sleep_for (3000);
CMD_200WX=1;
thread_sleep_for (30000); // on laisse 30 s de mesures
while((flag_GPGGA==0)||(flag_WIMDA==0)){ // afaire en boucle tant que l'on a pas recu les infos
// si une trame est recue
if (flag_ISR_read==1) {
//printf("%s",trame); // ligne de test gps
char* token = strtok(trame,"*"); //on met dans trame_cpy la trame sans crc
strcpy(trame_cpy,token);
token = strtok(NULL,"*"); // on copie la valeur du crc recu dans crc
strcpy(crc,token);
// on calcule la valeur du crc dans la trame recue
val_crc=gencrc2((uint8_t *)trame_cpy);
uint8_t val_crc2;
sscanf(crc,"%x",&val_crc2);
// on teste la validite du crc recu
if (val_crc!=val_crc2) {
// printf ("crc error\n");
} else {
// traitement de la trame en cas de crc correct
//printf ("crc OK\n");
//printf("trame :%s \n",trame_cpy);
char* token = strtok((char*)trame_cpy,separators);
strcpy(type,token);
if ((strcmp(type,"$GPGGA")==0)&&(flag_GPGGA==0)) { // traitement d'une trame GPGGA
//printf("trame GPGGA : %s \n",trame);
token = strtok(NULL,separators);
if (strcmp(token,"")!=0) strcpy(horaire,token);
token = strtok(NULL,separators);
strcpy(lattitude,token);
token = strtok(NULL,separators);
strcpy(hemisphere,token);
token = strtok(NULL,separators);
strcpy(longitude,token);
token = strtok(NULL,separators);
strcpy(dir,token);
token = strtok(NULL,separators);
strcpy(quality,token);
token = strtok(NULL,separators);
strcpy(nb_satellites,token);
token = strtok(NULL,separators);
strcpy(HDOP,token);
token = strtok(NULL,separators);
strcpy(altitude,token);
token = strtok(NULL,separators);
strcpy(M,token);
token = strtok(NULL,separators);
strcpy(altitude_cor,token);
token = strtok(NULL,separators);
token = strtok(NULL,separators);
if (strcmp(nb_satellites,"")!=0) n_sat=atoi(nb_satellites); // calcul du nombre de satellites
if (n_sat>3)
{
flag_GPGGA=1; // on signale la fin d'une lecture correcte GPGGA
}
} // fin if GPGGA
else if ((strcmp(type,"$WIMDA")==0)&&(flag_WIMDA==0)) { // traitement d'une trame WIMDA
//capt.detach();
//printf("trame WIMDA : %s \n",trame);
token = strtok(NULL,separators); // champ1
if (strcmp(token,"")!=0)strcpy(pres_inch,token);
token = strtok(NULL,separators); // champ2
if (strcmp(token,"")!=0) strcpy(I,token);
token = strtok(NULL,separators); // champ3
if (strcmp(token,"")!=0)strcpy(pres_bar,token);
token = strtok(NULL,separators); // champ4
if (strcmp(token,"")!=0)strcpy(B,token);
token = strtok(NULL,separators); // champ5
if (strcmp(token,"")!=0)strcpy(air_temp,token);
token = strtok(NULL,separators); // champ6
strcpy(C1,token);
token = strtok(NULL,separators); // champ7
if (strcmp(token,"")!=0)strcpy(wat_temp,token);
token = strtok(NULL,separators); // champ8
if (strcmp(token,"")!=0)strcpy(C2,token);
token = strtok(NULL,separators); // champ9
if (strcmp(token,"")!=0)strcpy(rel_hum,token);
token = strtok(NULL,separators); // champ10
if (strcmp(token,"")!=0)strcpy(abs_hum,token);
token = strtok(NULL,separators); // champ11
if (strcmp(token,"")!=0)strcpy(dew_point,token);
token = strtok(NULL,separators); // champ12
if (strcmp(token,"")!=0)strcpy(C3,token);
token = strtok(NULL,separators); // champ13
if (strcmp(token,"")!=0)strcpy(Wind_dir_T,token);
token = strtok(NULL,separators); // champ14
if (strcmp(token,"")!=0)strcpy(T,token);
token = strtok(NULL,separators); // champ15
if (strcmp(token,"")!=0)strcpy(Wind_dir_M,token);
token = strtok(NULL,separators); // champ16
if (strcmp(token,"")!=0)strcpy(M2,token);
token = strtok(NULL,separators); // champ17
if (strcmp(token,"")!=0)strcpy(Wind_speed_knots,token);
token = strtok(NULL,separators); // champ18
if (strcmp(token,"")!=0)strcpy(N,token);
token = strtok(NULL,separators); // champ19
if (strcmp(token,"")!=0)strcpy(Wind_speed_ms,token);
token = strtok(NULL,separators); // champ20
if (strcmp(token,"")!=0)strcpy(M3,token);
if (strcmp(air_temp,"")!=0) flag_WIMDA=1; // on signale la fin d'une lecture correcte WIMDA
} // fin if WIMDA
} // fin else (traitement trame correcte)
}//fin if read enable
} // fin while flags ==0
// On affiche les informations GPGGA
printf("horaire UTC :%s \n", horaire); // heure UTC (champ 1)
printf("lattitude :%s \n",lattitude); // Lattitude (champ 2)
printf("hemisphere :%s \n",hemisphere); // Hemisphere (N/S) (champ 3)
printf("longitude:%s \n",longitude); // Longitude (champ 4)
printf("dir:%s \n",dir); // direction (E/W) (champ 5)
printf("quality:%s \n",quality); // GPS quality indicator (0 a 8) (champ 6)
printf("nb_satellites:%s \n",nb_satellites); // Number of satellites in use, 0-12 (champ 7)
printf("HDOP:%s \n",HDOP); // Horizontal dilution of precision (HDOP) (champ 8)
printf("altitude:%s \n",altitude); // Altitude relative to mean-sea-level (geoid), meters (to the nearest whole meter) (champ 9)
printf("unit :%s \n",M);
printf("altitude_cor:%s \n",altitude_cor); // Geoidal separation, meters (to the nearest whole meter). (champ 10)
// On affiche les informations WIMDA
printf("Barometric Pressure :%s %s \n", pres_inch,I); // pression inch + unite (champs 1 et 2)
printf("Barometric Pressure :%s %s \n", pres_bar,B); // pression bars + unite (champs 3 et 4)
printf("Air Temperature:%s %s\n", air_temp,C1); // temperature de l'air + unite (champs 5 et 6)
printf("Water Temperature:%s %s\n", wat_temp,C2); // temperature de l'air + unite (champs 7 et 8)
printf("Relative humidity :%s \n", rel_hum); // humidite relative (champs 9)
printf("Absolute humidity :%s \n", abs_hum); // humidite relative (champs 10)
printf("Dew Point :%s %s\n", dew_point,C3); // point de rosee + unite (champs 11 et 12)
printf("Wind direction :%s %s\n", Wind_dir_T,T); // direction du vent en degres vrais + unite (champs 13 et 14)
printf("Wind direction magne :%s %s\n", Wind_dir_M,M2); // direction du vent en degres vrais + unite (champs 15 et 16)
printf("Wind speed :%s %s\n", Wind_speed_knots,N); // vitesse du vent en noeuds + unite (champs 17 et 18)
printf("Wind speed :%s %s\n", Wind_speed_ms,M3); // vitesse du vent en m/s + unite (champs 17 et 18)
} // fin programme
/* char* token = strtok(trame,"*"); //on met dans trame cpy la trame sans crc
strcpy(trame_cpy,token);
token = strtok(NULL,"*"); // on copie le crc
strcpy(crc,token);
val_crc=gencrc2((uint8_t *)trame_cpy); //calcul du crc sur la trame
uint8_t val_crc2;
sscanf(crc,"%x",&val_crc2);
if (val_crc!=val_crc2) {
printf ("crc error\n");
} else {
printf ("crc OK\n");
char* token = strtok((char*)trame_cpy,separators);
strcpy(type,token);
if (strcmp(type,"$GPRMC")==0) { // traitement d'une trame RMC
token = strtok(NULL,separators);
strcpy(horaire,token);
token = strtok(NULL,separators);
strcpy(alerte,token);
token = strtok(NULL,separators);
strcpy(lattitude,token);
token = strtok(NULL,separators);
strcpy(hemisphere,token);
token = strtok(NULL,separators);
strcpy(longitude,token);
token = strtok(NULL,separators);
strcpy(dir,token);
token = strtok(NULL,separators);
strcpy(vitesse,token);
token = strtok(NULL,separators);
strcpy(cap,token);
token = strtok(NULL,separators);
strcpy(date,token);
token = strtok(NULL,separators);
strcpy(magn,token);
//printf ("%s : %s : %s : %s \n",date,horaire,lattitude, longitude);
} // fin compare chaines RMC
if (strcmp(type,"$GPGGA")==0) { // traitement d'une trame GGA
token = strtok(NULL,separators);
strcpy(horaire,token);
token = strtok(NULL,separators);
strcpy(lattitude,token);
token = strtok(NULL,separators);
strcpy(hemisphere,token);
token = strtok(NULL,separators);
strcpy(longitude,token);
token = strtok(NULL,separators);
strcpy(dir,token);
token = strtok(NULL,separators);
strcpy(qualif,token);
token = strtok(NULL,separators);
strcpy(nb_satellites,token);
token = strtok(NULL,separators);
strcpy(DOP,token);
token = strtok(NULL,separators);
strcpy(altitude,token);
token = strtok(NULL,separators);
strcpy(altitude_cor,token);
token = strtok(NULL,separators);
token = strtok(NULL,separators);
int n=atoi(nb_satellites); // calcul du nombre de satellites
printf("n=%d date = %s \n",n,date);
if (n>3&&(strcmp(date,"")!=0)) { // si precision correcte on envoie la trame et on éteint le GPS
// enregistrement des donnees GPS dans la carte SD
// serial_port.attach(0,SerialBase::RxIrq);
Led_rouge=0;
// Montage de la carte SD (Led rouge temoin allumee 1s)
Led_rouge=0;
thread_sleep_for (1000);
// Montage du systeme de fichier
BlockDevice *bd = BlockDevice::get_default_instance();
FATFileSystem fs("fs");
printf("Montage de la carte SD ... : ");
int err = fs.mount(bd);
if (err!=0) printf("ERREUR !!! \n"); // Message d'erreur
else {
printf("OK \n");
Led_rouge=1;
}
//thread_sleep_for (1000);
printf("Opening \"/fs/test1.txt\"... : ");
FILE *fichier = fopen("/fs/test1.txt", "a");
if (fichier==NULL) printf("ERREUR !!! \n"); // Message d'erreur
else {
printf(" OK \n");
fprintf (fichier,"%s : %s : %s : %s : %s\n : %s \n ",date,horaire,lattitude, longitude,altitude,nb_satellites);
fclose(fichier); // Fermeture du fichier test1.txt
err = fs.unmount();
Cmd_GPS=0; // on eteintle GPS lors d'une lecture correcte
Led_rouge=1;
// on charge la nouvelle alarme
horloge.get_time(&time); // lecture heure courante sur htr
printf ("Programme Lance a %d : %d : %d \n",time.hours,time.minutes,time.seconds);
secondes=time.seconds;
minutes=time.minutes+1;
heures=time.hours;
if (secondes>60) {
secondes = secondes-60;
minutes=minutes+1; // calcul temps alarme prochain
if (minutes >59) {
minutes=minutes-60;
heures = heures+1;
if (heures>23) heures=0;
}//fin minutes
}//fin secondes
alarm.seconds=secondes;
alarm.minutes=minutes;
alarm.hours=heures;
alarm.am1=0; // on declenche ici une alarme à l'heure reglee
alarm.am2=0;
alarm.am3=0;
alarm.am4=1;
test = horloge.set_alarm(alarm, 1);
// on coupe l'alarme alim repart a l'alerme programmee
DS_3231_cntl_stat_t data = {0x05, 0x00}; // configuration registres etat et controle
horloge.set_cntl_stat_reg(data);
}// fin else
}// fin n>4
} // fin compare chaines GGA
} //fin traitement chaine sans erreur de crc
flag_ISR_read=0; // raz ISR
} //fin if (flag_ISR_read==1)
}// fin while true
}// fin programme principal
}
}
*/
/******* Sous fonctions ***************/
void ISR_read() { // lecture liaison serie
char carac;
capt.read(&carac, 1);
if (index==100 || carac=='$') index=0;
Rx_buffer[index]=carac;
index++;
if (carac==0x0a) {
Rx_buffer[index]=0;
for (char i=0; i<index+1; i++) trame[i] = Rx_buffer[i];
index=0;
flag_ISR_read=1;
}
}
uint8_t gencrc2(uint8_t *data) { // calcul crc NMEA
uint8_t crc;
crc=data[1];
char i=2;
while (data[i]!=0) {
crc = crc^data[i];
i++;
}
return crc;
}