gsm
Dependencies: DebouncedIn mbed
Fork of CLASEDELGSM1 by
main.cpp@7:08dac00ecff7, 2016-05-11 (annotated)
- Committer:
- tony63
- Date:
- Wed May 11 01:36:00 2016 +0000
- Revision:
- 7:08dac00ecff7
- Parent:
- 6:4cece0df6a3b
mas mejoras mas estable
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tony63 | 6:4cece0df6a3b | 1 | /*************************Alarma Contra Robo*******ver:01.may 10 2016 ************************************************** |
tony63 | 6:4cece0df6a3b | 2 | Como modem usa un celular SIEMENS a56i (ver compatibilidad mandos AT otros modems) |
tony63 | 4:0b889f7a8eba | 3 | Envia el mensaje intruso y otras cosas (mire el codigo de abajo) |
tony63 | 4:0b889f7a8eba | 4 | Como modem GSM usa un celular SIEMENS A56i |
tony63 | 4:0b889f7a8eba | 5 | El conector se distingue asi: |
tony63 | 6:4cece0df6a3b | 6 | Cable verde es RX conectelo a PTE0 cable blanco es TX conectelo a PTE1 (placa FRDMKL25Z de NXP antes freescale) |
tony63 | 4:0b889f7a8eba | 7 | La alarma se dispara segun la señal digital de entrada de un sensor PIR que se le filtra el ruido por medio de |
tony63 | 4:0b889f7a8eba | 8 | una validacion por retardo |
tony63 | 4:0b889f7a8eba | 9 | La alarma puede hacer accionamientos como: |
tony63 | 4:0b889f7a8eba | 10 | Accionamiento Permanente de una sirena |
tony63 | 4:0b889f7a8eba | 11 | Accionaminto temporizado de una sirena |
tony63 | 4:0b889f7a8eba | 12 | Accionamiento de un Mensaje de voz reperido y temporizado con un modulo de voz parallax |
tony63 | 4:0b889f7a8eba | 13 | puede marcar un telefono fijo y uno de celular e inyectar audio, como voz robotizada a la linea de audio |
tony63 | 4:0b889f7a8eba | 14 | usando un modulo GSM/GPRS SIM900 para arduino con entradas y salidas de audio |
tony63 | 4:0b889f7a8eba | 15 | puede leer coordenadas de gps y enviarlas con el link de googlemaps. |
tony63 | 0:edc50cf04b46 | 16 | // OJO CON ESTO |
tony63 | 0:edc50cf04b46 | 17 | // conector del siemens cable verde es RX conectelo a PTE0 cable blanco es TX conectelo a PTE1 |
tony63 | 4:0b889f7a8eba | 18 | |
tony63 | 4:0b889f7a8eba | 19 | ESTA ALARMA SE PUEDE PROBAR DE LA SIGUIENTE FORMA |
tony63 | 4:0b889f7a8eba | 20 | LOS TELEFONOS ESTAN PREGRAVADOS O CON UN CAMBIO SIMPLE EN EL PROGRAMA EL SISTEMA PUEDE CONTESTAR |
tony63 | 4:0b889f7a8eba | 21 | CON EL TELEFONO QUE LO LLAMO |
tony63 | 4:0b889f7a8eba | 22 | EN EL CASO DE RASTREADORES |
tony63 | 4:0b889f7a8eba | 23 | HAY UN SUICHE QUE SE LLAMA PIR UBICADO EN PTA13..SI SE PULSA POR UN INSTANTE DE TIEMPO |
tony63 | 4:0b889f7a8eba | 24 | EL LED VERDE DESTELLA INDICANDO QUE SE ENVIO EL MENSAJE AL NUMERO CODIFICADO EN LA CADENA SEGUN LAS TRAMAS PDU |
tony63 | 4:0b889f7a8eba | 25 | ENVIA EL MENSAJE "HAY INTRUSOS EN LA FINCA...." |
tony63 | 4:0b889f7a8eba | 26 | |
tony63 | 4:0b889f7a8eba | 27 | EN EL CASO DE ENVIAR ORDENES SE DISEÑARON DOS |
tony63 | 4:0b889f7a8eba | 28 | SI SE ENVIA UN MENSAJE CON Alarma1 prende el led azul 5 segundos y retorna a verde |
tony63 | 4:0b889f7a8eba | 29 | SI se envia el mensaje con Alarma2 prende el led rojo 5 segundos y retorna a verde |
tony63 | 4:0b889f7a8eba | 30 | si el telefono falla da indicacion con rojo |
tony63 | 4:0b889f7a8eba | 31 | el sistema reintenta de forma infinita la configuracion hasta que lo logra |
tony63 | 4:0b889f7a8eba | 32 | *****se verifico que no se de bloqueo y asi resulto bajo intensas pruebas******** |
tony63 | 4:0b889f7a8eba | 33 | este sistema detecta el mensaje, el tamaño del mensaje y el numero telefonico de quien llama pero etos parametros no se estan usando |
tony63 | 4:0b889f7a8eba | 34 | se podrian usar en programas mas complejos como rastreadores satelitales o sistemas de monitoreo de variablees fisicas |
tony63 | 4:0b889f7a8eba | 35 | que se consultan por medio de ordenes previas segun algun Mensaje corto. |
tony63 | 4:0b889f7a8eba | 36 | |
tony63 | 4:0b889f7a8eba | 37 | */ |
tony63 | 0:edc50cf04b46 | 38 | #include "mbed.h" |
tony63 | 0:edc50cf04b46 | 39 | #include "DebouncedIn.h" |
tony63 | 0:edc50cf04b46 | 40 | #include "stdio.h" |
tony63 | 0:edc50cf04b46 | 41 | #include "string.h" |
tony63 | 0:edc50cf04b46 | 42 | Timer t; |
tony63 | 0:edc50cf04b46 | 43 | DigitalOut LedVerde(LED2); |
tony63 | 0:edc50cf04b46 | 44 | DigitalOut LedRojo(LED1); |
tony63 | 6:4cece0df6a3b | 45 | DigitalOut camara(PTE20 ); |
tony63 | 6:4cece0df6a3b | 46 | DigitalOut sirena(PTE21 ); |
tony63 | 6:4cece0df6a3b | 47 | DigitalOut luces(PTE22 ); |
tony63 | 6:4cece0df6a3b | 48 | DigitalOut audio(PTE23 ); |
tony63 | 0:edc50cf04b46 | 49 | DigitalOut LedAzul(LED3); |
tony63 | 4:0b889f7a8eba | 50 | DebouncedIn PIR(PTA13); //señal que inicia el envio del mensaje |
tony63 | 0:edc50cf04b46 | 51 | DebouncedIn button1(PTC12); //señal que inicia el envio del mensaje |
tony63 | 6:4cece0df6a3b | 52 | Serial GSM(PTE0,PTE1); //Configura puerto UART1 de la FRDMKL25Z |
tony63 | 6:4cece0df6a3b | 53 | Serial voz(PTA2,PTA1); //Configura puerto UART0 de la FRDMKL25Z |
tony63 | 0:edc50cf04b46 | 54 | Serial pc(USBTX,USBRX);//Configura puerto USB a la consola serial del PC conectado. |
tony63 | 0:edc50cf04b46 | 55 | void Rx_interrupt(); |
tony63 | 0:edc50cf04b46 | 56 | int position=0; |
tony63 | 2:0377af333c98 | 57 | int intentos=0; |
tony63 | 0:edc50cf04b46 | 58 | int lenpack=6; |
tony63 | 2:0377af333c98 | 59 | int ret=1; |
tony63 | 0:edc50cf04b46 | 60 | int longi=0; |
tony63 | 1:6b506dde0a6e | 61 | char tel[11]; |
tony63 | 0:edc50cf04b46 | 62 | char DE[50]; |
tony63 | 5:dbb0384120e0 | 63 | char buffer[200]; |
tony63 | 5:dbb0384120e0 | 64 | //char buffermsg[200]; |
tony63 | 5:dbb0384120e0 | 65 | //char buffer1[200]; |
tony63 | 5:dbb0384120e0 | 66 | //char datos[200]; |
tony63 | 2:0377af333c98 | 67 | char NUMBER[13]; |
tony63 | 2:0377af333c98 | 68 | char resp[6]; |
tony63 | 2:0377af333c98 | 69 | char CMT[]="+CMTI"; |
tony63 | 2:0377af333c98 | 70 | char tam[2]; |
tony63 | 0:edc50cf04b46 | 71 | int index; |
tony63 | 0:edc50cf04b46 | 72 | int count; |
tony63 | 0:edc50cf04b46 | 73 | int i = 0; |
tony63 | 1:6b506dde0a6e | 74 | int j = 0; |
tony63 | 0:edc50cf04b46 | 75 | int c=0; |
tony63 | 0:edc50cf04b46 | 76 | unsigned char CtrlZ = 0x1A; // comodin de emision controlZ |
tony63 | 0:edc50cf04b46 | 77 | bool Flag = false; // bandera |
tony63 | 0:edc50cf04b46 | 78 | char r[]=""; //Cadena de recepcion de la trama PDU si se usa!! |
tony63 | 5:dbb0384120e0 | 79 | char msg[50]; |
tony63 | 0:edc50cf04b46 | 80 | char char1; |
tony63 | 0:edc50cf04b46 | 81 | //Flush serial para el buffer |
tony63 | 0:edc50cf04b46 | 82 | void FlushGSM(void) { |
tony63 | 0:edc50cf04b46 | 83 | char1 = 0; |
tony63 | 0:edc50cf04b46 | 84 | while (GSM.readable()){ |
tony63 | 0:edc50cf04b46 | 85 | char1 = GSM.getc();} |
tony63 | 0:edc50cf04b46 | 86 | return;} |
tony63 | 0:edc50cf04b46 | 87 | |
tony63 | 0:edc50cf04b46 | 88 | void callback() { |
tony63 | 0:edc50cf04b46 | 89 | // Note: you need to actually read from the serial to clear the RX interrupt |
tony63 | 0:edc50cf04b46 | 90 | pc.printf("%c\n", GSM.getc()); |
tony63 | 0:edc50cf04b46 | 91 | |
tony63 | 0:edc50cf04b46 | 92 | } |
tony63 | 2:0377af333c98 | 93 | //**************************************************************************************************************** |
tony63 | 2:0377af333c98 | 94 | //esta funcion de abajo lee todo un bufer hasta encontrar CR o LF y el resto lo rellena de |
tony63 | 2:0377af333c98 | 95 | //$, count es lo que va a leer.Lo leido lo mete en buffer que es una cadena previamente definida |
tony63 | 2:0377af333c98 | 96 | //incorpora medida de tiempo si se demora mas de tres segundos retorna fracaso con -1 |
tony63 | 2:0377af333c98 | 97 | //**************************************************************************************************************** |
tony63 | 0:edc50cf04b46 | 98 | int readBuffer(char *buffer,int count) |
tony63 | 0:edc50cf04b46 | 99 | { |
tony63 | 0:edc50cf04b46 | 100 | int i=0; |
tony63 | 0:edc50cf04b46 | 101 | t.start(); // start timer |
tony63 | 0:edc50cf04b46 | 102 | while(1) { |
tony63 | 0:edc50cf04b46 | 103 | while (GSM.readable()) { |
tony63 | 0:edc50cf04b46 | 104 | char c = GSM.getc(); |
tony63 | 0:edc50cf04b46 | 105 | if (c == '\r' || c == '\n') c = '$'; |
tony63 | 0:edc50cf04b46 | 106 | buffer[i++] = c; |
tony63 | 0:edc50cf04b46 | 107 | if(i > count)break; |
tony63 | 0:edc50cf04b46 | 108 | } |
tony63 | 0:edc50cf04b46 | 109 | if(i > count)break; |
tony63 | 0:edc50cf04b46 | 110 | if(t.read() > 3) { |
tony63 | 0:edc50cf04b46 | 111 | t.stop(); |
tony63 | 0:edc50cf04b46 | 112 | t.reset(); |
tony63 | 0:edc50cf04b46 | 113 | break; |
tony63 | 0:edc50cf04b46 | 114 | } |
tony63 | 0:edc50cf04b46 | 115 | } |
tony63 | 0:edc50cf04b46 | 116 | wait(0.5); |
tony63 | 0:edc50cf04b46 | 117 | while(GSM.readable()) { // display the other thing.. |
tony63 | 0:edc50cf04b46 | 118 | char c = GSM.getc(); |
tony63 | 0:edc50cf04b46 | 119 | } |
tony63 | 0:edc50cf04b46 | 120 | return 0; |
tony63 | 0:edc50cf04b46 | 121 | } |
tony63 | 2:0377af333c98 | 122 | //******************************************************************************** |
tony63 | 0:edc50cf04b46 | 123 | /* esta funcion de abajo limpia o borra todo un "buffer" de tamaño "count" |
tony63 | 2:0377af333c98 | 124 | lo revisa elemento por elemento y le mete el caracter null que indica fin de cadena |
tony63 | 0:edc50cf04b46 | 125 | no retorna nada |
tony63 | 0:edc50cf04b46 | 126 | */ |
tony63 | 0:edc50cf04b46 | 127 | //*************************************************************************************** |
tony63 | 0:edc50cf04b46 | 128 | void cleanBuffer(char *buffer, int count) |
tony63 | 0:edc50cf04b46 | 129 | { |
tony63 | 0:edc50cf04b46 | 130 | for(int i=0; i < count; i++) { |
tony63 | 0:edc50cf04b46 | 131 | buffer[i] = '\0'; |
tony63 | 0:edc50cf04b46 | 132 | } |
tony63 | 0:edc50cf04b46 | 133 | } |
tony63 | 0:edc50cf04b46 | 134 | /* esta funcion de abajo envia un comando parametrizado como cadena |
tony63 | 2:0377af333c98 | 135 | puede ser un comando tipo AT |
tony63 | 0:edc50cf04b46 | 136 | */ |
tony63 | 0:edc50cf04b46 | 137 | //*************************************************************************************** |
tony63 | 0:edc50cf04b46 | 138 | void sendCmd(char *cmd) |
tony63 | 0:edc50cf04b46 | 139 | { |
tony63 | 0:edc50cf04b46 | 140 | GSM.puts(cmd); |
tony63 | 0:edc50cf04b46 | 141 | } |
tony63 | 2:0377af333c98 | 142 | //**************************************************************************************** |
tony63 | 0:edc50cf04b46 | 143 | /* esta funcion de abajo espera la respuesta de un comando que debe ser identica a la cadena "resp" y un tiempo timeout" |
tony63 | 0:edc50cf04b46 | 144 | si todo sale bien retorna un cero que en la programacion hay que validar |
tony63 | 0:edc50cf04b46 | 145 | si algo sale mal ( no se parece o se demora mucho )retorna -1 que debera validarse con alguna expresion logica |
tony63 | 0:edc50cf04b46 | 146 | */ |
tony63 | 0:edc50cf04b46 | 147 | //*************************************************************************************** |
tony63 | 0:edc50cf04b46 | 148 | int waitForResp(char *resp, int timeout) |
tony63 | 0:edc50cf04b46 | 149 | { |
tony63 | 0:edc50cf04b46 | 150 | int len = strlen(resp); |
tony63 | 0:edc50cf04b46 | 151 | int sum=0; |
tony63 | 0:edc50cf04b46 | 152 | t.start(); |
tony63 | 0:edc50cf04b46 | 153 | |
tony63 | 0:edc50cf04b46 | 154 | while(1) { |
tony63 | 0:edc50cf04b46 | 155 | if(GSM.readable()) { |
tony63 | 0:edc50cf04b46 | 156 | char c = GSM.getc(); |
tony63 | 0:edc50cf04b46 | 157 | sum = (c==resp[sum]) ? sum+1 : 0;// esta linea de C# sum se incrementa o se hace cero segun c |
tony63 | 0:edc50cf04b46 | 158 | if(sum == len)break; //ya acabo se sale |
tony63 | 0:edc50cf04b46 | 159 | } |
tony63 | 0:edc50cf04b46 | 160 | if(t.read() > timeout) { // time out chequea el tiempo minimo antes de salir perdiendo |
tony63 | 0:edc50cf04b46 | 161 | t.stop(); |
tony63 | 0:edc50cf04b46 | 162 | t.reset(); |
tony63 | 0:edc50cf04b46 | 163 | return -1; |
tony63 | 0:edc50cf04b46 | 164 | } |
tony63 | 0:edc50cf04b46 | 165 | } |
tony63 | 0:edc50cf04b46 | 166 | t.stop(); // stop timer antes de retornar |
tony63 | 0:edc50cf04b46 | 167 | t.reset(); // clear timer |
tony63 | 0:edc50cf04b46 | 168 | while(GSM.readable()) { // display the other thing.. |
tony63 | 0:edc50cf04b46 | 169 | char c = GSM.getc(); |
tony63 | 0:edc50cf04b46 | 170 | } |
tony63 | 0:edc50cf04b46 | 171 | |
tony63 | 0:edc50cf04b46 | 172 | return 0; |
tony63 | 0:edc50cf04b46 | 173 | } |
tony63 | 0:edc50cf04b46 | 174 | /* esta funcion de abajo es muy completa e util se encarga de enviar el comando y esperar la respuesta |
tony63 | 0:edc50cf04b46 | 175 | si todo sale bien retorna un cero(herencia de las funciones contenedoras) que en la programacion hay que validar |
tony63 | 0:edc50cf04b46 | 176 | con alguna expresion logica |
tony63 | 0:edc50cf04b46 | 177 | */ |
tony63 | 0:edc50cf04b46 | 178 | //*************************************************************************************** |
tony63 | 0:edc50cf04b46 | 179 | int sendCmdAndWaitForResp(char *cmd, char *resp, int timeout) |
tony63 | 0:edc50cf04b46 | 180 | { |
tony63 | 0:edc50cf04b46 | 181 | sendCmd(cmd); |
tony63 | 0:edc50cf04b46 | 182 | return waitForResp(resp,timeout); |
tony63 | 0:edc50cf04b46 | 183 | } |
tony63 | 0:edc50cf04b46 | 184 | /* esta funcion de abajo chequea que el modem este vivo envia AT y le contesta con OK y espera 2 segundos |
tony63 | 0:edc50cf04b46 | 185 | */ |
tony63 | 0:edc50cf04b46 | 186 | //*************************************************************************************** |
tony63 | 0:edc50cf04b46 | 187 | int powerCheck(void)// este comando se manda para verificar si el modem esta vivo o conectado |
tony63 | 0:edc50cf04b46 | 188 | { |
tony63 | 0:edc50cf04b46 | 189 | return sendCmdAndWaitForResp("AT\r\n", "OK", 2); |
tony63 | 0:edc50cf04b46 | 190 | } |
tony63 | 0:edc50cf04b46 | 191 | /* esta funcion de abajo chequea el estado de la sim card |
tony63 | 0:edc50cf04b46 | 192 | y si todo sale bien retorna un cero que en la programacion hay que validar |
tony63 | 0:edc50cf04b46 | 193 | con alguna expresion logica |
tony63 | 0:edc50cf04b46 | 194 | */ |
tony63 | 0:edc50cf04b46 | 195 | //*************************************************************************************** |
tony63 | 0:edc50cf04b46 | 196 | int checkSIMStatus(void) |
tony63 | 0:edc50cf04b46 | 197 | { |
tony63 | 0:edc50cf04b46 | 198 | char gprsBuffer[30]; |
tony63 | 0:edc50cf04b46 | 199 | int count = 0; |
tony63 | 0:edc50cf04b46 | 200 | cleanBuffer(gprsBuffer,30); |
tony63 | 0:edc50cf04b46 | 201 | while(count < 3) { |
tony63 | 0:edc50cf04b46 | 202 | sendCmd("AT+CPIN?\r\n"); |
tony63 | 0:edc50cf04b46 | 203 | readBuffer(gprsBuffer,30); |
tony63 | 0:edc50cf04b46 | 204 | if((NULL != strstr(gprsBuffer,"+CPIN: READY"))) { |
tony63 | 0:edc50cf04b46 | 205 | break; |
tony63 | 0:edc50cf04b46 | 206 | } |
tony63 | 0:edc50cf04b46 | 207 | count++; |
tony63 | 0:edc50cf04b46 | 208 | wait(1); |
tony63 | 0:edc50cf04b46 | 209 | } |
tony63 | 0:edc50cf04b46 | 210 | |
tony63 | 0:edc50cf04b46 | 211 | if(count == 3) { |
tony63 | 0:edc50cf04b46 | 212 | return -1; |
tony63 | 0:edc50cf04b46 | 213 | } |
tony63 | 0:edc50cf04b46 | 214 | return 0; |
tony63 | 0:edc50cf04b46 | 215 | } |
tony63 | 0:edc50cf04b46 | 216 | /* esta funcion de abajo chequea la calidad de la señal |
tony63 | 2:0377af333c98 | 217 | y si todo sale bien retorna con el valor de señal util o un -1 si no es aceptable, en la programacion hay que validar |
tony63 | 0:edc50cf04b46 | 218 | con alguna expresion logica |
tony63 | 0:edc50cf04b46 | 219 | */ |
tony63 | 0:edc50cf04b46 | 220 | //*************************************************************************************** |
tony63 | 0:edc50cf04b46 | 221 | int checkSignalStrength(void) |
tony63 | 0:edc50cf04b46 | 222 | { |
tony63 | 0:edc50cf04b46 | 223 | char gprsBuffer[100]; |
tony63 | 0:edc50cf04b46 | 224 | int index,count = 0; |
tony63 | 0:edc50cf04b46 | 225 | cleanBuffer(gprsBuffer,100); |
tony63 | 0:edc50cf04b46 | 226 | while(count < 3) { |
tony63 | 0:edc50cf04b46 | 227 | sendCmd("AT+CSQ\r\n"); |
tony63 | 0:edc50cf04b46 | 228 | readBuffer(gprsBuffer,25); |
tony63 | 0:edc50cf04b46 | 229 | if(sscanf(gprsBuffer, "AT+CSQ$$$$+CSQ: %d", &index)>0) { |
tony63 | 0:edc50cf04b46 | 230 | break; |
tony63 | 0:edc50cf04b46 | 231 | } |
tony63 | 0:edc50cf04b46 | 232 | count++; |
tony63 | 0:edc50cf04b46 | 233 | wait(1); |
tony63 | 0:edc50cf04b46 | 234 | } |
tony63 | 0:edc50cf04b46 | 235 | if(count == 3) { |
tony63 | 0:edc50cf04b46 | 236 | return -1; |
tony63 | 0:edc50cf04b46 | 237 | } |
tony63 | 0:edc50cf04b46 | 238 | return index; |
tony63 | 0:edc50cf04b46 | 239 | } |
tony63 | 0:edc50cf04b46 | 240 | |
tony63 | 0:edc50cf04b46 | 241 | /* esta funcion de abajo inicaliza el modem se compone de un grupo de subfunciones ya definidas previamente |
tony63 | 0:edc50cf04b46 | 242 | primero chequea que este vivo |
tony63 | 0:edc50cf04b46 | 243 | segundo chequea el estado de la simcard |
tony63 | 0:edc50cf04b46 | 244 | tercero chequea la intencidad de señal celular |
tony63 | 0:edc50cf04b46 | 245 | cuarto aplica la configuracion |
tony63 | 0:edc50cf04b46 | 246 | y si todo sale bien retorna un cero que en la programacion hay que validar |
tony63 | 0:edc50cf04b46 | 247 | con alguna expresion logica |
tony63 | 0:edc50cf04b46 | 248 | */ |
tony63 | 0:edc50cf04b46 | 249 | //*************************************************************************************** |
tony63 | 0:edc50cf04b46 | 250 | int init() |
tony63 | 0:edc50cf04b46 | 251 | { |
tony63 | 2:0377af333c98 | 252 | if (0 != sendCmdAndWaitForResp("AT\r\n", "OK", 3)){ |
tony63 | 2:0377af333c98 | 253 | return -1; |
tony63 | 2:0377af333c98 | 254 | } |
tony63 | 2:0377af333c98 | 255 | if (0 != sendCmdAndWaitForResp("AT+CNMI=1,1\r\n", "OK", 3)){ |
tony63 | 2:0377af333c98 | 256 | return -1; |
tony63 | 2:0377af333c98 | 257 | } |
tony63 | 2:0377af333c98 | 258 | if (0 != sendCmdAndWaitForResp("AT+CMGF=0\r\n", "OK", 3)){ |
tony63 | 0:edc50cf04b46 | 259 | return -1; |
tony63 | 2:0377af333c98 | 260 | } |
tony63 | 6:4cece0df6a3b | 261 | if (0 != sendCmdAndWaitForResp("AT+CBST=7,0,1\r\n", "OK", 3)){ //velocidad fija a 9600, modem asincronico no transparente |
tony63 | 0:edc50cf04b46 | 262 | return -1; |
tony63 | 2:0377af333c98 | 263 | } |
tony63 | 6:4cece0df6a3b | 264 | if (0 != sendCmdAndWaitForResp("ATE\r\n", "OK", 3)){ //se le quita el eco al modem GSM |
tony63 | 5:dbb0384120e0 | 265 | return -1; |
tony63 | 5:dbb0384120e0 | 266 | } |
tony63 | 2:0377af333c98 | 267 | LedVerde=0; |
tony63 | 2:0377af333c98 | 268 | return 0; |
tony63 | 2:0377af333c98 | 269 | } |
tony63 | 2:0377af333c98 | 270 | |
tony63 | 0:edc50cf04b46 | 271 | /* esta funcion de abajo intenta leer un mensaje de texto en formato PDU o HEX |
tony63 | 0:edc50cf04b46 | 272 | y si todo sale bien retorna un cero que en la programacion hay que validar |
tony63 | 0:edc50cf04b46 | 273 | con alguna expresion logica |
tony63 | 0:edc50cf04b46 | 274 | */ |
tony63 | 0:edc50cf04b46 | 275 | //*************************************************************************************** |
tony63 | 0:edc50cf04b46 | 276 | int readSMSpdu(char *message, int index) |
tony63 | 0:edc50cf04b46 | 277 | { |
tony63 | 0:edc50cf04b46 | 278 | int i = 0; |
tony63 | 0:edc50cf04b46 | 279 | char gprsBuffer[100]; |
tony63 | 0:edc50cf04b46 | 280 | char *p,*s; |
tony63 | 0:edc50cf04b46 | 281 | GSM.printf("AT+CMGR=%d\r\n",index); |
tony63 | 0:edc50cf04b46 | 282 | cleanBuffer(gprsBuffer,100); |
tony63 | 0:edc50cf04b46 | 283 | readBuffer(gprsBuffer,100); |
tony63 | 0:edc50cf04b46 | 284 | if(NULL == ( s = strstr(gprsBuffer,"+CMGR"))) { |
tony63 | 0:edc50cf04b46 | 285 | return -1; |
tony63 | 0:edc50cf04b46 | 286 | } |
tony63 | 0:edc50cf04b46 | 287 | if(NULL != ( s = strstr(gprsBuffer,"+32"))) { |
tony63 | 0:edc50cf04b46 | 288 | p = s + 6; |
tony63 | 0:edc50cf04b46 | 289 | while((*p != '$')&&(i < 5)) { |
tony63 | 0:edc50cf04b46 | 290 | message[i++] = *(p++); |
tony63 | 0:edc50cf04b46 | 291 | } |
tony63 | 0:edc50cf04b46 | 292 | message[i] = '\0'; |
tony63 | 0:edc50cf04b46 | 293 | } |
tony63 | 0:edc50cf04b46 | 294 | return 0; |
tony63 | 0:edc50cf04b46 | 295 | } |
tony63 | 0:edc50cf04b46 | 296 | /* esta funcion de abajo borra mensajes SMS del modem |
tony63 | 0:edc50cf04b46 | 297 | y si todo sale bien retorna un cero que en la programacion hay que validar |
tony63 | 0:edc50cf04b46 | 298 | con alguna expresion logica |
tony63 | 0:edc50cf04b46 | 299 | */ |
tony63 | 0:edc50cf04b46 | 300 | //*************************************************************************************** |
tony63 | 0:edc50cf04b46 | 301 | int deleteSMS(int index) |
tony63 | 0:edc50cf04b46 | 302 | { |
tony63 | 0:edc50cf04b46 | 303 | char cmd[32]; |
tony63 | 0:edc50cf04b46 | 304 | snprintf(cmd,sizeof(cmd),"AT+CMGD=%d\r\n",index); |
tony63 | 0:edc50cf04b46 | 305 | sendCmd(cmd); |
tony63 | 0:edc50cf04b46 | 306 | return 0; |
tony63 | 0:edc50cf04b46 | 307 | } |
tony63 | 6:4cece0df6a3b | 308 | //********Esta funcion devuelve la confirmacion del mensaje a quien lo envio************************** |
tony63 | 6:4cece0df6a3b | 309 | int recibe_ok(){ |
tony63 | 6:4cece0df6a3b | 310 | GSM.printf("AT+CMGS=32\n\r"); |
tony63 | 6:4cece0df6a3b | 311 | pc.printf("AT+CMGS=32\n\r"); |
tony63 | 7:08dac00ecff7 | 312 | wait_ms(100); |
tony63 | 6:4cece0df6a3b | 313 | GSM.printf("0011000A91%s0000AA15CDB27B1E569741F2F2382D4E93DFA0E7321402",tel); |
tony63 | 6:4cece0df6a3b | 314 | pc.printf("0011000A91%s0000AA15CDB27B1E569741F2F2382D4E93DFA0E7321402",tel); |
tony63 | 7:08dac00ecff7 | 315 | wait_ms(100); |
tony63 | 6:4cece0df6a3b | 316 | GSM.putc((char)0x1A); |
tony63 | 6:4cece0df6a3b | 317 | return 0; |
tony63 | 6:4cece0df6a3b | 318 | } |
tony63 | 2:0377af333c98 | 319 | //************************************************************************************ |
tony63 | 4:0b889f7a8eba | 320 | //*****************SE INICIA EL PROGRAMA PRINCIPAL******************************************************* |
tony63 | 4:0b889f7a8eba | 321 | int main() { |
tony63 | 4:0b889f7a8eba | 322 | //configuramos los puertos seriales |
tony63 | 4:0b889f7a8eba | 323 | GSM.baud(9600);//configura los baudios de la FRDMKL25Z en 9600 |
tony63 | 4:0b889f7a8eba | 324 | GSM.format(8,Serial::None,1); //configura el formato de los datos de la UART |
tony63 | 4:0b889f7a8eba | 325 | LedVerde=1; //APAGO LOS LEDS |
tony63 | 4:0b889f7a8eba | 326 | LedRojo=1; |
tony63 | 4:0b889f7a8eba | 327 | LedAzul=1; |
tony63 | 4:0b889f7a8eba | 328 | LedRojo=0; // PRENDO EL LED ROJO |
tony63 | 4:0b889f7a8eba | 329 | //quito el eco del modem |
tony63 | 4:0b889f7a8eba | 330 | |
tony63 | 4:0b889f7a8eba | 331 | //****************CONFIGURAMOS EL MODEM GSM (TELEFONO CELULAR SIEMENS A56i) |
tony63 | 4:0b889f7a8eba | 332 | inicio1: |
tony63 | 4:0b889f7a8eba | 333 | ret = init(); |
tony63 | 4:0b889f7a8eba | 334 | if(ret==0){ |
tony63 | 4:0b889f7a8eba | 335 | LedRojo=1; |
tony63 | 4:0b889f7a8eba | 336 | LedVerde=0; |
tony63 | 4:0b889f7a8eba | 337 | } |
tony63 | 4:0b889f7a8eba | 338 | else{ |
tony63 | 7:08dac00ecff7 | 339 | wait(1); |
tony63 | 4:0b889f7a8eba | 340 | goto inicio1; |
tony63 | 4:0b889f7a8eba | 341 | } |
tony63 | 0:edc50cf04b46 | 342 | |
tony63 | 2:0377af333c98 | 343 | //RUTINA PRINCIPAL******************************************************************************************* |
tony63 | 4:0b889f7a8eba | 344 | |
tony63 | 2:0377af333c98 | 345 | //********************************************************************************************************************* |
tony63 | 2:0377af333c98 | 346 | while(1){ |
tony63 | 4:0b889f7a8eba | 347 | //***************ALARMA POR ACTIVACION PIR*************************************************** |
tony63 | 4:0b889f7a8eba | 348 | if (PIR.falling()) |
tony63 | 2:0377af333c98 | 349 | { |
tony63 | 4:0b889f7a8eba | 350 | wait(1); |
tony63 | 4:0b889f7a8eba | 351 | if (!PIR) |
tony63 | 2:0377af333c98 | 352 | { |
tony63 | 4:0b889f7a8eba | 353 | |
tony63 | 4:0b889f7a8eba | 354 | //**********se envia la palabra "Hay Intrusos En La Finca de Rionegro" |
tony63 | 4:0b889f7a8eba | 355 | //******Gustavo |
tony63 | 4:0b889f7a8eba | 356 | GSM.printf("AT+CMGS=50\n\r"); |
tony63 | 7:08dac00ecff7 | 357 | wait_ms(100); |
tony63 | 4:0b889f7a8eba | 358 | GSM.printf("0011000A9113223717370000AA2A21970B19CE83926EBABC3E7FCF41453788190699D3EE7118442E83A4E9B7BB7C96BF5DAE10"); |
tony63 | 7:08dac00ecff7 | 359 | wait_ms(100); |
tony63 | 4:0b889f7a8eba | 360 | GSM.putc((char)0x1A); |
tony63 | 5:dbb0384120e0 | 361 | for(i=0;i<60;i++){ //por mas ruido se envia un mensaje cada minuto |
tony63 | 2:0377af333c98 | 362 | LedVerde=1; |
tony63 | 5:dbb0384120e0 | 363 | wait(0.5); |
tony63 | 2:0377af333c98 | 364 | LedVerde=0; |
tony63 | 5:dbb0384120e0 | 365 | wait(0.5); |
tony63 | 4:0b889f7a8eba | 366 | } |
tony63 | 4:0b889f7a8eba | 367 | } |
tony63 | 4:0b889f7a8eba | 368 | } |
tony63 | 4:0b889f7a8eba | 369 | // **************apartir de aca se pueden escribir mas alarmas o mensajes de salida activar leds salidas o generar respuestas |
tony63 | 4:0b889f7a8eba | 370 | |
tony63 | 4:0b889f7a8eba | 371 | |
tony63 | 2:0377af333c98 | 372 | |
tony63 | 2:0377af333c98 | 373 | |
tony63 | 4:0b889f7a8eba | 374 | //***************inicia la recepcion de un mensaje de texto********************************** |
tony63 | 4:0b889f7a8eba | 375 | //la comparacion de estos mensajes constituye generar accionamiento abrir o cerrar circuitos o contestar mensajes |
tony63 | 4:0b889f7a8eba | 376 | |
tony63 | 2:0377af333c98 | 377 | if (GSM.readable()) { |
tony63 | 5:dbb0384120e0 | 378 | readBuffer(buffer,110); |
tony63 | 2:0377af333c98 | 379 | pc.printf("%s\r\n",buffer); |
tony63 | 2:0377af333c98 | 380 | for(i=0;i<5;i++) |
tony63 | 2:0377af333c98 | 381 | { |
tony63 | 2:0377af333c98 | 382 | resp[i]=buffer[i]; |
tony63 | 2:0377af333c98 | 383 | } |
tony63 | 2:0377af333c98 | 384 | |
tony63 | 2:0377af333c98 | 385 | pc.printf("%s\r\n",resp); |
tony63 | 2:0377af333c98 | 386 | if(strcmp("$$+CM",resp) == 0){ //COMPARA resp con "+CMTI" |
tony63 | 2:0377af333c98 | 387 | pc.printf("llego MSG\r\n"); |
tony63 | 2:0377af333c98 | 388 | cleanBuffer(buffer,10); |
tony63 | 2:0377af333c98 | 389 | GSM.printf("AT+CMGL=0\r\n");//envio comando para leer mensaje |
tony63 | 2:0377af333c98 | 390 | pc.printf("AT+CMGL=0\r\n"); |
tony63 | 2:0377af333c98 | 391 | //if (GSM.readable()) { |
tony63 | 2:0377af333c98 | 392 | GSM.printf("AT+CMGD=0\r\n"); |
tony63 | 7:08dac00ecff7 | 393 | readBuffer(buffer,110); |
tony63 | 2:0377af333c98 | 394 | pc.printf("%s\r\n",buffer); |
tony63 | 7:08dac00ecff7 | 395 | //leer telefono |
tony63 | 2:0377af333c98 | 396 | for(i=0;i<10;i++){ |
tony63 | 2:0377af333c98 | 397 | tel[i]=buffer[i+40]; |
tony63 | 2:0377af333c98 | 398 | } |
tony63 | 2:0377af333c98 | 399 | pc.printf("%s-\r\n",tel); |
tony63 | 2:0377af333c98 | 400 | //leer tamaño |
tony63 | 2:0377af333c98 | 401 | for(i=0;i<2;i++){ |
tony63 | 2:0377af333c98 | 402 | tam[i]=buffer[i+68]; |
tony63 | 2:0377af333c98 | 403 | } |
tony63 | 2:0377af333c98 | 404 | pc.printf("%s-\r\n",tam); |
tony63 | 2:0377af333c98 | 405 | //leer mensaje |
tony63 | 5:dbb0384120e0 | 406 | for(i=0;i<20;i++){ |
tony63 | 5:dbb0384120e0 | 407 | msg[i]=buffer[i+70]; //OJO SE LEE EL MENSAJE APARTIR DE LA POSICION 70 14 caracteres |
tony63 | 2:0377af333c98 | 408 | } |
tony63 | 2:0377af333c98 | 409 | pc.printf("%s-\r\n",msg); |
tony63 | 2:0377af333c98 | 410 | //decodificar mensaje |
tony63 | 2:0377af333c98 | 411 | //comparar mensaje |
tony63 | 6:4cece0df6a3b | 412 | deleteSMS(1);//se borra los mensajes por medio de una funcion |
tony63 | 6:4cece0df6a3b | 413 | readBuffer(buffer,200); |
tony63 | 6:4cece0df6a3b | 414 | readBuffer(buffer,200); |
tony63 | 5:dbb0384120e0 | 415 | if(strncmp("417658DE0EC700",msg,14) == 0){ //COMPARA resp con "417658DE0EC700" que es Alarma1 |
tony63 | 6:4cece0df6a3b | 416 | recibe_ok(); |
tony63 | 2:0377af333c98 | 417 | LedVerde=1; |
tony63 | 2:0377af333c98 | 418 | LedAzul=0; |
tony63 | 2:0377af333c98 | 419 | wait(15); |
tony63 | 2:0377af333c98 | 420 | LedAzul=1; |
tony63 | 2:0377af333c98 | 421 | LedVerde=0; |
tony63 | 2:0377af333c98 | 422 | } |
tony63 | 5:dbb0384120e0 | 423 | if(strncmp("417658DE0ECB00",msg,14) == 0){ //COMPARA resp con "417658DE0ECB00" que es Alarma2 |
tony63 | 6:4cece0df6a3b | 424 | recibe_ok(); |
tony63 | 2:0377af333c98 | 425 | LedVerde=1; |
tony63 | 2:0377af333c98 | 426 | LedRojo=0; |
tony63 | 2:0377af333c98 | 427 | wait(15); |
tony63 | 2:0377af333c98 | 428 | LedRojo=1; |
tony63 | 2:0377af333c98 | 429 | LedVerde=0; |
tony63 | 2:0377af333c98 | 430 | } |
tony63 | 5:dbb0384120e0 | 431 | if(strncmp("C3703B2C0F83DE6E",msg,16) == 0){ //COMPARA resp con "417658DE0ECB00" que es Camara on |
tony63 | 6:4cece0df6a3b | 432 | recibe_ok(); |
tony63 | 5:dbb0384120e0 | 433 | camara=1; |
tony63 | 5:dbb0384120e0 | 434 | } |
tony63 | 5:dbb0384120e0 | 435 | if(strncmp("C3703B2C0F83DE66",msg,16) == 0){ //COMPARA resp con "417658DE0ECB00" que es Camara of |
tony63 | 6:4cece0df6a3b | 436 | recibe_ok(); |
tony63 | 5:dbb0384120e0 | 437 | camara=0; |
tony63 | 5:dbb0384120e0 | 438 | } |
tony63 | 5:dbb0384120e0 | 439 | if(strncmp("D3B4BCEC0E83DE6E",msg,16) == 0){ //COMPARA resp con "D3B4BCEC0E83DE6E" que es Sirena on |
tony63 | 6:4cece0df6a3b | 440 | recibe_ok(); |
tony63 | 6:4cece0df6a3b | 441 | sirena=1; |
tony63 | 6:4cece0df6a3b | 442 | wait(30); //prende la sirena 30 segundos |
tony63 | 6:4cece0df6a3b | 443 | sirena=0; |
tony63 | 6:4cece0df6a3b | 444 | } |
tony63 | 6:4cece0df6a3b | 445 | /* if(strncmp(" ",msg,16) == 0){ //COMPARA resp con " " que es luz on |
tony63 | 6:4cece0df6a3b | 446 | recibe_ok(); |
tony63 | 6:4cece0df6a3b | 447 | luces=1; |
tony63 | 6:4cece0df6a3b | 448 | wait(30); //prende la luz 30 segundos |
tony63 | 6:4cece0df6a3b | 449 | luces=0; |
tony63 | 5:dbb0384120e0 | 450 | } |
tony63 | 6:4cece0df6a3b | 451 | if(strncmp(" ",msg,16) == 0){ //COMPARA resp con "" que es Msg1 |
tony63 | 6:4cece0df6a3b | 452 | recibe_ok(); |
tony63 | 6:4cece0df6a3b | 453 | } |
tony63 | 6:4cece0df6a3b | 454 | if(strncmp(" ",msg,16) == 0){ //COMPARA resp con "" que es Msg2 |
tony63 | 6:4cece0df6a3b | 455 | recibe_ok(); |
tony63 | 6:4cece0df6a3b | 456 | } |
tony63 | 6:4cece0df6a3b | 457 | if(strncmp(" ",msg,16) == 0){ //COMPARA resp con "" que es Msg3 |
tony63 | 6:4cece0df6a3b | 458 | recibe_ok(); |
tony63 | 6:4cece0df6a3b | 459 | } |
tony63 | 6:4cece0df6a3b | 460 | if(strncmp(" ",msg,16) == 0){ //COMPARA resp con "" que es Msg123 |
tony63 | 6:4cece0df6a3b | 461 | recibe_ok(); |
tony63 | 6:4cece0df6a3b | 462 | } |
tony63 | 6:4cece0df6a3b | 463 | */ |
tony63 | 5:dbb0384120e0 | 464 | //strncmp(str1, str2, 6) compara hasta 6 caracteres |
tony63 | 6:4cece0df6a3b | 465 | //recibe_ok(); |
tony63 | 2:0377af333c98 | 466 | |
tony63 | 4:0b889f7a8eba | 467 | //ejecurar orden si esta es mas que prender leds |
tony63 | 7:08dac00ecff7 | 468 | readBuffer(buffer,200); |
tony63 | 7:08dac00ecff7 | 469 | cleanBuffer(buffer,110); |
tony63 | 2:0377af333c98 | 470 | |
tony63 | 2:0377af333c98 | 471 | } |
tony63 | 1:6b506dde0a6e | 472 | } |
tony63 | 2:0377af333c98 | 473 | |
tony63 | 2:0377af333c98 | 474 | |
tony63 | 2:0377af333c98 | 475 | } |
tony63 | 2:0377af333c98 | 476 | |
tony63 | 1:6b506dde0a6e | 477 | |
tony63 | 2:0377af333c98 | 478 | //********************************************************************************************************************** |
tony63 | 2:0377af333c98 | 479 | loop1: LedRojo=0; |
tony63 | 2:0377af333c98 | 480 | wait(0.3); |
tony63 | 2:0377af333c98 | 481 | LedRojo=1; |
tony63 | 2:0377af333c98 | 482 | wait(0.3); |
tony63 | 2:0377af333c98 | 483 | goto loop1; |
tony63 | 2:0377af333c98 | 484 | |
tony63 | 4:0b889f7a8eba | 485 | } |
tony63 | 4:0b889f7a8eba | 486 | |
tony63 | 4:0b889f7a8eba | 487 | /* |
tony63 | 4:0b889f7a8eba | 488 | si se manda para enviar mensaje |
tony63 | 4:0b889f7a8eba | 489 | AT+CMGS=20 |
tony63 | 4:0b889f7a8eba | 490 | //un salto de linea y el simbolo mayor |
tony63 | 4:0b889f7a8eba | 491 | > |
tony63 | 5:dbb0384120e0 | 492 | patrones de alarma y mensajes |
tony63 | 5:dbb0384120e0 | 493 | Camara on |
tony63 | 5:dbb0384120e0 | 494 | |
tony63 | 5:dbb0384120e0 | 495 | da esto |
tony63 | 5:dbb0384120e0 | 496 | +CMGL: 1,0,,26 |
tony63 | 5:dbb0384120e0 | 497 | 0791751330512411040AA1132237173700006150709060250A09C3703B2C0F83DE6E |
tony63 | 5:dbb0384120e0 | 498 | |
tony63 | 5:dbb0384120e0 | 499 | Camara of da esto |
tony63 | 5:dbb0384120e0 | 500 | +CMGL: 2,0,,26 |
tony63 | 5:dbb0384120e0 | 501 | 0791751330080089040AA1132237173700006150709001520A09C3703B2C0F83DE66 |
tony63 | 5:dbb0384120e0 | 502 | mensaje=C3703B2C0F83DE66 |
tony63 | 5:dbb0384120e0 | 503 | OK |
tony63 | 5:dbb0384120e0 | 504 | |
tony63 | 5:dbb0384120e0 | 505 | Sirena on |
tony63 | 5:dbb0384120e0 | 506 | da esto |
tony63 | 5:dbb0384120e0 | 507 | |
tony63 | 5:dbb0384120e0 | 508 | +CMGL: 3,0,,26 |
tony63 | 5:dbb0384120e0 | 509 | 0791751330512411040AA1132237173700006150709031540A09D3B4BCEC0E83DE6E |
tony63 | 5:dbb0384120e0 | 510 | mensaje=D3B4BCEC0E83DE6E |
tony63 | 5:dbb0384120e0 | 511 | OK |
tony63 | 5:dbb0384120e0 | 512 | |
tony63 | 5:dbb0384120e0 | 513 | Sirena of |
tony63 | 5:dbb0384120e0 | 514 | |
tony63 | 5:dbb0384120e0 | 515 | da esto |
tony63 | 5:dbb0384120e0 | 516 | +CMGL: 4,0,,26 |
tony63 | 5:dbb0384120e0 | 517 | 0791751330512411040AA1132237173700006150709051750A09D3B4BCEC0E83DE66 |
tony63 | 5:dbb0384120e0 | 518 | mensaje=D3B4BCEC0E83DE66 |
tony63 | 5:dbb0384120e0 | 519 | OK |
tony63 | 5:dbb0384120e0 | 520 | |
tony63 | 5:dbb0384120e0 | 521 | Alarma 1 |
tony63 | 5:dbb0384120e0 | 522 | |
tony63 | 5:dbb0384120e0 | 523 | +CMGL: 1,0,,25 |
tony63 | 5:dbb0384120e0 | 524 | 0791751330512411040AA1132237173700006150702153640A08417658DE0E8362 |
tony63 | 5:dbb0384120e0 | 525 | mensaje=417658DE0E8362 |
tony63 | 5:dbb0384120e0 | 526 | 5 |
tony63 | 4:0b889f7a8eba | 527 | |
tony63 | 6:4cece0df6a3b | 528 | Mensaje recibido ok! |
tony63 | 6:4cece0df6a3b | 529 | AT+CMGS=31 |
tony63 | 6:4cece0df6a3b | 530 | 0011000A9113223717370000AA14CDB27B1E569741F2F2382D4E93DFA0F73A04 |
tony63 | 6:4cece0df6a3b | 531 | |
tony63 | 6:4cece0df6a3b | 532 | AT+CMGS=32 |
tony63 | 6:4cece0df6a3b | 533 | 0011000A9113223717370000AA15CDB27B1E569741F2F2382D4E93DFA0E7321402 |
tony63 | 4:0b889f7a8eba | 534 | |
tony63 | 4:0b889f7a8eba | 535 | |
tony63 | 4:0b889f7a8eba | 536 | |
tony63 | 6:4cece0df6a3b | 537 | void recibe_ok(void){ |
tony63 | 6:4cece0df6a3b | 538 | GSM.printf("AT+CMGS=31\n\r"); |
tony63 | 6:4cece0df6a3b | 539 | wait_ms(200); |
tony63 | 6:4cece0df6a3b | 540 | GSM.printf("0011000A91"); |
tony63 | 6:4cece0df6a3b | 541 | GSM.printf("%str",tel); |
tony63 | 6:4cece0df6a3b | 542 | GSM.printf("0000AA14CDB27B1E569741F2F2382D4E93DFA0F73A04\n\r"); // Mensaje recibido ok! |
tony63 | 6:4cece0df6a3b | 543 | wait_ms(200); |
tony63 | 6:4cece0df6a3b | 544 | GSM.putc((char)0x1A); |
tony63 | 6:4cece0df6a3b | 545 | } |
tony63 | 6:4cece0df6a3b | 546 | |
tony63 | 4:0b889f7a8eba | 547 | */ |