gsm
Dependencies: DebouncedIn mbed
Fork of CLASEDELGSM1 by
main.cpp@5:dbb0384120e0, 2016-05-10 (annotated)
- Committer:
- tony63
- Date:
- Tue May 10 10:08:40 2016 +0000
- Revision:
- 5:dbb0384120e0
- Parent:
- 4:0b889f7a8eba
- Child:
- 6:4cece0df6a3b
correcci?n sin eco
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tony63 | 4:0b889f7a8eba | 1 | /*************************Alarma Contra Robo********************************************************* |
tony63 | 4:0b889f7a8eba | 2 | Como modem usa un celular SIEMENS a56i |
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 | 4:0b889f7a8eba | 6 | Cable verde es RX conectelo a PTE0 cable blanco es TX conectelo a PTE1 |
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 | 5:dbb0384120e0 | 45 | DigitalOut camara(PTE5 ); |
tony63 | 0:edc50cf04b46 | 46 | DigitalOut LedAzul(LED3); |
tony63 | 4:0b889f7a8eba | 47 | DebouncedIn PIR(PTA13); //señal que inicia el envio del mensaje |
tony63 | 0:edc50cf04b46 | 48 | DebouncedIn button1(PTC12); //señal que inicia el envio del mensaje |
tony63 | 0:edc50cf04b46 | 49 | Serial GSM(PTE0,PTE1); //Configura puerto UART de la FRDMKL25Z |
tony63 | 0:edc50cf04b46 | 50 | Serial pc(USBTX,USBRX);//Configura puerto USB a la consola serial del PC conectado. |
tony63 | 0:edc50cf04b46 | 51 | void Rx_interrupt(); |
tony63 | 0:edc50cf04b46 | 52 | int position=0; |
tony63 | 2:0377af333c98 | 53 | int intentos=0; |
tony63 | 0:edc50cf04b46 | 54 | int lenpack=6; |
tony63 | 2:0377af333c98 | 55 | int ret=1; |
tony63 | 0:edc50cf04b46 | 56 | int longi=0; |
tony63 | 1:6b506dde0a6e | 57 | char tel[11]; |
tony63 | 0:edc50cf04b46 | 58 | char DE[50]; |
tony63 | 5:dbb0384120e0 | 59 | char buffer[200]; |
tony63 | 5:dbb0384120e0 | 60 | //char buffermsg[200]; |
tony63 | 5:dbb0384120e0 | 61 | //char buffer1[200]; |
tony63 | 5:dbb0384120e0 | 62 | //char datos[200]; |
tony63 | 2:0377af333c98 | 63 | char NUMBER[13]; |
tony63 | 2:0377af333c98 | 64 | char resp[6]; |
tony63 | 2:0377af333c98 | 65 | char CMT[]="+CMTI"; |
tony63 | 2:0377af333c98 | 66 | char tam[2]; |
tony63 | 0:edc50cf04b46 | 67 | int index; |
tony63 | 0:edc50cf04b46 | 68 | int count; |
tony63 | 0:edc50cf04b46 | 69 | int i = 0; |
tony63 | 1:6b506dde0a6e | 70 | int j = 0; |
tony63 | 0:edc50cf04b46 | 71 | int c=0; |
tony63 | 0:edc50cf04b46 | 72 | unsigned char CtrlZ = 0x1A; // comodin de emision controlZ |
tony63 | 0:edc50cf04b46 | 73 | bool Flag = false; // bandera |
tony63 | 0:edc50cf04b46 | 74 | char r[]=""; //Cadena de recepcion de la trama PDU si se usa!! |
tony63 | 5:dbb0384120e0 | 75 | char msg[50]; |
tony63 | 0:edc50cf04b46 | 76 | char char1; |
tony63 | 0:edc50cf04b46 | 77 | //Flush serial para el buffer |
tony63 | 0:edc50cf04b46 | 78 | void FlushGSM(void) { |
tony63 | 0:edc50cf04b46 | 79 | char1 = 0; |
tony63 | 0:edc50cf04b46 | 80 | while (GSM.readable()){ |
tony63 | 0:edc50cf04b46 | 81 | char1 = GSM.getc();} |
tony63 | 0:edc50cf04b46 | 82 | return;} |
tony63 | 0:edc50cf04b46 | 83 | |
tony63 | 0:edc50cf04b46 | 84 | void callback() { |
tony63 | 0:edc50cf04b46 | 85 | // Note: you need to actually read from the serial to clear the RX interrupt |
tony63 | 0:edc50cf04b46 | 86 | pc.printf("%c\n", GSM.getc()); |
tony63 | 0:edc50cf04b46 | 87 | |
tony63 | 0:edc50cf04b46 | 88 | } |
tony63 | 2:0377af333c98 | 89 | //**************************************************************************************************************** |
tony63 | 2:0377af333c98 | 90 | //esta funcion de abajo lee todo un bufer hasta encontrar CR o LF y el resto lo rellena de |
tony63 | 2:0377af333c98 | 91 | //$, count es lo que va a leer.Lo leido lo mete en buffer que es una cadena previamente definida |
tony63 | 2:0377af333c98 | 92 | //incorpora medida de tiempo si se demora mas de tres segundos retorna fracaso con -1 |
tony63 | 2:0377af333c98 | 93 | //**************************************************************************************************************** |
tony63 | 0:edc50cf04b46 | 94 | int readBuffer(char *buffer,int count) |
tony63 | 0:edc50cf04b46 | 95 | { |
tony63 | 0:edc50cf04b46 | 96 | int i=0; |
tony63 | 0:edc50cf04b46 | 97 | t.start(); // start timer |
tony63 | 0:edc50cf04b46 | 98 | while(1) { |
tony63 | 0:edc50cf04b46 | 99 | while (GSM.readable()) { |
tony63 | 0:edc50cf04b46 | 100 | char c = GSM.getc(); |
tony63 | 0:edc50cf04b46 | 101 | if (c == '\r' || c == '\n') c = '$'; |
tony63 | 0:edc50cf04b46 | 102 | buffer[i++] = c; |
tony63 | 0:edc50cf04b46 | 103 | if(i > count)break; |
tony63 | 0:edc50cf04b46 | 104 | } |
tony63 | 0:edc50cf04b46 | 105 | if(i > count)break; |
tony63 | 0:edc50cf04b46 | 106 | if(t.read() > 3) { |
tony63 | 0:edc50cf04b46 | 107 | t.stop(); |
tony63 | 0:edc50cf04b46 | 108 | t.reset(); |
tony63 | 0:edc50cf04b46 | 109 | break; |
tony63 | 0:edc50cf04b46 | 110 | } |
tony63 | 0:edc50cf04b46 | 111 | } |
tony63 | 0:edc50cf04b46 | 112 | wait(0.5); |
tony63 | 0:edc50cf04b46 | 113 | while(GSM.readable()) { // display the other thing.. |
tony63 | 0:edc50cf04b46 | 114 | char c = GSM.getc(); |
tony63 | 0:edc50cf04b46 | 115 | } |
tony63 | 0:edc50cf04b46 | 116 | return 0; |
tony63 | 0:edc50cf04b46 | 117 | } |
tony63 | 2:0377af333c98 | 118 | //******************************************************************************** |
tony63 | 0:edc50cf04b46 | 119 | /* esta funcion de abajo limpia o borra todo un "buffer" de tamaño "count" |
tony63 | 2:0377af333c98 | 120 | lo revisa elemento por elemento y le mete el caracter null que indica fin de cadena |
tony63 | 0:edc50cf04b46 | 121 | no retorna nada |
tony63 | 0:edc50cf04b46 | 122 | */ |
tony63 | 0:edc50cf04b46 | 123 | //*************************************************************************************** |
tony63 | 0:edc50cf04b46 | 124 | void cleanBuffer(char *buffer, int count) |
tony63 | 0:edc50cf04b46 | 125 | { |
tony63 | 0:edc50cf04b46 | 126 | for(int i=0; i < count; i++) { |
tony63 | 0:edc50cf04b46 | 127 | buffer[i] = '\0'; |
tony63 | 0:edc50cf04b46 | 128 | } |
tony63 | 0:edc50cf04b46 | 129 | } |
tony63 | 0:edc50cf04b46 | 130 | /* esta funcion de abajo envia un comando parametrizado como cadena |
tony63 | 2:0377af333c98 | 131 | puede ser un comando tipo AT |
tony63 | 0:edc50cf04b46 | 132 | */ |
tony63 | 0:edc50cf04b46 | 133 | //*************************************************************************************** |
tony63 | 0:edc50cf04b46 | 134 | void sendCmd(char *cmd) |
tony63 | 0:edc50cf04b46 | 135 | { |
tony63 | 0:edc50cf04b46 | 136 | GSM.puts(cmd); |
tony63 | 0:edc50cf04b46 | 137 | } |
tony63 | 2:0377af333c98 | 138 | //**************************************************************************************** |
tony63 | 0:edc50cf04b46 | 139 | /* 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 | 140 | si todo sale bien retorna un cero que en la programacion hay que validar |
tony63 | 0:edc50cf04b46 | 141 | si algo sale mal ( no se parece o se demora mucho )retorna -1 que debera validarse con alguna expresion logica |
tony63 | 0:edc50cf04b46 | 142 | */ |
tony63 | 0:edc50cf04b46 | 143 | //*************************************************************************************** |
tony63 | 0:edc50cf04b46 | 144 | int waitForResp(char *resp, int timeout) |
tony63 | 0:edc50cf04b46 | 145 | { |
tony63 | 0:edc50cf04b46 | 146 | int len = strlen(resp); |
tony63 | 0:edc50cf04b46 | 147 | int sum=0; |
tony63 | 0:edc50cf04b46 | 148 | t.start(); |
tony63 | 0:edc50cf04b46 | 149 | |
tony63 | 0:edc50cf04b46 | 150 | while(1) { |
tony63 | 0:edc50cf04b46 | 151 | if(GSM.readable()) { |
tony63 | 0:edc50cf04b46 | 152 | char c = GSM.getc(); |
tony63 | 0:edc50cf04b46 | 153 | sum = (c==resp[sum]) ? sum+1 : 0;// esta linea de C# sum se incrementa o se hace cero segun c |
tony63 | 0:edc50cf04b46 | 154 | if(sum == len)break; //ya acabo se sale |
tony63 | 0:edc50cf04b46 | 155 | } |
tony63 | 0:edc50cf04b46 | 156 | if(t.read() > timeout) { // time out chequea el tiempo minimo antes de salir perdiendo |
tony63 | 0:edc50cf04b46 | 157 | t.stop(); |
tony63 | 0:edc50cf04b46 | 158 | t.reset(); |
tony63 | 0:edc50cf04b46 | 159 | return -1; |
tony63 | 0:edc50cf04b46 | 160 | } |
tony63 | 0:edc50cf04b46 | 161 | } |
tony63 | 0:edc50cf04b46 | 162 | t.stop(); // stop timer antes de retornar |
tony63 | 0:edc50cf04b46 | 163 | t.reset(); // clear timer |
tony63 | 0:edc50cf04b46 | 164 | while(GSM.readable()) { // display the other thing.. |
tony63 | 0:edc50cf04b46 | 165 | char c = GSM.getc(); |
tony63 | 0:edc50cf04b46 | 166 | } |
tony63 | 0:edc50cf04b46 | 167 | |
tony63 | 0:edc50cf04b46 | 168 | return 0; |
tony63 | 0:edc50cf04b46 | 169 | } |
tony63 | 0:edc50cf04b46 | 170 | /* esta funcion de abajo es muy completa e util se encarga de enviar el comando y esperar la respuesta |
tony63 | 0:edc50cf04b46 | 171 | si todo sale bien retorna un cero(herencia de las funciones contenedoras) que en la programacion hay que validar |
tony63 | 0:edc50cf04b46 | 172 | con alguna expresion logica |
tony63 | 0:edc50cf04b46 | 173 | */ |
tony63 | 0:edc50cf04b46 | 174 | //*************************************************************************************** |
tony63 | 0:edc50cf04b46 | 175 | int sendCmdAndWaitForResp(char *cmd, char *resp, int timeout) |
tony63 | 0:edc50cf04b46 | 176 | { |
tony63 | 0:edc50cf04b46 | 177 | sendCmd(cmd); |
tony63 | 0:edc50cf04b46 | 178 | return waitForResp(resp,timeout); |
tony63 | 0:edc50cf04b46 | 179 | } |
tony63 | 0:edc50cf04b46 | 180 | /* esta funcion de abajo chequea que el modem este vivo envia AT y le contesta con OK y espera 2 segundos |
tony63 | 0:edc50cf04b46 | 181 | */ |
tony63 | 0:edc50cf04b46 | 182 | //*************************************************************************************** |
tony63 | 0:edc50cf04b46 | 183 | int powerCheck(void)// este comando se manda para verificar si el modem esta vivo o conectado |
tony63 | 0:edc50cf04b46 | 184 | { |
tony63 | 0:edc50cf04b46 | 185 | return sendCmdAndWaitForResp("AT\r\n", "OK", 2); |
tony63 | 0:edc50cf04b46 | 186 | } |
tony63 | 0:edc50cf04b46 | 187 | /* esta funcion de abajo chequea el estado de la sim card |
tony63 | 0:edc50cf04b46 | 188 | y si todo sale bien retorna un cero que en la programacion hay que validar |
tony63 | 0:edc50cf04b46 | 189 | con alguna expresion logica |
tony63 | 0:edc50cf04b46 | 190 | */ |
tony63 | 0:edc50cf04b46 | 191 | //*************************************************************************************** |
tony63 | 0:edc50cf04b46 | 192 | int checkSIMStatus(void) |
tony63 | 0:edc50cf04b46 | 193 | { |
tony63 | 0:edc50cf04b46 | 194 | char gprsBuffer[30]; |
tony63 | 0:edc50cf04b46 | 195 | int count = 0; |
tony63 | 0:edc50cf04b46 | 196 | cleanBuffer(gprsBuffer,30); |
tony63 | 0:edc50cf04b46 | 197 | while(count < 3) { |
tony63 | 0:edc50cf04b46 | 198 | sendCmd("AT+CPIN?\r\n"); |
tony63 | 0:edc50cf04b46 | 199 | readBuffer(gprsBuffer,30); |
tony63 | 0:edc50cf04b46 | 200 | if((NULL != strstr(gprsBuffer,"+CPIN: READY"))) { |
tony63 | 0:edc50cf04b46 | 201 | break; |
tony63 | 0:edc50cf04b46 | 202 | } |
tony63 | 0:edc50cf04b46 | 203 | count++; |
tony63 | 0:edc50cf04b46 | 204 | wait(1); |
tony63 | 0:edc50cf04b46 | 205 | } |
tony63 | 0:edc50cf04b46 | 206 | |
tony63 | 0:edc50cf04b46 | 207 | if(count == 3) { |
tony63 | 0:edc50cf04b46 | 208 | return -1; |
tony63 | 0:edc50cf04b46 | 209 | } |
tony63 | 0:edc50cf04b46 | 210 | return 0; |
tony63 | 0:edc50cf04b46 | 211 | } |
tony63 | 0:edc50cf04b46 | 212 | /* esta funcion de abajo chequea la calidad de la señal |
tony63 | 2:0377af333c98 | 213 | 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 | 214 | con alguna expresion logica |
tony63 | 0:edc50cf04b46 | 215 | */ |
tony63 | 0:edc50cf04b46 | 216 | //*************************************************************************************** |
tony63 | 0:edc50cf04b46 | 217 | int checkSignalStrength(void) |
tony63 | 0:edc50cf04b46 | 218 | { |
tony63 | 0:edc50cf04b46 | 219 | char gprsBuffer[100]; |
tony63 | 0:edc50cf04b46 | 220 | int index,count = 0; |
tony63 | 0:edc50cf04b46 | 221 | cleanBuffer(gprsBuffer,100); |
tony63 | 0:edc50cf04b46 | 222 | while(count < 3) { |
tony63 | 0:edc50cf04b46 | 223 | sendCmd("AT+CSQ\r\n"); |
tony63 | 0:edc50cf04b46 | 224 | readBuffer(gprsBuffer,25); |
tony63 | 0:edc50cf04b46 | 225 | if(sscanf(gprsBuffer, "AT+CSQ$$$$+CSQ: %d", &index)>0) { |
tony63 | 0:edc50cf04b46 | 226 | break; |
tony63 | 0:edc50cf04b46 | 227 | } |
tony63 | 0:edc50cf04b46 | 228 | count++; |
tony63 | 0:edc50cf04b46 | 229 | wait(1); |
tony63 | 0:edc50cf04b46 | 230 | } |
tony63 | 0:edc50cf04b46 | 231 | if(count == 3) { |
tony63 | 0:edc50cf04b46 | 232 | return -1; |
tony63 | 0:edc50cf04b46 | 233 | } |
tony63 | 0:edc50cf04b46 | 234 | return index; |
tony63 | 0:edc50cf04b46 | 235 | } |
tony63 | 0:edc50cf04b46 | 236 | |
tony63 | 0:edc50cf04b46 | 237 | /* esta funcion de abajo inicaliza el modem se compone de un grupo de subfunciones ya definidas previamente |
tony63 | 0:edc50cf04b46 | 238 | primero chequea que este vivo |
tony63 | 0:edc50cf04b46 | 239 | segundo chequea el estado de la simcard |
tony63 | 0:edc50cf04b46 | 240 | tercero chequea la intencidad de señal celular |
tony63 | 0:edc50cf04b46 | 241 | cuarto aplica la configuracion |
tony63 | 0:edc50cf04b46 | 242 | y si todo sale bien retorna un cero que en la programacion hay que validar |
tony63 | 0:edc50cf04b46 | 243 | con alguna expresion logica |
tony63 | 0:edc50cf04b46 | 244 | */ |
tony63 | 0:edc50cf04b46 | 245 | //*************************************************************************************** |
tony63 | 0:edc50cf04b46 | 246 | int init() |
tony63 | 0:edc50cf04b46 | 247 | { |
tony63 | 2:0377af333c98 | 248 | if (0 != sendCmdAndWaitForResp("AT\r\n", "OK", 3)){ |
tony63 | 2:0377af333c98 | 249 | return -1; |
tony63 | 2:0377af333c98 | 250 | } |
tony63 | 2:0377af333c98 | 251 | if (0 != sendCmdAndWaitForResp("AT+CNMI=1,1\r\n", "OK", 3)){ |
tony63 | 2:0377af333c98 | 252 | return -1; |
tony63 | 2:0377af333c98 | 253 | } |
tony63 | 2:0377af333c98 | 254 | if (0 != sendCmdAndWaitForResp("AT+CMGF=0\r\n", "OK", 3)){ |
tony63 | 0:edc50cf04b46 | 255 | return -1; |
tony63 | 2:0377af333c98 | 256 | } |
tony63 | 2:0377af333c98 | 257 | if (0 != sendCmdAndWaitForResp("AT+CBST=0,0,1\r\n", "OK", 3)){ |
tony63 | 0:edc50cf04b46 | 258 | return -1; |
tony63 | 2:0377af333c98 | 259 | } |
tony63 | 5:dbb0384120e0 | 260 | if (0 != sendCmdAndWaitForResp("ATE\r\n", "OK", 3)){ |
tony63 | 5:dbb0384120e0 | 261 | return -1; |
tony63 | 5:dbb0384120e0 | 262 | } |
tony63 | 2:0377af333c98 | 263 | LedVerde=0; |
tony63 | 2:0377af333c98 | 264 | return 0; |
tony63 | 2:0377af333c98 | 265 | } |
tony63 | 2:0377af333c98 | 266 | |
tony63 | 0:edc50cf04b46 | 267 | /* esta funcion de abajo intenta leer un mensaje de texto en formato PDU o HEX |
tony63 | 0:edc50cf04b46 | 268 | y si todo sale bien retorna un cero que en la programacion hay que validar |
tony63 | 0:edc50cf04b46 | 269 | con alguna expresion logica |
tony63 | 0:edc50cf04b46 | 270 | */ |
tony63 | 0:edc50cf04b46 | 271 | //*************************************************************************************** |
tony63 | 0:edc50cf04b46 | 272 | int readSMSpdu(char *message, int index) |
tony63 | 0:edc50cf04b46 | 273 | { |
tony63 | 0:edc50cf04b46 | 274 | int i = 0; |
tony63 | 0:edc50cf04b46 | 275 | char gprsBuffer[100]; |
tony63 | 0:edc50cf04b46 | 276 | char *p,*s; |
tony63 | 0:edc50cf04b46 | 277 | GSM.printf("AT+CMGR=%d\r\n",index); |
tony63 | 0:edc50cf04b46 | 278 | cleanBuffer(gprsBuffer,100); |
tony63 | 0:edc50cf04b46 | 279 | readBuffer(gprsBuffer,100); |
tony63 | 0:edc50cf04b46 | 280 | if(NULL == ( s = strstr(gprsBuffer,"+CMGR"))) { |
tony63 | 0:edc50cf04b46 | 281 | return -1; |
tony63 | 0:edc50cf04b46 | 282 | } |
tony63 | 0:edc50cf04b46 | 283 | if(NULL != ( s = strstr(gprsBuffer,"+32"))) { |
tony63 | 0:edc50cf04b46 | 284 | p = s + 6; |
tony63 | 0:edc50cf04b46 | 285 | while((*p != '$')&&(i < 5)) { |
tony63 | 0:edc50cf04b46 | 286 | message[i++] = *(p++); |
tony63 | 0:edc50cf04b46 | 287 | } |
tony63 | 0:edc50cf04b46 | 288 | message[i] = '\0'; |
tony63 | 0:edc50cf04b46 | 289 | } |
tony63 | 0:edc50cf04b46 | 290 | return 0; |
tony63 | 0:edc50cf04b46 | 291 | } |
tony63 | 0:edc50cf04b46 | 292 | /* esta funcion de abajo borra mensajes SMS del modem |
tony63 | 0:edc50cf04b46 | 293 | y si todo sale bien retorna un cero que en la programacion hay que validar |
tony63 | 0:edc50cf04b46 | 294 | con alguna expresion logica |
tony63 | 0:edc50cf04b46 | 295 | */ |
tony63 | 0:edc50cf04b46 | 296 | //*************************************************************************************** |
tony63 | 0:edc50cf04b46 | 297 | int deleteSMS(int index) |
tony63 | 0:edc50cf04b46 | 298 | { |
tony63 | 0:edc50cf04b46 | 299 | char cmd[32]; |
tony63 | 0:edc50cf04b46 | 300 | snprintf(cmd,sizeof(cmd),"AT+CMGD=%d\r\n",index); |
tony63 | 0:edc50cf04b46 | 301 | sendCmd(cmd); |
tony63 | 0:edc50cf04b46 | 302 | return 0; |
tony63 | 0:edc50cf04b46 | 303 | } |
tony63 | 2:0377af333c98 | 304 | //************************************************************************************ |
tony63 | 4:0b889f7a8eba | 305 | //*****************SE INICIA EL PROGRAMA PRINCIPAL******************************************************* |
tony63 | 4:0b889f7a8eba | 306 | int main() { |
tony63 | 4:0b889f7a8eba | 307 | //configuramos los puertos seriales |
tony63 | 4:0b889f7a8eba | 308 | GSM.baud(9600);//configura los baudios de la FRDMKL25Z en 9600 |
tony63 | 4:0b889f7a8eba | 309 | GSM.format(8,Serial::None,1); //configura el formato de los datos de la UART |
tony63 | 4:0b889f7a8eba | 310 | LedVerde=1; //APAGO LOS LEDS |
tony63 | 4:0b889f7a8eba | 311 | LedRojo=1; |
tony63 | 4:0b889f7a8eba | 312 | LedAzul=1; |
tony63 | 4:0b889f7a8eba | 313 | LedRojo=0; // PRENDO EL LED ROJO |
tony63 | 4:0b889f7a8eba | 314 | //quito el eco del modem |
tony63 | 4:0b889f7a8eba | 315 | |
tony63 | 4:0b889f7a8eba | 316 | //****************CONFIGURAMOS EL MODEM GSM (TELEFONO CELULAR SIEMENS A56i) |
tony63 | 4:0b889f7a8eba | 317 | inicio1: |
tony63 | 4:0b889f7a8eba | 318 | ret = init(); |
tony63 | 4:0b889f7a8eba | 319 | if(ret==0){ |
tony63 | 4:0b889f7a8eba | 320 | LedRojo=1; |
tony63 | 4:0b889f7a8eba | 321 | LedVerde=0; |
tony63 | 4:0b889f7a8eba | 322 | } |
tony63 | 4:0b889f7a8eba | 323 | else{ |
tony63 | 4:0b889f7a8eba | 324 | wait(5); |
tony63 | 4:0b889f7a8eba | 325 | goto inicio1; |
tony63 | 4:0b889f7a8eba | 326 | } |
tony63 | 0:edc50cf04b46 | 327 | |
tony63 | 2:0377af333c98 | 328 | //RUTINA PRINCIPAL******************************************************************************************* |
tony63 | 4:0b889f7a8eba | 329 | |
tony63 | 2:0377af333c98 | 330 | //********************************************************************************************************************* |
tony63 | 2:0377af333c98 | 331 | while(1){ |
tony63 | 4:0b889f7a8eba | 332 | //***************ALARMA POR ACTIVACION PIR*************************************************** |
tony63 | 4:0b889f7a8eba | 333 | if (PIR.falling()) |
tony63 | 2:0377af333c98 | 334 | { |
tony63 | 4:0b889f7a8eba | 335 | wait(1); |
tony63 | 4:0b889f7a8eba | 336 | if (!PIR) |
tony63 | 2:0377af333c98 | 337 | { |
tony63 | 4:0b889f7a8eba | 338 | |
tony63 | 4:0b889f7a8eba | 339 | //**********se envia la palabra "Hay Intrusos En La Finca de Rionegro" |
tony63 | 4:0b889f7a8eba | 340 | //******Gustavo |
tony63 | 4:0b889f7a8eba | 341 | GSM.printf("AT+CMGS=50\n\r"); |
tony63 | 4:0b889f7a8eba | 342 | wait_ms(200); |
tony63 | 4:0b889f7a8eba | 343 | GSM.printf("0011000A9113223717370000AA2A21970B19CE83926EBABC3E7FCF41453788190699D3EE7118442E83A4E9B7BB7C96BF5DAE10"); |
tony63 | 4:0b889f7a8eba | 344 | wait_ms(200); |
tony63 | 4:0b889f7a8eba | 345 | GSM.putc((char)0x1A); |
tony63 | 5:dbb0384120e0 | 346 | for(i=0;i<60;i++){ //por mas ruido se envia un mensaje cada minuto |
tony63 | 2:0377af333c98 | 347 | LedVerde=1; |
tony63 | 5:dbb0384120e0 | 348 | wait(0.5); |
tony63 | 2:0377af333c98 | 349 | LedVerde=0; |
tony63 | 5:dbb0384120e0 | 350 | wait(0.5); |
tony63 | 4:0b889f7a8eba | 351 | } |
tony63 | 4:0b889f7a8eba | 352 | } |
tony63 | 4:0b889f7a8eba | 353 | } |
tony63 | 4:0b889f7a8eba | 354 | // **************apartir de aca se pueden escribir mas alarmas o mensajes de salida activar leds salidas o generar respuestas |
tony63 | 4:0b889f7a8eba | 355 | |
tony63 | 4:0b889f7a8eba | 356 | |
tony63 | 2:0377af333c98 | 357 | |
tony63 | 2:0377af333c98 | 358 | |
tony63 | 4:0b889f7a8eba | 359 | //***************inicia la recepcion de un mensaje de texto********************************** |
tony63 | 4:0b889f7a8eba | 360 | //la comparacion de estos mensajes constituye generar accionamiento abrir o cerrar circuitos o contestar mensajes |
tony63 | 4:0b889f7a8eba | 361 | |
tony63 | 2:0377af333c98 | 362 | if (GSM.readable()) { |
tony63 | 5:dbb0384120e0 | 363 | readBuffer(buffer,110); |
tony63 | 2:0377af333c98 | 364 | pc.printf("%s\r\n",buffer); |
tony63 | 2:0377af333c98 | 365 | for(i=0;i<5;i++) |
tony63 | 2:0377af333c98 | 366 | { |
tony63 | 2:0377af333c98 | 367 | resp[i]=buffer[i]; |
tony63 | 2:0377af333c98 | 368 | } |
tony63 | 2:0377af333c98 | 369 | |
tony63 | 2:0377af333c98 | 370 | pc.printf("%s\r\n",resp); |
tony63 | 2:0377af333c98 | 371 | if(strcmp("$$+CM",resp) == 0){ //COMPARA resp con "+CMTI" |
tony63 | 2:0377af333c98 | 372 | pc.printf("llego MSG\r\n"); |
tony63 | 2:0377af333c98 | 373 | cleanBuffer(buffer,10); |
tony63 | 2:0377af333c98 | 374 | wait(0.5); |
tony63 | 2:0377af333c98 | 375 | GSM.printf("AT+CMGL=0\r\n");//envio comando para leer mensaje |
tony63 | 2:0377af333c98 | 376 | pc.printf("AT+CMGL=0\r\n"); |
tony63 | 2:0377af333c98 | 377 | //if (GSM.readable()) { |
tony63 | 2:0377af333c98 | 378 | GSM.printf("AT+CMGD=0\r\n"); |
tony63 | 2:0377af333c98 | 379 | readBuffer(buffer,100); |
tony63 | 2:0377af333c98 | 380 | pc.printf("%s\r\n",buffer); |
tony63 | 2:0377af333c98 | 381 | wait(5); |
tony63 | 2:0377af333c98 | 382 | //leer telefono |
tony63 | 2:0377af333c98 | 383 | for(i=0;i<10;i++){ |
tony63 | 2:0377af333c98 | 384 | tel[i]=buffer[i+40]; |
tony63 | 2:0377af333c98 | 385 | } |
tony63 | 2:0377af333c98 | 386 | pc.printf("%s-\r\n",tel); |
tony63 | 2:0377af333c98 | 387 | //leer tamaño |
tony63 | 2:0377af333c98 | 388 | for(i=0;i<2;i++){ |
tony63 | 2:0377af333c98 | 389 | tam[i]=buffer[i+68]; |
tony63 | 2:0377af333c98 | 390 | } |
tony63 | 2:0377af333c98 | 391 | pc.printf("%s-\r\n",tam); |
tony63 | 2:0377af333c98 | 392 | //leer mensaje |
tony63 | 5:dbb0384120e0 | 393 | for(i=0;i<20;i++){ |
tony63 | 5:dbb0384120e0 | 394 | msg[i]=buffer[i+70]; //OJO SE LEE EL MENSAJE APARTIR DE LA POSICION 70 14 caracteres |
tony63 | 2:0377af333c98 | 395 | } |
tony63 | 2:0377af333c98 | 396 | pc.printf("%s-\r\n",msg); |
tony63 | 2:0377af333c98 | 397 | //decodificar mensaje |
tony63 | 2:0377af333c98 | 398 | //comparar mensaje |
tony63 | 4:0b889f7a8eba | 399 | deleteSMS(1);//se borra el bufer por medio de una funcion |
tony63 | 5:dbb0384120e0 | 400 | if(strncmp("417658DE0EC700",msg,14) == 0){ //COMPARA resp con "417658DE0EC700" que es Alarma1 |
tony63 | 2:0377af333c98 | 401 | LedVerde=1; |
tony63 | 2:0377af333c98 | 402 | LedAzul=0; |
tony63 | 2:0377af333c98 | 403 | wait(15); |
tony63 | 2:0377af333c98 | 404 | LedAzul=1; |
tony63 | 2:0377af333c98 | 405 | LedVerde=0; |
tony63 | 2:0377af333c98 | 406 | } |
tony63 | 5:dbb0384120e0 | 407 | if(strncmp("417658DE0ECB00",msg,14) == 0){ //COMPARA resp con "417658DE0ECB00" que es Alarma2 |
tony63 | 2:0377af333c98 | 408 | LedVerde=1; |
tony63 | 2:0377af333c98 | 409 | LedRojo=0; |
tony63 | 2:0377af333c98 | 410 | wait(15); |
tony63 | 2:0377af333c98 | 411 | LedRojo=1; |
tony63 | 2:0377af333c98 | 412 | LedVerde=0; |
tony63 | 2:0377af333c98 | 413 | } |
tony63 | 5:dbb0384120e0 | 414 | if(strncmp("C3703B2C0F83DE6E",msg,16) == 0){ //COMPARA resp con "417658DE0ECB00" que es Camara on |
tony63 | 5:dbb0384120e0 | 415 | camara=1; |
tony63 | 5:dbb0384120e0 | 416 | } |
tony63 | 5:dbb0384120e0 | 417 | if(strncmp("C3703B2C0F83DE66",msg,16) == 0){ //COMPARA resp con "417658DE0ECB00" que es Camara of |
tony63 | 5:dbb0384120e0 | 418 | camara=0; |
tony63 | 5:dbb0384120e0 | 419 | } |
tony63 | 5:dbb0384120e0 | 420 | if(strncmp("D3B4BCEC0E83DE6E",msg,16) == 0){ //COMPARA resp con "D3B4BCEC0E83DE6E" que es Sirena on |
tony63 | 5:dbb0384120e0 | 421 | camara=0; |
tony63 | 5:dbb0384120e0 | 422 | } |
tony63 | 5:dbb0384120e0 | 423 | //strncmp(str1, str2, 6) compara hasta 6 caracteres |
tony63 | 2:0377af333c98 | 424 | |
tony63 | 2:0377af333c98 | 425 | |
tony63 | 4:0b889f7a8eba | 426 | //ejecurar orden si esta es mas que prender leds |
tony63 | 2:0377af333c98 | 427 | cleanBuffer(buffer,100); |
tony63 | 2:0377af333c98 | 428 | |
tony63 | 2:0377af333c98 | 429 | } |
tony63 | 1:6b506dde0a6e | 430 | } |
tony63 | 2:0377af333c98 | 431 | |
tony63 | 2:0377af333c98 | 432 | |
tony63 | 2:0377af333c98 | 433 | } |
tony63 | 2:0377af333c98 | 434 | |
tony63 | 1:6b506dde0a6e | 435 | |
tony63 | 2:0377af333c98 | 436 | //********************************************************************************************************************** |
tony63 | 2:0377af333c98 | 437 | loop1: LedRojo=0; |
tony63 | 2:0377af333c98 | 438 | wait(0.3); |
tony63 | 2:0377af333c98 | 439 | LedRojo=1; |
tony63 | 2:0377af333c98 | 440 | wait(0.3); |
tony63 | 2:0377af333c98 | 441 | goto loop1; |
tony63 | 2:0377af333c98 | 442 | |
tony63 | 4:0b889f7a8eba | 443 | } |
tony63 | 4:0b889f7a8eba | 444 | |
tony63 | 4:0b889f7a8eba | 445 | /* |
tony63 | 4:0b889f7a8eba | 446 | si se manda para enviar mensaje |
tony63 | 4:0b889f7a8eba | 447 | AT+CMGS=20 |
tony63 | 4:0b889f7a8eba | 448 | //un salto de linea y el simbolo mayor |
tony63 | 4:0b889f7a8eba | 449 | > |
tony63 | 5:dbb0384120e0 | 450 | patrones de alarma y mensajes |
tony63 | 5:dbb0384120e0 | 451 | Camara on |
tony63 | 5:dbb0384120e0 | 452 | |
tony63 | 5:dbb0384120e0 | 453 | da esto |
tony63 | 5:dbb0384120e0 | 454 | +CMGL: 1,0,,26 |
tony63 | 5:dbb0384120e0 | 455 | 0791751330512411040AA1132237173700006150709060250A09C3703B2C0F83DE6E |
tony63 | 5:dbb0384120e0 | 456 | |
tony63 | 5:dbb0384120e0 | 457 | Camara of da esto |
tony63 | 5:dbb0384120e0 | 458 | +CMGL: 2,0,,26 |
tony63 | 5:dbb0384120e0 | 459 | 0791751330080089040AA1132237173700006150709001520A09C3703B2C0F83DE66 |
tony63 | 5:dbb0384120e0 | 460 | mensaje=C3703B2C0F83DE66 |
tony63 | 5:dbb0384120e0 | 461 | OK |
tony63 | 5:dbb0384120e0 | 462 | |
tony63 | 5:dbb0384120e0 | 463 | Sirena on |
tony63 | 5:dbb0384120e0 | 464 | da esto |
tony63 | 5:dbb0384120e0 | 465 | |
tony63 | 5:dbb0384120e0 | 466 | +CMGL: 3,0,,26 |
tony63 | 5:dbb0384120e0 | 467 | 0791751330512411040AA1132237173700006150709031540A09D3B4BCEC0E83DE6E |
tony63 | 5:dbb0384120e0 | 468 | mensaje=D3B4BCEC0E83DE6E |
tony63 | 5:dbb0384120e0 | 469 | OK |
tony63 | 5:dbb0384120e0 | 470 | |
tony63 | 5:dbb0384120e0 | 471 | Sirena of |
tony63 | 5:dbb0384120e0 | 472 | |
tony63 | 5:dbb0384120e0 | 473 | da esto |
tony63 | 5:dbb0384120e0 | 474 | +CMGL: 4,0,,26 |
tony63 | 5:dbb0384120e0 | 475 | 0791751330512411040AA1132237173700006150709051750A09D3B4BCEC0E83DE66 |
tony63 | 5:dbb0384120e0 | 476 | mensaje=D3B4BCEC0E83DE66 |
tony63 | 5:dbb0384120e0 | 477 | OK |
tony63 | 5:dbb0384120e0 | 478 | |
tony63 | 5:dbb0384120e0 | 479 | Alarma 1 |
tony63 | 5:dbb0384120e0 | 480 | |
tony63 | 5:dbb0384120e0 | 481 | +CMGL: 1,0,,25 |
tony63 | 5:dbb0384120e0 | 482 | 0791751330512411040AA1132237173700006150702153640A08417658DE0E8362 |
tony63 | 5:dbb0384120e0 | 483 | mensaje=417658DE0E8362 |
tony63 | 5:dbb0384120e0 | 484 | 5 |
tony63 | 4:0b889f7a8eba | 485 | |
tony63 | 4:0b889f7a8eba | 486 | |
tony63 | 4:0b889f7a8eba | 487 | |
tony63 | 4:0b889f7a8eba | 488 | |
tony63 | 4:0b889f7a8eba | 489 | */ |