ENVIA MENSAJE Y RESPONDE CON LINK DE GOOGLE MAPS, ENVIA ON PRENDE UN LED Y RESPONDE AL CELULAR Q SE ENCENDIO EL CARRO, ENVIA OFF APAGA LED Y RESPONDE ALCELULKAR QUE SE APAGO ELO CARRO

Dependencies:   DebouncedIn GPS7 mbed

Fork of CLASEDELGSM by Gustavo Ramirez

Revision:
3:75c530fb1140
Parent:
2:ffcd0494ec92
diff -r ffcd0494ec92 -r 75c530fb1140 main.cpp
--- a/main.cpp	Wed May 27 23:19:02 2015 +0000
+++ b/main.cpp	Tue Jun 23 17:10:06 2015 +0000
@@ -1,59 +1,442 @@
-//  Programa para enviar desde la FRDMKL25Z un mensaje de texto en formatp PDU
-//   Como modem usa un celular SIEMENS a56i
-//   ENVIAR -----la palabra "ALARMA 1"
-//
-
-//   OJO CON ESTO
-//   conector del siemens cable verde es RX conectelo a PTE0  cable blanco es TX conectelo a PTE1
 #include "mbed.h"
 #include "DebouncedIn.h"
 #include "stdio.h"
 #include "string.h"
-#include  "math.h"
-#include  "gprs.h"
+#include "math.h"
+#include "gprs.h"
 #include "GPS.h"
+
 Timer t;
+int readBuffer(char *buffer,int count);
+void enviar(int LENIN1,char *DS,char *numero);//longitud de mensaje, cadena del mensaje, cadena del celular
 DigitalOut on_off(PTC17);  //señal que inicia el envio del mensaje
 Serial pc(USBTX,USBRX); //Configura puerto UART de la FRDMKL25Z
-GPS gps(PTE22, PTE23);
-//Serial GSM(USBTX,USBRX);
-//serial (PTA2,PTA1);/////////////////USBTX,USBRX  PTE0,PTE1
-Serial GSM(PTE0,PTE1);//Configura puerto USB a la consola serial del PC conectado.
-//void leer(void);
-void Rx_interrupt();
-int position=0;
-int lenpack=6;
-int longi=0;
-char lat1[9];
-char lon1[12];
-char tel[11];
+GPS gps(PTE0,PTE1);
+Serial GSM(PTE22, PTE23);//Configura puerto USB a la consola serial del PC conectado.
+char celular[10];
 char buffer[100];
-char buffer1[13];
-char datos[100];
-char NUMBER[13];
-int index;
-char buffermsg[100];
-int count1=0;
-int i = 0;
-int j = 0;
-char c;
-unsigned char CtrlZ = 0x1A;  // comodin de emision controlZ
-bool Flag = false; // bandera
-char r[]=""; //Cadena de recepcion de la trama PDU si se usa!!
-char msg[256];
-int z=0,g=0;
-char char1;
-char DE[255]={"http://maps.google.com/maps?q="};
+char DE1[255]={"http://maps.google.com/maps?q="};
+char DE[255];
+char DS[255];
+char coorden[50]={NULL};
+char coorden_org[50]={NULL};
 
-char DS[255];
-int K,C,LENOUT,LENIN;
-int frac_long, frac_lat,ent_long,ent_lat;
+char msg[4];
+int z=0,j=0,i=0,p=0,LENIN=0,LENIN1=0,count=0,flag=0,K=0,C=0,LENOUT=0; 
+int ent_long,ent_lat;
+int negativo=0;
+int frac_long, frac_lat,bandera=0;         
 
 
 
-int readBuffer(char *buffer,int count)
+int main(void)
 {
-    int i=0; 
+
+    GSM.baud(9600);//configura los baudios de la FRDMKL25Z en 9600
+    GSM.format(8,Serial::None,1); //configura el formato de los datos de la UART
+    wait(1);
+    pc.printf("Conectado\r\n");
+    
+    while(1) {
+            
+                       
+            
+            if (GSM.readable()) 
+            {
+                
+                readBuffer(buffer,100);
+                pc.printf(buffer);
+            }                
+                
+                if((buffer[10]=='S')&& (buffer[11]=='M'))
+                    {
+                    pc.printf("Llego mensaje\r\n ");         
+                    buffer[10]=' ';
+                    buffer[11]=' ';
+                    GSM.printf("AT+CMGR=%c\r\n",buffer[14]);
+                    pc.printf("AT+CMGR=%c\r\n",buffer[14]);
+                    }
+            
+          
+                
+                //va funcion de mensaje recibido           
+           
+           
+                        
+             
+                
+                
+     if(buffer[85]=='4'&& buffer[86]=='F'&&buffer[87]=='3'&& buffer[88]=='7')//recibe mensaje On
+             {
+                 
+                 for(i=55;i<65;i++)
+            {
+                celular[i-55]=buffer[i];
+            }
+           
+             on_off=1;
+             pc.printf("El mensaje es On\r\n");
+             buffer[85]=' ';
+             buffer[86]=' ';
+             buffer[87]=' ';
+             buffer[88]=' ';
+             GSM.printf("AT\r\n");//proto0colo  envio de mensaje
+       wait(0.5);
+       GSM.printf("AT+CNMI=1,1\r\n");
+       wait(0.5);
+       GSM.printf("AT+CMGF=0\r\n");
+       wait(0.5);
+       GSM.printf("ATE\r\n");
+       wait(0.5);
+       GSM.printf("CBST=0,0,1\r\n");
+       wait(0.5);  
+       GSM.printf("at+cmgs=%d\r\n",21);
+        pc.printf("at+cmgs=%d\r\n",21);
+             wait(0.5);
+       
+       GSM.printf("0011000A91"); 
+       pc.printf("0011000A91"); 
+       for(p=0;p<10;p++)
+       {
+        GSM.printf("%c",celular[p]);
+        pc.printf("%c",celular[p]);
+        }
+       
+       GSM.printf("0000AA0945F7B8EC26A7C96F"); //12 octetos + los octetos del mensaje---devuelve mensaje encendido enciende un led que esta en PTC17
+       pc.printf("0000AA0945F7B8EC26A7C96F"); 
+       
+         wait(0.5);
+           GSM.putc(0x1A); 
+           
+             }
+             
+             
+             
+             
+             
+             
+              if(buffer[85]=='4'&& buffer[86]=='F'&&buffer[87]=='B'&& buffer[88]=='3'&&buffer[89]=='1'&& buffer[90]=='9')//recibe mensaje Off
+             {
+                 
+                 for(i=55;i<65;i++)
+            {
+                celular[i-55]=buffer[i];
+            }
+           
+             on_off=0;
+             pc.printf("El mensaje es Off\r\n");// se borra el mensaje
+             buffer[85]=' ';
+             buffer[86]=' ';
+             buffer[87]=' ';
+             buffer[88]=' ';
+             buffer[89]=' ';
+             buffer[90]=' ';
+             GSM.printf("AT\r\n");//protocolo de envio de mensaje  
+       wait(0.5);
+       GSM.printf("AT+CNMI=1,1\r\n");
+       wait(0.5);
+       GSM.printf("AT+CMGF=0\r\n");
+       wait(0.5);
+       GSM.printf("ATE\r\n");
+       wait(0.5);
+       GSM.printf("CBST=0,0,1\r\n");
+       wait(0.5);  
+       GSM.printf("at+cmgs=%d\r\n",20);
+        pc.printf("at+cmgs=%d\r\n",20);
+             wait(0.5);
+           
+        GSM.printf("0011000A91"); 
+        pc.printf("0011000A91"); 
+        for(p=0;p<10;p++)
+        {
+        GSM.printf("%c",celular[p]);
+        pc.printf("%c",celular[p]);
+        }
+           
+           GSM.printf("0000AA074178F81C26BF01"); //12 octetos + los octetos del mensaje----devuelve mensaje apagado--apaga el led  de PTC17
+           pc.printf("0000AA074178F81C26BF01"); 
+             wait(0.5);
+           GSM.putc(0x1A); 
+            
+             }
+             
+                
+                
+                
+                
+             
+             
+                
+                
+                
+                //celular comienza en posicion 48
+                //longitud del mensaje en posicion 83,84
+                //mensaje en posicion 85
+         if((buffer[83]=='0')&&(buffer[84]=='4'))
+         {
+             buffer[83]=' ';
+             buffer[84]=' ';
+             
+         count=0;
+         LENIN1=4; //numero de caracteres de abajo son los octetos
+         for(i=0;i<8;i++)
+         {
+         
+         if(i%2==0)
+         {
+            if(buffer[85+i]>57)
+            {
+                DE[count]=(buffer[85+i]-55)<<4;
+            }
+            else
+            {
+                DE[count]=(buffer[85+i]-48)<<4;
+            }        
+          }
+          else
+          {     
+                if(buffer[85+i]>57)
+            {
+                DE[count]=(buffer[85+i]-55)+DE[count];
+            }
+            else
+            {
+                DE[count]=(buffer[85+i]-48)+DE[count];
+            } 
+            count++;
+          }  
+         
+         }
+         
+         
+         
+         
+         LENOUT= LENIN1*8/7;
+         K=7;
+         C=0;
+         DS[0]=DE[0] & 0x7F;  // la primera sola
+         pc.printf("%2X",DS[0]);
+         for (i=1;i < LENOUT;i++){  // inicia el algoritmo
+         DS[i]=(DE[i-1-C]>>K | DE[i-C]<<(8-K))& 0x7F;  //valido para todos
+         pc.printf("%2X",DS[i]);
+         if (K==0) {K=8;C++;}
+         K--;
+         }
+         for (i=0;i < LENOUT;i++){
+         pc.printf("%c",DS[i]);
+         msg[i]=DS[i];
+         }
+         pc.printf("\r\n");
+         pc.printf("Mensaje en el celular\r\n"); 
+         pc.printf(buffer);
+         
+            if((msg[0]=='C')&&(msg[1]=='o')&&(msg[2]=='r')&&(msg[3]=='d'))
+            {
+                //obtener coordenadas
+                pc.printf("Coordenadas GPS\r\n");
+
+                if(gps.sample()) { 
+                    ent_long=(int)gps.longitude;
+                    ent_lat=(int)gps.latitude;
+                    
+                    frac_long=abs((gps.longitude-(int)gps.longitude)*1000000);
+                    frac_lat=abs((gps.latitude-(int)gps.latitude)*1000000);
+                    
+                pc.printf("http://maps.google.com/maps?q=%f,%f\r\n", gps.latitude,gps.longitude);
+                            //coordenada longitud a cadena
+            
+           
+            while(frac_long>0)
+            {
+                coorden[j]=(frac_long%10)+48;
+                frac_long=frac_long/10;
+                pc.printf("%c",coorden[j]);
+                j++;
+            }
+            coorden[j]='.';
+            pc.printf("%c",coorden[j]);
+            j++;
+            if(ent_long<0)
+            {
+                negativo=1;
+                ent_long=ent_long*-1;
+            }
+            while(ent_long>0)
+            {
+                coorden[j]=(ent_long%10)+48;
+                ent_long=ent_long/10;
+                pc.printf("%c",coorden[j]);
+                j++;
+            }
+            
+            if(negativo==1)
+            {
+                coorden[j]='-';
+                pc.printf("%c",coorden[j]);
+                j++;
+            }
+            negativo=0;
+            
+            coorden[j]=',';
+                pc.printf("%c",coorden[j]);
+                j++;
+            
+            
+            while(frac_lat>0)
+            {
+                coorden[j]=(frac_lat%10)+48;
+                frac_lat=frac_lat/10;
+                pc.printf("%c",coorden[j]);
+                j++;
+            }
+            coorden[j]='.';
+            pc.printf("%c",coorden[j]);
+            j++;
+            if(ent_lat<0)
+            {
+                negativo=1;
+                ent_lat=ent_lat*-1;
+            }
+            while(ent_lat>0)
+            {
+                coorden[j]=(ent_lat%10)+48;
+                ent_lat=ent_lat/10;
+                pc.printf("%c",coorden[j]);
+                j++;
+            }
+            if(negativo==1)
+            {
+                coorden[j]='-';
+                pc.printf("%c",coorden[j]);
+                j++;
+            }
+            pc.printf("\r\n");
+            j--;
+            z=strlen(DE1);
+            
+            for(i=j;i>=0;i--)
+            {
+                DE1[z]=coorden[i];
+                z++;
+            }
+                
+                pc.printf(DE1);
+                pc.printf("\r\n");
+            
+                //strlen(DE);
+                
+                LENIN1=strlen(DE1);
+               K=0;C=0;
+         for (i=0;i < LENIN1;i++){
+         DS[i]=DE1[i+C]>>K | DE1[i+C+1]<<(7-K);
+         if (DS[i]==0x00) {LENOUT=i; goto salir;}
+         K++;
+         if (K==7) {K=0;C++;} // se chequea que ya se acabaron los bits en un ciclo de conversion.
+         }
+
+         //--------------------------------------------------------------
+salir:  
+            
+           //Tomamos el numero de celular
+            for(i=55;i<65;i++)
+            {
+                celular[i-55]=buffer[i];
+            }
+           enviar(LENIN1,DS,celular);     
+                
+                
+                
+                
+                }              
+            }
+         
+         
+         
+         
+         }       
+                 
+         
+            
+            
+}}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+void enviar(int LENIN1,char *DS,char *numero){
+       
+       GSM.printf("AT\r\n");//envia link con coordenadas
+       pc.printf("AT\r\n");//envia link con coordenadas
+       wait(0.5);
+       GSM.printf("AT+CNMI=1,1\r\n");
+       pc.printf("AT+CNMI=1,1\r\n");
+       wait(0.5);
+       GSM.printf("AT+CMGF=0\r\n");
+       pc.printf("AT+CMGF=0\r\n");
+       wait(0.5);
+       GSM.printf("ATE\r\n");
+       pc.printf("ATE\r\n");
+       wait(0.5);
+       GSM.printf("CBST=0,0,1\r\n");
+       pc.printf("CBST=0,0,1\r\n");
+       wait(0.5);  
+       GSM.printf("at+cmgs=%d\r\n",13+LENIN1);
+       pc.printf("at+cmgs=%d\r\n",13+LENIN1);
+       wait(0.5);
+       GSM.printf("0011000A91"); 
+       pc.printf("0011000A91"); 
+       for(p=0;p<10;p++)
+       {
+        GSM.printf("%c",celular[p]);
+        pc.printf("%c",celular[p]);
+        }
+       
+       GSM.printf("0000AA"); 
+       pc.printf("0000AA"); 
+       
+           if(LENIN1<=15)
+           {
+                pc.printf("0");
+                GSM.printf("0");
+                pc.printf("%X",LENIN1);
+                GSM.printf("%X",LENIN1);
+               }
+               else{    
+         pc.printf("%2X",LENIN1);
+         GSM.printf("%2X",LENIN1);
+         }
+           
+           for (p=0;p < LENIN1;p++){
+           if(DS[p]<=15)
+           {
+                pc.printf("0");
+                GSM.printf("0");
+                pc.printf("%X",DS[p]);
+                GSM.printf("%X",DS[p]);
+               }
+               else{    
+         pc.printf("%2X",DS[p]&0x000000FF);
+         GSM.printf("%2X",DS[p]&0x000000FF);
+         }
+         }
+           wait(0.5);
+           GSM.putc(0x1A);  
+           pc.putc(0x1A);
+           
+}
+
+int readBuffer(char *buffer,int count){
+    
     t.start(); 
     while(1) {
         while (GSM.readable()) {
@@ -75,222 +458,3 @@
     }
     return 0;
 }
-
-
-int main(void)
-{
-
-    GSM.baud(9600);//configura los baudios de la FRDMKL25Z en 9600
-    GSM.format(8,Serial::None,1); //configura el formato de los datos de la UART
-   
-    while(1) {
-        //wait(2);
-        
-        
-        
-            if (GSM.readable()) {
-            readBuffer(buffer,100);
-            
-            if(buffer[10]=='S'&& buffer[11]=='M'){
-             pc.printf("Llego mensaje\n\r ");         
-             buffer[10]=' ';
-             buffer[11]=' ';
-             GSM.printf("AT+CMGR=%c\r\n",buffer[14]);
-             pc.printf("AT+CMGR=%c\r\n",buffer[14]);
-             }
-             
-             if(buffer[68]=='C'&& buffer[69]=='3'&&buffer[70]=='B'&& buffer[71]=='7'&&buffer[72]=='1'&& buffer[73]=='C')//recibe mensaje Cor
-             {
-                 
-             pc.printf("El mensaje es Cor\n");
-             buffer[68]=' ';
-             buffer[69]=' ';
-             buffer[70]=' ';
-             buffer[71]=' ';
-             buffer[72]=' ';
-             buffer[73]=' ';
-             
-                
-                     if(gps.sample()) {
-            frac_long=(int)abs(1000000*gps.longitude-(int)gps.longitude);
-            ent_long=(int)gps.longitude;
-            frac_lat=(int)abs(1000000*gps.latitude-(int)gps.latitude);
-            ent_lat=(int)gps.latitude;
-           // pc.printf("%d.%d,%d.%d\n",ent_long,frac_long,ent_lat,frac_lat);
-            
-            lat1[0]=ent_lat+48;
-            lat1[1]='.';
-            
-            for(z=6;z>=0;z--)
-            {
-            g= pow(10,0.0+z);   
-            lat1[8-z]=(frac_lat/g)+48;
-            frac_lat=frac_lat%g;
-            }
-            
-            
-            lon1[0]='-';
-            ent_long=ent_long*-1;
-            lon1[1]=(ent_long/10)+48;
-            lon1[2]=(ent_long%10)+48;
-            lon1[3]='.';
-            for(z=7;z>=0;z--)
-            {
-            g= pow(10,0.0+z); 
-            lon1[11-z]=(frac_long/g)+48;
-            frac_long=frac_long%g;
-            }
-            
-       
-            
-            
-        }
-         
-         
-         LENIN=30;
-         
-              for(i=LENIN;i<(LENIN+9);i++)
-                {
-                    DE[i]=lat1[i-LENIN];
-                }
-             
-         LENIN=LENIN+9;
-         
-             DE[i]=',';
-             
-             LENIN=LENIN+1;
-             
-             for(i=LENIN;i<(LENIN+12);i++)
-         {
-             DE[i]=lon1[i-LENIN];
-         }
-         LENIN=LENIN+12;
-          
-         
-         
-         K=0;C=0;
-         for (i=0;i < LENIN;i++){
-         DS[i]=DE[i+C]>>K | DE[i+C+1]<<(7-K);
-         if (DS[i]==0x00) {LENOUT=i; goto salir;}
-         K++;
-         if (K==7) {K=0;C++;} // se chequea que ya se acabaron los bits en un ciclo de conversion.
-         }
-
-         //--------------------------------------------------------------
-salir: 
-        
-                   GSM.printf("AT\r\n");//envia link con coordenadas
-       wait(0.5);
-       GSM.printf("AT+CNMI=1,1\r\n");
-       wait(0.5);
-       GSM.printf("AT+CMGF=0\r\n");
-       wait(0.5);
-       GSM.printf("ATE\r\n");
-       wait(0.5);
-       GSM.printf("CBST=0,0,1\r\n");
-       wait(0.5);  
-       GSM.printf("at+cmgs=%d\r\n",13+LENIN);
-        pc.printf("at+cmgs=%d\r\n",13+LENIN);
-             wait(0.5);
-           GSM.printf("0011000A9113315442760000AA"); //12 octetos + los octetos del mensaje
-           pc.printf("0011000A9113315442760000AA"); //12 octetos + los octetos del mensaje
-           //3113452467
-           if(LENIN<=15)
-           {
-                pc.printf("0");
-                GSM.printf("0");
-                 pc.printf("%X",LENIN);
-                GSM.printf("%X",LENIN);
-               }
-               else{    
-         pc.printf("%2X",LENIN);
-         GSM.printf("%2X",LENIN);
-         }
-           
-           for (i=0;i < LENIN;i++){
-           if(DS[i]<=15)
-           {
-                pc.printf("0");
-                GSM.printf("0");
-                 pc.printf("%X",DS[i]);
-                GSM.printf("%X",DS[i]);
-               }
-               else{    
-         pc.printf("%2X",DS[i]&0x000000FF);
-         GSM.printf("%2X",DS[i]&0x000000FF);
-         }
-         }
-           //GSM.printf("%X",68); //12 octetos + los octetos del mensaje
-           //GSM.printf("9");//GSM.printf("%u",30);
-             wait(0.5);
-           GSM.putc(0x1A);  
-  
-                  }
-             
-             
-        }
-    
-     if(buffer[68]=='4'&& buffer[69]=='F'&&buffer[70]=='3'&& buffer[71]=='7')//recibe mensaje On
-             {
-             on_off=1;
-             pc.printf("El mensaje es On\n");
-             buffer[68]=' ';
-             buffer[69]=' ';
-             buffer[70]=' ';
-             buffer[71]=' ';
-             GSM.printf("AT\r\n");//proto0colo  envio de mensaje
-       wait(0.5);
-       GSM.printf("AT+CNMI=1,1\r\n");
-       wait(0.5);
-       GSM.printf("AT+CMGF=0\r\n");
-       wait(0.5);
-       GSM.printf("ATE\r\n");
-       wait(0.5);
-       GSM.printf("CBST=0,0,1\r\n");
-       wait(0.5);  
-       GSM.printf("at+cmgs=%d\r\n",21);
-        pc.printf("at+cmgs=%d\r\n",21);
-             wait(0.5);
-           GSM.printf("0011000A9113315442760000AA0945F7B8EC26A7C96F"); //12 octetos + los octetos del mensaje---devuelve mensaje encendido enciende un led que esta en PTC17
-           pc.printf("0011000A9113315442760000AA0945F7B8EC26A7C96F"); 
-             wait(0.5);
-           GSM.putc(0x1A); 
-             
-             }
-             
-              if(buffer[68]=='4'&& buffer[69]=='F'&&buffer[70]=='B'&& buffer[71]=='3'&&buffer[72]=='1'&& buffer[73]=='9')//recibe mensaje Off
-             {
-             on_off=0;
-             pc.printf("El mensaje es Off\n");// se borra el mensaje
-             buffer[68]=' ';
-             buffer[69]=' ';
-             buffer[70]=' ';
-             buffer[71]=' ';
-             buffer[72]=' ';
-             buffer[73]=' ';
-             GSM.printf("AT\r\n");//protocolo de envio de mensaje  
-       wait(0.5);
-       GSM.printf("AT+CNMI=1,1\r\n");
-       wait(0.5);
-       GSM.printf("AT+CMGF=0\r\n");
-       wait(0.5);
-       GSM.printf("ATE\r\n");
-       wait(0.5);
-       GSM.printf("CBST=0,0,1\r\n");
-       wait(0.5);  
-       GSM.printf("at+cmgs=%d\r\n",20);
-        pc.printf("at+cmgs=%d\r\n",20);
-             wait(0.5);
-           GSM.printf("0011000A9113315442760000AA074178F81C26BF01"); //12 octetos + los octetos del mensaje----devuelve mensaje apagado--apaga el led  de PTC17
-           pc.printf("0011000A9113315442760000AA074178F81C26BF01"); 
-             wait(0.5);
-           GSM.putc(0x1A); 
-           
-             }
-             
-
-}
-
-
-}
-// buffer[i] contiene lo que regresa
\ No newline at end of file