Equipo Azul
Dependencies: mbed RASTREO_GPRS DebouncedIn GPS_G
main.cpp@3:17d367fba9c1, 2019-08-30 (annotated)
- Committer:
- Giovani_Cardona
- Date:
- Fri Aug 30 21:22:41 2019 +0000
- Revision:
- 3:17d367fba9c1
- Parent:
- 2:f4483748eee0
Equipo Azul
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Giovani_Cardona | 3:17d367fba9c1 | 1 | /*-----Equipo Azul----- |
tony63 | 0:627d6e86a48e | 2 | PROGRAMA PARA HACER SEGUIMIENTO DE FLOTAS CON GPS Y CONEXION A LA RED GPRS |
tony63 | 0:627d6e86a48e | 3 | se pueden simular trayectorias gravando archivos kml en google earth y usando |
tony63 | 0:627d6e86a48e | 4 | SatGen |
tony63 | 0:627d6e86a48e | 5 | version 1.0, abril 2019 |
tony63 | 0:627d6e86a48e | 6 | |
tony63 | 0:627d6e86a48e | 7 | se asigna un APN segun el servicio GPRS de la SIMCARD |
tony63 | 0:627d6e86a48e | 8 | se asigna un nombre de host de su pagina web |
tony63 | 0:627d6e86a48e | 9 | se asigna el path de los archivos y mapas ejecutables en su servidor |
tony63 | 0:627d6e86a48e | 10 | se asigna un ciclo de repeticion de lecturas de GPS. |
tony63 | 0:627d6e86a48e | 11 | |
tony63 | 0:627d6e86a48e | 12 | */ |
tony63 | 0:627d6e86a48e | 13 | |
tony63 | 0:627d6e86a48e | 14 | #include "mbed.h" |
tony63 | 0:627d6e86a48e | 15 | #include "stdio.h" |
tony63 | 0:627d6e86a48e | 16 | #include "string.h" |
tony63 | 0:627d6e86a48e | 17 | #include "GPS.h" |
tony63 | 0:627d6e86a48e | 18 | |
tony63 | 0:627d6e86a48e | 19 | Timer t; |
tony63 | 0:627d6e86a48e | 20 | DigitalOut LedVerde(LED2); |
tony63 | 0:627d6e86a48e | 21 | DigitalOut LedRojo(LED1); |
tony63 | 0:627d6e86a48e | 22 | DigitalOut LedAzul(LED3); |
tony63 | 0:627d6e86a48e | 23 | InterruptIn button(PTA13); |
tony63 | 0:627d6e86a48e | 24 | |
tony63 | 0:627d6e86a48e | 25 | |
tony63 | 0:627d6e86a48e | 26 | float lo,la; |
tony63 | 0:627d6e86a48e | 27 | char lon[15], lat[15]; // Cadenas a capturar para latitud y longitud. |
tony63 | 0:627d6e86a48e | 28 | char gprsBuffer[20]; |
tony63 | 0:627d6e86a48e | 29 | char resp[15]; |
Giovani_Cardona | 3:17d367fba9c1 | 30 | |
Giovani_Cardona | 3:17d367fba9c1 | 31 | char aux[100]; |
Giovani_Cardona | 3:17d367fba9c1 | 32 | |
tony63 | 0:627d6e86a48e | 33 | Serial GSM(PTE0,PTE1); // Puertos del FRDM para el Módem. |
tony63 | 0:627d6e86a48e | 34 | Serial pc(USBTX,USBRX); |
tony63 | 0:627d6e86a48e | 35 | GPS gps(PTE22, PTE23); // Puerto del FDRM para el GPS. |
tony63 | 0:627d6e86a48e | 36 | int result; |
tony63 | 0:627d6e86a48e | 37 | int z=0; |
tony63 | 0:627d6e86a48e | 38 | int i=0; |
tony63 | 0:627d6e86a48e | 39 | int count=0; |
tony63 | 1:3e0a9c577f09 | 40 | int g=0; |
tony63 | 0:627d6e86a48e | 41 | |
Giovani_Cardona | 3:17d367fba9c1 | 42 | |
tony63 | 0:627d6e86a48e | 43 | //----------------------------------------------------------------------------------------------------------------------------- |
tony63 | 0:627d6e86a48e | 44 | // Esta funcion de abajo lee todo un bufer hasta encontrar CR o LF y el resto lo rellena de |
tony63 | 0:627d6e86a48e | 45 | // $, count es lo que va a leer. Lo leido lo mete en buffer que es una cadena previamente definida |
tony63 | 0:627d6e86a48e | 46 | // incorpora medida de tiempo si se demora mas de tres segundos retorna fracaso con -1 |
tony63 | 0:627d6e86a48e | 47 | int readBuffer(char *buffer,int count){ |
tony63 | 0:627d6e86a48e | 48 | int i=0; |
tony63 | 0:627d6e86a48e | 49 | t.start(); // start timer |
tony63 | 0:627d6e86a48e | 50 | while(1) { |
tony63 | 0:627d6e86a48e | 51 | while (GSM.readable()) { |
tony63 | 0:627d6e86a48e | 52 | char c = GSM.getc(); |
tony63 | 0:627d6e86a48e | 53 | if (c == '\r' || c == '\n') c = '$'; |
tony63 | 0:627d6e86a48e | 54 | buffer[i++] = c; |
tony63 | 0:627d6e86a48e | 55 | if(i > count)break; |
tony63 | 0:627d6e86a48e | 56 | } |
tony63 | 0:627d6e86a48e | 57 | if(i > count)break; |
tony63 | 0:627d6e86a48e | 58 | if(t.read() > 3) { |
tony63 | 0:627d6e86a48e | 59 | t.stop(); |
tony63 | 0:627d6e86a48e | 60 | t.reset(); |
tony63 | 0:627d6e86a48e | 61 | break; |
tony63 | 0:627d6e86a48e | 62 | } |
tony63 | 0:627d6e86a48e | 63 | } |
tony63 | 0:627d6e86a48e | 64 | wait(0.5); |
tony63 | 0:627d6e86a48e | 65 | while(GSM.readable()){ // display the other thing.. |
tony63 | 0:627d6e86a48e | 66 | char c = GSM.getc(); |
tony63 | 0:627d6e86a48e | 67 | } |
tony63 | 0:627d6e86a48e | 68 | return 0; |
tony63 | 0:627d6e86a48e | 69 | } |
tony63 | 0:627d6e86a48e | 70 | //-------------------------------------------------------------------------------------------------------------- |
tony63 | 0:627d6e86a48e | 71 | // Esta función de abajo limpia o borra todo un "buffer" de tamaño "count", |
tony63 | 0:627d6e86a48e | 72 | // lo revisa elemento por elemento y le mete el caracter null que indica fin de cadena. |
tony63 | 0:627d6e86a48e | 73 | // No retorna nada. |
tony63 | 0:627d6e86a48e | 74 | void cleanBuffer(char *buffer, int count){ |
tony63 | 0:627d6e86a48e | 75 | for(int i=0; i < count; i++) { |
tony63 | 0:627d6e86a48e | 76 | buffer[i] = '\0'; |
tony63 | 0:627d6e86a48e | 77 | } |
tony63 | 0:627d6e86a48e | 78 | } |
tony63 | 0:627d6e86a48e | 79 | |
tony63 | 0:627d6e86a48e | 80 | //-------------------------------------------------------------------------------------------------------------- |
tony63 | 0:627d6e86a48e | 81 | // Esta función de abajo envia un comando parametrizado como cadena |
tony63 | 0:627d6e86a48e | 82 | // puede ser un comando tipo AT. |
tony63 | 0:627d6e86a48e | 83 | void sendCmd(char *cmd){ |
tony63 | 0:627d6e86a48e | 84 | GSM.puts(cmd); |
tony63 | 0:627d6e86a48e | 85 | } |
tony63 | 0:627d6e86a48e | 86 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
tony63 | 0:627d6e86a48e | 87 | // Esta función de abajo espera la respuesta de un comando que debe ser idéntica a la cadena "resp" y un tiempo "timeout", |
tony63 | 0:627d6e86a48e | 88 | // si todo sale bien retorna un cero que en la programacion hay que validar, |
tony63 | 0:627d6e86a48e | 89 | // si algo sale mal (no se parece o se demora mucho) retorna -1 que debera validarse con alguna expresion logica. |
tony63 | 0:627d6e86a48e | 90 | int waitForResp(char *resp, int timeout){ |
tony63 | 0:627d6e86a48e | 91 | int len = strlen(resp); |
tony63 | 0:627d6e86a48e | 92 | int sum=0; |
tony63 | 0:627d6e86a48e | 93 | t.start(); |
Giovani_Cardona | 3:17d367fba9c1 | 94 | i = 0; |
tony63 | 0:627d6e86a48e | 95 | while(1) { |
tony63 | 0:627d6e86a48e | 96 | if(GSM.readable()) { |
tony63 | 0:627d6e86a48e | 97 | char c = GSM.getc(); |
Giovani_Cardona | 3:17d367fba9c1 | 98 | //pc.printf("\nc:%c",c); |
Giovani_Cardona | 3:17d367fba9c1 | 99 | aux[i] = c; |
Giovani_Cardona | 3:17d367fba9c1 | 100 | i++; |
tony63 | 0:627d6e86a48e | 101 | sum = (c==resp[sum]) ? sum+1 : 0;// esta linea de C# sum se incrementa o se hace cero segun c |
tony63 | 0:627d6e86a48e | 102 | if(sum == len)break; //ya acabo se sale |
tony63 | 0:627d6e86a48e | 103 | } |
tony63 | 0:627d6e86a48e | 104 | if(t.read() > timeout) { // time out chequea el tiempo minimo antes de salir perdiendo |
tony63 | 0:627d6e86a48e | 105 | t.stop(); |
tony63 | 0:627d6e86a48e | 106 | t.reset(); |
tony63 | 0:627d6e86a48e | 107 | return -1; |
tony63 | 0:627d6e86a48e | 108 | } |
tony63 | 0:627d6e86a48e | 109 | } |
tony63 | 0:627d6e86a48e | 110 | t.stop(); // stop timer antes de retornar |
tony63 | 0:627d6e86a48e | 111 | t.reset(); // clear timer |
tony63 | 0:627d6e86a48e | 112 | while(GSM.readable()) { // display the other thing.. |
tony63 | 0:627d6e86a48e | 113 | char c = GSM.getc(); |
tony63 | 0:627d6e86a48e | 114 | } |
tony63 | 0:627d6e86a48e | 115 | return 0; |
tony63 | 0:627d6e86a48e | 116 | } |
tony63 | 0:627d6e86a48e | 117 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
tony63 | 0:627d6e86a48e | 118 | // Esta función de abajo es muy completa y útil, se encarga de enviar el comando y esperar la respuesta. |
tony63 | 0:627d6e86a48e | 119 | // Si todo sale bien retorna un cero (herencia de las funciones contenedoras) que en la programacion hay que validar |
tony63 | 0:627d6e86a48e | 120 | // con alguna expresion lógica. |
tony63 | 0:627d6e86a48e | 121 | int sendCmdAndWaitForResp(char *cmd, char *resp, int timeout){ |
tony63 | 0:627d6e86a48e | 122 | sendCmd(cmd); |
tony63 | 0:627d6e86a48e | 123 | return waitForResp(resp,timeout); |
tony63 | 0:627d6e86a48e | 124 | } |
tony63 | 0:627d6e86a48e | 125 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
tony63 | 0:627d6e86a48e | 126 | // Esta función de abajo chequea que el módem este vivo, envia AT, le contesta con OK y espera 2 segundos. |
tony63 | 0:627d6e86a48e | 127 | int powerCheck(void){ // Este comando se manda para verificar si el módem esta vivo o conectado. |
tony63 | 0:627d6e86a48e | 128 | return sendCmdAndWaitForResp("AT\r\n", "OK", 2); |
tony63 | 0:627d6e86a48e | 129 | } |
tony63 | 0:627d6e86a48e | 130 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
tony63 | 0:627d6e86a48e | 131 | // Esta función de abajo chequea el estado de la sim card |
tony63 | 0:627d6e86a48e | 132 | // y si todo sale bien retorna un cero que en la programacion hay que validar |
tony63 | 0:627d6e86a48e | 133 | // con alguna expresión lógica. |
tony63 | 0:627d6e86a48e | 134 | int checkSIMStatus(void){ |
tony63 | 0:627d6e86a48e | 135 | char gprsBuffer[30]; |
tony63 | 0:627d6e86a48e | 136 | int count = 0; |
tony63 | 0:627d6e86a48e | 137 | cleanBuffer(gprsBuffer, 30); |
tony63 | 0:627d6e86a48e | 138 | while(count < 3){ |
tony63 | 0:627d6e86a48e | 139 | sendCmd("AT+CPIN?\r\n"); |
tony63 | 0:627d6e86a48e | 140 | readBuffer(gprsBuffer,30); |
tony63 | 0:627d6e86a48e | 141 | if((NULL != strstr(gprsBuffer,"+CPIN: READY"))){ |
tony63 | 0:627d6e86a48e | 142 | break; |
tony63 | 0:627d6e86a48e | 143 | } |
tony63 | 0:627d6e86a48e | 144 | count++; |
tony63 | 0:627d6e86a48e | 145 | wait(1); |
tony63 | 0:627d6e86a48e | 146 | } |
tony63 | 0:627d6e86a48e | 147 | |
tony63 | 0:627d6e86a48e | 148 | if(count == 3){ |
tony63 | 0:627d6e86a48e | 149 | return -1; |
tony63 | 0:627d6e86a48e | 150 | } |
tony63 | 0:627d6e86a48e | 151 | return 0; |
tony63 | 0:627d6e86a48e | 152 | } |
tony63 | 0:627d6e86a48e | 153 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
tony63 | 0:627d6e86a48e | 154 | |
tony63 | 0:627d6e86a48e | 155 | // Esta función de abajo chequea la calidad de la señal |
tony63 | 0:627d6e86a48e | 156 | // 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 |
tony63 | 0:627d6e86a48e | 157 | // con alguna expresión lógica. |
tony63 | 0:627d6e86a48e | 158 | int checkSignalStrength(void){ |
tony63 | 0:627d6e86a48e | 159 | char gprsBuffer[100]; |
tony63 | 0:627d6e86a48e | 160 | int index, count = 0; |
tony63 | 0:627d6e86a48e | 161 | cleanBuffer(gprsBuffer,100); |
tony63 | 0:627d6e86a48e | 162 | while(count < 3){ |
tony63 | 0:627d6e86a48e | 163 | sendCmd("AT+CSQ\r\n"); |
tony63 | 0:627d6e86a48e | 164 | readBuffer(gprsBuffer,25); |
tony63 | 0:627d6e86a48e | 165 | if(sscanf(gprsBuffer, "AT+CSQ$$$$+CSQ: %d", &index)>0) { |
tony63 | 0:627d6e86a48e | 166 | break; |
tony63 | 0:627d6e86a48e | 167 | } |
tony63 | 0:627d6e86a48e | 168 | count++; |
tony63 | 0:627d6e86a48e | 169 | wait(1); |
tony63 | 0:627d6e86a48e | 170 | } |
tony63 | 0:627d6e86a48e | 171 | if(count == 3){ |
tony63 | 0:627d6e86a48e | 172 | return -1; |
tony63 | 0:627d6e86a48e | 173 | } |
tony63 | 0:627d6e86a48e | 174 | return index; |
tony63 | 0:627d6e86a48e | 175 | } |
tony63 | 0:627d6e86a48e | 176 | |
tony63 | 0:627d6e86a48e | 177 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
tony63 | 0:627d6e86a48e | 178 | |
tony63 | 0:627d6e86a48e | 179 | // detectar direcion ip |
tony63 | 0:627d6e86a48e | 180 | int ip_detect(){ |
tony63 | 0:627d6e86a48e | 181 | if (GSM.readable()){ |
tony63 | 0:627d6e86a48e | 182 | readBuffer(gprsBuffer,15); |
tony63 | 0:627d6e86a48e | 183 | for(i=0;i<15;i++) |
tony63 | 0:627d6e86a48e | 184 | { |
tony63 | 0:627d6e86a48e | 185 | resp[i]=gprsBuffer[i]; |
tony63 | 0:627d6e86a48e | 186 | } |
tony63 | 0:627d6e86a48e | 187 | cleanBuffer(gprsBuffer,20); |
tony63 | 0:627d6e86a48e | 188 | for(i=0;i<15;i++){ |
tony63 | 0:627d6e86a48e | 189 | if(resp[i]== '.'){ |
tony63 | 0:627d6e86a48e | 190 | z++; |
tony63 | 0:627d6e86a48e | 191 | } |
tony63 | 0:627d6e86a48e | 192 | } |
tony63 | 1:3e0a9c577f09 | 193 | if(z==3){ //CUENTO TRES PUNTOS LO MAS PROBABLE ES QUE SEA UNA DIRECCION ip |
tony63 | 0:627d6e86a48e | 194 | pc.printf("llego ip=%d\r\n",z); |
tony63 | 0:627d6e86a48e | 195 | z=0; |
tony63 | 1:3e0a9c577f09 | 196 | return 0; //RETORNA CON CERO SI LLEGARON TRES PUNTOS |
tony63 | 0:627d6e86a48e | 197 | } |
tony63 | 0:627d6e86a48e | 198 | }//fin check GSM modem |
tony63 | 0:627d6e86a48e | 199 | z=0; |
tony63 | 1:3e0a9c577f09 | 200 | return -1; //NO LLEGARON 3 PUNTOS RETORNA CON -1 |
tony63 | 0:627d6e86a48e | 201 | }//fin funcion |
tony63 | 0:627d6e86a48e | 202 | |
tony63 | 0:627d6e86a48e | 203 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
tony63 | 0:627d6e86a48e | 204 | // Esta funcion de abajo inicia la comunicacion GPRS con la red celular, Se compone de un grupo de subfunciones ya definidas previamente |
tony63 | 0:627d6e86a48e | 205 | // envia las secuencias de forma ordenada esparando la respuesta adecuada |
tony63 | 0:627d6e86a48e | 206 | // si todo sale bien retorna un cero que en la programacion hay que validar |
tony63 | 0:627d6e86a48e | 207 | // con alguna expresión lógica. |
tony63 | 0:627d6e86a48e | 208 | //********************************************************************************************************* |
tony63 | 0:627d6e86a48e | 209 | |
tony63 | 0:627d6e86a48e | 210 | int init_gprs(void){ |
tony63 | 0:627d6e86a48e | 211 | |
tony63 | 0:627d6e86a48e | 212 | if (0 != sendCmdAndWaitForResp("AT\r\n", "OK", 3)){ |
Giovani_Cardona | 3:17d367fba9c1 | 213 | pc.printf("1"); |
tony63 | 0:627d6e86a48e | 214 | return -1; |
tony63 | 0:627d6e86a48e | 215 | } |
tony63 | 0:627d6e86a48e | 216 | if (0 != sendCmdAndWaitForResp("ATE0\r\n", "OK", 3)){ //sin eco |
Giovani_Cardona | 3:17d367fba9c1 | 217 | pc.printf("2"); |
tony63 | 0:627d6e86a48e | 218 | return -1; |
tony63 | 0:627d6e86a48e | 219 | } |
tony63 | 0:627d6e86a48e | 220 | if (0 != sendCmdAndWaitForResp("AT+CGATT=1\r\n", "OK", 3)){//inicia conexion GPRS |
Giovani_Cardona | 3:17d367fba9c1 | 221 | pc.printf("3"); |
tony63 | 0:627d6e86a48e | 222 | return -1; |
tony63 | 0:627d6e86a48e | 223 | } |
tony63 | 0:627d6e86a48e | 224 | if (0 != sendCmdAndWaitForResp("AT+CSTT=internet.comcel.com.co,comcel,comcel\r\n", "OK", 3)){ //set apn |
Giovani_Cardona | 3:17d367fba9c1 | 225 | pc.printf("4"); |
tony63 | 0:627d6e86a48e | 226 | return -1; |
tony63 | 0:627d6e86a48e | 227 | } |
tony63 | 0:627d6e86a48e | 228 | if (0 != sendCmdAndWaitForResp("AT+CIICR\r\n", "OK", 3)){ //habilitar conexion inalambrica |
Giovani_Cardona | 3:17d367fba9c1 | 229 | pc.printf("5"); |
tony63 | 0:627d6e86a48e | 230 | return -1; |
tony63 | 0:627d6e86a48e | 231 | } |
tony63 | 0:627d6e86a48e | 232 | |
tony63 | 0:627d6e86a48e | 233 | cleanBuffer(gprsBuffer,25); |
tony63 | 0:627d6e86a48e | 234 | sendCmd("AT+CIFSR\r\n"); //obtener direccion ip |
tony63 | 0:627d6e86a48e | 235 | if(0 != ip_detect()){ //esperar la llegada de un direccion IP |
Giovani_Cardona | 3:17d367fba9c1 | 236 | pc.printf("6"); |
tony63 | 0:627d6e86a48e | 237 | return -1; |
tony63 | 0:627d6e86a48e | 238 | } |
tony63 | 0:627d6e86a48e | 239 | wait(1); |
tony63 | 1:3e0a9c577f09 | 240 | LedVerde=0;//CONEXION OK... PRENDE LED VERDE.. |
Giovani_Cardona | 3:17d367fba9c1 | 241 | pc.printf("Led verde"); |
tony63 | 0:627d6e86a48e | 242 | return 0; |
tony63 | 0:627d6e86a48e | 243 | } |
tony63 | 0:627d6e86a48e | 244 | |
tony63 | 1:3e0a9c577f09 | 245 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
tony63 | 1:3e0a9c577f09 | 246 | //ESTA FUNCION DE ABAJO CIERRA UNA CONEXION GPRS |
tony63 | 0:627d6e86a48e | 247 | |
tony63 | 0:627d6e86a48e | 248 | int end_gprs(void){ |
tony63 | 0:627d6e86a48e | 249 | if (0 != sendCmdAndWaitForResp("AT+CIPSHUT\r\n", "OK", 3)){ |
tony63 | 0:627d6e86a48e | 250 | return -1; |
tony63 | 0:627d6e86a48e | 251 | } |
tony63 | 0:627d6e86a48e | 252 | if (0 != sendCmdAndWaitForResp("AT+CGATT=0\r\n", "SHUT OK", 3)){ |
tony63 | 0:627d6e86a48e | 253 | return -1; |
tony63 | 0:627d6e86a48e | 254 | } |
tony63 | 0:627d6e86a48e | 255 | LedVerde=0; |
tony63 | 0:627d6e86a48e | 256 | LedRojo=1; |
tony63 | 0:627d6e86a48e | 257 | return 0; |
tony63 | 0:627d6e86a48e | 258 | } |
tony63 | 0:627d6e86a48e | 259 | |
tony63 | 0:627d6e86a48e | 260 | //++++++++++++++++++++++++++++++++++++++++++++++++++++ |
tony63 | 0:627d6e86a48e | 261 | //interupcion perdida de energia |
tony63 | 0:627d6e86a48e | 262 | //++++++++++++++++++++++++++++++++++++++++++++++++++++ |
tony63 | 0:627d6e86a48e | 263 | |
tony63 | 0:627d6e86a48e | 264 | void off_gprs(){ |
tony63 | 0:627d6e86a48e | 265 | end_gprs(); |
tony63 | 0:627d6e86a48e | 266 | } |
tony63 | 0:627d6e86a48e | 267 | |
tony63 | 0:627d6e86a48e | 268 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++ |
tony63 | 0:627d6e86a48e | 269 | //inicio del programa principal |
tony63 | 0:627d6e86a48e | 270 | //+++++++++++++++++++++++++++++++++++++++++++++++++++++ |
tony63 | 0:627d6e86a48e | 271 | int main(){ |
tony63 | 1:3e0a9c577f09 | 272 | |
tony63 | 1:3e0a9c577f09 | 273 | loop1:g=gps.sample(); //GPS bien conectado???? de primero antes que todo!!!! |
tony63 | 1:3e0a9c577f09 | 274 | if(g){ |
tony63 | 1:3e0a9c577f09 | 275 | LedAzul=0;//prende led azul GPS ok!!! |
tony63 | 1:3e0a9c577f09 | 276 | pc.printf("GPS--OK\n\r"); |
tony63 | 1:3e0a9c577f09 | 277 | //si no hay GPS no intentar dada, ni conexion ni envio de datos y esperar el GPS |
tony63 | 1:3e0a9c577f09 | 278 | goto lop1; |
tony63 | 1:3e0a9c577f09 | 279 | } |
tony63 | 1:3e0a9c577f09 | 280 | goto loop1; //chequeo infinito de un GPS bien instalado... |
tony63 | 0:627d6e86a48e | 281 | lop1: |
tony63 | 1:3e0a9c577f09 | 282 | |
tony63 | 0:627d6e86a48e | 283 | if(init_gprs()<0){ |
tony63 | 1:3e0a9c577f09 | 284 | LedRojo=0;//PRENDE ROJO, APAGA VERDE |
tony63 | 0:627d6e86a48e | 285 | LedVerde=1; |
Giovani_Cardona | 3:17d367fba9c1 | 286 | pc.printf("\n\nlop1\n\n"); |
tony63 | 1:3e0a9c577f09 | 287 | goto lop1;//NO SE PUEDE RECONECTAR INFINITAMENTE ESTE SALTO ES PROVISIONAL |
tony63 | 1:3e0a9c577f09 | 288 | //CONTAR LOS INTENTOS Y DAR SEÑAL DE ERROR PERMANENTE |
tony63 | 0:627d6e86a48e | 289 | } |
Giovani_Cardona | 3:17d367fba9c1 | 290 | //button.fall(&off_gprs);//perdida de alimentacion,apagaron carro ejecuta interupcion |
tony63 | 2:f4483748eee0 | 291 | //que desconecta la conexion GPRS |
tony63 | 0:627d6e86a48e | 292 | while(1){ //si el GPS tiene conexion y datos se envian coordenadas a pagina web |
tony63 | 0:627d6e86a48e | 293 | LedAzul=1; |
tony63 | 0:627d6e86a48e | 294 | if(gps.sample()){ |
tony63 | 0:627d6e86a48e | 295 | LedAzul=0; |
tony63 | 0:627d6e86a48e | 296 | lo = gps.longitude; |
tony63 | 0:627d6e86a48e | 297 | la = gps.latitude; |
tony63 | 0:627d6e86a48e | 298 | sprintf (lon, "%f", lo);//pasa de flotante a caracter |
tony63 | 0:627d6e86a48e | 299 | sprintf (lat, "%f", la);//pasa de flotante a caracter |
Giovani_Cardona | 3:17d367fba9c1 | 300 | |
Giovani_Cardona | 3:17d367fba9c1 | 301 | GSM.printf("AT+CIPSTART=\"TCP\",\"unrobotica.com\",\"80\"\r\n"); |
Giovani_Cardona | 3:17d367fba9c1 | 302 | pc.printf("AT+CIPSTART=\"TCP\",\"unrobotica.com\",\"80\"\r\n"); |
Giovani_Cardona | 3:17d367fba9c1 | 303 | /*repetir1:if (0 != sendCmdAndWaitForResp("AT+CIPSTART=TCP,unrobotica.com,80\r\n","OK", 5)){ |
tony63 | 0:627d6e86a48e | 304 | wait(3); |
tony63 | 0:627d6e86a48e | 305 | LedVerde=1; |
tony63 | 0:627d6e86a48e | 306 | LedRojo=0; |
Giovani_Cardona | 3:17d367fba9c1 | 307 | pc.printf("\n\nRep1\n\n"); |
tony63 | 1:3e0a9c577f09 | 308 | goto repetir1;//salto provisional debe contar intentos y no hacer nada ya que es imposible conectarse |
Giovani_Cardona | 3:17d367fba9c1 | 309 | } */ |
tony63 | 0:627d6e86a48e | 310 | |
tony63 | 0:627d6e86a48e | 311 | repetir2:cleanBuffer(gprsBuffer,10); |
Giovani_Cardona | 3:17d367fba9c1 | 312 | if(0 !=sendCmdAndWaitForResp("AT+CIPSEND\r\n","",5)){ //devuelve control+Z |
tony63 | 0:627d6e86a48e | 313 | wait(1); |
Giovani_Cardona | 3:17d367fba9c1 | 314 | pc.printf("\n\nRep2\n\n"); |
tony63 | 1:3e0a9c577f09 | 315 | goto repetir2;//salto provisional debe contar intentos y no hacer nada ya que es imposible conectarse |
tony63 | 0:627d6e86a48e | 316 | } |
Giovani_Cardona | 3:17d367fba9c1 | 317 | for(i=0;i<100;i++) |
Giovani_Cardona | 3:17d367fba9c1 | 318 | { |
Giovani_Cardona | 3:17d367fba9c1 | 319 | pc.printf("%c",aux[i]); |
Giovani_Cardona | 3:17d367fba9c1 | 320 | } |
Giovani_Cardona | 3:17d367fba9c1 | 321 | wait(5); |
Giovani_Cardona | 3:17d367fba9c1 | 322 | pc.printf("\n\nRep3\n\n"); |
Giovani_Cardona | 3:17d367fba9c1 | 323 | GSM.printf("GET /gpstracker/gps1.php?lat=%s&lon=%s HTTP/1.1\r\n",lat,lon); |
Giovani_Cardona | 3:17d367fba9c1 | 324 | pc.printf("GET /gpstracker/gps1.php?lat=%s&lon=%s HTTP/1.1\r\n",lat,lon); |
Giovani_Cardona | 3:17d367fba9c1 | 325 | wait(5); |
Giovani_Cardona | 3:17d367fba9c1 | 326 | GSM.printf("Host: unrobotica.com\r\n"); |
Giovani_Cardona | 3:17d367fba9c1 | 327 | wait(5); |
tony63 | 0:627d6e86a48e | 328 | GSM.printf("\r\n"); |
Giovani_Cardona | 3:17d367fba9c1 | 329 | wait(5); |
Giovani_Cardona | 3:17d367fba9c1 | 330 | cleanBuffer(aux,100); |
Giovani_Cardona | 3:17d367fba9c1 | 331 | //GSM.putc((char)0x1A); |
Giovani_Cardona | 3:17d367fba9c1 | 332 | GSM.printf("\r\n"); |
Giovani_Cardona | 3:17d367fba9c1 | 333 | for(i=0;i<100;i++) |
Giovani_Cardona | 3:17d367fba9c1 | 334 | { |
Giovani_Cardona | 3:17d367fba9c1 | 335 | pc.printf("%c",aux[i]); |
Giovani_Cardona | 3:17d367fba9c1 | 336 | } |
Giovani_Cardona | 3:17d367fba9c1 | 337 | /*if(0 !=sendCmdAndWaitForResp(">\r\n","SEND OK",10)) |
tony63 | 1:3e0a9c577f09 | 338 | { |
Giovani_Cardona | 3:17d367fba9c1 | 339 | pc.printf("\n -\n"); |
Giovani_Cardona | 3:17d367fba9c1 | 340 | for(i=0;i<100;i++) |
Giovani_Cardona | 3:17d367fba9c1 | 341 | { |
Giovani_Cardona | 3:17d367fba9c1 | 342 | pc.printf("%c",aux[i]); |
Giovani_Cardona | 3:17d367fba9c1 | 343 | } |
tony63 | 1:3e0a9c577f09 | 344 | |
Giovani_Cardona | 3:17d367fba9c1 | 345 | pc.printf("Not - SEND OK"); |
tony63 | 1:3e0a9c577f09 | 346 | } |
Giovani_Cardona | 3:17d367fba9c1 | 347 | else{pc.printf("SEND OK");}*/ |
tony63 | 1:3e0a9c577f09 | 348 | //mas tarde devuelve SEND OK ...esto es suficiente para indicarnos que los datos se fueron a la nube |
tony63 | 0:627d6e86a48e | 349 | //cleanBuffer(gprsBuffer,20); |
tony63 | 0:627d6e86a48e | 350 | //leer bufer y encontrar respuesta exitosa devuelve "SEND OK" |
tony63 | 0:627d6e86a48e | 351 | |
tony63 | 0:627d6e86a48e | 352 | //readBuffer(gprsBuffer,10); |
tony63 | 0:627d6e86a48e | 353 | //if((NULL = strstr(gprsBuffer,""))){ |
tony63 | 0:627d6e86a48e | 354 | // wait(1); |
tony63 | 0:627d6e86a48e | 355 | // goto repetir2; |
tony63 | 0:627d6e86a48e | 356 | LedRojo=1; |
tony63 | 0:627d6e86a48e | 357 | LedVerde=0; //envio OK |
tony63 | 0:627d6e86a48e | 358 | }// del test del gps |
tony63 | 0:627d6e86a48e | 359 | LedRojo=0; // hay problemas |
tony63 | 0:627d6e86a48e | 360 | LedVerde=1; |
tony63 | 0:627d6e86a48e | 361 | |
tony63 | 0:627d6e86a48e | 362 | }//del while col 13 |
tony63 | 0:627d6e86a48e | 363 | |
tony63 | 0:627d6e86a48e | 364 | }//del main col 11 |
tony63 | 0:627d6e86a48e | 365 | |
tony63 | 0:627d6e86a48e | 366 | |
tony63 | 0:627d6e86a48e | 367 | |
tony63 | 0:627d6e86a48e | 368 | |
tony63 | 0:627d6e86a48e | 369 |