David Hernandez Vahos
/
RastreadorGSM
Rastreador GSM que manda mensajes de texto con un link de google maps desde un modem a un receptor.
main.cpp@3:8aef67673965, 2019-04-12 (annotated)
- Committer:
- tony63
- Date:
- Fri Apr 12 05:31:27 2019 +0000
- Revision:
- 3:8aef67673965
- Parent:
- 2:ea6275d1222f
- Child:
- 4:99bc22585808
RASTREO SATELITAL BASICO GSM
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tony63 | 3:8aef67673965 | 1 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
tony63 | 3:8aef67673965 | 2 | // Programa para establecer la comunicación con un módem Siemens A56 y un modulo FRDMKL25Z |
tony63 | 3:8aef67673965 | 3 | // opera como rastreador satelital para Geolocalizacion. |
tony63 | 3:8aef67673965 | 4 | // Por la UART (1) se conecta el MODEM y por la uart (2) el GPS (se lee en modo NEMEA) |
tony63 | 3:8aef67673965 | 5 | // Este sistema genera una cadena de geolocalizacion para GoogleMaps con las coordenadas locales |
tony63 | 3:8aef67673965 | 6 | // Si previamente se envia el mensaje (Coordenadas o coordenadas) |
tony63 | 3:8aef67673965 | 7 | // El sistema ademas recibe ordenes de tipo mensaje GSM PDU para accionar cargas |
tony63 | 3:8aef67673965 | 8 | // 1----Una supuesta valvula de combustible (On y Off.....on y off......) |
tony63 | 3:8aef67673965 | 9 | // 2----Una cantonera para cerradura electrica (Pulso o pulso) (pulso de 7 segundos) |
tony63 | 3:8aef67673965 | 10 | // Este sistema responde con un mensaje si el mensaje fue recibido (Mensaje Recibido) |
tony63 | 3:8aef67673965 | 11 | // |
tony63 | 3:8aef67673965 | 12 | // Adicionalmente este sistema mide un valor analogico en respuesta al mensaje..(Voltaje o voltaje) |
tony63 | 3:8aef67673965 | 13 | // El sistema dispone de un jumper que permite operar el sistema con o sin GPS (jumper a tierra) |
tony63 | 3:8aef67673965 | 14 | // El sistema detecta si el modem GSM esta bien conectado configurado y respondiendo correctamente |
tony63 | 3:8aef67673965 | 15 | // El sistema detecta si el GPS emite cadenas NEMEA y señaliza con un led si es exitosa la conexion |
tony63 | 3:8aef67673965 | 16 | // Presenta borrado automatico de SMS entrantes para evitar perdida de sincronismo en la deteccion |
tony63 | 3:8aef67673965 | 17 | // de cadenas y prefijos. |
tony63 | 3:8aef67673965 | 18 | // Este codigo compila sin problemas para un modulo chino bluepill STM32F103 |
tony63 | 3:8aef67673965 | 19 | // con las modificaciones adecuadas de puertos y leds |
joshema216 | 2:ea6275d1222f | 20 | |
tony63 | 3:8aef67673965 | 21 | //+++++++++++++++++++++++++++++++ARCHIVOS INCLUIDOS************************************************************************* |
procesadores_FAC | 0:b2a6aa7c0c8c | 22 | #include "mbed.h" |
procesadores_FAC | 0:b2a6aa7c0c8c | 23 | #include "DebouncedIn.h" |
procesadores_FAC | 0:b2a6aa7c0c8c | 24 | #include "stdio.h" |
procesadores_FAC | 0:b2a6aa7c0c8c | 25 | #include "string.h" |
procesadores_FAC | 0:b2a6aa7c0c8c | 26 | #include "GPS.h" |
joshema216 | 2:ea6275d1222f | 27 | |
procesadores_FAC | 0:b2a6aa7c0c8c | 28 | Timer t; |
tony63 | 3:8aef67673965 | 29 | //++++++++++++++++++++++++++++++++++++salidas y entradas digitales+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
procesadores_FAC | 0:b2a6aa7c0c8c | 30 | DigitalOut LedVerde(LED2); |
procesadores_FAC | 0:b2a6aa7c0c8c | 31 | DigitalOut LedRojo(LED1); |
procesadores_FAC | 0:b2a6aa7c0c8c | 32 | DigitalOut LedAzul(LED3); |
tony63 | 3:8aef67673965 | 33 | DigitalOut valvula(PTC5);//salida de la valvula |
tony63 | 3:8aef67673965 | 34 | DigitalOut puerta(PTC6);//salida de cerradura magnetica (en caso de usarlo en casa o desbloquear puertas de carro) |
tony63 | 3:8aef67673965 | 35 | DigitalIn sin_gps(PTA13); |
procesadores_FAC | 0:b2a6aa7c0c8c | 36 | |
joshema216 | 2:ea6275d1222f | 37 | // Entrada análoga |
joshema216 | 2:ea6275d1222f | 38 | AnalogIn v(PTB0); |
joshema216 | 2:ea6275d1222f | 39 | float medi; |
joshema216 | 2:ea6275d1222f | 40 | |
tony63 | 3:8aef67673965 | 41 | // Declaración de los puertos de la FRDM, Módem GSM y GPS. |
joshema216 | 2:ea6275d1222f | 42 | Serial GSM(PTE0,PTE1); // Puertos del FRDM para el Módem. |
procesadores_FAC | 0:b2a6aa7c0c8c | 43 | Serial pc(USBTX,USBRX); |
joshema216 | 2:ea6275d1222f | 44 | GPS gps(PTE22, PTE23); // Puerto del FDRM para el GPS. |
procesadores_FAC | 0:b2a6aa7c0c8c | 45 | |
joshema216 | 2:ea6275d1222f | 46 | // Declaración de variables |
joshema216 | 2:ea6275d1222f | 47 | // Cadenas de caracteres con las que se va a trabajar. |
joshema216 | 2:ea6275d1222f | 48 | char DE1[255]; |
joshema216 | 2:ea6275d1222f | 49 | char DS1[255]; |
joshema216 | 2:ea6275d1222f | 50 | char DE2[255]; |
joshema216 | 2:ea6275d1222f | 51 | char DS2[255]; |
procesadores_FAC | 0:b2a6aa7c0c8c | 52 | char buffer[512]; |
joshema216 | 2:ea6275d1222f | 53 | char resp[6]; |
joshema216 | 2:ea6275d1222f | 54 | char tam[2]; |
procesadores_FAC | 0:b2a6aa7c0c8c | 55 | char mensaje[100]; |
tony63 | 3:8aef67673965 | 56 | char gprsBuffer[30]; |
joshema216 | 2:ea6275d1222f | 57 | |
AaronGonzalez | 1:e2bd083802c0 | 58 | //Variables enteras y caracteres |
tony63 | 3:8aef67673965 | 59 | int g=0; |
procesadores_FAC | 0:b2a6aa7c0c8c | 60 | int count; |
joshema216 | 2:ea6275d1222f | 61 | int i, K, LENOUT1, LENIN1, LENOUT2, LENIN2, C; |
procesadores_FAC | 0:b2a6aa7c0c8c | 62 | int c=0; |
procesadores_FAC | 0:b2a6aa7c0c8c | 63 | char r[]=""; |
procesadores_FAC | 0:b2a6aa7c0c8c | 64 | char msg[256]; |
procesadores_FAC | 0:b2a6aa7c0c8c | 65 | char char1; |
joshema216 | 2:ea6275d1222f | 66 | int ind; |
joshema216 | 2:ea6275d1222f | 67 | float med; |
joshema216 | 2:ea6275d1222f | 68 | char outmed[16], outmedn[16]; |
joshema216 | 2:ea6275d1222f | 69 | int ret = 1; |
joshema216 | 2:ea6275d1222f | 70 | |
joshema216 | 2:ea6275d1222f | 71 | // Adquisición de números de teléfono, emisor - receptor |
joshema216 | 2:ea6275d1222f | 72 | char tel[15]; |
procesadores_FAC | 0:b2a6aa7c0c8c | 73 | |
joshema216 | 2:ea6275d1222f | 74 | // El GPS entregará al celular coordenadas expresadas en latitud y longitud |
joshema216 | 2:ea6275d1222f | 75 | // según la ubicación que encuentre, por lo tanto se declaran estas variables. |
procesadores_FAC | 0:b2a6aa7c0c8c | 76 | float lo,la; |
joshema216 | 2:ea6275d1222f | 77 | char clo[255], cla[255]; // Cadenas a capturar para latitud y longitud. |
joshema216 | 2:ea6275d1222f | 78 | char la_lo[255], volt[255]; |
joshema216 | 2:ea6275d1222f | 79 | |
joshema216 | 2:ea6275d1222f | 80 | // Cadena de google maps |
procesadores_FAC | 0:b2a6aa7c0c8c | 81 | char http2[255]; |
procesadores_FAC | 0:b2a6aa7c0c8c | 82 | char http[] = "http://maps.google.com/maps?q="; |
procesadores_FAC | 0:b2a6aa7c0c8c | 83 | char buf[100]; |
procesadores_FAC | 0:b2a6aa7c0c8c | 84 | |
joshema216 | 2:ea6275d1222f | 85 | // Relleno de datos propio del protocolo de SMS. |
joshema216 | 2:ea6275d1222f | 86 | char relle1[] = "0011000A91"; |
joshema216 | 2:ea6275d1222f | 87 | char relle2[] = "0000AA"; |
procesadores_FAC | 0:b2a6aa7c0c8c | 88 | |
joshema216 | 2:ea6275d1222f | 89 | // Reverses a string 'str' of length 'len' |
joshema216 | 2:ea6275d1222f | 90 | // driver program to test above funtion. |
joshema216 | 2:ea6275d1222f | 91 | void reverse(char *str, int len) |
joshema216 | 2:ea6275d1222f | 92 | { |
joshema216 | 2:ea6275d1222f | 93 | int i=0, j=len-1, temp; |
joshema216 | 2:ea6275d1222f | 94 | while (i<j) |
joshema216 | 2:ea6275d1222f | 95 | { |
joshema216 | 2:ea6275d1222f | 96 | temp = str[i]; |
joshema216 | 2:ea6275d1222f | 97 | str[i] = str[j]; |
joshema216 | 2:ea6275d1222f | 98 | str[j] = temp; |
joshema216 | 2:ea6275d1222f | 99 | i++; j--; |
joshema216 | 2:ea6275d1222f | 100 | } |
joshema216 | 2:ea6275d1222f | 101 | } |
joshema216 | 2:ea6275d1222f | 102 | |
joshema216 | 2:ea6275d1222f | 103 | // Converts a given integer x to string str[]. d is the number |
joshema216 | 2:ea6275d1222f | 104 | // of digits required in output. If d is more than the number |
joshema216 | 2:ea6275d1222f | 105 | // of digits in x, then 0s are added at the beginning. |
joshema216 | 2:ea6275d1222f | 106 | int intToStr(int x, char str[], int d) |
joshema216 | 2:ea6275d1222f | 107 | { |
joshema216 | 2:ea6275d1222f | 108 | int i = 0; |
joshema216 | 2:ea6275d1222f | 109 | while (x) |
joshema216 | 2:ea6275d1222f | 110 | { |
joshema216 | 2:ea6275d1222f | 111 | str[i++] = (x%10) + '0'; |
joshema216 | 2:ea6275d1222f | 112 | x = x/10; |
joshema216 | 2:ea6275d1222f | 113 | } |
joshema216 | 2:ea6275d1222f | 114 | |
joshema216 | 2:ea6275d1222f | 115 | // If number of digits required is more, then |
joshema216 | 2:ea6275d1222f | 116 | // add 0s at the beginning |
joshema216 | 2:ea6275d1222f | 117 | while (i < d) |
joshema216 | 2:ea6275d1222f | 118 | str[i++] = '0'; |
joshema216 | 2:ea6275d1222f | 119 | |
joshema216 | 2:ea6275d1222f | 120 | reverse(str, i); |
joshema216 | 2:ea6275d1222f | 121 | str[i] = '\0'; |
joshema216 | 2:ea6275d1222f | 122 | return i; |
joshema216 | 2:ea6275d1222f | 123 | } |
procesadores_FAC | 0:b2a6aa7c0c8c | 124 | |
joshema216 | 2:ea6275d1222f | 125 | // Converts a floating point number to string. |
joshema216 | 2:ea6275d1222f | 126 | void ftoa(float n, char *res, int afterpoint) |
joshema216 | 2:ea6275d1222f | 127 | { |
joshema216 | 2:ea6275d1222f | 128 | // Extract integer part |
joshema216 | 2:ea6275d1222f | 129 | int ipart = (int)n; |
joshema216 | 2:ea6275d1222f | 130 | |
joshema216 | 2:ea6275d1222f | 131 | // Extract floating part |
joshema216 | 2:ea6275d1222f | 132 | float fpart = n - (float)ipart; |
joshema216 | 2:ea6275d1222f | 133 | |
joshema216 | 2:ea6275d1222f | 134 | // convert integer part to string |
joshema216 | 2:ea6275d1222f | 135 | int i = intToStr(ipart, res, 0); |
joshema216 | 2:ea6275d1222f | 136 | |
joshema216 | 2:ea6275d1222f | 137 | // check for display option after point |
joshema216 | 2:ea6275d1222f | 138 | if (afterpoint != 0) |
joshema216 | 2:ea6275d1222f | 139 | { |
joshema216 | 2:ea6275d1222f | 140 | res[i] = '.'; // add dot |
procesadores_FAC | 0:b2a6aa7c0c8c | 141 | |
joshema216 | 2:ea6275d1222f | 142 | // Get the value of fraction part upto given no. |
joshema216 | 2:ea6275d1222f | 143 | // of points after dot. The third parameter is needed |
joshema216 | 2:ea6275d1222f | 144 | // to handle cases like 233.007 |
joshema216 | 2:ea6275d1222f | 145 | float fp=10; |
joshema216 | 2:ea6275d1222f | 146 | fpart =fpart * pow(fp,afterpoint); |
joshema216 | 2:ea6275d1222f | 147 | |
joshema216 | 2:ea6275d1222f | 148 | intToStr((int)fpart, res + i + 1, afterpoint); |
joshema216 | 2:ea6275d1222f | 149 | } |
joshema216 | 2:ea6275d1222f | 150 | } |
tony63 | 3:8aef67673965 | 151 | //++++++++++++++++++++++++++++++++++++libreria para vaciar el modem+++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
joshema216 | 2:ea6275d1222f | 152 | void FlushGSM(void) { |
joshema216 | 2:ea6275d1222f | 153 | char1 = 0; |
joshema216 | 2:ea6275d1222f | 154 | while (GSM.readable()){ |
joshema216 | 2:ea6275d1222f | 155 | char1 = GSM.getc(); |
joshema216 | 2:ea6275d1222f | 156 | } |
joshema216 | 2:ea6275d1222f | 157 | return; |
joshema216 | 2:ea6275d1222f | 158 | } |
tony63 | 3:8aef67673965 | 159 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
joshema216 | 2:ea6275d1222f | 160 | void callback(){ |
joshema216 | 2:ea6275d1222f | 161 | // Note: you need to actually read from the serial to clear the RX interrupt |
joshema216 | 2:ea6275d1222f | 162 | pc.printf("%c\n", GSM.getc()); |
joshema216 | 2:ea6275d1222f | 163 | } |
tony63 | 3:8aef67673965 | 164 | //----------------------------------------------------------------------------------------------------------------------------- |
joshema216 | 2:ea6275d1222f | 165 | // Esta funcion de abajo lee todo un bufer hasta encontrar CR o LF y el resto lo rellena de |
joshema216 | 2:ea6275d1222f | 166 | // $, count es lo que va a leer. Lo leido lo mete en buffer que es una cadena previamente definida |
joshema216 | 2:ea6275d1222f | 167 | // incorpora medida de tiempo si se demora mas de tres segundos retorna fracaso con -1 |
joshema216 | 2:ea6275d1222f | 168 | int readBuffer(char *buffer,int count){ |
procesadores_FAC | 0:b2a6aa7c0c8c | 169 | int i=0; |
joshema216 | 2:ea6275d1222f | 170 | t.start(); // start timer |
procesadores_FAC | 0:b2a6aa7c0c8c | 171 | while(1) { |
procesadores_FAC | 0:b2a6aa7c0c8c | 172 | while (GSM.readable()) { |
procesadores_FAC | 0:b2a6aa7c0c8c | 173 | char c = GSM.getc(); |
procesadores_FAC | 0:b2a6aa7c0c8c | 174 | if (c == '\r' || c == '\n') c = '$'; |
procesadores_FAC | 0:b2a6aa7c0c8c | 175 | buffer[i++] = c; |
procesadores_FAC | 0:b2a6aa7c0c8c | 176 | if(i > count)break; |
procesadores_FAC | 0:b2a6aa7c0c8c | 177 | } |
joshema216 | 2:ea6275d1222f | 178 | if(i > count)break; |
procesadores_FAC | 0:b2a6aa7c0c8c | 179 | if(t.read() > 3) { |
procesadores_FAC | 0:b2a6aa7c0c8c | 180 | t.stop(); |
procesadores_FAC | 0:b2a6aa7c0c8c | 181 | t.reset(); |
procesadores_FAC | 0:b2a6aa7c0c8c | 182 | break; |
procesadores_FAC | 0:b2a6aa7c0c8c | 183 | } |
procesadores_FAC | 0:b2a6aa7c0c8c | 184 | } |
procesadores_FAC | 0:b2a6aa7c0c8c | 185 | wait(0.5); |
joshema216 | 2:ea6275d1222f | 186 | while(GSM.readable()){ // display the other thing.. |
joshema216 | 2:ea6275d1222f | 187 | char c = GSM.getc(); |
joshema216 | 2:ea6275d1222f | 188 | } |
joshema216 | 2:ea6275d1222f | 189 | return 0; |
joshema216 | 2:ea6275d1222f | 190 | } |
tony63 | 3:8aef67673965 | 191 | //-------------------------------------------------------------------------------------------------------------- |
joshema216 | 2:ea6275d1222f | 192 | // Esta función de abajo limpia o borra todo un "buffer" de tamaño "count", |
joshema216 | 2:ea6275d1222f | 193 | // lo revisa elemento por elemento y le mete el caracter null que indica fin de cadena. |
joshema216 | 2:ea6275d1222f | 194 | // No retorna nada. |
joshema216 | 2:ea6275d1222f | 195 | void cleanBuffer(char *buffer, int count){ |
joshema216 | 2:ea6275d1222f | 196 | for(int i=0; i < count; i++) { |
joshema216 | 2:ea6275d1222f | 197 | buffer[i] = '\0'; |
joshema216 | 2:ea6275d1222f | 198 | } |
joshema216 | 2:ea6275d1222f | 199 | } |
tony63 | 3:8aef67673965 | 200 | //-------------------------------------------------------------------------------------------------------------- |
joshema216 | 2:ea6275d1222f | 201 | // Esta función de abajo envia un comando parametrizado como cadena |
joshema216 | 2:ea6275d1222f | 202 | // puede ser un comando tipo AT. |
joshema216 | 2:ea6275d1222f | 203 | void sendCmd(char *cmd){ |
joshema216 | 2:ea6275d1222f | 204 | GSM.puts(cmd); |
joshema216 | 2:ea6275d1222f | 205 | } |
tony63 | 3:8aef67673965 | 206 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
joshema216 | 2:ea6275d1222f | 207 | // Esta función de abajo espera la respuesta de un comando que debe ser idéntica a la cadena "resp" y un tiempo "timeout", |
joshema216 | 2:ea6275d1222f | 208 | // si todo sale bien retorna un cero que en la programacion hay que validar, |
joshema216 | 2:ea6275d1222f | 209 | // si algo sale mal (no se parece o se demora mucho) retorna -1 que debera validarse con alguna expresion logica. |
joshema216 | 2:ea6275d1222f | 210 | int waitForResp(char *resp, int timeout){ |
joshema216 | 2:ea6275d1222f | 211 | int len = strlen(resp); |
joshema216 | 2:ea6275d1222f | 212 | int sum=0; |
joshema216 | 2:ea6275d1222f | 213 | t.start(); |
joshema216 | 2:ea6275d1222f | 214 | |
joshema216 | 2:ea6275d1222f | 215 | while(1) { |
joshema216 | 2:ea6275d1222f | 216 | if(GSM.readable()) { |
joshema216 | 2:ea6275d1222f | 217 | char c = GSM.getc(); |
joshema216 | 2:ea6275d1222f | 218 | sum = (c==resp[sum]) ? sum+1 : 0;// esta linea de C# sum se incrementa o se hace cero segun c |
joshema216 | 2:ea6275d1222f | 219 | if(sum == len)break; //ya acabo se sale |
joshema216 | 2:ea6275d1222f | 220 | } |
joshema216 | 2:ea6275d1222f | 221 | if(t.read() > timeout) { // time out chequea el tiempo minimo antes de salir perdiendo |
joshema216 | 2:ea6275d1222f | 222 | t.stop(); |
joshema216 | 2:ea6275d1222f | 223 | t.reset(); |
joshema216 | 2:ea6275d1222f | 224 | return -1; |
joshema216 | 2:ea6275d1222f | 225 | } |
joshema216 | 2:ea6275d1222f | 226 | } |
joshema216 | 2:ea6275d1222f | 227 | t.stop(); // stop timer antes de retornar |
joshema216 | 2:ea6275d1222f | 228 | t.reset(); // clear timer |
joshema216 | 2:ea6275d1222f | 229 | while(GSM.readable()) { // display the other thing.. |
procesadores_FAC | 0:b2a6aa7c0c8c | 230 | char c = GSM.getc(); |
procesadores_FAC | 0:b2a6aa7c0c8c | 231 | } |
procesadores_FAC | 0:b2a6aa7c0c8c | 232 | return 0; |
procesadores_FAC | 0:b2a6aa7c0c8c | 233 | } |
tony63 | 3:8aef67673965 | 234 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
joshema216 | 2:ea6275d1222f | 235 | // Esta función de abajo es muy completa y útil, se encarga de enviar el comando y esperar la respuesta. |
joshema216 | 2:ea6275d1222f | 236 | // Si todo sale bien retorna un cero (herencia de las funciones contenedoras) que en la programacion hay que validar |
joshema216 | 2:ea6275d1222f | 237 | // con alguna expresion lógica. |
joshema216 | 2:ea6275d1222f | 238 | int sendCmdAndWaitForResp(char *cmd, char *resp, int timeout){ |
joshema216 | 2:ea6275d1222f | 239 | sendCmd(cmd); |
joshema216 | 2:ea6275d1222f | 240 | return waitForResp(resp,timeout); |
joshema216 | 2:ea6275d1222f | 241 | } |
tony63 | 3:8aef67673965 | 242 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
joshema216 | 2:ea6275d1222f | 243 | // Esta función de abajo chequea que el módem este vivo, envia AT, le contesta con OK y espera 2 segundos. |
joshema216 | 2:ea6275d1222f | 244 | int powerCheck(void){ // Este comando se manda para verificar si el módem esta vivo o conectado. |
joshema216 | 2:ea6275d1222f | 245 | return sendCmdAndWaitForResp("AT\r\n", "OK", 2); |
joshema216 | 2:ea6275d1222f | 246 | } |
tony63 | 3:8aef67673965 | 247 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
joshema216 | 2:ea6275d1222f | 248 | // Esta función de abajo chequea el estado de la sim card |
joshema216 | 2:ea6275d1222f | 249 | // y si todo sale bien retorna un cero que en la programacion hay que validar |
joshema216 | 2:ea6275d1222f | 250 | // con alguna expresión lógica. |
joshema216 | 2:ea6275d1222f | 251 | int checkSIMStatus(void){ |
joshema216 | 2:ea6275d1222f | 252 | char gprsBuffer[30]; |
joshema216 | 2:ea6275d1222f | 253 | int count = 0; |
joshema216 | 2:ea6275d1222f | 254 | cleanBuffer(gprsBuffer, 30); |
joshema216 | 2:ea6275d1222f | 255 | while(count < 3){ |
joshema216 | 2:ea6275d1222f | 256 | sendCmd("AT+CPIN?\r\n"); |
joshema216 | 2:ea6275d1222f | 257 | readBuffer(gprsBuffer,30); |
joshema216 | 2:ea6275d1222f | 258 | if((NULL != strstr(gprsBuffer,"+CPIN: READY"))){ |
joshema216 | 2:ea6275d1222f | 259 | break; |
joshema216 | 2:ea6275d1222f | 260 | } |
joshema216 | 2:ea6275d1222f | 261 | count++; |
joshema216 | 2:ea6275d1222f | 262 | wait(1); |
joshema216 | 2:ea6275d1222f | 263 | } |
joshema216 | 2:ea6275d1222f | 264 | |
joshema216 | 2:ea6275d1222f | 265 | if(count == 3){ |
joshema216 | 2:ea6275d1222f | 266 | return -1; |
joshema216 | 2:ea6275d1222f | 267 | } |
joshema216 | 2:ea6275d1222f | 268 | return 0; |
joshema216 | 2:ea6275d1222f | 269 | } |
tony63 | 3:8aef67673965 | 270 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
joshema216 | 2:ea6275d1222f | 271 | // Esta función de abajo chequea la calidad de la señal |
joshema216 | 2:ea6275d1222f | 272 | // y si todo sale bien retorna con el valor de señal útil o un -1 si no es aceptable, en la programacion hay que validar |
joshema216 | 2:ea6275d1222f | 273 | // con alguna expresión lógica. |
joshema216 | 2:ea6275d1222f | 274 | int checkSignalStrength(void){ |
joshema216 | 2:ea6275d1222f | 275 | char gprsBuffer[100]; |
joshema216 | 2:ea6275d1222f | 276 | int index, count = 0; |
joshema216 | 2:ea6275d1222f | 277 | cleanBuffer(gprsBuffer,100); |
joshema216 | 2:ea6275d1222f | 278 | while(count < 3){ |
joshema216 | 2:ea6275d1222f | 279 | sendCmd("AT+CSQ\r\n"); |
joshema216 | 2:ea6275d1222f | 280 | readBuffer(gprsBuffer,25); |
joshema216 | 2:ea6275d1222f | 281 | if(sscanf(gprsBuffer, "AT+CSQ$$$$+CSQ: %d", &index)>0) { |
joshema216 | 2:ea6275d1222f | 282 | break; |
joshema216 | 2:ea6275d1222f | 283 | } |
joshema216 | 2:ea6275d1222f | 284 | count++; |
joshema216 | 2:ea6275d1222f | 285 | wait(1); |
joshema216 | 2:ea6275d1222f | 286 | } |
joshema216 | 2:ea6275d1222f | 287 | if(count == 3){ |
joshema216 | 2:ea6275d1222f | 288 | return -1; |
joshema216 | 2:ea6275d1222f | 289 | } |
joshema216 | 2:ea6275d1222f | 290 | return index; |
joshema216 | 2:ea6275d1222f | 291 | } |
tony63 | 3:8aef67673965 | 292 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
joshema216 | 2:ea6275d1222f | 293 | // Esta funcion de abajo inicaliza el módem. Se compone de un grupo de subfunciones ya definidas previamente |
joshema216 | 2:ea6275d1222f | 294 | // primero chequea que este vivo, |
joshema216 | 2:ea6275d1222f | 295 | // segundo chequea el estado de la simcard, |
joshema216 | 2:ea6275d1222f | 296 | // tercero chequea la intencidad de señal celular, |
joshema216 | 2:ea6275d1222f | 297 | // cuarto aplica la configuracion |
joshema216 | 2:ea6275d1222f | 298 | // y si todo sale bien retorna un cero que en la programacion hay que validar |
joshema216 | 2:ea6275d1222f | 299 | // con alguna expresión lógica. |
joshema216 | 2:ea6275d1222f | 300 | int init(){ |
joshema216 | 2:ea6275d1222f | 301 | if (0 != sendCmdAndWaitForResp("AT\r\n", "OK", 3)){ |
joshema216 | 2:ea6275d1222f | 302 | return -1; |
joshema216 | 2:ea6275d1222f | 303 | } |
joshema216 | 2:ea6275d1222f | 304 | if (0 != sendCmdAndWaitForResp("AT+CNMI=1,1\r\n", "OK", 3)){ |
joshema216 | 2:ea6275d1222f | 305 | return -1; |
joshema216 | 2:ea6275d1222f | 306 | } |
joshema216 | 2:ea6275d1222f | 307 | if (0 != sendCmdAndWaitForResp("AT+CMGF=0\r\n", "OK", 3)){ |
joshema216 | 2:ea6275d1222f | 308 | return -1; |
joshema216 | 2:ea6275d1222f | 309 | } |
joshema216 | 2:ea6275d1222f | 310 | if (0 != sendCmdAndWaitForResp("AT+CBST=7,0,1\r\n", "OK", 3)){ //velocidad fija a 9600, modem asincronico no transparente |
joshema216 | 2:ea6275d1222f | 311 | return -1; |
joshema216 | 2:ea6275d1222f | 312 | } |
joshema216 | 2:ea6275d1222f | 313 | if (0 != sendCmdAndWaitForResp("ATE\r\n", "OK", 3)){ //se le quita el eco al modem GSM |
joshema216 | 2:ea6275d1222f | 314 | return -1; |
joshema216 | 2:ea6275d1222f | 315 | } |
joshema216 | 2:ea6275d1222f | 316 | LedVerde=0; |
joshema216 | 2:ea6275d1222f | 317 | return 0; |
joshema216 | 2:ea6275d1222f | 318 | } |
tony63 | 3:8aef67673965 | 319 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
joshema216 | 2:ea6275d1222f | 320 | // Esta funcion de abajo intenta leer un mensaje de texto en formato PDU o HEX |
joshema216 | 2:ea6275d1222f | 321 | // y si todo sale bien retorna un cero que en la programacion hay que validar |
joshema216 | 2:ea6275d1222f | 322 | // con alguna expresión lógica. |
joshema216 | 2:ea6275d1222f | 323 | int readSMSpdu(char *message, int index){ |
joshema216 | 2:ea6275d1222f | 324 | int i = 0; |
joshema216 | 2:ea6275d1222f | 325 | char gprsBuffer[100]; |
joshema216 | 2:ea6275d1222f | 326 | char *p,*s; |
joshema216 | 2:ea6275d1222f | 327 | GSM.printf("AT+CMGR=%d\r\n",index); |
joshema216 | 2:ea6275d1222f | 328 | cleanBuffer(gprsBuffer,100); |
joshema216 | 2:ea6275d1222f | 329 | readBuffer(gprsBuffer,100); |
joshema216 | 2:ea6275d1222f | 330 | if(NULL == ( s = strstr(gprsBuffer,"+CMGR"))) { |
joshema216 | 2:ea6275d1222f | 331 | return -1; |
joshema216 | 2:ea6275d1222f | 332 | } |
joshema216 | 2:ea6275d1222f | 333 | if(NULL != ( s = strstr(gprsBuffer,"+32"))) { |
joshema216 | 2:ea6275d1222f | 334 | p = s + 6; |
joshema216 | 2:ea6275d1222f | 335 | while((*p != '$')&&(i < 5)) { |
joshema216 | 2:ea6275d1222f | 336 | message[i++] = *(p++); |
joshema216 | 2:ea6275d1222f | 337 | } |
joshema216 | 2:ea6275d1222f | 338 | message[i] = '\0'; |
joshema216 | 2:ea6275d1222f | 339 | } |
joshema216 | 2:ea6275d1222f | 340 | return 0; |
joshema216 | 2:ea6275d1222f | 341 | } |
tony63 | 3:8aef67673965 | 342 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
joshema216 | 2:ea6275d1222f | 343 | // Esta función de abajo borra mensajes SMS del modem |
joshema216 | 2:ea6275d1222f | 344 | // y si todo sale bien retorna un cero que en la programacion hay que validar |
joshema216 | 2:ea6275d1222f | 345 | // con alguna expresion logica. |
joshema216 | 2:ea6275d1222f | 346 | int deleteSMS(int index){ |
joshema216 | 2:ea6275d1222f | 347 | char cmd[32]; |
joshema216 | 2:ea6275d1222f | 348 | snprintf(cmd,sizeof(cmd),"AT+CMGD=%d\r\n",index); |
joshema216 | 2:ea6275d1222f | 349 | sendCmd(cmd); |
joshema216 | 2:ea6275d1222f | 350 | return 0; |
joshema216 | 2:ea6275d1222f | 351 | } |
tony63 | 3:8aef67673965 | 352 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
joshema216 | 2:ea6275d1222f | 353 | // Esta función devuelve la confirmacion del mensaje a quien lo envio. |
joshema216 | 2:ea6275d1222f | 354 | int recibe_ok(){ |
tony63 | 3:8aef67673965 | 355 | pc.printf("AT+CMGS=27\n\r"); |
joshema216 | 2:ea6275d1222f | 356 | GSM.printf("AT+CMGS=27\n\r"); |
tony63 | 3:8aef67673965 | 357 | wait(1); |
joshema216 | 2:ea6275d1222f | 358 | GSM.printf("0011000A91%s0000AA10CDB27B1E569741F2F2382D4E93DF",tel); |
tony63 | 3:8aef67673965 | 359 | GSM.printf("\n\r"); |
tony63 | 3:8aef67673965 | 360 | pc.printf("0011000A91%s0000AA10CDB27B1E569741F2F2382D4E93DF\n\r",tel); |
tony63 | 3:8aef67673965 | 361 | pc.printf("\n\r");//control+Z |
tony63 | 3:8aef67673965 | 362 | wait(1); |
joshema216 | 2:ea6275d1222f | 363 | return 0; |
joshema216 | 2:ea6275d1222f | 364 | } |
tony63 | 3:8aef67673965 | 365 | //******************************************************************************************************************************* |
joshema216 | 2:ea6275d1222f | 366 | // Programas a ejecutar. |
joshema216 | 2:ea6275d1222f | 367 | int main(){ |
joshema216 | 2:ea6275d1222f | 368 | //configuramos los puertos seriales |
joshema216 | 2:ea6275d1222f | 369 | GSM.baud(9600);//configura los baudios de la FRDMKL25Z en 9600 |
joshema216 | 2:ea6275d1222f | 370 | GSM.format(8,Serial::None,1); //configura el formato de los datos de la UART |
joshema216 | 2:ea6275d1222f | 371 | LedVerde = 1; // APAGO LOS LEDS |
joshema216 | 2:ea6275d1222f | 372 | LedRojo = 1; |
joshema216 | 2:ea6275d1222f | 373 | LedAzul = 1; |
joshema216 | 2:ea6275d1222f | 374 | LedRojo = 0; // PRENDO EL LED ROJO |
tony63 | 3:8aef67673965 | 375 | valvula=1; // se enciende la valvula por defecto |
tony63 | 3:8aef67673965 | 376 | |
tony63 | 3:8aef67673965 | 377 | //****************** CONFIGURACIÓN DEL MODEM GSM (TELEFONO CELULAR SIEMENS A56i). |
tony63 | 3:8aef67673965 | 378 | inicio1: |
joshema216 | 2:ea6275d1222f | 379 | ret = init(); |
tony63 | 3:8aef67673965 | 380 | if(ret==0){ // esta bien conectado el modem GSM......................................................................... |
tony63 | 3:8aef67673965 | 381 | LedRojo = 1; // apagar led rojo |
tony63 | 3:8aef67673965 | 382 | LedVerde = 0; // Enciende LED Verde para confirmar la comunicación OK con el módem. |
tony63 | 3:8aef67673965 | 383 | pc.printf("Modem configurado\n");//envia mensaje a la terminal........................................................ |
joshema216 | 2:ea6275d1222f | 384 | } |
joshema216 | 2:ea6275d1222f | 385 | else{ |
joshema216 | 2:ea6275d1222f | 386 | wait(1); |
tony63 | 3:8aef67673965 | 387 | goto inicio1; // se hace un reintento de conexion o el sistema no funcionara |
joshema216 | 2:ea6275d1222f | 388 | } |
joshema216 | 2:ea6275d1222f | 389 | |
tony63 | 3:8aef67673965 | 390 | //Luego se verifica GPS bien conectado (si esta habilitado por dip sw)y recibiendo satelites |
tony63 | 3:8aef67673965 | 391 | if(!sin_gps){ |
tony63 | 3:8aef67673965 | 392 | pc.printf("SIN GPS\n\r");//confirma con teminal que no se habilito el gps |
tony63 | 3:8aef67673965 | 393 | goto seguir9;// de inmediato funcionara sin gps |
tony63 | 3:8aef67673965 | 394 | } |
tony63 | 3:8aef67673965 | 395 | //*****************************************Test para un GPS bien instalado*********************************************** |
tony63 | 3:8aef67673965 | 396 | |
tony63 | 3:8aef67673965 | 397 | //******************************************esta bien el GPS***************************************************************** |
tony63 | 3:8aef67673965 | 398 | pc.printf("CON GPS\n\r"); |
tony63 | 3:8aef67673965 | 399 | g=gps.sample(); |
tony63 | 3:8aef67673965 | 400 | if(g){ |
tony63 | 3:8aef67673965 | 401 | LedAzul=0; |
tony63 | 3:8aef67673965 | 402 | pc.printf("GPS--OK\n\r"); |
tony63 | 3:8aef67673965 | 403 | } |
tony63 | 3:8aef67673965 | 404 | |
tony63 | 3:8aef67673965 | 405 | seguir9: |
tony63 | 3:8aef67673965 | 406 | |
tony63 | 3:8aef67673965 | 407 | //**********************inicia codigo ciclico********************************************** |
tony63 | 3:8aef67673965 | 408 | //**********************inicia codigo ciclico********************************************** |
tony63 | 3:8aef67673965 | 409 | //**********************inicia codigo ciclico********************************************** |
tony63 | 3:8aef67673965 | 410 | |
tony63 | 3:8aef67673965 | 411 | //****************DESDE ACA SE LEE CUALQUIER MENSAJE SMS ENTRANTE************************** |
tony63 | 3:8aef67673965 | 412 | while(1){ |
joshema216 | 2:ea6275d1222f | 413 | if (GSM.readable()){ |
joshema216 | 2:ea6275d1222f | 414 | readBuffer(buffer,110); |
joshema216 | 2:ea6275d1222f | 415 | pc.printf("%s\r\n",buffer); |
joshema216 | 2:ea6275d1222f | 416 | for(i=0; i<5; i++){ |
joshema216 | 2:ea6275d1222f | 417 | resp[i] = buffer[i]; |
joshema216 | 2:ea6275d1222f | 418 | } |
joshema216 | 2:ea6275d1222f | 419 | |
joshema216 | 2:ea6275d1222f | 420 | pc.printf("%s\r\n", resp); |
joshema216 | 2:ea6275d1222f | 421 | if(strcmp("$$+CM", resp) == 0){ //COMPARA resp con "+CMTI" |
joshema216 | 2:ea6275d1222f | 422 | pc.printf("Llego MSG\r\n"); |
joshema216 | 2:ea6275d1222f | 423 | cleanBuffer(buffer,10); |
joshema216 | 2:ea6275d1222f | 424 | GSM.printf("AT+CMGL=0\r\n"); // Envío comando para leer mensaje |
joshema216 | 2:ea6275d1222f | 425 | pc.printf("AT+CMGL=0\r\n"); |
joshema216 | 2:ea6275d1222f | 426 | //GSM.printf("AT+CMGD=0\r\n"); // Envío comando para borrar el mensaje. |
joshema216 | 2:ea6275d1222f | 427 | readBuffer(buffer,110); |
joshema216 | 2:ea6275d1222f | 428 | pc.printf("%s\r\n",buffer); |
AaronGonzalez | 1:e2bd083802c0 | 429 | |
joshema216 | 2:ea6275d1222f | 430 | // Lectura el teléfono emisor |
joshema216 | 2:ea6275d1222f | 431 | for(i=0; i<10; i++){ |
joshema216 | 2:ea6275d1222f | 432 | tel[i] = buffer[i+40]; |
joshema216 | 2:ea6275d1222f | 433 | } |
joshema216 | 2:ea6275d1222f | 434 | pc.printf("Telefono: %c%c%c%c%c%c%c%c%c%c\r\n", tel[1], tel[0], tel[3], tel[2], tel[5], tel[4], tel[7], tel[6], tel[9], tel[8]); |
joshema216 | 2:ea6275d1222f | 435 | |
joshema216 | 2:ea6275d1222f | 436 | // Lectura del tamaño |
joshema216 | 2:ea6275d1222f | 437 | for(i=0;i<2;i++){ |
joshema216 | 2:ea6275d1222f | 438 | tam[i] = buffer[i + 68]; |
joshema216 | 2:ea6275d1222f | 439 | } |
joshema216 | 2:ea6275d1222f | 440 | pc.printf("%s-\r\n", tam); |
joshema216 | 2:ea6275d1222f | 441 | |
joshema216 | 2:ea6275d1222f | 442 | // Lectura del mensaje |
joshema216 | 2:ea6275d1222f | 443 | for(i=0;i<26;i++){ |
joshema216 | 2:ea6275d1222f | 444 | msg[i] = buffer[i+70]; // Lee un mensaje de 26 caracteres máximo desde la posición 70 del buffer. |
joshema216 | 2:ea6275d1222f | 445 | } |
joshema216 | 2:ea6275d1222f | 446 | pc.printf("%s-\r\n", msg); |
joshema216 | 2:ea6275d1222f | 447 | |
joshema216 | 2:ea6275d1222f | 448 | // Decodificación del mensaje |
joshema216 | 2:ea6275d1222f | 449 | |
tony63 | 3:8aef67673965 | 450 | |
joshema216 | 2:ea6275d1222f | 451 | // Comparar el mensaje |
joshema216 | 2:ea6275d1222f | 452 | deleteSMS(1); // Se borran los mensajes por medio de una función |
tony63 | 3:8aef67673965 | 453 | readBuffer(buffer, 110); |
tony63 | 3:8aef67673965 | 454 | //*************************SEFGUNDO CASO ACTIVAR SALIDAS************************************ } |
tony63 | 3:8aef67673965 | 455 | //.................activar valvula de combustible...............On..on....................... |
tony63 | 3:8aef67673965 | 456 | if((strncmp("4F37", msg, 4) == 0) || (strncmp("6F37", msg, 4) == 0)){ |
tony63 | 3:8aef67673965 | 457 | recibe_ok(); |
tony63 | 3:8aef67673965 | 458 | valvula = 1; // Encender valvula. |
tony63 | 3:8aef67673965 | 459 | |
tony63 | 3:8aef67673965 | 460 | } |
tony63 | 3:8aef67673965 | 461 | // ...................................................... off y Off........................ |
tony63 | 3:8aef67673965 | 462 | if((strncmp("4FB319", msg, 6) == 0) || (strncmp("6FB319", msg, 6) == 0)){ |
tony63 | 3:8aef67673965 | 463 | recibe_ok(); |
tony63 | 3:8aef67673965 | 464 | valvula = 0; // apagar valvula. |
tony63 | 3:8aef67673965 | 465 | |
tony63 | 3:8aef67673965 | 466 | } |
tony63 | 3:8aef67673965 | 467 | |
tony63 | 3:8aef67673965 | 468 | //............................Envia un Pulso Para Puerta Pulso...D03A7BFE06 pulso.F03A7BFE06.............. |
tony63 | 3:8aef67673965 | 469 | if((strncmp("D03A7BFE06", msg, 10) == 0) || (strncmp("F03A7BFE06", msg, 10) == 0)){ |
tony63 | 3:8aef67673965 | 470 | recibe_ok();//CONFIRMACION DE RECEPCION REMOTA |
tony63 | 3:8aef67673965 | 471 | puerta = 1; // Encender Puerta. |
tony63 | 3:8aef67673965 | 472 | wait(7);//pulso de 7 segundos |
tony63 | 3:8aef67673965 | 473 | puerta = 0;//apaga puerta |
tony63 | 3:8aef67673965 | 474 | |
tony63 | 3:8aef67673965 | 475 | } |
tony63 | 3:8aef67673965 | 476 | |
joshema216 | 2:ea6275d1222f | 477 | // COMPARA resp con "E3F75B4E2EBBC3E4F01C" que es "coordenadas", o "C3F75B4E2EBBC3E4F01C" que es "Coordenadas". |
joshema216 | 2:ea6275d1222f | 478 | if((strncmp("E3F75B4E2EBBC3E4F01C", msg, 20) == 0) || (strncmp("C3F75B4E2EBBC3E4F01C", msg, 20) == 0)){ |
tony63 | 3:8aef67673965 | 479 | recibe_ok(); |
joshema216 | 2:ea6275d1222f | 480 | LedVerde = 1; // Encender LED azul. |
joshema216 | 2:ea6275d1222f | 481 | LedAzul = 0; |
joshema216 | 2:ea6275d1222f | 482 | wait(2); |
joshema216 | 2:ea6275d1222f | 483 | |
joshema216 | 2:ea6275d1222f | 484 | if(gps.sample()){ |
joshema216 | 2:ea6275d1222f | 485 | lo = gps.longitude; |
joshema216 | 2:ea6275d1222f | 486 | la = gps.latitude; |
joshema216 | 2:ea6275d1222f | 487 | pc.printf("\nLongitud entera = %f, Latitud entera = %f\n", lo, la); |
joshema216 | 2:ea6275d1222f | 488 | //wait(0.5); |
joshema216 | 2:ea6275d1222f | 489 | |
joshema216 | 2:ea6275d1222f | 490 | //LONGITUD |
joshema216 | 2:ea6275d1222f | 491 | sprintf (clo, "%f", lo); |
joshema216 | 2:ea6275d1222f | 492 | pc.printf ("\nLongitud = %s\n",clo); |
joshema216 | 2:ea6275d1222f | 493 | //wait(0.5); |
joshema216 | 2:ea6275d1222f | 494 | |
joshema216 | 2:ea6275d1222f | 495 | // LATITUD |
joshema216 | 2:ea6275d1222f | 496 | sprintf (cla, "%f", la); |
joshema216 | 2:ea6275d1222f | 497 | pc.printf ( "\nLatitud = %s\n",cla); |
joshema216 | 2:ea6275d1222f | 498 | //wait(0.5); |
joshema216 | 2:ea6275d1222f | 499 | |
joshema216 | 2:ea6275d1222f | 500 | // Concatenando las cadenas de Latitud y Longitud |
joshema216 | 2:ea6275d1222f | 501 | strcpy(la_lo,cla); |
joshema216 | 2:ea6275d1222f | 502 | strcat(la_lo,","); |
joshema216 | 2:ea6275d1222f | 503 | strcat(la_lo,clo); |
joshema216 | 2:ea6275d1222f | 504 | pc.printf("\nLatitud, Longitud: %s\n",la_lo); |
procesadores_FAC | 0:b2a6aa7c0c8c | 505 | |
joshema216 | 2:ea6275d1222f | 506 | //Ahora se juntan las cadenas obtenidas y se agrega el protocolo de transferencia de hipertextos http |
joshema216 | 2:ea6275d1222f | 507 | strcpy(DE1,http); |
joshema216 | 2:ea6275d1222f | 508 | strcat(DE1,la_lo); |
joshema216 | 2:ea6275d1222f | 509 | pc.printf("\nDireccion: %s\n",DE1); |
joshema216 | 2:ea6275d1222f | 510 | pc.printf("\n"); |
joshema216 | 2:ea6275d1222f | 511 | LENIN1 = strlen(DE1); |
joshema216 | 2:ea6275d1222f | 512 | |
joshema216 | 2:ea6275d1222f | 513 | //Conversión a octetos. |
joshema216 | 2:ea6275d1222f | 514 | K = 0; |
joshema216 | 2:ea6275d1222f | 515 | C = 0; |
joshema216 | 2:ea6275d1222f | 516 | for (i = 0; i < LENIN1; i++){ |
joshema216 | 2:ea6275d1222f | 517 | DS1[i] = DE1[i + C] >> K | DE1[i + C + 1] << (7 - K); |
joshema216 | 2:ea6275d1222f | 518 | if(DS1[i] == 0x00) {LENOUT1 = i; goto salir1;} |
joshema216 | 2:ea6275d1222f | 519 | K++; |
joshema216 | 2:ea6275d1222f | 520 | if (K == 7) {K = 0; C++;} // se chequea que ya se acabaron los bits en un ciclo de conversion. |
joshema216 | 2:ea6275d1222f | 521 | } |
joshema216 | 2:ea6275d1222f | 522 | |
joshema216 | 2:ea6275d1222f | 523 | salir1: |
joshema216 | 2:ea6275d1222f | 524 | for (i = 0; i < LENIN1; i++){ |
joshema216 | 2:ea6275d1222f | 525 | pc.printf("%X", DE1[i]); |
joshema216 | 2:ea6275d1222f | 526 | } |
joshema216 | 2:ea6275d1222f | 527 | |
joshema216 | 2:ea6275d1222f | 528 | pc.printf(":\r\n"); |
joshema216 | 2:ea6275d1222f | 529 | for (i = 0; i < LENOUT1; i++){ |
joshema216 | 2:ea6275d1222f | 530 | pc.printf("%2X", DS1[i]&0x000000FF); |
joshema216 | 2:ea6275d1222f | 531 | } |
joshema216 | 2:ea6275d1222f | 532 | pc.printf("\r\nLENOUT GPS: %d, LENIN GPS: %2X\r\n", LENOUT1, LENIN1); |
joshema216 | 2:ea6275d1222f | 533 | |
joshema216 | 2:ea6275d1222f | 534 | // Concatenación del mensaje en formato PDU y envío del mismo. |
joshema216 | 2:ea6275d1222f | 535 | ind = 14 + LENOUT1 - 1; |
joshema216 | 2:ea6275d1222f | 536 | |
joshema216 | 2:ea6275d1222f | 537 | GSM.printf("AT+CMGS=%d\r\n",ind); |
joshema216 | 2:ea6275d1222f | 538 | pc.printf("AT+CMGS=%d\r\n",ind); |
joshema216 | 2:ea6275d1222f | 539 | pc.printf(relle1); |
joshema216 | 2:ea6275d1222f | 540 | GSM.printf(relle1); |
joshema216 | 2:ea6275d1222f | 541 | |
joshema216 | 2:ea6275d1222f | 542 | for (i=0 ;i<=9; i++) { |
joshema216 | 2:ea6275d1222f | 543 | pc.printf("%c",tel[i]); |
joshema216 | 2:ea6275d1222f | 544 | GSM.printf("%c",tel[i]); |
joshema216 | 2:ea6275d1222f | 545 | } |
joshema216 | 2:ea6275d1222f | 546 | |
joshema216 | 2:ea6275d1222f | 547 | pc.printf(relle2); |
joshema216 | 2:ea6275d1222f | 548 | GSM.printf(relle2); |
joshema216 | 2:ea6275d1222f | 549 | pc.printf("%2X", LENIN1); |
joshema216 | 2:ea6275d1222f | 550 | GSM.printf("%2X", LENIN1); |
joshema216 | 2:ea6275d1222f | 551 | |
joshema216 | 2:ea6275d1222f | 552 | for (i = 0; i < LENOUT1; i++){ |
joshema216 | 2:ea6275d1222f | 553 | pc.printf("%02X", DS1[i]&0x000000FF); |
joshema216 | 2:ea6275d1222f | 554 | GSM.printf("%02X", DS1[i]&0x000000FF); |
joshema216 | 2:ea6275d1222f | 555 | } |
joshema216 | 2:ea6275d1222f | 556 | wait(1); |
joshema216 | 2:ea6275d1222f | 557 | GSM.putc((char)0x1A); // Ctrl - Z. |
joshema216 | 2:ea6275d1222f | 558 | GSM.scanf("%s",buf); // Estado del mensaje (Envió o Error). |
joshema216 | 2:ea6275d1222f | 559 | pc.printf(">%s\n",buf); |
joshema216 | 2:ea6275d1222f | 560 | pc.printf("\n"); |
procesadores_FAC | 0:b2a6aa7c0c8c | 561 | } |
procesadores_FAC | 0:b2a6aa7c0c8c | 562 | |
joshema216 | 2:ea6275d1222f | 563 | wait(2); |
joshema216 | 2:ea6275d1222f | 564 | LedAzul = 1; |
joshema216 | 2:ea6275d1222f | 565 | LedVerde = 0; |
joshema216 | 2:ea6275d1222f | 566 | GSM.printf("AT+CMGD=0\r\n"); // Borra el mensaje actual (Posición "0"). |
joshema216 | 2:ea6275d1222f | 567 | //pc.printf("\n%s\n\n", "Borro mensaje"); |
joshema216 | 2:ea6275d1222f | 568 | } |
joshema216 | 2:ea6275d1222f | 569 | |
joshema216 | 2:ea6275d1222f | 570 | // COMPARA resp con "F6379B1E569701" que es "voltaje", o "F6379B1E569701" que es "Voltaje". |
joshema216 | 2:ea6275d1222f | 571 | if((strncmp("F6379B1E569701", msg, 14) == 0) || (strncmp("D6379B1E569701", msg, 14) == 0)){ |
joshema216 | 2:ea6275d1222f | 572 | //recibe_ok(); |
joshema216 | 2:ea6275d1222f | 573 | LedRojo = 0; // Encender LED amarillo. |
joshema216 | 2:ea6275d1222f | 574 | LedVerde = 0; |
joshema216 | 2:ea6275d1222f | 575 | LedAzul = 1; |
joshema216 | 2:ea6275d1222f | 576 | wait(2); |
joshema216 | 2:ea6275d1222f | 577 | |
joshema216 | 2:ea6275d1222f | 578 | med = v.read()*3.3; |
joshema216 | 2:ea6275d1222f | 579 | medi = v.read(); |
joshema216 | 2:ea6275d1222f | 580 | pc.printf("\n%f\n", medi); |
joshema216 | 2:ea6275d1222f | 581 | |
joshema216 | 2:ea6275d1222f | 582 | cleanBuffer(outmed, 16); |
joshema216 | 2:ea6275d1222f | 583 | if (med < 1){ // Se convierte la Medida a caracter. |
joshema216 | 2:ea6275d1222f | 584 | strcat(outmed, "0"); |
joshema216 | 2:ea6275d1222f | 585 | ftoa(med, outmedn, 5); |
joshema216 | 2:ea6275d1222f | 586 | strcat(outmed, outmedn); |
joshema216 | 2:ea6275d1222f | 587 | } |
joshema216 | 2:ea6275d1222f | 588 | else{ |
joshema216 | 2:ea6275d1222f | 589 | ftoa(med, outmed, 5); |
joshema216 | 2:ea6275d1222f | 590 | } |
joshema216 | 2:ea6275d1222f | 591 | strcpy(DE2,"Voltaje: "); |
joshema216 | 2:ea6275d1222f | 592 | strcat(DE2,outmed); |
joshema216 | 2:ea6275d1222f | 593 | pc.printf("\n%s\n\n", DE2); |
joshema216 | 2:ea6275d1222f | 594 | LENIN2 = strlen(DE2); |
joshema216 | 2:ea6275d1222f | 595 | |
joshema216 | 2:ea6275d1222f | 596 | //Conversión a octetos. |
joshema216 | 2:ea6275d1222f | 597 | K = 0; |
joshema216 | 2:ea6275d1222f | 598 | C = 0; |
joshema216 | 2:ea6275d1222f | 599 | for (i = 0; i < LENIN2; i++){ |
joshema216 | 2:ea6275d1222f | 600 | DS2[i] = DE2[i + C] >> K | DE2[i + C + 1] << (7 - K); |
joshema216 | 2:ea6275d1222f | 601 | if(DS2[i] == 0x00) {LENOUT2 = i; goto salir2;} |
joshema216 | 2:ea6275d1222f | 602 | K++; |
joshema216 | 2:ea6275d1222f | 603 | if (K == 7) {K = 0; C++;} // se chequea que ya se acabaron los bits en un ciclo de conversion. |
joshema216 | 2:ea6275d1222f | 604 | } |
joshema216 | 2:ea6275d1222f | 605 | |
joshema216 | 2:ea6275d1222f | 606 | salir2: |
joshema216 | 2:ea6275d1222f | 607 | for (i = 0; i < LENIN2; i++){ |
joshema216 | 2:ea6275d1222f | 608 | pc.printf("%X", DE2[i]); |
joshema216 | 2:ea6275d1222f | 609 | } |
joshema216 | 2:ea6275d1222f | 610 | |
joshema216 | 2:ea6275d1222f | 611 | pc.printf(":\r\n"); |
joshema216 | 2:ea6275d1222f | 612 | for (i = 0; i < LENOUT2; i++){ |
joshema216 | 2:ea6275d1222f | 613 | pc.printf("%2X", DS2[i]&0x000000FF); |
joshema216 | 2:ea6275d1222f | 614 | } |
joshema216 | 2:ea6275d1222f | 615 | pc.printf("\r\nLENOUT VOLTAJE: %d, LENIN VOLTAJE: %2X\r\n", LENOUT2, LENIN2); |
joshema216 | 2:ea6275d1222f | 616 | |
joshema216 | 2:ea6275d1222f | 617 | // Concatenación del mensaje en formato PDU y envío del mismo. |
joshema216 | 2:ea6275d1222f | 618 | ind = 14 + LENOUT2 - 1; |
joshema216 | 2:ea6275d1222f | 619 | |
joshema216 | 2:ea6275d1222f | 620 | GSM.printf("AT+CMGS=%d\r\n",ind); |
joshema216 | 2:ea6275d1222f | 621 | pc.printf("AT+CMGS=%d\r\n",ind); |
joshema216 | 2:ea6275d1222f | 622 | pc.printf(relle1); |
joshema216 | 2:ea6275d1222f | 623 | GSM.printf(relle1); |
joshema216 | 2:ea6275d1222f | 624 | |
joshema216 | 2:ea6275d1222f | 625 | for (i=0; i <= 9; i++) { |
joshema216 | 2:ea6275d1222f | 626 | pc.printf("%c",tel[i]); |
joshema216 | 2:ea6275d1222f | 627 | GSM.printf("%c",tel[i]); |
joshema216 | 2:ea6275d1222f | 628 | } |
joshema216 | 2:ea6275d1222f | 629 | |
joshema216 | 2:ea6275d1222f | 630 | pc.printf(relle2); |
joshema216 | 2:ea6275d1222f | 631 | GSM.printf(relle2); |
joshema216 | 2:ea6275d1222f | 632 | pc.printf("%2X", LENIN2); |
joshema216 | 2:ea6275d1222f | 633 | GSM.printf("%2X", LENIN2); |
joshema216 | 2:ea6275d1222f | 634 | /*pc.printf("0F"); |
joshema216 | 2:ea6275d1222f | 635 | GSM.printf("0F");*/ |
joshema216 | 2:ea6275d1222f | 636 | |
joshema216 | 2:ea6275d1222f | 637 | for (i = 0; i < LENOUT2; i++){ |
joshema216 | 2:ea6275d1222f | 638 | pc.printf("%02X", DS2[i]&0x000000FF); |
joshema216 | 2:ea6275d1222f | 639 | GSM.printf("%02X", DS2[i]&0x000000FF); |
joshema216 | 2:ea6275d1222f | 640 | } |
joshema216 | 2:ea6275d1222f | 641 | |
joshema216 | 2:ea6275d1222f | 642 | wait(1); |
joshema216 | 2:ea6275d1222f | 643 | GSM.putc((char)0x1A); // Ctrl - Z |
joshema216 | 2:ea6275d1222f | 644 | GSM.scanf("%s",buf); // Estado del mensaje (Envió o Error). |
joshema216 | 2:ea6275d1222f | 645 | pc.printf(">%s\n\n",buf); |
procesadores_FAC | 0:b2a6aa7c0c8c | 646 | pc.printf("\n"); |
procesadores_FAC | 0:b2a6aa7c0c8c | 647 | |
joshema216 | 2:ea6275d1222f | 648 | wait(2); |
joshema216 | 2:ea6275d1222f | 649 | LedRojo=1; |
joshema216 | 2:ea6275d1222f | 650 | LedVerde=0; |
joshema216 | 2:ea6275d1222f | 651 | GSM.printf("AT+CMGD=0\r\n"); // Borra el mensaje actual (Posición "0"). |
joshema216 | 2:ea6275d1222f | 652 | //pc.printf("\n%s\n\n", "Borro mensaje"); |
joshema216 | 2:ea6275d1222f | 653 | } |
joshema216 | 2:ea6275d1222f | 654 | } |
joshema216 | 2:ea6275d1222f | 655 | } |
joshema216 | 2:ea6275d1222f | 656 | } |
AaronGonzalez | 1:e2bd083802c0 | 657 | } |