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.
Diff: main.cpp
- Revision:
- 30:2457f9928392
- Parent:
- 29:fbc5f53d8d0f
- Child:
- 31:73be27ad5d69
diff -r fbc5f53d8d0f -r 2457f9928392 main.cpp --- a/main.cpp Mon Jun 20 14:38:44 2022 +0000 +++ b/main.cpp Tue Jun 21 09:38:40 2022 +0000 @@ -4,6 +4,8 @@ /**************************************************************/ void ISR_read(); // lecture liaison serie uint8_t gencrc2(uint8_t *data); // calcul crc NMEA +void lire_luminosite(); +void lire_WX200(); DigitalOut myled(PTB22); DigitalOut CMD_200WX(PTA1,0); // initialisation de la liaison serie vers le capteur WX200 @@ -45,17 +47,17 @@ 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 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 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 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 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 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]; @@ -72,15 +74,72 @@ int main() { - // autorise departs en interruption liaison serie + while(true) + { + // lecture luminosite + printf ("mesure luminosite \n"); + lire_luminosite(); + printf ("lecture WX200 \n"); + lire_WX200(); + printf ("attente 5s.... \n"); + thread_sleep_for (5000); + } +} // fin programme + + + +/******* 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; + } +} + +/************** Lecture Pyranometre ************************/ +void lire_luminosite() +{ + DigitalOut Cmd_Pyr(PTA2); + AnalogIn ain0(A0); + Cmd_Pyr=1; // on alimente le pyranometre! + printf("Alimentation du pyranometre, mesures dans 10s... \n"); + thread_sleep_for (10000); // on laisse 10 s pour la mesure + const float Vcc=3.3; + float tension= Vcc*ain0.read(); + int lum_Wm=tension*1000+(-400); + float lum_lux=lum_Wm*683; + //pc.printf("ain0 : %1.3f V \n\n",ain0.read()); + //pc.printf("mesure : %1.3f V \n\n",tension); + printf("luminosite : %d W/m^2 \n\n",lum_Wm); + //pc.printf("luminosite : %1.0f lux \n\n",lum_lux); + printf ("coupure du pyranometre \n"); + Cmd_Pyr=0; // on coupe le pyranometre! +} + + +/************** Lecture Station WX200 ************************/ +void lire_WX200() +{ + // autorise departs en interruption liaison serie capt.attach(&ISR_read,SerialBase::RxIrq); // attente 3 secondes puis alimentation du capteur - thread_sleep_for (3000); + thread_sleep_for (500); + printf("lecture station meteo WX200 dans 10 s\n"); + printf("Lecture trame GPGGA \n"); + thread_sleep_for (10000); // on laisse 10 s de mesures 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 + while(flag_GPGGA==0) { // afaire en boucle tant que l'on a pas recu les infos // si une trame est recue if (flag_ISR_read==1) { + flag_ISR_read=0; //printf("%s",trame); // ligne de test gps char* token = strtok(trame,"*"); //on met dans trame_cpy la trame sans crc strcpy(trame_cpy,token); @@ -92,16 +151,15 @@ sscanf(crc,"%x",&val_crc2); // on teste la validite du crc recu if (val_crc!=val_crc2) { - // printf ("crc error\n"); + // printf ("crc error\n"); } else { // traitement de la trame en cas de crc correct - //printf ("crc OK\n"); + // 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); + printf("trame GPGGA : %s \n",trame); token = strtok(NULL,separators); if (strcmp(token,"")!=0) strcpy(horaire,token); token = strtok(NULL,separators); @@ -126,16 +184,43 @@ 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 - } + 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 + printf("GPGGA OK\n"); + } } // fin if GPGGA - - else if ((strcmp(type,"$WIMDA")==0)&&(flag_WIMDA==0)) { // traitement d'une trame WIMDA + } // fin else (traitement trame correcte) + }//fin if read enable + } // fin while flags ==0 + while(flag_WIMDA==0) { // afaire en boucle tant que l'on a pas recu les infos + // si une trame est recue + printf("Lecture trame WIMDA \n"); + if (flag_ISR_read==1) { + flag_ISR_read=0; + //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,"$WIMDA")==0)&&(flag_WIMDA==0)) { // traitement d'une trame WIMDA //capt.detach(); - //printf("trame WIMDA : %s \n",trame); + printf("trame WIMDA : %s \n",trame); token = strtok(NULL,separators); // champ1 if (strcmp(token,"")!=0)strcpy(pres_inch,token); token = strtok(NULL,separators); // champ2 @@ -148,20 +233,20 @@ 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); // 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); + 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 @@ -169,194 +254,61 @@ 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); + 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); + if (strcmp(token,"")!=0)strcpy(Wind_speed_ms,token); token = strtok(NULL,separators); // champ20 - if (strcmp(token,"")!=0)strcpy(M3,token); + 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 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 + // fin mesures station meteo + + printf("fin d'alimentation station meteo WX200 \n"); + CMD_200WX=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) + } - } - */ - /******* 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; - } + +/************** Calcul CRC ************************/ +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++; } - - 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; - } \ No newline at end of file + return crc; +} \ No newline at end of file