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.
Fork of GPS_GSM by
main.cpp
00001 #include "mbed.h" 00002 #include "MODSERIAL.h" 00003 #include "MACROS.h" 00004 #include <string> 00005 #include "stdio.h" 00006 #include "Serial.h" 00007 #include <cstdlib> 00008 00009 00010 char position[16]={"45.1234,1.12345"}; 00011 char reponse_gsm[256]; 00012 int element=0; 00013 00014 // variables gps 00015 00016 DigitalOut led5(p7); 00017 00018 00019 char debut_trame[7]="XXXXXX"; 00020 char trame[81] ; 00021 char lat[8]; 00022 char lat_NS; 00023 char lon[9]; 00024 char lon_EW; 00025 float lat_cnv; 00026 float lon_cnv; 00027 int lat_deg = 0; 00028 float lat_dec; 00029 int lon_deg = 0; 00030 float lon_dec; 00031 char GPS [25]; 00032 char rx ; 00033 int e ; 00034 int pos_ok = 0 ; 00035 00036 // liaisons series 00037 Serial pc(USBTX, USBRX, 115200); 00038 MODSERIAL gsm(p9,p10); 00039 Serial gps(p13, p14, 4800); 00040 00041 00042 00043 DigitalOut pwrkey(p21); 00044 DigitalOut emerg_off(p22); 00045 DigitalIn status(p11); 00046 00047 void reception(); 00048 void clear_pc(void); 00049 void clear_buffer(void); 00050 void power_on(); 00051 void init_gsm(); 00052 void data_gsm(); 00053 00054 int main() { 00055 00056 for (int i=0;i<20;i++) reponse_gsm[i]=0; //initialisation tableau réponse 00057 00058 gsm.baud(115200); 00059 // pc.baud(115200); 00060 00061 clear_buffer(); 00062 pc.printf("Le buffer serie est vide\r\n"); 00063 wait(2); 00064 clear_pc(); 00065 pc.printf("Le buffer pc est vide\r\n"); 00066 00067 init_gsm(); 00068 pc.printf("gsm initialised \r\n"); 00069 clear_buffer(); 00070 /* 00071 gsm.printf("AT+CMGD=1,4\r"); 00072 wait(0.5); 00073 pc.printf("Memoire sms est vide\r\n"); 00074 */ 00075 while(1) 00076 { 00077 // debut gps 00078 led5=0; 00079 pc.printf("debut GPS "); 00080 e = 0 ; 00081 pos_ok= 0 ; 00082 00083 while(pos_ok==0 ) { 00084 if (gps.readable()) 00085 { 00086 rx = gps.getc(); 00087 // pc.printf("%c",rx); 00088 for(int i = 0; i <80; i++) 00089 { 00090 trame[i] = trame[i+1]; 00091 00092 } 00093 trame[76]=rx ; 00094 } 00095 strncpy(debut_trame,trame,6); 00096 00097 if (strcmp(debut_trame,"$GPGGA") == 0) 00098 { 00099 00100 00101 for (int i = 0; i<=8; i++) 00102 { 00103 lat[i] = trame [18+i]; 00104 lat_cnv = atof(lat); 00105 } 00106 lat_NS = trame [28]; 00107 00108 for (int a=0; a<=9; a++) 00109 { 00110 lon[a] = trame [30+a]; 00111 lon_cnv = atof(lon); 00112 } 00113 lon_EW = trame [41]; 00114 00115 lat_cnv = lat_cnv/100; 00116 lat_deg = (int)lat_cnv; 00117 lat_dec = lat_cnv - lat_deg; 00118 lat_dec = (lat_dec/60)*100; 00119 lat_cnv = lat_deg + lat_dec; 00120 00121 lon_cnv = lon_cnv/100; 00122 lon_deg = (int)lon_cnv; 00123 lon_dec = lon_cnv - lon_deg; 00124 lon_dec = (lon_dec/60)*100; 00125 lon_cnv = lon_deg + lon_dec; 00126 00127 if (lat_NS=='N') 00128 { 00129 lat_cnv = lat_cnv*1; 00130 } 00131 00132 else if (lat_NS=='S') 00133 { 00134 lat_cnv = lat_cnv*(-1); 00135 } 00136 00137 if (lon_EW=='E') 00138 { 00139 lat_cnv = lat_cnv*1; 00140 } 00141 00142 else if (lon_EW=='W') 00143 { 00144 lat_cnv = lat_cnv*(-1); 00145 } 00146 sprintf(GPS, "POS: %f,%f", lat_cnv, lon_cnv); 00147 00148 pos_ok = 1 ; 00149 } 00150 00151 } // fin while pos_ok 00152 00153 pc.printf("%s \n\r", GPS); 00154 reception(); 00155 wait(5); 00156 } 00157 00158 } 00159 00160 00161 void init_gsm(){ 00162 /* 00163 pwrkey=1; 00164 wait(5); 00165 pwrkey=0; 00166 */ 00167 power_on(); 00168 wait(3); 00169 pc.printf("AT\r\n"); 00170 gsm.printf("AT\r"); 00171 // wait(0.5); 00172 pc.printf("AT\r\n"); 00173 data_gsm(); 00174 00175 gsm.printf("AT+CPIN=1234\r"); // je rentre le code pin 00176 wait(5); 00177 pc.printf("AT+CPIN=1234\r\n"); 00178 data_gsm(); 00179 00180 gsm.printf("ATE0\r"); // pas d'echo vers le DTE pour les l'envoi des commandes 00181 wait(5); 00182 00183 00184 gsm.printf("AT+CMGF=1\r"); // je passe en mode texte 00185 wait(2); 00186 00187 00188 gsm.printf("AT+CNMI=2,1,0,0,0\r"); 00189 wait(2); 00190 00191 00192 } 00193 00194 00195 void data_gsm(){ 00196 for(int i=0;i<255;i++) reponse_gsm[i]=0; 00197 element=0; 00198 while(gsm.readable()){ 00199 reponse_gsm[element]=gsm.getc(); 00200 element++; 00201 } 00202 for(int i=0; i<element; i++)pc.printf("%c",reponse_gsm[i]); 00203 pc.printf("\n\r"); 00204 00205 } 00206 00207 void power_on(){ 00208 pwrkey=1; 00209 wait(3); 00210 while(status==0); 00211 wait(1); 00212 pwrkey=0; 00213 pc.printf("Le module est connecter ! \r\n"); 00214 } 00215 00216 void clear_buffer(){ 00217 char char1; 00218 while(gsm.readable()){ 00219 char1 = gsm.getc(); 00220 } 00221 return; 00222 } 00223 00224 void clear_pc(){ 00225 char char1; 00226 while(pc.readable()){ 00227 char1 = pc.getc(); 00228 pc.printf("Le buffer pc est vide\r\n"); 00229 } 00230 return; 00231 } 00232 00233 void reception(){ 00234 int nouveau_message=0 ; 00235 char sms[256]; 00236 char sms_brut[256]; 00237 char num_mem[3]; 00238 int longueur_msg1=0; 00239 int longueur_msg2=0; 00240 pc.printf("Reception ! \n"); 00241 // attente notification 00242 while(gsm.readable()){ 00243 sms[longueur_msg1] = gsm.getc(); 00244 pc.printf("%c ",sms[longueur_msg1]); 00245 longueur_msg1++; 00246 nouveau_message = 1 ; 00247 } 00248 // si notification 00249 if (nouveau_message == 1) 00250 { 00251 num_mem[0]=sms[14]; 00252 num_mem[1]=sms[15]; 00253 num_mem[2]='\0'; 00254 pc.printf("Notification recue : %s, longueur_msg= %d", sms, longueur_msg1); 00255 pc.printf("Numero memoire : %s", num_mem ); 00256 // lecture memoire sms 00257 pc.printf("AT+CMGR=%s",num_mem ); 00258 gsm.printf("AT+CMGR=%s\r",num_mem ); 00259 wait(1); 00260 longueur_msg2 = 0 ; 00261 while(gsm.readable()){ 00262 sms[longueur_msg2] = gsm.getc(); 00263 // pc.printf("%c ",sms[longueur_msg2]); 00264 longueur_msg2++; 00265 } 00266 sms[longueur_msg2] = '\0'; // le dernier caractere = fin de chaine ! 00267 pc.printf("sms recu : %s, longueur_msg= %d \n", sms, longueur_msg2); 00268 // suppression en tete 00269 for (int i=0;i<100;i++) 00270 { 00271 sms_brut[i]= sms[66+i]; 00272 } 00273 // affichage message brut 00274 pc.printf("sms brut : %s", sms_brut); 00275 00276 if (strncmp(sms_brut,"POS?",4)==0) 00277 { pc.printf("Demande de position !"); 00278 gsm.printf("AT+CMGS=\"0618981390\"\r"); 00279 gsm.printf("%s",GPS); 00280 gsm.putc(0x1A); 00281 nouveau_message = 0 ; 00282 } 00283 00284 } 00285 00286 00287 00288 00289 00290 }
Generated on Fri Jul 15 2022 12:18:42 by
1.7.2
