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:
Thu Jan 29 05:24:03 2015 +0000
Revision:
0:f9ce3b274d95
Child:
1:f21dc295f75c
programa para recibir unmensaje de texto y conmutar un led

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 0:f9ce3b274d95 3 #include "mbed.h"
tony63 0:f9ce3b274d95 4 #include "DebouncedIn.h"
tony63 0:f9ce3b274d95 5 #include "stdio.h"
tony63 0:f9ce3b274d95 6 #include "string.h"
tony63 0:f9ce3b274d95 7 Timer t;
tony63 0:f9ce3b274d95 8 DigitalOut LedVerde(LED2);
tony63 0:f9ce3b274d95 9 DigitalOut LedRojo(LED1);
tony63 0:f9ce3b274d95 10 DigitalOut LedAzul(LED3);
tony63 0:f9ce3b274d95 11 DebouncedIn button1(PTC12);
tony63 0:f9ce3b274d95 12 Serial GSM(PTE0,PTE1); //puertos del FRDM para el modem
tony63 0:f9ce3b274d95 13 Serial pc(USBTX,USBRX);
tony63 0:f9ce3b274d95 14 void Rx_interrupt();
tony63 0:f9ce3b274d95 15 int position=0;
tony63 0:f9ce3b274d95 16 int lenpack=6;
tony63 0:f9ce3b274d95 17 int longi=0;
tony63 0:f9ce3b274d95 18 char tel[10];
tony63 0:f9ce3b274d95 19 char DE[50];
tony63 0:f9ce3b274d95 20 char buffer[512];
tony63 0:f9ce3b274d95 21 char buffermsg[100];
tony63 0:f9ce3b274d95 22 char buffer1[13];
tony63 0:f9ce3b274d95 23 char mensaje[100];
tony63 0:f9ce3b274d95 24 char NUMBER[13];
tony63 0:f9ce3b274d95 25 int index;
tony63 0:f9ce3b274d95 26 int count;
tony63 0:f9ce3b274d95 27 int i = 0;
tony63 0:f9ce3b274d95 28 int c=0;
tony63 0:f9ce3b274d95 29 int cont=0;
tony63 0:f9ce3b274d95 30 unsigned char CtrlZ = 0x1A;
tony63 0:f9ce3b274d95 31 bool Flag = false;
tony63 0:f9ce3b274d95 32 char r[]="";
tony63 0:f9ce3b274d95 33 char msg[256];
tony63 0:f9ce3b274d95 34 char char1;
tony63 0:f9ce3b274d95 35 void FlushGSM(void) {
tony63 0:f9ce3b274d95 36 char1 = 0;
tony63 0:f9ce3b274d95 37 while (GSM.readable()){
tony63 0:f9ce3b274d95 38 char1 = GSM.getc();}
tony63 0:f9ce3b274d95 39 return;}
tony63 0:f9ce3b274d95 40
tony63 0:f9ce3b274d95 41 void callback() {
tony63 0:f9ce3b274d95 42
tony63 0:f9ce3b274d95 43 pc.printf("%c\n", GSM.getc());
tony63 0:f9ce3b274d95 44
tony63 0:f9ce3b274d95 45 }
tony63 0:f9ce3b274d95 46
tony63 0:f9ce3b274d95 47 int readBuffer(char *buffer,int count)
tony63 0:f9ce3b274d95 48 {
tony63 0:f9ce3b274d95 49 int i=0;
tony63 0:f9ce3b274d95 50 t.start();
tony63 0:f9ce3b274d95 51 while(1) {
tony63 0:f9ce3b274d95 52 while (GSM.readable()) {
tony63 0:f9ce3b274d95 53 char c = GSM.getc();
tony63 0:f9ce3b274d95 54 if (c == '\r' || c == '\n') c = '$';
tony63 0:f9ce3b274d95 55 buffer[i++] = c;
tony63 0:f9ce3b274d95 56 if(i > count)break;
tony63 0:f9ce3b274d95 57 }
tony63 0:f9ce3b274d95 58 if(i > count)break;
tony63 0:f9ce3b274d95 59 if(t.read() > 3) {
tony63 0:f9ce3b274d95 60 t.stop();
tony63 0:f9ce3b274d95 61 t.reset();
tony63 0:f9ce3b274d95 62 break;
tony63 0:f9ce3b274d95 63 }
tony63 0:f9ce3b274d95 64 }
tony63 0:f9ce3b274d95 65 wait(0.5);
tony63 0:f9ce3b274d95 66 while(GSM.readable()) {
tony63 0:f9ce3b274d95 67 char c = GSM.getc();
tony63 0:f9ce3b274d95 68 }
tony63 0:f9ce3b274d95 69 return 0;
tony63 0:f9ce3b274d95 70 }
tony63 0:f9ce3b274d95 71
tony63 0:f9ce3b274d95 72 void cleanBuffer(char *buffer, int count)
tony63 0:f9ce3b274d95 73 {
tony63 0:f9ce3b274d95 74 for(int i=0; i < count; i++) {
tony63 0:f9ce3b274d95 75 buffer[i] = '\0';
tony63 0:f9ce3b274d95 76 }
tony63 0:f9ce3b274d95 77 }
tony63 0:f9ce3b274d95 78
tony63 0:f9ce3b274d95 79 void sendCmd(char *cmd)
tony63 0:f9ce3b274d95 80 {
tony63 0:f9ce3b274d95 81 GSM.puts(cmd);
tony63 0:f9ce3b274d95 82 }
tony63 0:f9ce3b274d95 83
tony63 0:f9ce3b274d95 84 int waitForResp(char *resp, int timeout)
tony63 0:f9ce3b274d95 85 {
tony63 0:f9ce3b274d95 86 int len = strlen(resp);
tony63 0:f9ce3b274d95 87 int sum=0;
tony63 0:f9ce3b274d95 88 t.start();
tony63 0:f9ce3b274d95 89
tony63 0:f9ce3b274d95 90 while(1) {
tony63 0:f9ce3b274d95 91 if(GSM.readable()) {
tony63 0:f9ce3b274d95 92 char c = GSM.getc();
tony63 0:f9ce3b274d95 93 sum = (c==resp[sum]) ? sum+1 : 0;
tony63 0:f9ce3b274d95 94 if(sum == len)break;
tony63 0:f9ce3b274d95 95 }
tony63 0:f9ce3b274d95 96 if(t.read() > timeout) {
tony63 0:f9ce3b274d95 97 t.stop();
tony63 0:f9ce3b274d95 98 t.reset();
tony63 0:f9ce3b274d95 99 return -1;
tony63 0:f9ce3b274d95 100 }
tony63 0:f9ce3b274d95 101 }
tony63 0:f9ce3b274d95 102 t.stop();
tony63 0:f9ce3b274d95 103 t.reset();
tony63 0:f9ce3b274d95 104 while(GSM.readable()) {
tony63 0:f9ce3b274d95 105 char c = GSM.getc();
tony63 0:f9ce3b274d95 106 }
tony63 0:f9ce3b274d95 107
tony63 0:f9ce3b274d95 108 return 0;
tony63 0:f9ce3b274d95 109 }
tony63 0:f9ce3b274d95 110
tony63 0:f9ce3b274d95 111 int sendCmdAndWaitForResp(char *cmd, char *resp, int timeout)
tony63 0:f9ce3b274d95 112 {
tony63 0:f9ce3b274d95 113 sendCmd(cmd);
tony63 0:f9ce3b274d95 114 return waitForResp(resp,timeout);
tony63 0:f9ce3b274d95 115 }
tony63 0:f9ce3b274d95 116
tony63 0:f9ce3b274d95 117 int powerCheck(void)
tony63 0:f9ce3b274d95 118 {
tony63 0:f9ce3b274d95 119 return sendCmdAndWaitForResp("AT\r\n", "OK", 2);
tony63 0:f9ce3b274d95 120 }
tony63 0:f9ce3b274d95 121
tony63 0:f9ce3b274d95 122 int checkSIMStatus(void)
tony63 0:f9ce3b274d95 123 {
tony63 0:f9ce3b274d95 124 char gprsBuffer[30];
tony63 0:f9ce3b274d95 125 int count = 0;
tony63 0:f9ce3b274d95 126 cleanBuffer(gprsBuffer,30);
tony63 0:f9ce3b274d95 127 while(count < 3) {
tony63 0:f9ce3b274d95 128 sendCmd("AT+CPIN?\r\n");
tony63 0:f9ce3b274d95 129 readBuffer(gprsBuffer,30);
tony63 0:f9ce3b274d95 130 if((NULL != strstr(gprsBuffer,"+CPIN: READY"))) {
tony63 0:f9ce3b274d95 131 break;
tony63 0:f9ce3b274d95 132 }
tony63 0:f9ce3b274d95 133 count++;
tony63 0:f9ce3b274d95 134 wait(1);
tony63 0:f9ce3b274d95 135 }
tony63 0:f9ce3b274d95 136
tony63 0:f9ce3b274d95 137 if(count == 3) {
tony63 0:f9ce3b274d95 138 return -1;
tony63 0:f9ce3b274d95 139 }
tony63 0:f9ce3b274d95 140 return 0;
tony63 0:f9ce3b274d95 141 }
tony63 0:f9ce3b274d95 142
tony63 0:f9ce3b274d95 143 int checkSignalStrength(void)
tony63 0:f9ce3b274d95 144 {
tony63 0:f9ce3b274d95 145 char gprsBuffer[100];
tony63 0:f9ce3b274d95 146 int index,count = 0;
tony63 0:f9ce3b274d95 147 cleanBuffer(gprsBuffer,100);
tony63 0:f9ce3b274d95 148 while(count < 3) {
tony63 0:f9ce3b274d95 149 sendCmd("AT+CSQ\r\n");
tony63 0:f9ce3b274d95 150 readBuffer(gprsBuffer,25);
tony63 0:f9ce3b274d95 151 if(sscanf(gprsBuffer, "AT+CSQ$$$$+CSQ: %d", &index)>0) {
tony63 0:f9ce3b274d95 152 break;
tony63 0:f9ce3b274d95 153 }
tony63 0:f9ce3b274d95 154 count++;
tony63 0:f9ce3b274d95 155 wait(1);
tony63 0:f9ce3b274d95 156 }
tony63 0:f9ce3b274d95 157 if(count == 3) {
tony63 0:f9ce3b274d95 158 return -1;
tony63 0:f9ce3b274d95 159 }
tony63 0:f9ce3b274d95 160 return index;
tony63 0:f9ce3b274d95 161 }
tony63 0:f9ce3b274d95 162
tony63 0:f9ce3b274d95 163 int init()
tony63 0:f9ce3b274d95 164 {
tony63 0:f9ce3b274d95 165 for(int i = 0; i < 3; i++){
tony63 0:f9ce3b274d95 166 sendCmdAndWaitForResp("AT\r\n", "OK", 1);
tony63 0:f9ce3b274d95 167 wait(0.5);
tony63 0:f9ce3b274d95 168 }
tony63 0:f9ce3b274d95 169 if(0 != checkSIMStatus()) {
tony63 0:f9ce3b274d95 170 return -1;
tony63 0:f9ce3b274d95 171 }
tony63 0:f9ce3b274d95 172 if(checkSignalStrength()<1) {
tony63 0:f9ce3b274d95 173 return -1;
tony63 0:f9ce3b274d95 174 }
tony63 0:f9ce3b274d95 175
tony63 0:f9ce3b274d95 176 GSM.attach(&Rx_interrupt, Serial::RxIrq);
tony63 0:f9ce3b274d95 177 return 0;
tony63 0:f9ce3b274d95 178 }
tony63 0:f9ce3b274d95 179
tony63 0:f9ce3b274d95 180 int readSMSpdu(char *message, int index)
tony63 0:f9ce3b274d95 181 {
tony63 0:f9ce3b274d95 182 int i = 0;
tony63 0:f9ce3b274d95 183 char gprsBuffer[100];
tony63 0:f9ce3b274d95 184 char *p,*s;
tony63 0:f9ce3b274d95 185 GSM.printf("AT+CMGR=%d\r\n",index);
tony63 0:f9ce3b274d95 186 cleanBuffer(gprsBuffer,100);
tony63 0:f9ce3b274d95 187 readBuffer(gprsBuffer,100);
tony63 0:f9ce3b274d95 188 if(NULL == ( s = strstr(gprsBuffer,"+CMGR"))) {
tony63 0:f9ce3b274d95 189 return -1;
tony63 0:f9ce3b274d95 190 }
tony63 0:f9ce3b274d95 191 if(NULL != ( s = strstr(gprsBuffer,"+32"))) {
tony63 0:f9ce3b274d95 192 p = s + 6;
tony63 0:f9ce3b274d95 193 while((*p != '$')&&(i < 5)) {
tony63 0:f9ce3b274d95 194 message[i++] = *(p++);
tony63 0:f9ce3b274d95 195 }
tony63 0:f9ce3b274d95 196 message[i] = '\0';
tony63 0:f9ce3b274d95 197 }
tony63 0:f9ce3b274d95 198 return 0;
tony63 0:f9ce3b274d95 199 }
tony63 0:f9ce3b274d95 200
tony63 0:f9ce3b274d95 201 int deleteSMS(int index)
tony63 0:f9ce3b274d95 202 {
tony63 0:f9ce3b274d95 203 char cmd[32];
tony63 0:f9ce3b274d95 204 snprintf(cmd,sizeof(cmd),"AT+CMGD=%d\r\n",index);
tony63 0:f9ce3b274d95 205 sendCmd(cmd);
tony63 0:f9ce3b274d95 206 return 0;
tony63 0:f9ce3b274d95 207 }
tony63 0:f9ce3b274d95 208
tony63 0:f9ce3b274d95 209 void Rx_interrupt(){
tony63 0:f9ce3b274d95 210 }
tony63 0:f9ce3b274d95 211
tony63 0:f9ce3b274d95 212 int main(void)
tony63 0:f9ce3b274d95 213 {
tony63 0:f9ce3b274d95 214
tony63 0:f9ce3b274d95 215 LedVerde=1;
tony63 0:f9ce3b274d95 216 LedRojo=1;
tony63 0:f9ce3b274d95 217 LedAzul=1;
tony63 0:f9ce3b274d95 218 lenpack=6;
tony63 0:f9ce3b274d95 219 GSM.baud(9600);
tony63 0:f9ce3b274d95 220 GSM.format(8,Serial::None,1);
tony63 0:f9ce3b274d95 221
tony63 0:f9ce3b274d95 222 GSM.printf("AT\r\n");
tony63 0:f9ce3b274d95 223 wait(0.5);
tony63 0:f9ce3b274d95 224 GSM.printf("AT+CNMI=1,1\r\n");
tony63 0:f9ce3b274d95 225 wait(0.5);
tony63 0:f9ce3b274d95 226 GSM.printf("AT+CMGF=0\r\n");
tony63 0:f9ce3b274d95 227 wait(0.5);
tony63 0:f9ce3b274d95 228 GSM.printf("ATE\r\n");
tony63 0:f9ce3b274d95 229 wait(0.5);
tony63 0:f9ce3b274d95 230 GSM.printf("CBST=0,0,1\r\n");
tony63 0:f9ce3b274d95 231 wait(0.5);
tony63 0:f9ce3b274d95 232 LedVerde=0;
tony63 0:f9ce3b274d95 233
tony63 0:f9ce3b274d95 234
tony63 0:f9ce3b274d95 235
tony63 0:f9ce3b274d95 236
tony63 0:f9ce3b274d95 237
tony63 0:f9ce3b274d95 238 while(1){
tony63 0:f9ce3b274d95 239
tony63 0:f9ce3b274d95 240 if (button1.falling())
tony63 0:f9ce3b274d95 241 { LedVerde=1;
tony63 0:f9ce3b274d95 242 wait(2);
tony63 0:f9ce3b274d95 243 if (!button1)
tony63 0:f9ce3b274d95 244 {LedVerde=0;
tony63 0:f9ce3b274d95 245 index=20;
tony63 0:f9ce3b274d95 246
tony63 0:f9ce3b274d95 247 GSM.printf("AT+CMGS=%d\r\n",index);
tony63 0:f9ce3b274d95 248 wait(0.2);
tony63 0:f9ce3b274d95 249 GSM.printf("0011000A9113613102650000AA08416650DA0C8262");
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 }