![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
este programa recibe ordenes codificadas en mensajes de texto y envia mensajes cuando pulsan un boton use Alarma1 y Alarma2 de prueba...regresa por pulsador Alarma1. captura el numero de telefono de quien manda la orden
main.cpp@0:d1f7bad3a2cf, 2015-11-25 (annotated)
- Committer:
- Meli
- Date:
- Wed Nov 25 02:12:44 2015 +0000
- Revision:
- 0:d1f7bad3a2cf
este programa recibe ordenes codificadas en mensajes de texto y envia mensajes cuando pulsan un boton use Alarma1 y Alarma2 de prueba...regresa por pulsador Alarma1. captura el numero de telefono de quien manda la orden;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Meli | 0:d1f7bad3a2cf | 1 | // Programa para enviar desde la FRDMKL25Z un mensaje de texto en formatp PDU |
Meli | 0:d1f7bad3a2cf | 2 | // Como modem usa un celular SIEMENS a56i |
Meli | 0:d1f7bad3a2cf | 3 | // ENVIAR -----la palabra "ALARMA 1" |
Meli | 0:d1f7bad3a2cf | 4 | // |
Meli | 0:d1f7bad3a2cf | 5 | |
Meli | 0:d1f7bad3a2cf | 6 | // OJO CON ESTO |
Meli | 0:d1f7bad3a2cf | 7 | // conector del siemens cable verde es RX conectelo a PTE0 cable blanco es TX conectelo a PTE1 |
Meli | 0:d1f7bad3a2cf | 8 | #include "mbed.h" |
Meli | 0:d1f7bad3a2cf | 9 | #include "DebouncedIn.h" |
Meli | 0:d1f7bad3a2cf | 10 | #include "stdio.h" |
Meli | 0:d1f7bad3a2cf | 11 | #include "string.h" |
Meli | 0:d1f7bad3a2cf | 12 | Timer t; |
Meli | 0:d1f7bad3a2cf | 13 | DigitalOut LedVerde(LED2); |
Meli | 0:d1f7bad3a2cf | 14 | DigitalOut LedRojo(LED1); |
Meli | 0:d1f7bad3a2cf | 15 | DigitalOut LedAzul(LED3); |
Meli | 0:d1f7bad3a2cf | 16 | DebouncedIn button1(PTC12); //señal que inicia el envio del mensaje |
Meli | 0:d1f7bad3a2cf | 17 | Serial GSM(PTE0,PTE1); //Configura puerto UART de la FRDMKL25Z |
Meli | 0:d1f7bad3a2cf | 18 | Serial pc(USBTX,USBRX);//Configura puerto USB a la consola serial del PC conectado. |
Meli | 0:d1f7bad3a2cf | 19 | void Rx_interrupt(); |
Meli | 0:d1f7bad3a2cf | 20 | int position=0; |
Meli | 0:d1f7bad3a2cf | 21 | int intentos=0; |
Meli | 0:d1f7bad3a2cf | 22 | int lenpack=6; |
Meli | 0:d1f7bad3a2cf | 23 | int ret=1; |
Meli | 0:d1f7bad3a2cf | 24 | int longi=0; |
Meli | 0:d1f7bad3a2cf | 25 | char tel[11]; //Numero de celular |
Meli | 0:d1f7bad3a2cf | 26 | char DE[50]; |
Meli | 0:d1f7bad3a2cf | 27 | char buffer[100]; |
Meli | 0:d1f7bad3a2cf | 28 | char buffermsg[100]; |
Meli | 0:d1f7bad3a2cf | 29 | char buffer1[100]; |
Meli | 0:d1f7bad3a2cf | 30 | char datos[100]; |
Meli | 0:d1f7bad3a2cf | 31 | char NUMBER[13]; |
Meli | 0:d1f7bad3a2cf | 32 | char resp[6]; |
Meli | 0:d1f7bad3a2cf | 33 | char CMT[]="+CMTI"; |
Meli | 0:d1f7bad3a2cf | 34 | char tam[2]; |
Meli | 0:d1f7bad3a2cf | 35 | int index; |
Meli | 0:d1f7bad3a2cf | 36 | int count; |
Meli | 0:d1f7bad3a2cf | 37 | int i = 0; |
Meli | 0:d1f7bad3a2cf | 38 | int j = 0; |
Meli | 0:d1f7bad3a2cf | 39 | int c=0; |
Meli | 0:d1f7bad3a2cf | 40 | unsigned char CtrlZ = 0x1A; // comodin de emision controlZ |
Meli | 0:d1f7bad3a2cf | 41 | bool Flag = false; // bandera |
Meli | 0:d1f7bad3a2cf | 42 | char r[]=""; //Cadena de recepcion de la trama PDU si se usa!! |
Meli | 0:d1f7bad3a2cf | 43 | char msg[256]; |
Meli | 0:d1f7bad3a2cf | 44 | char char1; |
Meli | 0:d1f7bad3a2cf | 45 | //Flush serial para el buffer |
Meli | 0:d1f7bad3a2cf | 46 | void FlushGSM(void) { |
Meli | 0:d1f7bad3a2cf | 47 | char1 = 0; |
Meli | 0:d1f7bad3a2cf | 48 | while (GSM.readable()){ //mientras el celular este recibiendo datos |
Meli | 0:d1f7bad3a2cf | 49 | char1 = GSM.getc();} |
Meli | 0:d1f7bad3a2cf | 50 | return;} |
Meli | 0:d1f7bad3a2cf | 51 | |
Meli | 0:d1f7bad3a2cf | 52 | void callback() { |
Meli | 0:d1f7bad3a2cf | 53 | // Note: you need to actually read from the serial to clear the RX interrupt |
Meli | 0:d1f7bad3a2cf | 54 | pc.printf("%c\n", GSM.getc()); |
Meli | 0:d1f7bad3a2cf | 55 | |
Meli | 0:d1f7bad3a2cf | 56 | } |
Meli | 0:d1f7bad3a2cf | 57 | //**************************************************************************************************************** |
Meli | 0:d1f7bad3a2cf | 58 | //esta funcion de abajo lee todo un bufer hasta encontrar CR o LF y el resto lo rellena de |
Meli | 0:d1f7bad3a2cf | 59 | //$, count es lo que va a leer.Lo leido lo mete en buffer que es una cadena previamente definida |
Meli | 0:d1f7bad3a2cf | 60 | //incorpora medida de tiempo si se demora mas de tres segundos retorna fracaso con -1 |
Meli | 0:d1f7bad3a2cf | 61 | //**************************************************************************************************************** |
Meli | 0:d1f7bad3a2cf | 62 | int readBuffer(char *buffer,int count) |
Meli | 0:d1f7bad3a2cf | 63 | { |
Meli | 0:d1f7bad3a2cf | 64 | int i=0; |
Meli | 0:d1f7bad3a2cf | 65 | t.start(); // start timer |
Meli | 0:d1f7bad3a2cf | 66 | while(1) { |
Meli | 0:d1f7bad3a2cf | 67 | while (GSM.readable()) { |
Meli | 0:d1f7bad3a2cf | 68 | char c = GSM.getc(); //lo obtenido en GSM se guarda en la variable c como caracter |
Meli | 0:d1f7bad3a2cf | 69 | if (c == '\r' || c == '\n') c = '$'; //si c es igual a un espacio o a un enter, entonces que c pasa a ser $ |
Meli | 0:d1f7bad3a2cf | 70 | buffer[i++] = c; //incrementa la posicion del buffer (count) |
Meli | 0:d1f7bad3a2cf | 71 | if(i > count)break; //si el contador supera el tamaño del buffer, entonces para |
Meli | 0:d1f7bad3a2cf | 72 | } |
Meli | 0:d1f7bad3a2cf | 73 | if(i > count)break; |
Meli | 0:d1f7bad3a2cf | 74 | if(t.read() > 3) { //si el timer supera 3 segundos, retorma fracaso |
Meli | 0:d1f7bad3a2cf | 75 | t.stop(); |
Meli | 0:d1f7bad3a2cf | 76 | t.reset(); |
Meli | 0:d1f7bad3a2cf | 77 | break; |
Meli | 0:d1f7bad3a2cf | 78 | } |
Meli | 0:d1f7bad3a2cf | 79 | } |
Meli | 0:d1f7bad3a2cf | 80 | wait(0.5); |
Meli | 0:d1f7bad3a2cf | 81 | while(GSM.readable()) { // display the other thing.. |
Meli | 0:d1f7bad3a2cf | 82 | char c = GSM.getc(); |
Meli | 0:d1f7bad3a2cf | 83 | } |
Meli | 0:d1f7bad3a2cf | 84 | return 0; |
Meli | 0:d1f7bad3a2cf | 85 | } |
Meli | 0:d1f7bad3a2cf | 86 | //******************************************************************************** |
Meli | 0:d1f7bad3a2cf | 87 | /* esta funcion de abajo limpia o borra todo un "buffer" de tamaño "count" |
Meli | 0:d1f7bad3a2cf | 88 | lo revisa elemento por elemento y le mete el caracter null que indica fin de cadena |
Meli | 0:d1f7bad3a2cf | 89 | no retorna nada |
Meli | 0:d1f7bad3a2cf | 90 | */ |
Meli | 0:d1f7bad3a2cf | 91 | //*************************************************************************************** |
Meli | 0:d1f7bad3a2cf | 92 | void cleanBuffer(char *buffer, int count) |
Meli | 0:d1f7bad3a2cf | 93 | { |
Meli | 0:d1f7bad3a2cf | 94 | for(int i=0; i < count; i++) { |
Meli | 0:d1f7bad3a2cf | 95 | buffer[i] = '\0'; |
Meli | 0:d1f7bad3a2cf | 96 | } |
Meli | 0:d1f7bad3a2cf | 97 | } |
Meli | 0:d1f7bad3a2cf | 98 | /* esta funcion de abajo envia un comando parametrizado como cadena |
Meli | 0:d1f7bad3a2cf | 99 | puede ser un comando tipo AT |
Meli | 0:d1f7bad3a2cf | 100 | */ |
Meli | 0:d1f7bad3a2cf | 101 | //*************************************************************************************** |
Meli | 0:d1f7bad3a2cf | 102 | void sendCmd(char *cmd) |
Meli | 0:d1f7bad3a2cf | 103 | { |
Meli | 0:d1f7bad3a2cf | 104 | GSM.puts(cmd); |
Meli | 0:d1f7bad3a2cf | 105 | } |
Meli | 0:d1f7bad3a2cf | 106 | //**************************************************************************************** |
Meli | 0:d1f7bad3a2cf | 107 | /* esta funcion de abajo espera la respuesta de un comando que debe ser identica a la cadena "resp" y un tiempo timeout" |
Meli | 0:d1f7bad3a2cf | 108 | si todo sale bien retorna un cero que en la programacion hay que validar |
Meli | 0:d1f7bad3a2cf | 109 | si algo sale mal ( no se parece o se demora mucho )retorna -1 que debera validarse con alguna expresion logica |
Meli | 0:d1f7bad3a2cf | 110 | */ |
Meli | 0:d1f7bad3a2cf | 111 | //*************************************************************************************** |
Meli | 0:d1f7bad3a2cf | 112 | int waitForResp(char *resp, int timeout) |
Meli | 0:d1f7bad3a2cf | 113 | { |
Meli | 0:d1f7bad3a2cf | 114 | int len = strlen(resp); |
Meli | 0:d1f7bad3a2cf | 115 | int sum=0; |
Meli | 0:d1f7bad3a2cf | 116 | t.start(); // |
Meli | 0:d1f7bad3a2cf | 117 | |
Meli | 0:d1f7bad3a2cf | 118 | while(1) { |
Meli | 0:d1f7bad3a2cf | 119 | if(GSM.readable()) { |
Meli | 0:d1f7bad3a2cf | 120 | char c = GSM.getc(); //lo obtenido en GSM se guarda en la variable c como caracter |
Meli | 0:d1f7bad3a2cf | 121 | sum = (c==resp[sum]) ? sum+1 : 0;// esta linea de C# sum se incrementa o se hace cero segun c |
Meli | 0:d1f7bad3a2cf | 122 | if(sum == len)break; //ya acabo se sale |
Meli | 0:d1f7bad3a2cf | 123 | } |
Meli | 0:d1f7bad3a2cf | 124 | if(t.read() > timeout) { // time out chequea el tiempo minimo antes de salir perdiendo |
Meli | 0:d1f7bad3a2cf | 125 | t.stop(); |
Meli | 0:d1f7bad3a2cf | 126 | t.reset(); |
Meli | 0:d1f7bad3a2cf | 127 | return -1; |
Meli | 0:d1f7bad3a2cf | 128 | } |
Meli | 0:d1f7bad3a2cf | 129 | } |
Meli | 0:d1f7bad3a2cf | 130 | t.stop(); // stop timer antes de retornar |
Meli | 0:d1f7bad3a2cf | 131 | t.reset(); // clear timer |
Meli | 0:d1f7bad3a2cf | 132 | while(GSM.readable()) { // display the other thing.. |
Meli | 0:d1f7bad3a2cf | 133 | char c = GSM.getc(); |
Meli | 0:d1f7bad3a2cf | 134 | } |
Meli | 0:d1f7bad3a2cf | 135 | |
Meli | 0:d1f7bad3a2cf | 136 | return 0; |
Meli | 0:d1f7bad3a2cf | 137 | } |
Meli | 0:d1f7bad3a2cf | 138 | /* esta funcion de abajo es muy completa e util se encarga de enviar el comando y esperar la respuesta |
Meli | 0:d1f7bad3a2cf | 139 | si todo sale bien retorna un cero(herencia de las funciones contenedoras) que en la programacion hay que validar |
Meli | 0:d1f7bad3a2cf | 140 | con alguna expresion logica |
Meli | 0:d1f7bad3a2cf | 141 | */ |
Meli | 0:d1f7bad3a2cf | 142 | //*************************************************************************************** |
Meli | 0:d1f7bad3a2cf | 143 | int sendCmdAndWaitForResp(char *cmd, char *resp, int timeout) |
Meli | 0:d1f7bad3a2cf | 144 | { |
Meli | 0:d1f7bad3a2cf | 145 | sendCmd(cmd); |
Meli | 0:d1f7bad3a2cf | 146 | return waitForResp(resp,timeout); |
Meli | 0:d1f7bad3a2cf | 147 | } |
Meli | 0:d1f7bad3a2cf | 148 | /* esta funcion de abajo chequea que el modem este vivo envia AT y le contesta con OK y espera 2 segundos |
Meli | 0:d1f7bad3a2cf | 149 | */ |
Meli | 0:d1f7bad3a2cf | 150 | //*************************************************************************************** |
Meli | 0:d1f7bad3a2cf | 151 | int powerCheck(void)// este comando se manda para verificar si el modem esta vivo o conectado |
Meli | 0:d1f7bad3a2cf | 152 | { |
Meli | 0:d1f7bad3a2cf | 153 | return sendCmdAndWaitForResp("AT\r\n", "OK", 2); |
Meli | 0:d1f7bad3a2cf | 154 | } |
Meli | 0:d1f7bad3a2cf | 155 | /* esta funcion de abajo chequea el estado de la sim card |
Meli | 0:d1f7bad3a2cf | 156 | y si todo sale bien retorna un cero que en la programacion hay que validar |
Meli | 0:d1f7bad3a2cf | 157 | con alguna expresion logica |
Meli | 0:d1f7bad3a2cf | 158 | */ |
Meli | 0:d1f7bad3a2cf | 159 | //*************************************************************************************** |
Meli | 0:d1f7bad3a2cf | 160 | int checkSIMStatus(void) |
Meli | 0:d1f7bad3a2cf | 161 | { |
Meli | 0:d1f7bad3a2cf | 162 | char gprsBuffer[30]; |
Meli | 0:d1f7bad3a2cf | 163 | int count = 0; |
Meli | 0:d1f7bad3a2cf | 164 | cleanBuffer(gprsBuffer,30); |
Meli | 0:d1f7bad3a2cf | 165 | while(count < 3) { |
Meli | 0:d1f7bad3a2cf | 166 | sendCmd("AT+CPIN?\r\n"); |
Meli | 0:d1f7bad3a2cf | 167 | readBuffer(gprsBuffer,30); |
Meli | 0:d1f7bad3a2cf | 168 | if((NULL != strstr(gprsBuffer,"+CPIN: READY"))) { |
Meli | 0:d1f7bad3a2cf | 169 | break; |
Meli | 0:d1f7bad3a2cf | 170 | } |
Meli | 0:d1f7bad3a2cf | 171 | count++; |
Meli | 0:d1f7bad3a2cf | 172 | wait(1); |
Meli | 0:d1f7bad3a2cf | 173 | } |
Meli | 0:d1f7bad3a2cf | 174 | |
Meli | 0:d1f7bad3a2cf | 175 | if(count == 3) { |
Meli | 0:d1f7bad3a2cf | 176 | return -1; |
Meli | 0:d1f7bad3a2cf | 177 | } |
Meli | 0:d1f7bad3a2cf | 178 | return 0; |
Meli | 0:d1f7bad3a2cf | 179 | } |
Meli | 0:d1f7bad3a2cf | 180 | /* esta funcion de abajo chequea la calidad de la señal |
Meli | 0:d1f7bad3a2cf | 181 | 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 |
Meli | 0:d1f7bad3a2cf | 182 | con alguna expresion logica |
Meli | 0:d1f7bad3a2cf | 183 | */ |
Meli | 0:d1f7bad3a2cf | 184 | //*************************************************************************************** |
Meli | 0:d1f7bad3a2cf | 185 | int checkSignalStrength(void) |
Meli | 0:d1f7bad3a2cf | 186 | { |
Meli | 0:d1f7bad3a2cf | 187 | char gprsBuffer[100]; |
Meli | 0:d1f7bad3a2cf | 188 | int index,count = 0; |
Meli | 0:d1f7bad3a2cf | 189 | cleanBuffer(gprsBuffer,100); |
Meli | 0:d1f7bad3a2cf | 190 | while(count < 3) { |
Meli | 0:d1f7bad3a2cf | 191 | sendCmd("AT+CSQ\r\n"); |
Meli | 0:d1f7bad3a2cf | 192 | readBuffer(gprsBuffer,25); |
Meli | 0:d1f7bad3a2cf | 193 | if(sscanf(gprsBuffer, "AT+CSQ$$$$+CSQ: %d", &index)>0) { |
Meli | 0:d1f7bad3a2cf | 194 | break; |
Meli | 0:d1f7bad3a2cf | 195 | } |
Meli | 0:d1f7bad3a2cf | 196 | count++; |
Meli | 0:d1f7bad3a2cf | 197 | wait(1); |
Meli | 0:d1f7bad3a2cf | 198 | } |
Meli | 0:d1f7bad3a2cf | 199 | if(count == 3) { |
Meli | 0:d1f7bad3a2cf | 200 | return -1; |
Meli | 0:d1f7bad3a2cf | 201 | } |
Meli | 0:d1f7bad3a2cf | 202 | return index; |
Meli | 0:d1f7bad3a2cf | 203 | } |
Meli | 0:d1f7bad3a2cf | 204 | |
Meli | 0:d1f7bad3a2cf | 205 | /* esta funcion de abajo inicaliza el modem se compone de un grupo de subfunciones ya definidas previamente |
Meli | 0:d1f7bad3a2cf | 206 | primero chequea que este vivo |
Meli | 0:d1f7bad3a2cf | 207 | segundo chequea el estado de la simcard |
Meli | 0:d1f7bad3a2cf | 208 | tercero chequea la intencidad de señal celular |
Meli | 0:d1f7bad3a2cf | 209 | cuarto aplica la configuracion |
Meli | 0:d1f7bad3a2cf | 210 | y si todo sale bien retorna un cero que en la programacion hay que validar |
Meli | 0:d1f7bad3a2cf | 211 | con alguna expresion logica |
Meli | 0:d1f7bad3a2cf | 212 | */ |
Meli | 0:d1f7bad3a2cf | 213 | //*************************************************************************************** |
Meli | 0:d1f7bad3a2cf | 214 | int init() |
Meli | 0:d1f7bad3a2cf | 215 | { |
Meli | 0:d1f7bad3a2cf | 216 | if (0 != sendCmdAndWaitForResp("AT\r\n", "OK", 3)){ //chequea que este vivo, es decir, que responda con 0 |
Meli | 0:d1f7bad3a2cf | 217 | return -1; // de lo contrario, de vuelve fracaso |
Meli | 0:d1f7bad3a2cf | 218 | } |
Meli | 0:d1f7bad3a2cf | 219 | if (0 != sendCmdAndWaitForResp("AT+CNMI=1,1\r\n", "OK", 3)){ //chequea el estado de la simcard, es decir, que responda con 0 |
Meli | 0:d1f7bad3a2cf | 220 | return -1; // de lo contrario, de vuelve fracaso |
Meli | 0:d1f7bad3a2cf | 221 | } |
Meli | 0:d1f7bad3a2cf | 222 | if (0 != sendCmdAndWaitForResp("AT+CMGF=0\r\n", "OK", 3)){ //chequea la intensidad de señal, es decir, que responda con 0 |
Meli | 0:d1f7bad3a2cf | 223 | return -1; // de lo contrario, de vuelve fracaso |
Meli | 0:d1f7bad3a2cf | 224 | } |
Meli | 0:d1f7bad3a2cf | 225 | if (0 != sendCmdAndWaitForResp("AT+CBST=0,0,1\r\n", "OK", 3)){ //aplica la configuracion |
Meli | 0:d1f7bad3a2cf | 226 | return -1; // de lo contrario, de vuelve fracaso |
Meli | 0:d1f7bad3a2cf | 227 | } |
Meli | 0:d1f7bad3a2cf | 228 | LedVerde=0; //led verde encendido |
Meli | 0:d1f7bad3a2cf | 229 | return 0; |
Meli | 0:d1f7bad3a2cf | 230 | } |
Meli | 0:d1f7bad3a2cf | 231 | |
Meli | 0:d1f7bad3a2cf | 232 | /* esta funcion de abajo intenta leer un mensaje de texto en formato PDU o HEX |
Meli | 0:d1f7bad3a2cf | 233 | y si todo sale bien retorna un cero que en la programacion hay que validar |
Meli | 0:d1f7bad3a2cf | 234 | con alguna expresion logica |
Meli | 0:d1f7bad3a2cf | 235 | */ |
Meli | 0:d1f7bad3a2cf | 236 | //*************************************************************************************** |
Meli | 0:d1f7bad3a2cf | 237 | int readSMSpdu(char *message, int index) |
Meli | 0:d1f7bad3a2cf | 238 | { |
Meli | 0:d1f7bad3a2cf | 239 | int i = 0; |
Meli | 0:d1f7bad3a2cf | 240 | char gprsBuffer[100]; |
Meli | 0:d1f7bad3a2cf | 241 | char *p,*s; |
Meli | 0:d1f7bad3a2cf | 242 | GSM.printf("AT+CMGR=%d\r\n",index); |
Meli | 0:d1f7bad3a2cf | 243 | cleanBuffer(gprsBuffer,100); |
Meli | 0:d1f7bad3a2cf | 244 | readBuffer(gprsBuffer,100); |
Meli | 0:d1f7bad3a2cf | 245 | if(NULL == ( s = strstr(gprsBuffer,"+CMGR"))) { |
Meli | 0:d1f7bad3a2cf | 246 | return -1; |
Meli | 0:d1f7bad3a2cf | 247 | } |
Meli | 0:d1f7bad3a2cf | 248 | if(NULL != ( s = strstr(gprsBuffer,"+32"))) { |
Meli | 0:d1f7bad3a2cf | 249 | p = s + 6; |
Meli | 0:d1f7bad3a2cf | 250 | while((*p != '$')&&(i < 5)) { |
Meli | 0:d1f7bad3a2cf | 251 | message[i++] = *(p++); |
Meli | 0:d1f7bad3a2cf | 252 | } |
Meli | 0:d1f7bad3a2cf | 253 | message[i] = '\0'; |
Meli | 0:d1f7bad3a2cf | 254 | } |
Meli | 0:d1f7bad3a2cf | 255 | return 0; |
Meli | 0:d1f7bad3a2cf | 256 | } |
Meli | 0:d1f7bad3a2cf | 257 | /* esta funcion de abajo borra mensajes SMS del modem |
Meli | 0:d1f7bad3a2cf | 258 | y si todo sale bien retorna un cero que en la programacion hay que validar |
Meli | 0:d1f7bad3a2cf | 259 | con alguna expresion logica |
Meli | 0:d1f7bad3a2cf | 260 | */ |
Meli | 0:d1f7bad3a2cf | 261 | //*************************************************************************************** |
Meli | 0:d1f7bad3a2cf | 262 | int deleteSMS(int index) |
Meli | 0:d1f7bad3a2cf | 263 | { |
Meli | 0:d1f7bad3a2cf | 264 | char cmd[32]; |
Meli | 0:d1f7bad3a2cf | 265 | snprintf(cmd,sizeof(cmd),"AT+CMGD=%d\r\n",index); |
Meli | 0:d1f7bad3a2cf | 266 | sendCmd(cmd); |
Meli | 0:d1f7bad3a2cf | 267 | return 0; |
Meli | 0:d1f7bad3a2cf | 268 | } |
Meli | 0:d1f7bad3a2cf | 269 | //************************************************************************************ |
Meli | 0:d1f7bad3a2cf | 270 | |
Meli | 0:d1f7bad3a2cf | 271 | //RUTINA PRINCIPAL******************************************************************************************* |
Meli | 0:d1f7bad3a2cf | 272 | int main(void) |
Meli | 0:d1f7bad3a2cf | 273 | { |
Meli | 0:d1f7bad3a2cf | 274 | //configuramos los puertos seriales |
Meli | 0:d1f7bad3a2cf | 275 | GSM.baud(9600);//configura los baudios de la FRDMKL25Z en 9600 |
Meli | 0:d1f7bad3a2cf | 276 | GSM.format(8,Serial::None,1); //configura el formato de los datos de la UART |
Meli | 0:d1f7bad3a2cf | 277 | //apagamos los 3 leds |
Meli | 0:d1f7bad3a2cf | 278 | LedVerde=1; |
Meli | 0:d1f7bad3a2cf | 279 | LedRojo=1; |
Meli | 0:d1f7bad3a2cf | 280 | LedAzul=1; |
Meli | 0:d1f7bad3a2cf | 281 | //quito el eco del modem |
Meli | 0:d1f7bad3a2cf | 282 | GSM.printf("ATE0\r\n"); |
Meli | 0:d1f7bad3a2cf | 283 | pc.printf("ATE0\r\n"); |
Meli | 0:d1f7bad3a2cf | 284 | for(i=0;i<6;i++){ |
Meli | 0:d1f7bad3a2cf | 285 | GSM.printf("AT+CMGD=%d\r\n",i); |
Meli | 0:d1f7bad3a2cf | 286 | wait(0.2); |
Meli | 0:d1f7bad3a2cf | 287 | } |
Meli | 0:d1f7bad3a2cf | 288 | //cleanBuffer(buffer,10); |
Meli | 0:d1f7bad3a2cf | 289 | //definicion de algunas variables |
Meli | 0:d1f7bad3a2cf | 290 | lenpack=6; //tamaño de "ALARMA" |
Meli | 0:d1f7bad3a2cf | 291 | //Configuro el Modem, le doy solo 10 intentos si esos fracasan se bloquea y prende intermitente el led rojo |
Meli | 0:d1f7bad3a2cf | 292 | inicio:if(init()<0){ |
Meli | 0:d1f7bad3a2cf | 293 | intentos++; |
Meli | 0:d1f7bad3a2cf | 294 | if (intentos==10){goto loop1;} |
Meli | 0:d1f7bad3a2cf | 295 | goto inicio; |
Meli | 0:d1f7bad3a2cf | 296 | } |
Meli | 0:d1f7bad3a2cf | 297 | //cleanBuffer(buffer,50); |
Meli | 0:d1f7bad3a2cf | 298 | //inicia el programa ciclico |
Meli | 0:d1f7bad3a2cf | 299 | //esperar señales de alarma por boton1 |
Meli | 0:d1f7bad3a2cf | 300 | //se envia "Alarma1" |
Meli | 0:d1f7bad3a2cf | 301 | //al telefono que envio mensaje antes |
Meli | 0:d1f7bad3a2cf | 302 | |
Meli | 0:d1f7bad3a2cf | 303 | //********************************************************************************************************************* |
Meli | 0:d1f7bad3a2cf | 304 | while(1){ |
Meli | 0:d1f7bad3a2cf | 305 | if (button1.falling()) // cuando se oprima el boton de PTC12 |
Meli | 0:d1f7bad3a2cf | 306 | { |
Meli | 0:d1f7bad3a2cf | 307 | wait(2); |
Meli | 0:d1f7bad3a2cf | 308 | if (!button1) |
Meli | 0:d1f7bad3a2cf | 309 | { |
Meli | 0:d1f7bad3a2cf | 310 | //inicia el envio de un sms |
Meli | 0:d1f7bad3a2cf | 311 | index=19; |
Meli | 0:d1f7bad3a2cf | 312 | GSM.printf("AT+CMGS=%d\r\n",index); //envia el index (mensaje a transmitir) |
Meli | 0:d1f7bad3a2cf | 313 | wait(0.2); |
Meli | 0:d1f7bad3a2cf | 314 | GSM.printf("0011000A91"); //imprime el relleno |
Meli | 0:d1f7bad3a2cf | 315 | GSM.printf("%s",tel); //identifica el numero del remitente (invertido por pares) |
Meli | 0:d1f7bad3a2cf | 316 | GSM.printf("0000AA07417658DE0EC7"); //segundo relleno+la cantidad de septetos+1+el mensaje(gallina cafe) |
Meli | 0:d1f7bad3a2cf | 317 | GSM.printf("\r\n"); //espacio y enter |
Meli | 0:d1f7bad3a2cf | 318 | wait(0.2); |
Meli | 0:d1f7bad3a2cf | 319 | GSM.putc(0x1A); //el mensaje ya fue enviado con esto |
Meli | 0:d1f7bad3a2cf | 320 | for(i=0;i<6;i++){ |
Meli | 0:d1f7bad3a2cf | 321 | LedVerde=1; |
Meli | 0:d1f7bad3a2cf | 322 | wait(0.5); |
Meli | 0:d1f7bad3a2cf | 323 | LedVerde=0; |
Meli | 0:d1f7bad3a2cf | 324 | wait(0.5); |
Meli | 0:d1f7bad3a2cf | 325 | } |
Meli | 0:d1f7bad3a2cf | 326 | } |
Meli | 0:d1f7bad3a2cf | 327 | } |
Meli | 0:d1f7bad3a2cf | 328 | |
Meli | 0:d1f7bad3a2cf | 329 | |
Meli | 0:d1f7bad3a2cf | 330 | |
Meli | 0:d1f7bad3a2cf | 331 | //inicia la recepcion de un mensaje de texto |
Meli | 0:d1f7bad3a2cf | 332 | if (GSM.readable()) { |
Meli | 0:d1f7bad3a2cf | 333 | readBuffer(buffer,100); |
Meli | 0:d1f7bad3a2cf | 334 | pc.printf("%s\r\n",buffer); |
Meli | 0:d1f7bad3a2cf | 335 | for(i=0;i<5;i++) |
Meli | 0:d1f7bad3a2cf | 336 | { |
Meli | 0:d1f7bad3a2cf | 337 | resp[i]=buffer[i]; |
Meli | 0:d1f7bad3a2cf | 338 | } |
Meli | 0:d1f7bad3a2cf | 339 | |
Meli | 0:d1f7bad3a2cf | 340 | |
Meli | 0:d1f7bad3a2cf | 341 | pc.printf("%s\r\n",resp); |
Meli | 0:d1f7bad3a2cf | 342 | if(strcmp("$$+CM",resp) == 0){ //COMPARA resp con "+CMTI" |
Meli | 0:d1f7bad3a2cf | 343 | pc.printf("llego MSG\r\n"); |
Meli | 0:d1f7bad3a2cf | 344 | cleanBuffer(buffer,10); //limpia el buffer |
Meli | 0:d1f7bad3a2cf | 345 | wait(0.5); |
Meli | 0:d1f7bad3a2cf | 346 | GSM.printf("AT+CMGL=0\r\n");//envio comando para leer mensaje |
Meli | 0:d1f7bad3a2cf | 347 | pc.printf("AT+CMGL=0\r\n");//imprime que abrio los mensajes entrantes |
Meli | 0:d1f7bad3a2cf | 348 | //if (GSM.readable()) { |
Meli | 0:d1f7bad3a2cf | 349 | GSM.printf("AT+CMGD=0\r\n"); |
Meli | 0:d1f7bad3a2cf | 350 | readBuffer(buffer,100); |
Meli | 0:d1f7bad3a2cf | 351 | pc.printf("%s\r\n",buffer); |
Meli | 0:d1f7bad3a2cf | 352 | wait(5); |
Meli | 0:d1f7bad3a2cf | 353 | //leer telefono |
Meli | 0:d1f7bad3a2cf | 354 | for(i=0;i<10;i++){ |
Meli | 0:d1f7bad3a2cf | 355 | tel[i]=buffer[i+40]; //toma el numero de celular apartir de la cadena |
Meli | 0:d1f7bad3a2cf | 356 | } |
Meli | 0:d1f7bad3a2cf | 357 | pc.printf("%s-\r\n",tel); //imprime el numero de celular |
Meli | 0:d1f7bad3a2cf | 358 | //leer tamaño |
Meli | 0:d1f7bad3a2cf | 359 | for(i=0;i<2;i++){ |
Meli | 0:d1f7bad3a2cf | 360 | tam[i]=buffer[i+68]; |
Meli | 0:d1f7bad3a2cf | 361 | } |
Meli | 0:d1f7bad3a2cf | 362 | pc.printf("%s-\r\n",tam); |
Meli | 0:d1f7bad3a2cf | 363 | //leer mensaje |
Meli | 0:d1f7bad3a2cf | 364 | for(i=0;i<14;i++){ |
Meli | 0:d1f7bad3a2cf | 365 | msg[i]=buffer[i+70]; |
Meli | 0:d1f7bad3a2cf | 366 | } |
Meli | 0:d1f7bad3a2cf | 367 | pc.printf("%s-\r\n",msg); |
Meli | 0:d1f7bad3a2cf | 368 | //decodificar mensaje |
Meli | 0:d1f7bad3a2cf | 369 | //comparar mensaje |
Meli | 0:d1f7bad3a2cf | 370 | if(strcmp("417658DE0EC700",msg) == 0){ //COMPARA resp con "417658DE0EC700" que es Alarma1 |
Meli | 0:d1f7bad3a2cf | 371 | //Como necesitamos un mensaje en especifico, comparamos si el mensaje que llego es igual a Alarma1 (Compara cadena en formato string) |
Meli | 0:d1f7bad3a2cf | 372 | LedVerde=1; |
Meli | 0:d1f7bad3a2cf | 373 | LedAzul=0; |
Meli | 0:d1f7bad3a2cf | 374 | wait(15); |
Meli | 0:d1f7bad3a2cf | 375 | LedAzul=1; |
Meli | 0:d1f7bad3a2cf | 376 | LedVerde=0; |
Meli | 0:d1f7bad3a2cf | 377 | } |
Meli | 0:d1f7bad3a2cf | 378 | if(strcmp("417658DE0ECB00",msg) == 0){ //COMPARA resp con "417658DE0ECB00" que es Alarma2 |
Meli | 0:d1f7bad3a2cf | 379 | LedVerde=1; |
Meli | 0:d1f7bad3a2cf | 380 | LedRojo=0; |
Meli | 0:d1f7bad3a2cf | 381 | wait(15); |
Meli | 0:d1f7bad3a2cf | 382 | LedRojo=1; |
Meli | 0:d1f7bad3a2cf | 383 | LedVerde=0; |
Meli | 0:d1f7bad3a2cf | 384 | } |
Meli | 0:d1f7bad3a2cf | 385 | |
Meli | 0:d1f7bad3a2cf | 386 | |
Meli | 0:d1f7bad3a2cf | 387 | //ejecurar orden //ya se ejecuto |
Meli | 0:d1f7bad3a2cf | 388 | //08-D6379B1E569763 esto es Voltaje1 |
Meli | 0:d1f7bad3a2cf | 389 | |
Meli | 0:d1f7bad3a2cf | 390 | |
Meli | 0:d1f7bad3a2cf | 391 | cleanBuffer(buffer,100); |
Meli | 0:d1f7bad3a2cf | 392 | |
Meli | 0:d1f7bad3a2cf | 393 | } |
Meli | 0:d1f7bad3a2cf | 394 | } |
Meli | 0:d1f7bad3a2cf | 395 | |
Meli | 0:d1f7bad3a2cf | 396 | |
Meli | 0:d1f7bad3a2cf | 397 | } |
Meli | 0:d1f7bad3a2cf | 398 | |
Meli | 0:d1f7bad3a2cf | 399 | |
Meli | 0:d1f7bad3a2cf | 400 | //********************************************************************************************************************** |
Meli | 0:d1f7bad3a2cf | 401 | loop1: LedRojo=0; |
Meli | 0:d1f7bad3a2cf | 402 | wait(0.3); |
Meli | 0:d1f7bad3a2cf | 403 | LedRojo=1; |
Meli | 0:d1f7bad3a2cf | 404 | wait(0.3); |
Meli | 0:d1f7bad3a2cf | 405 | goto loop1; |
Meli | 0:d1f7bad3a2cf | 406 | |
Meli | 0:d1f7bad3a2cf | 407 | } |