Este programa se encarga de leer voltaje y pwm, usando Gsm . Se puede visualizar el voltaje desde mensaje de texto.
main.cpp@0:239b65262a52, 2017-12-01 (annotated)
- Committer:
- steffania1596
- Date:
- Fri Dec 01 16:50:41 2017 +0000
- Revision:
- 0:239b65262a52
Este codigo es la tarea de procesadores sobre el gsm, para medir voltaje y Pwm.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
steffania1596 | 0:239b65262a52 | 1 | #include "mbed.h" |
steffania1596 | 0:239b65262a52 | 2 | #include "stdio.h" |
steffania1596 | 0:239b65262a52 | 3 | #include "string.h" |
steffania1596 | 0:239b65262a52 | 4 | #include "GPS.h" |
steffania1596 | 0:239b65262a52 | 5 | |
steffania1596 | 0:239b65262a52 | 6 | Timer t; |
steffania1596 | 0:239b65262a52 | 7 | DigitalOut LedVerde(LED2); |
steffania1596 | 0:239b65262a52 | 8 | PwmOut LedRojo(LED1); |
steffania1596 | 0:239b65262a52 | 9 | DigitalOut LedAzul(LED3); |
steffania1596 | 0:239b65262a52 | 10 | AnalogIn in(PTB0); |
steffania1596 | 0:239b65262a52 | 11 | PwmOut Pwm1(PTD1); |
steffania1596 | 0:239b65262a52 | 12 | |
steffania1596 | 0:239b65262a52 | 13 | |
steffania1596 | 0:239b65262a52 | 14 | Serial GSM(PTE0,PTE1); |
steffania1596 | 0:239b65262a52 | 15 | Serial pc(USBTX,USBRX); |
steffania1596 | 0:239b65262a52 | 16 | |
steffania1596 | 0:239b65262a52 | 17 | char DEvol[255]; |
steffania1596 | 0:239b65262a52 | 18 | char DSvol[255]; |
steffania1596 | 0:239b65262a52 | 19 | char buffer[512]; |
steffania1596 | 0:239b65262a52 | 20 | char resp[6]; |
steffania1596 | 0:239b65262a52 | 21 | char tam[2]; |
steffania1596 | 0:239b65262a52 | 22 | char mensaje[100]; |
steffania1596 | 0:239b65262a52 | 23 | int duty; |
steffania1596 | 0:239b65262a52 | 24 | float pwm; |
steffania1596 | 0:239b65262a52 | 25 | |
steffania1596 | 0:239b65262a52 | 26 | int i, K, C, LENOUTvol, LENINvol; |
steffania1596 | 0:239b65262a52 | 27 | int c=0; |
steffania1596 | 0:239b65262a52 | 28 | char msg[256]; |
steffania1596 | 0:239b65262a52 | 29 | char char1; |
steffania1596 | 0:239b65262a52 | 30 | int cmgs; |
steffania1596 | 0:239b65262a52 | 31 | char out[16], OtroOut[16]; |
steffania1596 | 0:239b65262a52 | 32 | int ret = 1; |
steffania1596 | 0:239b65262a52 | 33 | char tel[15]; |
steffania1596 | 0:239b65262a52 | 34 | float vol,yolo; |
steffania1596 | 0:239b65262a52 | 35 | char volt[255]; |
steffania1596 | 0:239b65262a52 | 36 | char buf[100]; |
steffania1596 | 0:239b65262a52 | 37 | char trama1[] = "0011000A91"; |
steffania1596 | 0:239b65262a52 | 38 | char trama2[] = "0000AA"; |
steffania1596 | 0:239b65262a52 | 39 | |
steffania1596 | 0:239b65262a52 | 40 | void reverse(char *str, int len) |
steffania1596 | 0:239b65262a52 | 41 | { |
steffania1596 | 0:239b65262a52 | 42 | int i=0, j=len-1, temp; |
steffania1596 | 0:239b65262a52 | 43 | while (i<j) |
steffania1596 | 0:239b65262a52 | 44 | { |
steffania1596 | 0:239b65262a52 | 45 | temp = str[i]; |
steffania1596 | 0:239b65262a52 | 46 | str[i] = str[j]; |
steffania1596 | 0:239b65262a52 | 47 | str[j] = temp; |
steffania1596 | 0:239b65262a52 | 48 | i++; j--; |
steffania1596 | 0:239b65262a52 | 49 | } |
steffania1596 | 0:239b65262a52 | 50 | } |
steffania1596 | 0:239b65262a52 | 51 | |
steffania1596 | 0:239b65262a52 | 52 | // Converts a given integer x to string str[]. d is the number |
steffania1596 | 0:239b65262a52 | 53 | // of digits required in output. If d is more than the number |
steffania1596 | 0:239b65262a52 | 54 | // of digits in x, then 0s are added at the beginning. |
steffania1596 | 0:239b65262a52 | 55 | int intToStr(int x, char str[], int d) |
steffania1596 | 0:239b65262a52 | 56 | { |
steffania1596 | 0:239b65262a52 | 57 | int i = 0; |
steffania1596 | 0:239b65262a52 | 58 | while (x) |
steffania1596 | 0:239b65262a52 | 59 | { |
steffania1596 | 0:239b65262a52 | 60 | str[i++] = (x%10) + '0'; |
steffania1596 | 0:239b65262a52 | 61 | x = x/10; |
steffania1596 | 0:239b65262a52 | 62 | } |
steffania1596 | 0:239b65262a52 | 63 | |
steffania1596 | 0:239b65262a52 | 64 | // If number of digits required is more, then |
steffania1596 | 0:239b65262a52 | 65 | // add 0s at the beginning |
steffania1596 | 0:239b65262a52 | 66 | while (i < d) |
steffania1596 | 0:239b65262a52 | 67 | str[i++] = '0'; |
steffania1596 | 0:239b65262a52 | 68 | |
steffania1596 | 0:239b65262a52 | 69 | reverse(str, i); |
steffania1596 | 0:239b65262a52 | 70 | str[i] = '\0'; |
steffania1596 | 0:239b65262a52 | 71 | return i; |
steffania1596 | 0:239b65262a52 | 72 | } |
steffania1596 | 0:239b65262a52 | 73 | |
steffania1596 | 0:239b65262a52 | 74 | // Converts a floating point number to string. |
steffania1596 | 0:239b65262a52 | 75 | void ftoa(float n, char *res, int afterpoint) |
steffania1596 | 0:239b65262a52 | 76 | { |
steffania1596 | 0:239b65262a52 | 77 | // Extract integer part |
steffania1596 | 0:239b65262a52 | 78 | int ipart = (int)n; |
steffania1596 | 0:239b65262a52 | 79 | |
steffania1596 | 0:239b65262a52 | 80 | // Extract floating part |
steffania1596 | 0:239b65262a52 | 81 | float fpart = n - (float)ipart; |
steffania1596 | 0:239b65262a52 | 82 | |
steffania1596 | 0:239b65262a52 | 83 | // convert integer part to string |
steffania1596 | 0:239b65262a52 | 84 | int i = intToStr(ipart, res, 0); |
steffania1596 | 0:239b65262a52 | 85 | |
steffania1596 | 0:239b65262a52 | 86 | // check for display option after point |
steffania1596 | 0:239b65262a52 | 87 | if (afterpoint != 0) |
steffania1596 | 0:239b65262a52 | 88 | { |
steffania1596 | 0:239b65262a52 | 89 | res[i] = '.'; // add dot |
steffania1596 | 0:239b65262a52 | 90 | |
steffania1596 | 0:239b65262a52 | 91 | // Get the value of fraction part upto given no. |
steffania1596 | 0:239b65262a52 | 92 | // of points after dot. The third parameter is needed |
steffania1596 | 0:239b65262a52 | 93 | // to handle cases like 233.007 |
steffania1596 | 0:239b65262a52 | 94 | float fp=10; |
steffania1596 | 0:239b65262a52 | 95 | fpart =fpart * pow(fp,afterpoint); |
steffania1596 | 0:239b65262a52 | 96 | |
steffania1596 | 0:239b65262a52 | 97 | intToStr((int)fpart, res + i + 1, afterpoint); |
steffania1596 | 0:239b65262a52 | 98 | } |
steffania1596 | 0:239b65262a52 | 99 | } |
steffania1596 | 0:239b65262a52 | 100 | |
steffania1596 | 0:239b65262a52 | 101 | void FlushGSM(void) { |
steffania1596 | 0:239b65262a52 | 102 | char1 = 0; |
steffania1596 | 0:239b65262a52 | 103 | while (GSM.readable()){ |
steffania1596 | 0:239b65262a52 | 104 | char1 = GSM.getc(); |
steffania1596 | 0:239b65262a52 | 105 | } |
steffania1596 | 0:239b65262a52 | 106 | return; |
steffania1596 | 0:239b65262a52 | 107 | } |
steffania1596 | 0:239b65262a52 | 108 | |
steffania1596 | 0:239b65262a52 | 109 | void callback(){ |
steffania1596 | 0:239b65262a52 | 110 | // Note: you need to actually read from the serial to clear the RX interrupt |
steffania1596 | 0:239b65262a52 | 111 | pc.printf("%c\n", GSM.getc()); |
steffania1596 | 0:239b65262a52 | 112 | } |
steffania1596 | 0:239b65262a52 | 113 | |
steffania1596 | 0:239b65262a52 | 114 | // Esta funcion de abajo lee todo un bufer hasta encontrar CR o LF y el resto lo rellena de |
steffania1596 | 0:239b65262a52 | 115 | // $, count es lo que va a leer. Lo leido lo mete en buffer que es una cadena previamente definida |
steffania1596 | 0:239b65262a52 | 116 | // incorpora medida de tiempo si se demora mas de tres segundos retorna fracaso con -1 |
steffania1596 | 0:239b65262a52 | 117 | int readBuffer(char *buffer,int count){ |
steffania1596 | 0:239b65262a52 | 118 | int i=0; |
steffania1596 | 0:239b65262a52 | 119 | t.start(); // start timer |
steffania1596 | 0:239b65262a52 | 120 | while(1) { |
steffania1596 | 0:239b65262a52 | 121 | while (GSM.readable()) { |
steffania1596 | 0:239b65262a52 | 122 | char c = GSM.getc(); |
steffania1596 | 0:239b65262a52 | 123 | if (c == '\r' || c == '\n') c = '$'; |
steffania1596 | 0:239b65262a52 | 124 | buffer[i++] = c; |
steffania1596 | 0:239b65262a52 | 125 | if(i > count)break; |
steffania1596 | 0:239b65262a52 | 126 | } |
steffania1596 | 0:239b65262a52 | 127 | if(i > count)break; |
steffania1596 | 0:239b65262a52 | 128 | if(t.read() > 3) { |
steffania1596 | 0:239b65262a52 | 129 | t.stop(); |
steffania1596 | 0:239b65262a52 | 130 | t.reset(); |
steffania1596 | 0:239b65262a52 | 131 | break; |
steffania1596 | 0:239b65262a52 | 132 | } |
steffania1596 | 0:239b65262a52 | 133 | } |
steffania1596 | 0:239b65262a52 | 134 | wait(0.5); |
steffania1596 | 0:239b65262a52 | 135 | while(GSM.readable()){ // display the other thing.. |
steffania1596 | 0:239b65262a52 | 136 | char c = GSM.getc(); |
steffania1596 | 0:239b65262a52 | 137 | } |
steffania1596 | 0:239b65262a52 | 138 | return 0; |
steffania1596 | 0:239b65262a52 | 139 | } |
steffania1596 | 0:239b65262a52 | 140 | |
steffania1596 | 0:239b65262a52 | 141 | // Esta función de abajo limpia o borra todo un "buffer" de tamaño "count", |
steffania1596 | 0:239b65262a52 | 142 | // lo revisa elemento por elemento y le mete el caracter null que indica fin de cadena. |
steffania1596 | 0:239b65262a52 | 143 | // No retorna nada. |
steffania1596 | 0:239b65262a52 | 144 | void cleanBuffer(char *buffer, int count){ |
steffania1596 | 0:239b65262a52 | 145 | for(int i=0; i < count; i++) { |
steffania1596 | 0:239b65262a52 | 146 | buffer[i] = '\0'; |
steffania1596 | 0:239b65262a52 | 147 | } |
steffania1596 | 0:239b65262a52 | 148 | } |
steffania1596 | 0:239b65262a52 | 149 | |
steffania1596 | 0:239b65262a52 | 150 | // Esta función de abajo envia un comando parametrizado como cadena |
steffania1596 | 0:239b65262a52 | 151 | // puede ser un comando tipo AT. |
steffania1596 | 0:239b65262a52 | 152 | void sendCmd(char *cmd){ |
steffania1596 | 0:239b65262a52 | 153 | GSM.puts(cmd); |
steffania1596 | 0:239b65262a52 | 154 | } |
steffania1596 | 0:239b65262a52 | 155 | |
steffania1596 | 0:239b65262a52 | 156 | // Esta función de abajo espera la respuesta de un comando que debe ser idéntica a la cadena "resp" y un tiempo "timeout", |
steffania1596 | 0:239b65262a52 | 157 | // si todo sale bien retorna un cero que en la programacion hay que validar, |
steffania1596 | 0:239b65262a52 | 158 | // si algo sale mal (no se parece o se demora mucho) retorna -1 que debera validarse con alguna expresion logica. |
steffania1596 | 0:239b65262a52 | 159 | int waitForResp(char *resp, int timeout){ |
steffania1596 | 0:239b65262a52 | 160 | int len = strlen(resp); |
steffania1596 | 0:239b65262a52 | 161 | int sum=0; |
steffania1596 | 0:239b65262a52 | 162 | t.start(); |
steffania1596 | 0:239b65262a52 | 163 | |
steffania1596 | 0:239b65262a52 | 164 | while(1) { |
steffania1596 | 0:239b65262a52 | 165 | if(GSM.readable()) { |
steffania1596 | 0:239b65262a52 | 166 | char c = GSM.getc(); |
steffania1596 | 0:239b65262a52 | 167 | sum = (c==resp[sum]) ? sum+1 : 0;// esta linea de C# sum se incrementa o se hace cero segun c |
steffania1596 | 0:239b65262a52 | 168 | if(sum == len)break; //ya acabo se sale |
steffania1596 | 0:239b65262a52 | 169 | } |
steffania1596 | 0:239b65262a52 | 170 | if(t.read() > timeout) { // time out chequea el tiempo minimo antes de salir perdiendo |
steffania1596 | 0:239b65262a52 | 171 | t.stop(); |
steffania1596 | 0:239b65262a52 | 172 | t.reset(); |
steffania1596 | 0:239b65262a52 | 173 | return -1; |
steffania1596 | 0:239b65262a52 | 174 | } |
steffania1596 | 0:239b65262a52 | 175 | } |
steffania1596 | 0:239b65262a52 | 176 | t.stop(); // stop timer antes de retornar |
steffania1596 | 0:239b65262a52 | 177 | t.reset(); // clear timer |
steffania1596 | 0:239b65262a52 | 178 | while(GSM.readable()) { // display the other thing.. |
steffania1596 | 0:239b65262a52 | 179 | char c = GSM.getc(); |
steffania1596 | 0:239b65262a52 | 180 | } |
steffania1596 | 0:239b65262a52 | 181 | return 0; |
steffania1596 | 0:239b65262a52 | 182 | } |
steffania1596 | 0:239b65262a52 | 183 | |
steffania1596 | 0:239b65262a52 | 184 | // Esta función de abajo es muy completa y útil, se encarga de enviar el comando y esperar la respuesta. |
steffania1596 | 0:239b65262a52 | 185 | // Si todo sale bien retorna un cero (herencia de las funciones contenedoras) que en la programacion hay que validar |
steffania1596 | 0:239b65262a52 | 186 | // con alguna expresion lógica. |
steffania1596 | 0:239b65262a52 | 187 | int sendCmdAndWaitForResp(char *cmd, char *resp, int timeout){ |
steffania1596 | 0:239b65262a52 | 188 | sendCmd(cmd); |
steffania1596 | 0:239b65262a52 | 189 | return waitForResp(resp,timeout); |
steffania1596 | 0:239b65262a52 | 190 | } |
steffania1596 | 0:239b65262a52 | 191 | |
steffania1596 | 0:239b65262a52 | 192 | // Esta función de abajo chequea que el módem este vivo, envia AT, le contesta con OK y espera 2 segundos. |
steffania1596 | 0:239b65262a52 | 193 | int powerCheck(void){ // Este comando se manda para verificar si el módem esta vivo o conectado. |
steffania1596 | 0:239b65262a52 | 194 | return sendCmdAndWaitForResp("AT\r\n", "OK", 2); |
steffania1596 | 0:239b65262a52 | 195 | } |
steffania1596 | 0:239b65262a52 | 196 | |
steffania1596 | 0:239b65262a52 | 197 | // Esta función de abajo chequea el estado de la sim card |
steffania1596 | 0:239b65262a52 | 198 | // y si todo sale bien retorna un cero que en la programacion hay que validar |
steffania1596 | 0:239b65262a52 | 199 | // con alguna expresión lógica. |
steffania1596 | 0:239b65262a52 | 200 | int checkSIMStatus(void){ |
steffania1596 | 0:239b65262a52 | 201 | char gprsBuffer[30]; |
steffania1596 | 0:239b65262a52 | 202 | int count = 0; |
steffania1596 | 0:239b65262a52 | 203 | cleanBuffer(gprsBuffer, 30); |
steffania1596 | 0:239b65262a52 | 204 | while(count < 3){ |
steffania1596 | 0:239b65262a52 | 205 | sendCmd("AT+CPIN?\r\n"); |
steffania1596 | 0:239b65262a52 | 206 | readBuffer(gprsBuffer,30); |
steffania1596 | 0:239b65262a52 | 207 | if((NULL != strstr(gprsBuffer,"+CPIN: READY"))){ |
steffania1596 | 0:239b65262a52 | 208 | break; |
steffania1596 | 0:239b65262a52 | 209 | } |
steffania1596 | 0:239b65262a52 | 210 | count++; |
steffania1596 | 0:239b65262a52 | 211 | wait(1); |
steffania1596 | 0:239b65262a52 | 212 | } |
steffania1596 | 0:239b65262a52 | 213 | |
steffania1596 | 0:239b65262a52 | 214 | if(count == 3){ |
steffania1596 | 0:239b65262a52 | 215 | return -1; |
steffania1596 | 0:239b65262a52 | 216 | } |
steffania1596 | 0:239b65262a52 | 217 | return 0; |
steffania1596 | 0:239b65262a52 | 218 | } |
steffania1596 | 0:239b65262a52 | 219 | |
steffania1596 | 0:239b65262a52 | 220 | // Esta función de abajo chequea la calidad de la señal |
steffania1596 | 0:239b65262a52 | 221 | // y si todo sale bien retorna con el valor de señal útil o un -1 si no es aceptable, en la programacion hay que validar |
steffania1596 | 0:239b65262a52 | 222 | // con alguna expresión lógica. |
steffania1596 | 0:239b65262a52 | 223 | int checkSignalStrength(void){ |
steffania1596 | 0:239b65262a52 | 224 | char gprsBuffer[100]; |
steffania1596 | 0:239b65262a52 | 225 | int index, count = 0; |
steffania1596 | 0:239b65262a52 | 226 | cleanBuffer(gprsBuffer,100); |
steffania1596 | 0:239b65262a52 | 227 | while(count < 3){ |
steffania1596 | 0:239b65262a52 | 228 | sendCmd("AT+CSQ\r\n"); |
steffania1596 | 0:239b65262a52 | 229 | readBuffer(gprsBuffer,25); |
steffania1596 | 0:239b65262a52 | 230 | if(sscanf(gprsBuffer, "AT+CSQ$$$$+CSQ: %d", &index)>0) { |
steffania1596 | 0:239b65262a52 | 231 | break; |
steffania1596 | 0:239b65262a52 | 232 | } |
steffania1596 | 0:239b65262a52 | 233 | count++; |
steffania1596 | 0:239b65262a52 | 234 | wait(1); |
steffania1596 | 0:239b65262a52 | 235 | } |
steffania1596 | 0:239b65262a52 | 236 | if(count == 3){ |
steffania1596 | 0:239b65262a52 | 237 | return -1; |
steffania1596 | 0:239b65262a52 | 238 | } |
steffania1596 | 0:239b65262a52 | 239 | return index; |
steffania1596 | 0:239b65262a52 | 240 | } |
steffania1596 | 0:239b65262a52 | 241 | |
steffania1596 | 0:239b65262a52 | 242 | // Esta funcion de abajo inicaliza el módem. Se compone de un grupo de subfunciones ya definidas previamente |
steffania1596 | 0:239b65262a52 | 243 | // primero chequea que este vivo, |
steffania1596 | 0:239b65262a52 | 244 | // segundo chequea el estado de la simcard, |
steffania1596 | 0:239b65262a52 | 245 | // tercero chequea la intencidad de señal celular, |
steffania1596 | 0:239b65262a52 | 246 | // cuarto aplica la configuracion |
steffania1596 | 0:239b65262a52 | 247 | // y si todo sale bien retorna un cero que en la programacion hay que validar |
steffania1596 | 0:239b65262a52 | 248 | // con alguna expresión lógica. |
steffania1596 | 0:239b65262a52 | 249 | int init(){ |
steffania1596 | 0:239b65262a52 | 250 | if (0 != sendCmdAndWaitForResp("AT\r\n", "OK", 3)){ |
steffania1596 | 0:239b65262a52 | 251 | return -1; |
steffania1596 | 0:239b65262a52 | 252 | } |
steffania1596 | 0:239b65262a52 | 253 | if (0 != sendCmdAndWaitForResp("AT+CNMI=1,1\r\n", "OK", 3)){ |
steffania1596 | 0:239b65262a52 | 254 | return -1; |
steffania1596 | 0:239b65262a52 | 255 | } |
steffania1596 | 0:239b65262a52 | 256 | if (0 != sendCmdAndWaitForResp("AT+CMGF=0\r\n", "OK", 3)){ |
steffania1596 | 0:239b65262a52 | 257 | return -1; |
steffania1596 | 0:239b65262a52 | 258 | } |
steffania1596 | 0:239b65262a52 | 259 | if (0 != sendCmdAndWaitForResp("AT+CBST=7,0,1\r\n", "OK", 3)){ //velocidad fija a 9600, modem asincronico no transparente |
steffania1596 | 0:239b65262a52 | 260 | return -1; |
steffania1596 | 0:239b65262a52 | 261 | } |
steffania1596 | 0:239b65262a52 | 262 | if (0 != sendCmdAndWaitForResp("ATE\r\n", "OK", 3)){ //se le quita el eco al modem GSM |
steffania1596 | 0:239b65262a52 | 263 | return -1; |
steffania1596 | 0:239b65262a52 | 264 | } |
steffania1596 | 0:239b65262a52 | 265 | LedVerde=0; |
steffania1596 | 0:239b65262a52 | 266 | return 0; |
steffania1596 | 0:239b65262a52 | 267 | } |
steffania1596 | 0:239b65262a52 | 268 | |
steffania1596 | 0:239b65262a52 | 269 | // Esta funcion de abajo intenta leer un mensaje de texto en formato PDU o HEX |
steffania1596 | 0:239b65262a52 | 270 | // y si todo sale bien retorna un cero que en la programacion hay que validar |
steffania1596 | 0:239b65262a52 | 271 | // con alguna expresión lógica. |
steffania1596 | 0:239b65262a52 | 272 | int readSMSpdu(char *message, int index){ |
steffania1596 | 0:239b65262a52 | 273 | int i = 0; |
steffania1596 | 0:239b65262a52 | 274 | char gprsBuffer[100]; |
steffania1596 | 0:239b65262a52 | 275 | char *p,*s; |
steffania1596 | 0:239b65262a52 | 276 | GSM.printf("AT+CMGR=%d\r\n",index); |
steffania1596 | 0:239b65262a52 | 277 | cleanBuffer(gprsBuffer,100); |
steffania1596 | 0:239b65262a52 | 278 | readBuffer(gprsBuffer,100); |
steffania1596 | 0:239b65262a52 | 279 | if(NULL == ( s = strstr(gprsBuffer,"+CMGR"))) { |
steffania1596 | 0:239b65262a52 | 280 | return -1; |
steffania1596 | 0:239b65262a52 | 281 | } |
steffania1596 | 0:239b65262a52 | 282 | if(NULL != ( s = strstr(gprsBuffer,"+32"))) { |
steffania1596 | 0:239b65262a52 | 283 | p = s + 6; |
steffania1596 | 0:239b65262a52 | 284 | while((*p != '$')&&(i < 5)) { |
steffania1596 | 0:239b65262a52 | 285 | message[i++] = *(p++); |
steffania1596 | 0:239b65262a52 | 286 | } |
steffania1596 | 0:239b65262a52 | 287 | message[i] = '\0'; |
steffania1596 | 0:239b65262a52 | 288 | } |
steffania1596 | 0:239b65262a52 | 289 | return 0; |
steffania1596 | 0:239b65262a52 | 290 | } |
steffania1596 | 0:239b65262a52 | 291 | |
steffania1596 | 0:239b65262a52 | 292 | // Esta función de abajo borra mensajes SMS del modem |
steffania1596 | 0:239b65262a52 | 293 | // y si todo sale bien retorna un cero que en la programacion hay que validar |
steffania1596 | 0:239b65262a52 | 294 | // con alguna expresion logica. |
steffania1596 | 0:239b65262a52 | 295 | int deleteSMS(int index){ |
steffania1596 | 0:239b65262a52 | 296 | char cmd[32]; |
steffania1596 | 0:239b65262a52 | 297 | snprintf(cmd,sizeof(cmd),"AT+CMGD=%d\r\n",index); |
steffania1596 | 0:239b65262a52 | 298 | sendCmd(cmd); |
steffania1596 | 0:239b65262a52 | 299 | return 0; |
steffania1596 | 0:239b65262a52 | 300 | } |
steffania1596 | 0:239b65262a52 | 301 | |
steffania1596 | 0:239b65262a52 | 302 | |
steffania1596 | 0:239b65262a52 | 303 | int main(){ |
steffania1596 | 0:239b65262a52 | 304 | |
steffania1596 | 0:239b65262a52 | 305 | GSM.baud(9600); |
steffania1596 | 0:239b65262a52 | 306 | GSM.format(8,Serial::None,1); |
steffania1596 | 0:239b65262a52 | 307 | LedVerde = 1; // APAGO LOS LEDS |
steffania1596 | 0:239b65262a52 | 308 | //LedRojo = 1; |
steffania1596 | 0:239b65262a52 | 309 | LedAzul = 1; |
steffania1596 | 0:239b65262a52 | 310 | //LedRojo = 0; // PRENDO EL LED ROJO |
steffania1596 | 0:239b65262a52 | 311 | // Quito el eco del modem |
steffania1596 | 0:239b65262a52 | 312 | |
steffania1596 | 0:239b65262a52 | 313 | // CONFIGURACIÓN DEL MODEM GSM (TELEFONO CELULAR SIEMENS A56i). |
steffania1596 | 0:239b65262a52 | 314 | inicio1: |
steffania1596 | 0:239b65262a52 | 315 | ret = init(); |
steffania1596 | 0:239b65262a52 | 316 | if(ret==0){ |
steffania1596 | 0:239b65262a52 | 317 | //LedRojo = 1; |
steffania1596 | 0:239b65262a52 | 318 | LedVerde = 0; // Apagar LED Verde para confirmar la comunicación con el módem. |
steffania1596 | 0:239b65262a52 | 319 | pc.printf("Listo!\n"); |
steffania1596 | 0:239b65262a52 | 320 | } |
steffania1596 | 0:239b65262a52 | 321 | else{ |
steffania1596 | 0:239b65262a52 | 322 | wait(1); |
steffania1596 | 0:239b65262a52 | 323 | goto inicio1; |
steffania1596 | 0:239b65262a52 | 324 | } |
steffania1596 | 0:239b65262a52 | 325 | |
steffania1596 | 0:239b65262a52 | 326 | while(1){ |
steffania1596 | 0:239b65262a52 | 327 | if (GSM.readable()){ |
steffania1596 | 0:239b65262a52 | 328 | readBuffer(buffer,110); |
steffania1596 | 0:239b65262a52 | 329 | pc.printf("%s\r\n",buffer); |
steffania1596 | 0:239b65262a52 | 330 | for(i=0; i<5; i++){ |
steffania1596 | 0:239b65262a52 | 331 | resp[i] = buffer[i]; |
steffania1596 | 0:239b65262a52 | 332 | } |
steffania1596 | 0:239b65262a52 | 333 | |
steffania1596 | 0:239b65262a52 | 334 | pc.printf("%s\r\n", resp); |
steffania1596 | 0:239b65262a52 | 335 | if(strcmp("$$+CM", resp) == 0){ //COMPARA resp con "+CMTI" |
steffania1596 | 0:239b65262a52 | 336 | pc.printf("Llego MSG\r\n"); |
steffania1596 | 0:239b65262a52 | 337 | cleanBuffer(buffer,10); |
steffania1596 | 0:239b65262a52 | 338 | GSM.printf("AT+CMGL=0\r\n"); // Envío comando para leer mensaje |
steffania1596 | 0:239b65262a52 | 339 | pc.printf("AT+CMGL=0\r\n"); |
steffania1596 | 0:239b65262a52 | 340 | readBuffer(buffer,110); |
steffania1596 | 0:239b65262a52 | 341 | pc.printf("%s\r\n",buffer); |
steffania1596 | 0:239b65262a52 | 342 | |
steffania1596 | 0:239b65262a52 | 343 | // Lectura el teléfono emisor |
steffania1596 | 0:239b65262a52 | 344 | for(i=0; i<10; i++){ |
steffania1596 | 0:239b65262a52 | 345 | tel[i] = buffer[i+40]; |
steffania1596 | 0:239b65262a52 | 346 | } |
steffania1596 | 0:239b65262a52 | 347 | |
steffania1596 | 0:239b65262a52 | 348 | // Lectura del tamaño |
steffania1596 | 0:239b65262a52 | 349 | for(i=0;i<2;i++){ |
steffania1596 | 0:239b65262a52 | 350 | tam[i] = buffer[i + 68]; |
steffania1596 | 0:239b65262a52 | 351 | } |
steffania1596 | 0:239b65262a52 | 352 | pc.printf("%s-\r\n", tam); |
steffania1596 | 0:239b65262a52 | 353 | |
steffania1596 | 0:239b65262a52 | 354 | // Lectura del mensaje |
steffania1596 | 0:239b65262a52 | 355 | for(i=0;i<26;i++){ |
steffania1596 | 0:239b65262a52 | 356 | msg[i] = buffer[i+70]; // Lee un mensaje de 26 caracteres máximo desde la posición 70 del buffer. |
steffania1596 | 0:239b65262a52 | 357 | } |
steffania1596 | 0:239b65262a52 | 358 | pc.printf("%s-\r\n", msg); |
steffania1596 | 0:239b65262a52 | 359 | |
steffania1596 | 0:239b65262a52 | 360 | // Decodificación del mensaje |
steffania1596 | 0:239b65262a52 | 361 | |
steffania1596 | 0:239b65262a52 | 362 | // Comparar el mensaje |
steffania1596 | 0:239b65262a52 | 363 | deleteSMS(1); // Se borran los mensajes por medio de una función |
steffania1596 | 0:239b65262a52 | 364 | readBuffer(buffer, 200); |
steffania1596 | 0:239b65262a52 | 365 | //"D07B5B" es "Pwm".En PDU |
steffania1596 | 0:239b65262a52 | 366 | if((strncmp("D07B5B", msg, 6) == 0)){ |
steffania1596 | 0:239b65262a52 | 367 | |
steffania1596 | 0:239b65262a52 | 368 | //LedRojo = 0; |
steffania1596 | 0:239b65262a52 | 369 | LedVerde = 1; |
steffania1596 | 0:239b65262a52 | 370 | LedAzul = 0; |
steffania1596 | 0:239b65262a52 | 371 | wait(2); |
steffania1596 | 0:239b65262a52 | 372 | |
steffania1596 | 0:239b65262a52 | 373 | switch(msg[6]){ |
steffania1596 | 0:239b65262a52 | 374 | case ('0'): |
steffania1596 | 0:239b65262a52 | 375 | duty=10*0; |
steffania1596 | 0:239b65262a52 | 376 | break; |
steffania1596 | 0:239b65262a52 | 377 | case ('1'): |
steffania1596 | 0:239b65262a52 | 378 | duty=10*1; |
steffania1596 | 0:239b65262a52 | 379 | break; |
steffania1596 | 0:239b65262a52 | 380 | case ('2'): |
steffania1596 | 0:239b65262a52 | 381 | duty=10*2; |
steffania1596 | 0:239b65262a52 | 382 | break; |
steffania1596 | 0:239b65262a52 | 383 | case ('3'): |
steffania1596 | 0:239b65262a52 | 384 | duty=10*3; |
steffania1596 | 0:239b65262a52 | 385 | break; |
steffania1596 | 0:239b65262a52 | 386 | case ('4'): |
steffania1596 | 0:239b65262a52 | 387 | duty=10*4; |
steffania1596 | 0:239b65262a52 | 388 | break; |
steffania1596 | 0:239b65262a52 | 389 | case ('5'): |
steffania1596 | 0:239b65262a52 | 390 | duty=10*5; |
steffania1596 | 0:239b65262a52 | 391 | break; |
steffania1596 | 0:239b65262a52 | 392 | case ('6'): |
steffania1596 | 0:239b65262a52 | 393 | duty=10*6; |
steffania1596 | 0:239b65262a52 | 394 | break; |
steffania1596 | 0:239b65262a52 | 395 | case ('7'): |
steffania1596 | 0:239b65262a52 | 396 | duty=10*7; |
steffania1596 | 0:239b65262a52 | 397 | break; |
steffania1596 | 0:239b65262a52 | 398 | case ('8'): |
steffania1596 | 0:239b65262a52 | 399 | duty=10*8; |
steffania1596 | 0:239b65262a52 | 400 | break; |
steffania1596 | 0:239b65262a52 | 401 | case ('9'): |
steffania1596 | 0:239b65262a52 | 402 | duty=10*9; |
steffania1596 | 0:239b65262a52 | 403 | break; |
steffania1596 | 0:239b65262a52 | 404 | } |
steffania1596 | 0:239b65262a52 | 405 | |
steffania1596 | 0:239b65262a52 | 406 | if(msg[8]=='8' && msg[9]=='3'){duty=duty+0;} |
steffania1596 | 0:239b65262a52 | 407 | if(msg[8]=='8' && msg[9]=='B'){duty=duty+1;} |
steffania1596 | 0:239b65262a52 | 408 | if(msg[8]=='9' && msg[9]=='3'){duty=duty+2;} |
steffania1596 | 0:239b65262a52 | 409 | if(msg[8]=='9' && msg[9]=='B'){duty=duty+3;} |
steffania1596 | 0:239b65262a52 | 410 | if(msg[8]=='A' && msg[9]=='3'){duty=duty+4;} |
steffania1596 | 0:239b65262a52 | 411 | if(msg[8]=='A' && msg[9]=='B'){duty=duty+5;} |
steffania1596 | 0:239b65262a52 | 412 | if(msg[8]=='B' && msg[9]=='3'){duty=duty+6;} |
steffania1596 | 0:239b65262a52 | 413 | if(msg[8]=='B' && msg[9]=='B'){duty=duty+7;} |
steffania1596 | 0:239b65262a52 | 414 | if(msg[8]=='C' && msg[9]=='3'){duty=duty+8;} |
steffania1596 | 0:239b65262a52 | 415 | if(msg[8]=='C' && msg[9]=='B'){duty=duty+9;} |
steffania1596 | 0:239b65262a52 | 416 | |
steffania1596 | 0:239b65262a52 | 417 | pc.printf("%d\r\n", duty); |
steffania1596 | 0:239b65262a52 | 418 | |
steffania1596 | 0:239b65262a52 | 419 | pwm=float(duty)/100.0; |
steffania1596 | 0:239b65262a52 | 420 | pwm=1-pwm; |
steffania1596 | 0:239b65262a52 | 421 | LedVerde=1; |
steffania1596 | 0:239b65262a52 | 422 | LedAzul =1; |
steffania1596 | 0:239b65262a52 | 423 | LedRojo.period(5.0f); // 5 second period |
steffania1596 | 0:239b65262a52 | 424 | LedRojo.write(pwm); |
steffania1596 | 0:239b65262a52 | 425 | Pwm1.period(5.0f); |
steffania1596 | 0:239b65262a52 | 426 | Pwm1.write(pwm); |
steffania1596 | 0:239b65262a52 | 427 | |
steffania1596 | 0:239b65262a52 | 428 | } |
steffania1596 | 0:239b65262a52 | 429 | |
steffania1596 | 0:239b65262a52 | 430 | // "F6379B1E569701" es "voltaje". "F6379B1E569701" es "Voltaje". En PDU |
steffania1596 | 0:239b65262a52 | 431 | if((strncmp("F6379B1E569701", msg, 14) == 0) || (strncmp("D6379B1E569701", msg, 14) == 0)){ |
steffania1596 | 0:239b65262a52 | 432 | |
steffania1596 | 0:239b65262a52 | 433 | LedRojo = 0; |
steffania1596 | 0:239b65262a52 | 434 | LedVerde = 1; |
steffania1596 | 0:239b65262a52 | 435 | LedAzul = 0; |
steffania1596 | 0:239b65262a52 | 436 | wait(2); |
steffania1596 | 0:239b65262a52 | 437 | |
steffania1596 | 0:239b65262a52 | 438 | yolo = in.read()*3.3; |
steffania1596 | 0:239b65262a52 | 439 | vol =in.read(); |
steffania1596 | 0:239b65262a52 | 440 | pc.printf("\n%f\n", vol); |
steffania1596 | 0:239b65262a52 | 441 | |
steffania1596 | 0:239b65262a52 | 442 | cleanBuffer(out, 16); |
steffania1596 | 0:239b65262a52 | 443 | if (yolo < 1){ |
steffania1596 | 0:239b65262a52 | 444 | strcat(out, "0"); |
steffania1596 | 0:239b65262a52 | 445 | ftoa(yolo, OtroOut, 5); |
steffania1596 | 0:239b65262a52 | 446 | strcat(out, OtroOut); |
steffania1596 | 0:239b65262a52 | 447 | } |
steffania1596 | 0:239b65262a52 | 448 | else{ |
steffania1596 | 0:239b65262a52 | 449 | ftoa(yolo, out, 5); |
steffania1596 | 0:239b65262a52 | 450 | } |
steffania1596 | 0:239b65262a52 | 451 | strcpy(DEvol,"Voltaje: "); |
steffania1596 | 0:239b65262a52 | 452 | strcat(DEvol,out); |
steffania1596 | 0:239b65262a52 | 453 | pc.printf("\n%s\n\n", DEvol); |
steffania1596 | 0:239b65262a52 | 454 | LENINvol = strlen(DEvol); |
steffania1596 | 0:239b65262a52 | 455 | |
steffania1596 | 0:239b65262a52 | 456 | //Conversión a octetos. |
steffania1596 | 0:239b65262a52 | 457 | K = 0; |
steffania1596 | 0:239b65262a52 | 458 | C = 0; |
steffania1596 | 0:239b65262a52 | 459 | for (i = 0; i < LENINvol; i++){ |
steffania1596 | 0:239b65262a52 | 460 | DSvol[i] = DEvol[i + C] >> K | DEvol[i + C + 1] << (7 - K); |
steffania1596 | 0:239b65262a52 | 461 | if(DSvol[i] == 0x00) {LENOUTvol = i; goto salir2;} |
steffania1596 | 0:239b65262a52 | 462 | K++; |
steffania1596 | 0:239b65262a52 | 463 | if (K == 7) {K = 0; C++;} // se chequea que ya se acabaron los bits en un ciclo de conversion. |
steffania1596 | 0:239b65262a52 | 464 | } |
steffania1596 | 0:239b65262a52 | 465 | |
steffania1596 | 0:239b65262a52 | 466 | salir2: |
steffania1596 | 0:239b65262a52 | 467 | for (i = 0; i < LENINvol; i++){ |
steffania1596 | 0:239b65262a52 | 468 | pc.printf("%X", DEvol[i]); |
steffania1596 | 0:239b65262a52 | 469 | } |
steffania1596 | 0:239b65262a52 | 470 | |
steffania1596 | 0:239b65262a52 | 471 | pc.printf(":\r\n"); |
steffania1596 | 0:239b65262a52 | 472 | for (i = 0; i < LENOUTvol; i++){ |
steffania1596 | 0:239b65262a52 | 473 | pc.printf("%2X", DSvol[i]&0x000000FF); |
steffania1596 | 0:239b65262a52 | 474 | } |
steffania1596 | 0:239b65262a52 | 475 | pc.printf("\r\nLENOUT VOLTAJE: %d, LENIN VOLTAJE: %2X\r\n", LENOUTvol, LENINvol); |
steffania1596 | 0:239b65262a52 | 476 | |
steffania1596 | 0:239b65262a52 | 477 | cmgs = 14 + LENOUTvol - 1; |
steffania1596 | 0:239b65262a52 | 478 | |
steffania1596 | 0:239b65262a52 | 479 | GSM.printf("AT+CMGS=%d\r\n",cmgs); |
steffania1596 | 0:239b65262a52 | 480 | pc.printf("AT+CMGS=%d\r\n",cmgs); |
steffania1596 | 0:239b65262a52 | 481 | pc.printf(trama1); |
steffania1596 | 0:239b65262a52 | 482 | GSM.printf(trama1); |
steffania1596 | 0:239b65262a52 | 483 | |
steffania1596 | 0:239b65262a52 | 484 | for (i=0; i <= 9; i++) { |
steffania1596 | 0:239b65262a52 | 485 | pc.printf("%c",tel[i]); |
steffania1596 | 0:239b65262a52 | 486 | GSM.printf("%c",tel[i]); |
steffania1596 | 0:239b65262a52 | 487 | } |
steffania1596 | 0:239b65262a52 | 488 | |
steffania1596 | 0:239b65262a52 | 489 | pc.printf(trama2); |
steffania1596 | 0:239b65262a52 | 490 | GSM.printf(trama2); |
steffania1596 | 0:239b65262a52 | 491 | pc.printf("%2X", LENINvol); |
steffania1596 | 0:239b65262a52 | 492 | GSM.printf("%2X", LENINvol); |
steffania1596 | 0:239b65262a52 | 493 | |
steffania1596 | 0:239b65262a52 | 494 | for (i = 0; i < LENOUTvol; i++){ |
steffania1596 | 0:239b65262a52 | 495 | pc.printf("%02X", DSvol[i]&0x000000FF); |
steffania1596 | 0:239b65262a52 | 496 | GSM.printf("%02X", DSvol[i]&0x000000FF); |
steffania1596 | 0:239b65262a52 | 497 | } |
steffania1596 | 0:239b65262a52 | 498 | |
steffania1596 | 0:239b65262a52 | 499 | wait(1); |
steffania1596 | 0:239b65262a52 | 500 | GSM.putc((char)0x1A); |
steffania1596 | 0:239b65262a52 | 501 | pc.printf("\n"); |
steffania1596 | 0:239b65262a52 | 502 | |
steffania1596 | 0:239b65262a52 | 503 | wait(2); |
steffania1596 | 0:239b65262a52 | 504 | LedRojo=1; |
steffania1596 | 0:239b65262a52 | 505 | LedVerde=0; |
steffania1596 | 0:239b65262a52 | 506 | GSM.printf("AT+CMGD=0\r\n"); |
steffania1596 | 0:239b65262a52 | 507 | } |
steffania1596 | 0:239b65262a52 | 508 | } |
steffania1596 | 0:239b65262a52 | 509 | } |
steffania1596 | 0:239b65262a52 | 510 | } |
steffania1596 | 0:239b65262a52 | 511 | } |