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