este programa acciona un led con un mensaje de texto "Led verde" y apaga con "Lgeverde" el led azul

Dependencies:   mbed

/media/uploads/tony63/pruebatx.png

Committer:
tony63
Date:
Fri Apr 28 15:46:29 2017 +0000
Revision:
1:f21dc295f75c
Parent:
0:f9ce3b274d95
versi?n corregida abril 28/2017

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tony63 0:f9ce3b274d95 1 //programa para celular siemens que recibe un mensaje de texto t activa una salida en formato
tony63 0:f9ce3b274d95 2 //pdu hex
tony63 1:f21dc295f75c 3 //VERSION ABRIL 28-2017
tony63 0:f9ce3b274d95 4 #include "mbed.h"
tony63 0:f9ce3b274d95 5 #include "DebouncedIn.h"
tony63 0:f9ce3b274d95 6 #include "stdio.h"
tony63 0:f9ce3b274d95 7 #include "string.h"
tony63 0:f9ce3b274d95 8 Timer t;
tony63 0:f9ce3b274d95 9 DigitalOut LedVerde(LED2);
tony63 0:f9ce3b274d95 10 DigitalOut LedRojo(LED1);
tony63 0:f9ce3b274d95 11 DigitalOut LedAzul(LED3);
tony63 0:f9ce3b274d95 12 DebouncedIn button1(PTC12);
tony63 0:f9ce3b274d95 13 Serial GSM(PTE0,PTE1); //puertos del FRDM para el modem
tony63 0:f9ce3b274d95 14 Serial pc(USBTX,USBRX);
tony63 0:f9ce3b274d95 15 void Rx_interrupt();
tony63 0:f9ce3b274d95 16 int position=0;
tony63 0:f9ce3b274d95 17 int lenpack=6;
tony63 0:f9ce3b274d95 18 int longi=0;
tony63 0:f9ce3b274d95 19 char tel[10];
tony63 0:f9ce3b274d95 20 char DE[50];
tony63 0:f9ce3b274d95 21 char buffer[512];
tony63 0:f9ce3b274d95 22 char buffermsg[100];
tony63 0:f9ce3b274d95 23 char buffer1[13];
tony63 0:f9ce3b274d95 24 char mensaje[100];
tony63 0:f9ce3b274d95 25 char NUMBER[13];
tony63 0:f9ce3b274d95 26 int index;
tony63 0:f9ce3b274d95 27 int count;
tony63 0:f9ce3b274d95 28 int i = 0;
tony63 0:f9ce3b274d95 29 int c=0;
tony63 0:f9ce3b274d95 30 int cont=0;
tony63 0:f9ce3b274d95 31 unsigned char CtrlZ = 0x1A;
tony63 0:f9ce3b274d95 32 bool Flag = false;
tony63 0:f9ce3b274d95 33 char r[]="";
tony63 0:f9ce3b274d95 34 char msg[256];
tony63 0:f9ce3b274d95 35 char char1;
tony63 1:f21dc295f75c 36 //---------------------------------------------------------------
tony63 0:f9ce3b274d95 37 void FlushGSM(void) {
tony63 0:f9ce3b274d95 38 char1 = 0;
tony63 0:f9ce3b274d95 39 while (GSM.readable()){
tony63 0:f9ce3b274d95 40 char1 = GSM.getc();}
tony63 0:f9ce3b274d95 41 return;}
tony63 1:f21dc295f75c 42 //----------------------------------------------------------------
tony63 0:f9ce3b274d95 43 void callback() {
tony63 0:f9ce3b274d95 44
tony63 0:f9ce3b274d95 45 pc.printf("%c\n", GSM.getc());
tony63 0:f9ce3b274d95 46
tony63 0:f9ce3b274d95 47 }
tony63 0:f9ce3b274d95 48
tony63 0:f9ce3b274d95 49 int readBuffer(char *buffer,int count)
tony63 0:f9ce3b274d95 50 {
tony63 0:f9ce3b274d95 51 int i=0;
tony63 0:f9ce3b274d95 52 t.start();
tony63 0:f9ce3b274d95 53 while(1) {
tony63 0:f9ce3b274d95 54 while (GSM.readable()) {
tony63 0:f9ce3b274d95 55 char c = GSM.getc();
tony63 0:f9ce3b274d95 56 if (c == '\r' || c == '\n') c = '$';
tony63 0:f9ce3b274d95 57 buffer[i++] = c;
tony63 0:f9ce3b274d95 58 if(i > count)break;
tony63 0:f9ce3b274d95 59 }
tony63 0:f9ce3b274d95 60 if(i > count)break;
tony63 0:f9ce3b274d95 61 if(t.read() > 3) {
tony63 0:f9ce3b274d95 62 t.stop();
tony63 0:f9ce3b274d95 63 t.reset();
tony63 0:f9ce3b274d95 64 break;
tony63 0:f9ce3b274d95 65 }
tony63 0:f9ce3b274d95 66 }
tony63 0:f9ce3b274d95 67 wait(0.5);
tony63 0:f9ce3b274d95 68 while(GSM.readable()) {
tony63 0:f9ce3b274d95 69 char c = GSM.getc();
tony63 0:f9ce3b274d95 70 }
tony63 0:f9ce3b274d95 71 return 0;
tony63 0:f9ce3b274d95 72 }
tony63 1:f21dc295f75c 73 //------------------------------------------------------------------------
tony63 0:f9ce3b274d95 74 void cleanBuffer(char *buffer, int count)
tony63 0:f9ce3b274d95 75 {
tony63 0:f9ce3b274d95 76 for(int i=0; i < count; i++) {
tony63 0:f9ce3b274d95 77 buffer[i] = '\0';
tony63 0:f9ce3b274d95 78 }
tony63 0:f9ce3b274d95 79 }
tony63 1:f21dc295f75c 80 //----------------------------------------------------------------------
tony63 0:f9ce3b274d95 81 void sendCmd(char *cmd)
tony63 0:f9ce3b274d95 82 {
tony63 0:f9ce3b274d95 83 GSM.puts(cmd);
tony63 0:f9ce3b274d95 84 }
tony63 0:f9ce3b274d95 85
tony63 0:f9ce3b274d95 86 int waitForResp(char *resp, int timeout)
tony63 0:f9ce3b274d95 87 {
tony63 0:f9ce3b274d95 88 int len = strlen(resp);
tony63 0:f9ce3b274d95 89 int sum=0;
tony63 0:f9ce3b274d95 90 t.start();
tony63 0:f9ce3b274d95 91
tony63 0:f9ce3b274d95 92 while(1) {
tony63 0:f9ce3b274d95 93 if(GSM.readable()) {
tony63 0:f9ce3b274d95 94 char c = GSM.getc();
tony63 0:f9ce3b274d95 95 sum = (c==resp[sum]) ? sum+1 : 0;
tony63 0:f9ce3b274d95 96 if(sum == len)break;
tony63 0:f9ce3b274d95 97 }
tony63 0:f9ce3b274d95 98 if(t.read() > timeout) {
tony63 0:f9ce3b274d95 99 t.stop();
tony63 0:f9ce3b274d95 100 t.reset();
tony63 0:f9ce3b274d95 101 return -1;
tony63 0:f9ce3b274d95 102 }
tony63 0:f9ce3b274d95 103 }
tony63 0:f9ce3b274d95 104 t.stop();
tony63 0:f9ce3b274d95 105 t.reset();
tony63 0:f9ce3b274d95 106 while(GSM.readable()) {
tony63 0:f9ce3b274d95 107 char c = GSM.getc();
tony63 0:f9ce3b274d95 108 }
tony63 0:f9ce3b274d95 109
tony63 0:f9ce3b274d95 110 return 0;
tony63 0:f9ce3b274d95 111 }
tony63 1:f21dc295f75c 112 //----------------------------------------------------------------------------
tony63 0:f9ce3b274d95 113 int sendCmdAndWaitForResp(char *cmd, char *resp, int timeout)
tony63 0:f9ce3b274d95 114 {
tony63 0:f9ce3b274d95 115 sendCmd(cmd);
tony63 0:f9ce3b274d95 116 return waitForResp(resp,timeout);
tony63 0:f9ce3b274d95 117 }
tony63 1:f21dc295f75c 118 //---------------------------------------------------------------------------
tony63 0:f9ce3b274d95 119 int powerCheck(void)
tony63 0:f9ce3b274d95 120 {
tony63 0:f9ce3b274d95 121 return sendCmdAndWaitForResp("AT\r\n", "OK", 2);
tony63 0:f9ce3b274d95 122 }
tony63 1:f21dc295f75c 123 //----------------------------------------------------------------------------
tony63 0:f9ce3b274d95 124 int checkSIMStatus(void)
tony63 0:f9ce3b274d95 125 {
tony63 0:f9ce3b274d95 126 char gprsBuffer[30];
tony63 0:f9ce3b274d95 127 int count = 0;
tony63 0:f9ce3b274d95 128 cleanBuffer(gprsBuffer,30);
tony63 0:f9ce3b274d95 129 while(count < 3) {
tony63 0:f9ce3b274d95 130 sendCmd("AT+CPIN?\r\n");
tony63 0:f9ce3b274d95 131 readBuffer(gprsBuffer,30);
tony63 0:f9ce3b274d95 132 if((NULL != strstr(gprsBuffer,"+CPIN: READY"))) {
tony63 0:f9ce3b274d95 133 break;
tony63 0:f9ce3b274d95 134 }
tony63 0:f9ce3b274d95 135 count++;
tony63 0:f9ce3b274d95 136 wait(1);
tony63 0:f9ce3b274d95 137 }
tony63 0:f9ce3b274d95 138
tony63 0:f9ce3b274d95 139 if(count == 3) {
tony63 0:f9ce3b274d95 140 return -1;
tony63 0:f9ce3b274d95 141 }
tony63 0:f9ce3b274d95 142 return 0;
tony63 0:f9ce3b274d95 143 }
tony63 1:f21dc295f75c 144 //--------------------------------------------------------------------------------------------------
tony63 0:f9ce3b274d95 145 int checkSignalStrength(void)
tony63 0:f9ce3b274d95 146 {
tony63 0:f9ce3b274d95 147 char gprsBuffer[100];
tony63 0:f9ce3b274d95 148 int index,count = 0;
tony63 0:f9ce3b274d95 149 cleanBuffer(gprsBuffer,100);
tony63 0:f9ce3b274d95 150 while(count < 3) {
tony63 0:f9ce3b274d95 151 sendCmd("AT+CSQ\r\n");
tony63 0:f9ce3b274d95 152 readBuffer(gprsBuffer,25);
tony63 0:f9ce3b274d95 153 if(sscanf(gprsBuffer, "AT+CSQ$$$$+CSQ: %d", &index)>0) {
tony63 0:f9ce3b274d95 154 break;
tony63 0:f9ce3b274d95 155 }
tony63 0:f9ce3b274d95 156 count++;
tony63 0:f9ce3b274d95 157 wait(1);
tony63 0:f9ce3b274d95 158 }
tony63 0:f9ce3b274d95 159 if(count == 3) {
tony63 0:f9ce3b274d95 160 return -1;
tony63 0:f9ce3b274d95 161 }
tony63 0:f9ce3b274d95 162 return index;
tony63 0:f9ce3b274d95 163 }
tony63 1:f21dc295f75c 164 //----------------------------------------------------------------------------------------
tony63 0:f9ce3b274d95 165 int init()
tony63 0:f9ce3b274d95 166 {
tony63 0:f9ce3b274d95 167 for(int i = 0; i < 3; i++){
tony63 0:f9ce3b274d95 168 sendCmdAndWaitForResp("AT\r\n", "OK", 1);
tony63 0:f9ce3b274d95 169 wait(0.5);
tony63 0:f9ce3b274d95 170 }
tony63 0:f9ce3b274d95 171 if(0 != checkSIMStatus()) {
tony63 0:f9ce3b274d95 172 return -1;
tony63 0:f9ce3b274d95 173 }
tony63 0:f9ce3b274d95 174 if(checkSignalStrength()<1) {
tony63 0:f9ce3b274d95 175 return -1;
tony63 0:f9ce3b274d95 176 }
tony63 0:f9ce3b274d95 177
tony63 0:f9ce3b274d95 178 GSM.attach(&Rx_interrupt, Serial::RxIrq);
tony63 0:f9ce3b274d95 179 return 0;
tony63 0:f9ce3b274d95 180 }
tony63 1:f21dc295f75c 181 //-------------------------------------------------------------------------------------------------
tony63 0:f9ce3b274d95 182 int readSMSpdu(char *message, int index)
tony63 0:f9ce3b274d95 183 {
tony63 0:f9ce3b274d95 184 int i = 0;
tony63 0:f9ce3b274d95 185 char gprsBuffer[100];
tony63 0:f9ce3b274d95 186 char *p,*s;
tony63 0:f9ce3b274d95 187 GSM.printf("AT+CMGR=%d\r\n",index);
tony63 0:f9ce3b274d95 188 cleanBuffer(gprsBuffer,100);
tony63 0:f9ce3b274d95 189 readBuffer(gprsBuffer,100);
tony63 0:f9ce3b274d95 190 if(NULL == ( s = strstr(gprsBuffer,"+CMGR"))) {
tony63 0:f9ce3b274d95 191 return -1;
tony63 0:f9ce3b274d95 192 }
tony63 0:f9ce3b274d95 193 if(NULL != ( s = strstr(gprsBuffer,"+32"))) {
tony63 0:f9ce3b274d95 194 p = s + 6;
tony63 0:f9ce3b274d95 195 while((*p != '$')&&(i < 5)) {
tony63 0:f9ce3b274d95 196 message[i++] = *(p++);
tony63 0:f9ce3b274d95 197 }
tony63 0:f9ce3b274d95 198 message[i] = '\0';
tony63 0:f9ce3b274d95 199 }
tony63 0:f9ce3b274d95 200 return 0;
tony63 0:f9ce3b274d95 201 }
tony63 1:f21dc295f75c 202 //-----------------------------------------------------------------------------------
tony63 0:f9ce3b274d95 203 int deleteSMS(int index)
tony63 0:f9ce3b274d95 204 {
tony63 0:f9ce3b274d95 205 char cmd[32];
tony63 0:f9ce3b274d95 206 snprintf(cmd,sizeof(cmd),"AT+CMGD=%d\r\n",index);
tony63 0:f9ce3b274d95 207 sendCmd(cmd);
tony63 0:f9ce3b274d95 208 return 0;
tony63 0:f9ce3b274d95 209 }
tony63 1:f21dc295f75c 210 //---------------------------------------------------------------------------------------------
tony63 0:f9ce3b274d95 211 void Rx_interrupt(){
tony63 0:f9ce3b274d95 212 }
tony63 0:f9ce3b274d95 213
tony63 0:f9ce3b274d95 214 int main(void)
tony63 0:f9ce3b274d95 215 {
tony63 0:f9ce3b274d95 216
tony63 0:f9ce3b274d95 217 LedVerde=1;
tony63 0:f9ce3b274d95 218 LedRojo=1;
tony63 0:f9ce3b274d95 219 LedAzul=1;
tony63 0:f9ce3b274d95 220 lenpack=6;
tony63 0:f9ce3b274d95 221 GSM.baud(9600);
tony63 0:f9ce3b274d95 222 GSM.format(8,Serial::None,1);
tony63 0:f9ce3b274d95 223
tony63 0:f9ce3b274d95 224 GSM.printf("AT\r\n");
tony63 0:f9ce3b274d95 225 wait(0.5);
tony63 0:f9ce3b274d95 226 GSM.printf("AT+CNMI=1,1\r\n");
tony63 0:f9ce3b274d95 227 wait(0.5);
tony63 0:f9ce3b274d95 228 GSM.printf("AT+CMGF=0\r\n");
tony63 0:f9ce3b274d95 229 wait(0.5);
tony63 0:f9ce3b274d95 230 GSM.printf("ATE\r\n");
tony63 0:f9ce3b274d95 231 wait(0.5);
tony63 0:f9ce3b274d95 232 GSM.printf("CBST=0,0,1\r\n");
tony63 0:f9ce3b274d95 233 wait(0.5);
tony63 0:f9ce3b274d95 234 LedVerde=0;
tony63 1:f21dc295f75c 235 //------------------El Ciclico----------------------------------------
tony63 0:f9ce3b274d95 236
tony63 0:f9ce3b274d95 237 while(1){
tony63 0:f9ce3b274d95 238
tony63 0:f9ce3b274d95 239 if (button1.falling())
tony63 0:f9ce3b274d95 240 { LedVerde=1;
tony63 0:f9ce3b274d95 241 wait(2);
tony63 0:f9ce3b274d95 242 if (!button1)
tony63 0:f9ce3b274d95 243 {LedVerde=0;
tony63 0:f9ce3b274d95 244 index=20;
tony63 0:f9ce3b274d95 245
tony63 0:f9ce3b274d95 246 GSM.printf("AT+CMGS=%d\r\n",index);
tony63 0:f9ce3b274d95 247 wait(0.2);
tony63 1:f21dc295f75c 248 // onverde= 0011000A9113223717370000AA076FB7BD2C279701
tony63 1:f21dc295f75c 249 GSM.printf("0011000A9113223717370000AA076FB7BD2C279701");
tony63 0:f9ce3b274d95 250 wait(0.5);
tony63 0:f9ce3b274d95 251 GSM.putc((char)0x1A);
tony63 0:f9ce3b274d95 252 LedVerde=1;
tony63 0:f9ce3b274d95 253 LedRojo=0;
tony63 0:f9ce3b274d95 254 wait(3);
tony63 0:f9ce3b274d95 255 LedRojo=1;
tony63 0:f9ce3b274d95 256 LedVerde=0;
tony63 0:f9ce3b274d95 257 }
tony63 0:f9ce3b274d95 258 }
tony63 0:f9ce3b274d95 259
tony63 0:f9ce3b274d95 260 if (GSM.readable()) {
tony63 0:f9ce3b274d95 261 readBuffer(buffer,100);
tony63 0:f9ce3b274d95 262 pc.printf("buffer= %s\n\r ",buffer);
tony63 0:f9ce3b274d95 263 pc.printf("buffer= %c %c\n\r ",buffer[10],buffer[11]);
tony63 0:f9ce3b274d95 264 if(buffer[67]=='A'){for(i=0;i<86;i++)
tony63 0:f9ce3b274d95 265 {buffermsg[i]=buffer[i];}
tony63 0:f9ce3b274d95 266 pc.printf("mensaje= %s\n\r ",buffermsg);
tony63 0:f9ce3b274d95 267 pc.printf("mensaje[72]= %c mensaje[73]=%c\n\r ",buffermsg[72],buffermsg[73]);
tony63 0:f9ce3b274d95 268 buffer[67]='c';
tony63 0:f9ce3b274d95 269 }
tony63 0:f9ce3b274d95 270
tony63 0:f9ce3b274d95 271 if(buffer[10]=='S'&& buffer[11]=='M'){
tony63 0:f9ce3b274d95 272 for(i=0;i<5;i++)
tony63 0:f9ce3b274d95 273 {buffer1[i]=buffer[2+i];}
tony63 0:f9ce3b274d95 274 pc.printf("buffer1= %s\n\r ",buffer1);
tony63 0:f9ce3b274d95 275 buffer[10]='c';
tony63 0:f9ce3b274d95 276 buffer[11]='c';
tony63 0:f9ce3b274d95 277 }
tony63 0:f9ce3b274d95 278 if(buffer1[3]=='T'){pc.printf("AT+CMGL=0\n\r");
tony63 0:f9ce3b274d95 279 wait(0.5);
tony63 0:f9ce3b274d95 280 GSM.printf("AT+CMGL=0\r\n");
tony63 0:f9ce3b274d95 281 buffer1[3]='p';
tony63 0:f9ce3b274d95 282 }
tony63 0:f9ce3b274d95 283
tony63 0:f9ce3b274d95 284
tony63 0:f9ce3b274d95 285 // CC3219642FCBC965 ESTO SIGNIFICA "Led verde" si mandan ese mensaje de texto se prende el led azul
tony63 0:f9ce3b274d95 286
tony63 0:f9ce3b274d95 287 if(buffermsg[70]=='C' && buffermsg[71]=='C' && buffermsg[72]=='3' && buffermsg[73]=='2' && buffermsg[74]=='1' && buffermsg[75]=='9' && buffermsg[76]=='6' && buffermsg[77]=='4' && buffermsg[78]=='2' && buffermsg[79]=='F' && buffermsg[80]=='C' && buffermsg[81]=='B' && buffermsg[82]=='C' && buffermsg[83]=='9' && buffermsg[84]=='6' && buffermsg[85]=='5'){LedAzul=0;}
tony63 0:f9ce3b274d95 288 if(buffermsg[72]=='7' && buffermsg[73]=='3'){LedAzul=1;} //apaga con Lgeverde
tony63 0:f9ce3b274d95 289
tony63 0:f9ce3b274d95 290
tony63 0:f9ce3b274d95 291 }
tony63 0:f9ce3b274d95 292 }
tony63 0:f9ce3b274d95 293 }
tony63 1:f21dc295f75c 294 /*
tony63 1:f21dc295f75c 295 cadena real --Led verde= 0011000A9113223717370000AA09CC3219642FCBC965
tony63 1:f21dc295f75c 296 esto llega luego de mandar -------------------Led verde----------------
tony63 1:f21dc295f75c 297 buffer=
tony63 1:f21dc295f75c 298
tony63 1:f21dc295f75c 299 buffer= [00] [00]
tony63 1:f21dc295f75c 300
tony63 1:f21dc295f75c 301 buffer= $$+CMTI: "SM",1$$
tony63 1:f21dc295f75c 302
tony63 1:f21dc295f75c 303 buffer= S M
tony63 1:f21dc295f75c 304
tony63 1:f21dc295f75c 305 buffer1= +CMTI
tony63 1:f21dc295f75c 306
tony63 1:f21dc295f75c 307 AT+CMGL=0
tony63 1:f21dc295f75c 308
tony63 1:f21dc295f75c 309 buffer= $$+CMGL: 1,0,,26$$0791751330512411040AA1132237173700007140820173340A09CC3219642FCBC965$$$$OK$$
tony63 1:f21dc295f75c 310
tony63 1:f21dc295f75c 311 buffer= , 0
tony63 1:f21dc295f75c 312
tony63 1:f21dc295f75c 313 mensaje= $$+CMGL: 1,0,,26$$0791751330512411040AA1132237173700007140820173340A09CC3219642FCBC965
tony63 1:f21dc295f75c 314
tony63 1:f21dc295f75c 315 mensaje[72]= 3 mensaje[73]=2
tony63 1:f21dc295f75c 316
tony63 1:f21dc295f75c 317
tony63 1:f21dc295f75c 318
tony63 1:f21dc295f75c 319 */