![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Envia valor de voltaje leido escalado por bluetooth.
Fork of VOLTIMETRO1 by
main.cpp@0:4676a90107ca, 2015-04-14 (annotated)
- Committer:
- tony63
- Date:
- Tue Apr 14 02:05:54 2015 +0000
- Revision:
- 0:4676a90107ca
- Child:
- 1:d0a4ddc1bd88
Programa que transmite cadenas, util para verificar si APPINVENTOR recibe cadenas, tres entradas digitales suicheadas controlan en modo toggle, tres colores en la pantalla.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tony63 | 0:4676a90107ca | 1 | //programa para enviar cadenas de caracters y probarlas con app inventor |
tony63 | 0:4676a90107ca | 2 | //el programa tambien recibe cadenas |
tony63 | 0:4676a90107ca | 3 | |
tony63 | 0:4676a90107ca | 4 | #include "mbed.h" |
tony63 | 0:4676a90107ca | 5 | #include "DebouncedIn.h" |
tony63 | 0:4676a90107ca | 6 | #include "stdio.h" |
tony63 | 0:4676a90107ca | 7 | #include "string.h" |
tony63 | 0:4676a90107ca | 8 | Timer t; |
tony63 | 0:4676a90107ca | 9 | DigitalOut LedVerde(LED2); |
tony63 | 0:4676a90107ca | 10 | DigitalOut LedRojo(LED1); |
tony63 | 0:4676a90107ca | 11 | DigitalOut LedAzul(LED3); |
tony63 | 0:4676a90107ca | 12 | DebouncedIn button1(PTC12); |
tony63 | 0:4676a90107ca | 13 | DebouncedIn button2(PTC13); |
tony63 | 0:4676a90107ca | 14 | DebouncedIn button3(PTC16); |
tony63 | 0:4676a90107ca | 15 | Serial GSM(PTE0,PTE1); //puertos del FRDM para el modem |
tony63 | 0:4676a90107ca | 16 | Serial pc(USBTX,USBRX); |
tony63 | 0:4676a90107ca | 17 | void Rx_interrupt(); |
tony63 | 0:4676a90107ca | 18 | int position=0; |
tony63 | 0:4676a90107ca | 19 | int lenpack=6; |
tony63 | 0:4676a90107ca | 20 | int longi=0; |
tony63 | 0:4676a90107ca | 21 | char tel[10]; |
tony63 | 0:4676a90107ca | 22 | char DE[50]; |
tony63 | 0:4676a90107ca | 23 | char buffer[512]; |
tony63 | 0:4676a90107ca | 24 | char buffermsg[100]; |
tony63 | 0:4676a90107ca | 25 | char buffer1[13]; |
tony63 | 0:4676a90107ca | 26 | char mensaje[100]; |
tony63 | 0:4676a90107ca | 27 | char NUMBER[13]; |
tony63 | 0:4676a90107ca | 28 | short int index1; |
tony63 | 0:4676a90107ca | 29 | short int index2; |
tony63 | 0:4676a90107ca | 30 | short int index3; |
tony63 | 0:4676a90107ca | 31 | int count; |
tony63 | 0:4676a90107ca | 32 | int i = 0; |
tony63 | 0:4676a90107ca | 33 | int c=0; |
tony63 | 0:4676a90107ca | 34 | int cont=0; |
tony63 | 0:4676a90107ca | 35 | unsigned char CtrlZ = 0x1A; |
tony63 | 0:4676a90107ca | 36 | bool Flag = false; |
tony63 | 0:4676a90107ca | 37 | char r[]=""; |
tony63 | 0:4676a90107ca | 38 | char msg[256]; |
tony63 | 0:4676a90107ca | 39 | char char1; |
tony63 | 0:4676a90107ca | 40 | void FlushGSM(void) { |
tony63 | 0:4676a90107ca | 41 | char1 = 0; |
tony63 | 0:4676a90107ca | 42 | while (GSM.readable()){ |
tony63 | 0:4676a90107ca | 43 | char1 = GSM.getc();} |
tony63 | 0:4676a90107ca | 44 | return;} |
tony63 | 0:4676a90107ca | 45 | |
tony63 | 0:4676a90107ca | 46 | void callback() { |
tony63 | 0:4676a90107ca | 47 | |
tony63 | 0:4676a90107ca | 48 | pc.printf("%c\n", GSM.getc()); |
tony63 | 0:4676a90107ca | 49 | |
tony63 | 0:4676a90107ca | 50 | } |
tony63 | 0:4676a90107ca | 51 | |
tony63 | 0:4676a90107ca | 52 | int readBuffer(char *buffer,int count) |
tony63 | 0:4676a90107ca | 53 | { |
tony63 | 0:4676a90107ca | 54 | int i=0; |
tony63 | 0:4676a90107ca | 55 | t.start(); |
tony63 | 0:4676a90107ca | 56 | while(1) { |
tony63 | 0:4676a90107ca | 57 | while (GSM.readable()) { |
tony63 | 0:4676a90107ca | 58 | char c = GSM.getc(); |
tony63 | 0:4676a90107ca | 59 | if (c == '\r' || c == '\n') c = '$'; |
tony63 | 0:4676a90107ca | 60 | buffer[i++] = c; |
tony63 | 0:4676a90107ca | 61 | if(i > count)break; |
tony63 | 0:4676a90107ca | 62 | } |
tony63 | 0:4676a90107ca | 63 | if(i > count)break; |
tony63 | 0:4676a90107ca | 64 | if(t.read() > 3) { |
tony63 | 0:4676a90107ca | 65 | t.stop(); |
tony63 | 0:4676a90107ca | 66 | t.reset(); |
tony63 | 0:4676a90107ca | 67 | break; |
tony63 | 0:4676a90107ca | 68 | } |
tony63 | 0:4676a90107ca | 69 | } |
tony63 | 0:4676a90107ca | 70 | wait(0.5); |
tony63 | 0:4676a90107ca | 71 | while(GSM.readable()) { |
tony63 | 0:4676a90107ca | 72 | char c = GSM.getc(); |
tony63 | 0:4676a90107ca | 73 | } |
tony63 | 0:4676a90107ca | 74 | return 0; |
tony63 | 0:4676a90107ca | 75 | } |
tony63 | 0:4676a90107ca | 76 | |
tony63 | 0:4676a90107ca | 77 | void cleanBuffer(char *buffer, int count) |
tony63 | 0:4676a90107ca | 78 | { |
tony63 | 0:4676a90107ca | 79 | for(int i=0; i < count; i++) { |
tony63 | 0:4676a90107ca | 80 | buffer[i] = '\0'; |
tony63 | 0:4676a90107ca | 81 | } |
tony63 | 0:4676a90107ca | 82 | } |
tony63 | 0:4676a90107ca | 83 | |
tony63 | 0:4676a90107ca | 84 | void sendCmd(char *cmd) |
tony63 | 0:4676a90107ca | 85 | { |
tony63 | 0:4676a90107ca | 86 | GSM.puts(cmd); |
tony63 | 0:4676a90107ca | 87 | } |
tony63 | 0:4676a90107ca | 88 | |
tony63 | 0:4676a90107ca | 89 | int waitForResp(char *resp, int timeout) |
tony63 | 0:4676a90107ca | 90 | { |
tony63 | 0:4676a90107ca | 91 | int len = strlen(resp); |
tony63 | 0:4676a90107ca | 92 | int sum=0; |
tony63 | 0:4676a90107ca | 93 | t.start(); |
tony63 | 0:4676a90107ca | 94 | |
tony63 | 0:4676a90107ca | 95 | while(1) { |
tony63 | 0:4676a90107ca | 96 | if(GSM.readable()) { |
tony63 | 0:4676a90107ca | 97 | char c = GSM.getc(); |
tony63 | 0:4676a90107ca | 98 | sum = (c==resp[sum]) ? sum+1 : 0; |
tony63 | 0:4676a90107ca | 99 | if(sum == len)break; |
tony63 | 0:4676a90107ca | 100 | } |
tony63 | 0:4676a90107ca | 101 | if(t.read() > timeout) { |
tony63 | 0:4676a90107ca | 102 | t.stop(); |
tony63 | 0:4676a90107ca | 103 | t.reset(); |
tony63 | 0:4676a90107ca | 104 | return -1; |
tony63 | 0:4676a90107ca | 105 | } |
tony63 | 0:4676a90107ca | 106 | } |
tony63 | 0:4676a90107ca | 107 | t.stop(); |
tony63 | 0:4676a90107ca | 108 | t.reset(); |
tony63 | 0:4676a90107ca | 109 | while(GSM.readable()) { |
tony63 | 0:4676a90107ca | 110 | char c = GSM.getc(); |
tony63 | 0:4676a90107ca | 111 | } |
tony63 | 0:4676a90107ca | 112 | |
tony63 | 0:4676a90107ca | 113 | return 0; |
tony63 | 0:4676a90107ca | 114 | } |
tony63 | 0:4676a90107ca | 115 | |
tony63 | 0:4676a90107ca | 116 | int sendCmdAndWaitForResp(char *cmd, char *resp, int timeout) |
tony63 | 0:4676a90107ca | 117 | { |
tony63 | 0:4676a90107ca | 118 | sendCmd(cmd); |
tony63 | 0:4676a90107ca | 119 | return waitForResp(resp,timeout); |
tony63 | 0:4676a90107ca | 120 | } |
tony63 | 0:4676a90107ca | 121 | |
tony63 | 0:4676a90107ca | 122 | int powerCheck(void) |
tony63 | 0:4676a90107ca | 123 | { |
tony63 | 0:4676a90107ca | 124 | return sendCmdAndWaitForResp("AT\r\n", "OK", 2); |
tony63 | 0:4676a90107ca | 125 | } |
tony63 | 0:4676a90107ca | 126 | |
tony63 | 0:4676a90107ca | 127 | int checkSIMStatus(void) |
tony63 | 0:4676a90107ca | 128 | { |
tony63 | 0:4676a90107ca | 129 | char gprsBuffer[30]; |
tony63 | 0:4676a90107ca | 130 | int count = 0; |
tony63 | 0:4676a90107ca | 131 | cleanBuffer(gprsBuffer,30); |
tony63 | 0:4676a90107ca | 132 | while(count < 3) { |
tony63 | 0:4676a90107ca | 133 | sendCmd("AT+CPIN?\r\n"); |
tony63 | 0:4676a90107ca | 134 | readBuffer(gprsBuffer,30); |
tony63 | 0:4676a90107ca | 135 | if((NULL != strstr(gprsBuffer,"+CPIN: READY"))) { |
tony63 | 0:4676a90107ca | 136 | break; |
tony63 | 0:4676a90107ca | 137 | } |
tony63 | 0:4676a90107ca | 138 | count++; |
tony63 | 0:4676a90107ca | 139 | wait(1); |
tony63 | 0:4676a90107ca | 140 | } |
tony63 | 0:4676a90107ca | 141 | |
tony63 | 0:4676a90107ca | 142 | if(count == 3) { |
tony63 | 0:4676a90107ca | 143 | return -1; |
tony63 | 0:4676a90107ca | 144 | } |
tony63 | 0:4676a90107ca | 145 | return 0; |
tony63 | 0:4676a90107ca | 146 | } |
tony63 | 0:4676a90107ca | 147 | |
tony63 | 0:4676a90107ca | 148 | int checkSignalStrength(void) |
tony63 | 0:4676a90107ca | 149 | { |
tony63 | 0:4676a90107ca | 150 | char gprsBuffer[100]; |
tony63 | 0:4676a90107ca | 151 | int index,count = 0; |
tony63 | 0:4676a90107ca | 152 | cleanBuffer(gprsBuffer,100); |
tony63 | 0:4676a90107ca | 153 | while(count < 3) { |
tony63 | 0:4676a90107ca | 154 | sendCmd("AT+CSQ\r\n"); |
tony63 | 0:4676a90107ca | 155 | readBuffer(gprsBuffer,25); |
tony63 | 0:4676a90107ca | 156 | if(sscanf(gprsBuffer, "AT+CSQ$$$$+CSQ: %d", &index)>0) { |
tony63 | 0:4676a90107ca | 157 | break; |
tony63 | 0:4676a90107ca | 158 | } |
tony63 | 0:4676a90107ca | 159 | count++; |
tony63 | 0:4676a90107ca | 160 | wait(1); |
tony63 | 0:4676a90107ca | 161 | } |
tony63 | 0:4676a90107ca | 162 | if(count == 3) { |
tony63 | 0:4676a90107ca | 163 | return -1; |
tony63 | 0:4676a90107ca | 164 | } |
tony63 | 0:4676a90107ca | 165 | return index; |
tony63 | 0:4676a90107ca | 166 | } |
tony63 | 0:4676a90107ca | 167 | |
tony63 | 0:4676a90107ca | 168 | int init() |
tony63 | 0:4676a90107ca | 169 | { |
tony63 | 0:4676a90107ca | 170 | for(int i = 0; i < 3; i++){ |
tony63 | 0:4676a90107ca | 171 | sendCmdAndWaitForResp("AT\r\n", "OK", 1); |
tony63 | 0:4676a90107ca | 172 | wait(0.5); |
tony63 | 0:4676a90107ca | 173 | } |
tony63 | 0:4676a90107ca | 174 | if(0 != checkSIMStatus()) { |
tony63 | 0:4676a90107ca | 175 | return -1; |
tony63 | 0:4676a90107ca | 176 | } |
tony63 | 0:4676a90107ca | 177 | if(checkSignalStrength()<1) { |
tony63 | 0:4676a90107ca | 178 | return -1; |
tony63 | 0:4676a90107ca | 179 | } |
tony63 | 0:4676a90107ca | 180 | |
tony63 | 0:4676a90107ca | 181 | GSM.attach(&Rx_interrupt, Serial::RxIrq); |
tony63 | 0:4676a90107ca | 182 | return 0; |
tony63 | 0:4676a90107ca | 183 | } |
tony63 | 0:4676a90107ca | 184 | |
tony63 | 0:4676a90107ca | 185 | int readSMSpdu(char *message, int index) |
tony63 | 0:4676a90107ca | 186 | { |
tony63 | 0:4676a90107ca | 187 | int i = 0; |
tony63 | 0:4676a90107ca | 188 | char gprsBuffer[100]; |
tony63 | 0:4676a90107ca | 189 | char *p,*s; |
tony63 | 0:4676a90107ca | 190 | GSM.printf("AT+CMGR=%d\r\n",index); |
tony63 | 0:4676a90107ca | 191 | cleanBuffer(gprsBuffer,100); |
tony63 | 0:4676a90107ca | 192 | readBuffer(gprsBuffer,100); |
tony63 | 0:4676a90107ca | 193 | if(NULL == ( s = strstr(gprsBuffer,"+CMGR"))) { |
tony63 | 0:4676a90107ca | 194 | return -1; |
tony63 | 0:4676a90107ca | 195 | } |
tony63 | 0:4676a90107ca | 196 | if(NULL != ( s = strstr(gprsBuffer,"+32"))) { |
tony63 | 0:4676a90107ca | 197 | p = s + 6; |
tony63 | 0:4676a90107ca | 198 | while((*p != '$')&&(i < 5)) { |
tony63 | 0:4676a90107ca | 199 | message[i++] = *(p++); |
tony63 | 0:4676a90107ca | 200 | } |
tony63 | 0:4676a90107ca | 201 | message[i] = '\0'; |
tony63 | 0:4676a90107ca | 202 | } |
tony63 | 0:4676a90107ca | 203 | return 0; |
tony63 | 0:4676a90107ca | 204 | } |
tony63 | 0:4676a90107ca | 205 | |
tony63 | 0:4676a90107ca | 206 | int deleteSMS(int index) |
tony63 | 0:4676a90107ca | 207 | { |
tony63 | 0:4676a90107ca | 208 | char cmd[32]; |
tony63 | 0:4676a90107ca | 209 | snprintf(cmd,sizeof(cmd),"AT+CMGD=%d\r\n",index); |
tony63 | 0:4676a90107ca | 210 | sendCmd(cmd); |
tony63 | 0:4676a90107ca | 211 | return 0; |
tony63 | 0:4676a90107ca | 212 | } |
tony63 | 0:4676a90107ca | 213 | |
tony63 | 0:4676a90107ca | 214 | void Rx_interrupt(){ |
tony63 | 0:4676a90107ca | 215 | } |
tony63 | 0:4676a90107ca | 216 | |
tony63 | 0:4676a90107ca | 217 | int main(void) |
tony63 | 0:4676a90107ca | 218 | { |
tony63 | 0:4676a90107ca | 219 | LedVerde=1; |
tony63 | 0:4676a90107ca | 220 | LedRojo=1; |
tony63 | 0:4676a90107ca | 221 | LedAzul=1; |
tony63 | 0:4676a90107ca | 222 | GSM.baud(9600); |
tony63 | 0:4676a90107ca | 223 | GSM.format(8,Serial::None,1); |
tony63 | 0:4676a90107ca | 224 | |
tony63 | 0:4676a90107ca | 225 | |
tony63 | 0:4676a90107ca | 226 | while(1){ |
tony63 | 0:4676a90107ca | 227 | if (button1.falling()) |
tony63 | 0:4676a90107ca | 228 | { |
tony63 | 0:4676a90107ca | 229 | index1=!index1; |
tony63 | 0:4676a90107ca | 230 | if (index1==0){ |
tony63 | 0:4676a90107ca | 231 | GSM.printf("VERDEON");} |
tony63 | 0:4676a90107ca | 232 | else{ |
tony63 | 0:4676a90107ca | 233 | GSM.printf("VERDEOF"); |
tony63 | 0:4676a90107ca | 234 | } |
tony63 | 0:4676a90107ca | 235 | } |
tony63 | 0:4676a90107ca | 236 | if (button2.falling()) |
tony63 | 0:4676a90107ca | 237 | { |
tony63 | 0:4676a90107ca | 238 | index2=!index2; |
tony63 | 0:4676a90107ca | 239 | if (index2==0){ |
tony63 | 0:4676a90107ca | 240 | GSM.printf("ROJOON");} |
tony63 | 0:4676a90107ca | 241 | else{ |
tony63 | 0:4676a90107ca | 242 | GSM.printf("ROJOOF"); |
tony63 | 0:4676a90107ca | 243 | } |
tony63 | 0:4676a90107ca | 244 | } |
tony63 | 0:4676a90107ca | 245 | |
tony63 | 0:4676a90107ca | 246 | if (button3.falling()) |
tony63 | 0:4676a90107ca | 247 | { |
tony63 | 0:4676a90107ca | 248 | index3=!index3; |
tony63 | 0:4676a90107ca | 249 | if (index3==0){ |
tony63 | 0:4676a90107ca | 250 | GSM.printf("AZULON");} |
tony63 | 0:4676a90107ca | 251 | else{ |
tony63 | 0:4676a90107ca | 252 | GSM.printf("AZULOF"); |
tony63 | 0:4676a90107ca | 253 | } |
tony63 | 0:4676a90107ca | 254 | } |
tony63 | 0:4676a90107ca | 255 | |
tony63 | 0:4676a90107ca | 256 | |
tony63 | 0:4676a90107ca | 257 | |
tony63 | 0:4676a90107ca | 258 | |
tony63 | 0:4676a90107ca | 259 | |
tony63 | 0:4676a90107ca | 260 | |
tony63 | 0:4676a90107ca | 261 | |
tony63 | 0:4676a90107ca | 262 | if (GSM.readable()) { |
tony63 | 0:4676a90107ca | 263 | readBuffer(buffer,100); |
tony63 | 0:4676a90107ca | 264 | pc.printf("buffer= %s\n\r ",buffer); |
tony63 | 0:4676a90107ca | 265 | pc.printf("buffer= %c %c\n\r ",buffer[10],buffer[11]); |
tony63 | 0:4676a90107ca | 266 | if(buffer[67]=='A'){for(i=0;i<86;i++) |
tony63 | 0:4676a90107ca | 267 | {buffermsg[i]=buffer[i];} |
tony63 | 0:4676a90107ca | 268 | pc.printf("mensaje= %s\n\r ",buffermsg); |
tony63 | 0:4676a90107ca | 269 | pc.printf("mensaje[72]= %c mensaje[73]=%c\n\r ",buffermsg[72],buffermsg[73]); |
tony63 | 0:4676a90107ca | 270 | buffer[67]='c'; |
tony63 | 0:4676a90107ca | 271 | } |
tony63 | 0:4676a90107ca | 272 | |
tony63 | 0:4676a90107ca | 273 | if(buffer[10]=='S'&& buffer[11]=='M'){ |
tony63 | 0:4676a90107ca | 274 | for(i=0;i<5;i++) |
tony63 | 0:4676a90107ca | 275 | {buffer1[i]=buffer[2+i];} |
tony63 | 0:4676a90107ca | 276 | pc.printf("buffer1= %s\n\r ",buffer1); |
tony63 | 0:4676a90107ca | 277 | buffer[10]='c'; |
tony63 | 0:4676a90107ca | 278 | buffer[11]='c'; |
tony63 | 0:4676a90107ca | 279 | } |
tony63 | 0:4676a90107ca | 280 | if(buffer1[3]=='T'){pc.printf("AT+CMGL=0\n\r"); |
tony63 | 0:4676a90107ca | 281 | wait(0.5); |
tony63 | 0:4676a90107ca | 282 | GSM.printf("AT+CMGL=0\r\n"); |
tony63 | 0:4676a90107ca | 283 | buffer1[3]='p'; |
tony63 | 0:4676a90107ca | 284 | } |
tony63 | 0:4676a90107ca | 285 | |
tony63 | 0:4676a90107ca | 286 | |
tony63 | 0:4676a90107ca | 287 | // CC3219642FCBC965 ESTO SIGNIFICA "Led verde" si mandan ese mensaje de texto se prende el led azul |
tony63 | 0:4676a90107ca | 288 | |
tony63 | 0:4676a90107ca | 289 | 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:4676a90107ca | 290 | if(buffermsg[72]=='7' && buffermsg[73]=='3'){LedAzul=1;} //apaga con Lgeverde |
tony63 | 0:4676a90107ca | 291 | |
tony63 | 0:4676a90107ca | 292 | |
tony63 | 0:4676a90107ca | 293 | } |
tony63 | 0:4676a90107ca | 294 | } |
tony63 | 0:4676a90107ca | 295 | } |