este programa sirve para probar que el siemens manda mensajes de texto a su celular cambie mi numero por el suyo guiese del ultimo doc y mande un mensaje diferente
Dependencies: DebouncedIn mbed
main.cpp
00001 // Programa para enviar desde la FRDMKL25Z un mensaje de texto en formatp PDU 00002 // Como modem usa un celular SIEMENS a56i 00003 // ENVIAR -----la palabra "ALARMA 1" 00004 // 00005 00006 // OJO CON ESTO 00007 // conector del siemens cable verde es RX conectelo a PTE0 cable blanco es TX conectelo a PTE1 00008 #include "mbed.h" 00009 #include "DebouncedIn.h" 00010 #include "stdio.h" 00011 #include "string.h" 00012 Timer t; 00013 DigitalOut LedVerde(LED2); 00014 DigitalOut LedRojo(LED1); 00015 DigitalOut LedAzul(LED3); 00016 DebouncedIn button1(PTC12); //señal que inicia el envio del mensaje 00017 Serial GSM(PTE0,PTE1); //Configura puerto UART de la FRDMKL25Z 00018 Serial pc(USBTX,USBRX);//Configura puerto USB a la consola serial del PC conectado. 00019 void Rx_interrupt(); 00020 int position=0; 00021 int lenpack=6; 00022 int longi=0; 00023 char tel[11]; 00024 char DE[50]; 00025 char buffer[100]; 00026 char datos[100]; 00027 char NUMBER[13]; 00028 int index; 00029 int count; 00030 int i = 0; 00031 int j = 0; 00032 int c=0; 00033 unsigned char CtrlZ = 0x1A; // comodin de emision controlZ 00034 bool Flag = false; // bandera 00035 char r[]=""; //Cadena de recepcion de la trama PDU si se usa!! 00036 char msg[256]; 00037 char char1; 00038 //Flush serial para el buffer 00039 void FlushGSM(void) { 00040 char1 = 0; 00041 while (GSM.readable()){ 00042 char1 = GSM.getc();} 00043 return;} 00044 00045 void callback() { 00046 // Note: you need to actually read from the serial to clear the RX interrupt 00047 pc.printf("%c\n", GSM.getc()); 00048 00049 } 00050 00051 int readBuffer(char *buffer,int count) 00052 { 00053 int i=0; 00054 t.start(); // start timer 00055 while(1) { 00056 while (GSM.readable()) { 00057 char c = GSM.getc(); 00058 if (c == '\r' || c == '\n') c = '$'; 00059 buffer[i++] = c; 00060 if(i > count)break; 00061 } 00062 if(i > count)break; 00063 if(t.read() > 3) { 00064 t.stop(); 00065 t.reset(); 00066 break; 00067 } 00068 } 00069 wait(0.5); 00070 while(GSM.readable()) { // display the other thing.. 00071 char c = GSM.getc(); 00072 } 00073 return 0; 00074 } 00075 00076 /* esta funcion de abajo limpia o borra todo un "buffer" de tamaño "count" 00077 lo revisa uno por un elemento y le mete el caracter null que indica fin de cadena 00078 no retorna nada 00079 */ 00080 //*************************************************************************************** 00081 void cleanBuffer(char *buffer, int count) 00082 { 00083 for(int i=0; i < count; i++) { 00084 buffer[i] = '\0'; 00085 } 00086 } 00087 /* esta funcion de abajo envia un comando parametrizado como cadena 00088 */ 00089 //*************************************************************************************** 00090 void sendCmd(char *cmd) 00091 { 00092 GSM.puts(cmd); 00093 } 00094 /* esta funcion de abajo espera la respuesta de un comando que debe ser identica a la cadena "resp" y un tiempo timeout" 00095 si todo sale bien retorna un cero que en la programacion hay que validar 00096 si algo sale mal ( no se parece o se demora mucho )retorna -1 que debera validarse con alguna expresion logica 00097 */ 00098 //*************************************************************************************** 00099 int waitForResp(char *resp, int timeout) 00100 { 00101 int len = strlen(resp); 00102 int sum=0; 00103 t.start(); 00104 00105 while(1) { 00106 if(GSM.readable()) { 00107 char c = GSM.getc(); 00108 sum = (c==resp[sum]) ? sum+1 : 0;// esta linea de C# sum se incrementa o se hace cero segun c 00109 if(sum == len)break; //ya acabo se sale 00110 } 00111 if(t.read() > timeout) { // time out chequea el tiempo minimo antes de salir perdiendo 00112 t.stop(); 00113 t.reset(); 00114 return -1; 00115 } 00116 } 00117 t.stop(); // stop timer antes de retornar 00118 t.reset(); // clear timer 00119 while(GSM.readable()) { // display the other thing.. 00120 char c = GSM.getc(); 00121 } 00122 00123 return 0; 00124 } 00125 /* esta funcion de abajo es muy completa e util se encarga de enviar el comando y esperar la respuesta 00126 si todo sale bien retorna un cero(herencia de las funciones contenedoras) que en la programacion hay que validar 00127 con alguna expresion logica 00128 */ 00129 //*************************************************************************************** 00130 int sendCmdAndWaitForResp(char *cmd, char *resp, int timeout) 00131 { 00132 sendCmd(cmd); 00133 return waitForResp(resp,timeout); 00134 } 00135 /* esta funcion de abajo chequea que el modem este vivo envia AT y le contesta con OK y espera 2 segundos 00136 */ 00137 //*************************************************************************************** 00138 int powerCheck(void)// este comando se manda para verificar si el modem esta vivo o conectado 00139 { 00140 return sendCmdAndWaitForResp("AT\r\n", "OK", 2); 00141 } 00142 /* esta funcion de abajo chequea el estado de la sim card 00143 y si todo sale bien retorna un cero que en la programacion hay que validar 00144 con alguna expresion logica 00145 */ 00146 //*************************************************************************************** 00147 int checkSIMStatus(void) 00148 { 00149 char gprsBuffer[30]; 00150 int count = 0; 00151 cleanBuffer(gprsBuffer,30); 00152 while(count < 3) { 00153 sendCmd("AT+CPIN?\r\n"); 00154 readBuffer(gprsBuffer,30); 00155 if((NULL != strstr(gprsBuffer,"+CPIN: READY"))) { 00156 break; 00157 } 00158 count++; 00159 wait(1); 00160 } 00161 00162 if(count == 3) { 00163 return -1; 00164 } 00165 return 0; 00166 } 00167 /* esta funcion de abajo chequea la calidad de la señal 00168 y si todo sale bien retorna cun el valor de señal util o un -1 si nop es aceptable, en la programacion hay que validar 00169 con alguna expresion logica 00170 */ 00171 //*************************************************************************************** 00172 int checkSignalStrength(void) 00173 { 00174 char gprsBuffer[100]; 00175 int index,count = 0; 00176 cleanBuffer(gprsBuffer,100); 00177 while(count < 3) { 00178 sendCmd("AT+CSQ\r\n"); 00179 readBuffer(gprsBuffer,25); 00180 if(sscanf(gprsBuffer, "AT+CSQ$$$$+CSQ: %d", &index)>0) { 00181 break; 00182 } 00183 count++; 00184 wait(1); 00185 } 00186 if(count == 3) { 00187 return -1; 00188 } 00189 return index; 00190 } 00191 /* esta funcion de abajo configura el modem de forma inicial con comandas AT 00192 y si todo sale bien retorna un cero que en la programacion hay que validar 00193 con alguna expresion logica pero si algo sale mal retorna un -1 00194 */ 00195 //*************************************************************************************** 00196 /*int settingSMS() //esta funcion se invoca para configurar el modem al principio 00197 { 00198 if(0 != sendCmdAndWaitForResp("AT\r\n", "OK", 1)) { 00199 return -1; 00200 } 00201 if(0 != sendCmdAndWaitForResp("AT+CNMI=1,1\r\n", "OK", 1)) { 00202 return -1; 00203 } 00204 if(0 != sendCmdAndWaitForResp("AT+CMGF=0\r\n", "OK", 1)) { 00205 return -1; 00206 } 00207 if(0 != sendCmdAndWaitForResp("ATE\r\n", "OK", 1)) { 00208 return -1; 00209 } 00210 if(0 != sendCmdAndWaitForResp("CBST=0,0,1\r\n", "OK", 1)) { 00211 return -1; 00212 } 00213 return 0; 00214 } 00215 */ 00216 00217 00218 /* esta funcion de abajo inicaliza el modem se compone de un grupo de subfunciones ya definidas previamente 00219 primero chequea que este vivo 00220 segundo chequea el estado de la simcard 00221 tercero chequea la intencidad de señal celular 00222 cuarto aplica la configuracion 00223 y si todo sale bien retorna un cero que en la programacion hay que validar 00224 con alguna expresion logica 00225 */ 00226 //*************************************************************************************** 00227 int init() 00228 { 00229 for(int i = 0; i < 3; i++){ 00230 sendCmdAndWaitForResp("AT\r\n", "OK", 1); 00231 wait(0.5); 00232 } 00233 if(0 != checkSIMStatus()) { 00234 return -1; 00235 } 00236 if(checkSignalStrength()<1) { 00237 return -1; 00238 } 00239 00240 GSM.attach(&Rx_interrupt, Serial::RxIrq); 00241 return 0; 00242 } 00243 /* esta funcion de abajo intenta leer un mensaje de texto en formato PDU o HEX 00244 y si todo sale bien retorna un cero que en la programacion hay que validar 00245 con alguna expresion logica 00246 */ 00247 //*************************************************************************************** 00248 int readSMSpdu(char *message, int index) 00249 { 00250 int i = 0; 00251 char gprsBuffer[100]; 00252 char *p,*s; 00253 GSM.printf("AT+CMGR=%d\r\n",index); 00254 cleanBuffer(gprsBuffer,100); 00255 readBuffer(gprsBuffer,100); 00256 if(NULL == ( s = strstr(gprsBuffer,"+CMGR"))) { 00257 return -1; 00258 } 00259 if(NULL != ( s = strstr(gprsBuffer,"+32"))) { 00260 p = s + 6; 00261 while((*p != '$')&&(i < 5)) { 00262 message[i++] = *(p++); 00263 } 00264 message[i] = '\0'; 00265 } 00266 return 0; 00267 } 00268 /* esta funcion de abajo borra mensajes SMS del modem 00269 y si todo sale bien retorna un cero que en la programacion hay que validar 00270 con alguna expresion logica 00271 */ 00272 //*************************************************************************************** 00273 int deleteSMS(int index) 00274 { 00275 char cmd[32]; 00276 snprintf(cmd,sizeof(cmd),"AT+CMGD=%d\r\n",index); 00277 sendCmd(cmd); 00278 return 0; 00279 } 00280 //***0********************************************************************************* 00281 00282 // Interupt Routine to read in data from serial port 00283 void Rx_interrupt(){ 00284 } 00285 /* 00286 readBuffer(buffer,6); 00287 if(strncmp(buffer, "+CMTI", 5)){ 00288 wait(1); 00289 GSM.printf("AT+CMGL=%d\r\n",0); 00290 readBuffer(buffer,100);} 00291 if(strncmp(buffer, "+CMGL:", 5)){ 00292 for (i=0;i<31;i++){ 00293 buffer[5+i]=c; // 32 de basura 00294 } 00295 } 00296 for (i=0;i<10;i++){ 00297 buffer[36+i]=tel[i]; // numero telefonico 00298 } 00299 for (i=0;i<18;i++){ 00300 buffer[46+i]=c; // 18 de basura 00301 } 00302 for (i=0;i<2;i++){ 00303 buffer[64+i]=tel[i]; // tamaño de septetos 00304 } 00305 for (i=0;i<30;i++){ 00306 buffer[66+i]=DE[i]; // datos en octetos hex 00307 if (buffer[i]=='\0'){ 00308 } 00309 */ 00310 00311 00312 int main(void) 00313 { 00314 00315 //NVIC_DisableIRQ(UART1_IRQn); 00316 //apagamos los 3 leds 00317 LedVerde=1; 00318 LedRojo=1; 00319 LedAzul=1; 00320 lenpack=6; //tamaño de "ALARMA" 00321 GSM.baud(9600);//configura los baudios de la FRDMKL25Z en 9600 00322 GSM.format(8,Serial::None,1); //configura el formato de los datos de la UART 00323 //configuro modem GSM 00324 GSM.printf("AT\r\n"); 00325 wait(0.5); 00326 GSM.printf("AT+CNMI=1,1\r\n"); 00327 wait(0.5); 00328 GSM.printf("AT+CMGF=0\r\n"); 00329 wait(0.5); 00330 GSM.printf("ATE\r\n"); 00331 wait(0.5); 00332 GSM.printf("CBST=0,0,1\r\n"); 00333 wait(0.5); 00334 LedVerde=0; 00335 00336 //if(0!= init()){ //se configura y chequea el modem GSM 00337 // LedRojo=0; 00338 //} 00339 while(1){ 00340 if (button1.falling()) 00341 { LedVerde=1; 00342 wait(2); 00343 if (!button1) 00344 {LedVerde=0; 00345 index=20; 00346 //GSM.printf("AT+CNMI=1,1\r\n"); //configuracion inicial del MODEM! 00347 GSM.printf("AT+CMGS=%d\r\n",index); 00348 wait(0.2); 00349 GSM.printf("0011000A9113223717370000AA08416650DA0C8262"); //esto es "ALARMA 1" 00350 wait(0.5); 00351 GSM.putc((char)0x1A); //esto es controlZ 00352 LedVerde=1; 00353 LedRojo=0; //lo prendo 00354 wait(3); 00355 LedRojo=1; //lo apago 00356 LedVerde=0; 00357 } 00358 00359 } 00360 if(GSM.readable()) { //USE ESTE FRAGMENTO DE CODIGO PARA LEER LO QUE EL CELULAR RESPONDE 00361 while(GSM.readable()) { 00362 char c = GSM.getc(); 00363 buffer[count++] = c; 00364 if(count == 64) break; 00365 } 00366 pc.puts(buffer); 00367 00368 00369 for(int i = 0; i < count; i++) { 00370 buffer[i] = NULL; 00371 } 00372 00373 count = 0; 00374 00375 } 00376 00377 } 00378 } 00379 00380 // buffer[i] contiene lo que regresa
Generated on Thu Jul 14 2022 11:59:46 by 1.7.2