teste de publish
Dependencies: DS1820 HighSpeedAnalogIn devices mbed
modem.cpp@7:ae9c47f62946, 2017-07-11 (annotated)
- Committer:
- brunofgc
- Date:
- Tue Jul 11 11:55:56 2017 +0000
- Revision:
- 7:ae9c47f62946
- Parent:
- 6:d4ebbaaba295
- Child:
- 8:9f8ad3dbeb87
Vers?o UCSal piloto 1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
brunofgc | 0:1c0a769988ee | 1 | #include "modem.h" |
brunofgc | 0:1c0a769988ee | 2 | |
brunofgc | 0:1c0a769988ee | 3 | modemStatus modemCom::status; |
brunofgc | 0:1c0a769988ee | 4 | char* modemCom::bufIn; //Ponteiro para buffer de entrada do modem; |
brunofgc | 0:1c0a769988ee | 5 | uint32_t modemCom::timeOutModem; |
brunofgc | 0:1c0a769988ee | 6 | bool modemCom::atendendoWebServer; |
brunofgc | 0:1c0a769988ee | 7 | bool modemCom::exibeEntradaPacote; |
brunofgc | 0:1c0a769988ee | 8 | int16_t numeroDeBytesPorEnvio; |
brunofgc | 0:1c0a769988ee | 9 | uint8_t connID; |
brunofgc | 0:1c0a769988ee | 10 | uint16_t IPDNumBytes; |
brunofgc | 0:1c0a769988ee | 11 | bool enviaSendData; |
brunofgc | 0:1c0a769988ee | 12 | char *bufInPtr; |
brunofgc | 0:1c0a769988ee | 13 | bool mudaRede = false; |
brunofgc | 0:1c0a769988ee | 14 | |
brunofgc | 0:1c0a769988ee | 15 | void modemCom::atendeSRING(uint8_t conId){ |
brunofgc | 6:d4ebbaaba295 | 16 | //pc.printf("Atendendo a conexao entrante.\n"); |
brunofgc | 0:1c0a769988ee | 17 | modemCom::status.socketEntranteAtivo=true; |
brunofgc | 0:1c0a769988ee | 18 | modemCom::status.timeOut = 30; |
brunofgc | 0:1c0a769988ee | 19 | modemCom::timeOutModem = 50; |
brunofgc | 0:1c0a769988ee | 20 | while(modemCom::status.timeOut||modemCom::status.NO_CARRIER){ |
brunofgc | 0:1c0a769988ee | 21 | modemCom::status.timeOut--; |
brunofgc | 0:1c0a769988ee | 22 | /*//Aqui devo perguntar e pedir os dados em buffer |
brunofgc | 0:1c0a769988ee | 23 | */ |
brunofgc | 0:1c0a769988ee | 24 | osDelay(100); |
brunofgc | 0:1c0a769988ee | 25 | } |
brunofgc | 0:1c0a769988ee | 26 | |
brunofgc | 0:1c0a769988ee | 27 | //Para parar de colocar dentro do arquivo |
brunofgc | 0:1c0a769988ee | 28 | modemCom::status.recebendoArquivoDoServer = false; |
brunofgc | 0:1c0a769988ee | 29 | |
brunofgc | 0:1c0a769988ee | 30 | modemCom::status.serverConnected=0; |
brunofgc | 0:1c0a769988ee | 31 | modemCom::timeOutModem = 10; |
brunofgc | 0:1c0a769988ee | 32 | |
brunofgc | 0:1c0a769988ee | 33 | //Fechando o socket |
brunofgc | 0:1c0a769988ee | 34 | modemCom::closeConnection(conId); |
brunofgc | 0:1c0a769988ee | 35 | modemCom::status.socketEntranteAtivo=false; |
brunofgc | 0:1c0a769988ee | 36 | modemCom::status.emComunicacao = false; |
brunofgc | 0:1c0a769988ee | 37 | modemCom::timeOutModem = 10; |
brunofgc | 0:1c0a769988ee | 38 | } |
brunofgc | 0:1c0a769988ee | 39 | |
brunofgc | 0:1c0a769988ee | 40 | bool modemCom::sendBufferCommandMode(uint8_t sId, char *buffer, uint16_t bufferLength){ |
brunofgc | 0:1c0a769988ee | 41 | char aux[25]; |
brunofgc | 0:1c0a769988ee | 42 | uint16_t numeroByteSendoEnviado; |
brunofgc | 0:1c0a769988ee | 43 | |
brunofgc | 0:1c0a769988ee | 44 | sprintf(aux,"AT+CIPSEND=%u,%lu\r\n",sId,bufferLength); |
brunofgc | 7:ae9c47f62946 | 45 | if(modemCom::sendToModem(aux,1,&modemCom::status.PROMPT_ENVIO_COMMAND_MODE,NULL,300,10,1)) { |
brunofgc | 0:1c0a769988ee | 46 | for(numeroByteSendoEnviado = 0; numeroByteSendoEnviado<bufferLength; numeroByteSendoEnviado++) { |
brunofgc | 0:1c0a769988ee | 47 | modem.printf("%c",buffer[numeroByteSendoEnviado]); |
brunofgc | 0:1c0a769988ee | 48 | #ifdef xereta_modem |
brunofgc | 0:1c0a769988ee | 49 | pc.printf("%c",buffer[numeroByteSendoEnviado]); |
brunofgc | 0:1c0a769988ee | 50 | #endif |
brunofgc | 0:1c0a769988ee | 51 | } |
brunofgc | 7:ae9c47f62946 | 52 | numeroByteSendoEnviado = modemCom::sendToModem(NULL,1,&modemCom::status.SEND_OK,NULL,500,1,1); |
brunofgc | 0:1c0a769988ee | 53 | }else{ |
brunofgc | 0:1c0a769988ee | 54 | numeroByteSendoEnviado = false; //Reaproveitando numeroByteSendoEnviado para resultado de retorno |
brunofgc | 0:1c0a769988ee | 55 | } |
brunofgc | 0:1c0a769988ee | 56 | return numeroByteSendoEnviado; //Reaproveitando numeroByteSendoEnviado para resultado de retorno |
brunofgc | 0:1c0a769988ee | 57 | } |
brunofgc | 0:1c0a769988ee | 58 | |
brunofgc | 0:1c0a769988ee | 59 | void modemCom::leMAC(void) |
brunofgc | 0:1c0a769988ee | 60 | { |
brunofgc | 0:1c0a769988ee | 61 | char *ptr; |
brunofgc | 7:ae9c47f62946 | 62 | if(modemCom::sendToModem("AT+CIPSTAMAC?\r\n",1,&modemCom::status.modemResponse,NULL,250,20,1)){ |
brunofgc | 0:1c0a769988ee | 63 | //resposta: +CIPSTAMAC:"18:fe:34:f4:5f:c4" |
brunofgc | 0:1c0a769988ee | 64 | ptr = strtok(bufModem.getRowBuffer(),"\""); |
brunofgc | 0:1c0a769988ee | 65 | ptr = strtok(NULL,"\""); |
brunofgc | 0:1c0a769988ee | 66 | if(ptr) { |
brunofgc | 0:1c0a769988ee | 67 | strcpy(modemCom::status.MAC,ptr); |
brunofgc | 0:1c0a769988ee | 68 | pc.printf("Lido MAC <%s>.\n",modemCom::status.MAC); |
brunofgc | 0:1c0a769988ee | 69 | } |
brunofgc | 0:1c0a769988ee | 70 | } |
brunofgc | 0:1c0a769988ee | 71 | } |
brunofgc | 0:1c0a769988ee | 72 | |
brunofgc | 7:ae9c47f62946 | 73 | |
brunofgc | 0:1c0a769988ee | 74 | |
brunofgc | 1:0e0967c88590 | 75 | bool modemCom::writeStatusToSD(void){ |
brunofgc | 1:0e0967c88590 | 76 | char aux[30]; |
brunofgc | 1:0e0967c88590 | 77 | int16_t RSSI; |
brunofgc | 1:0e0967c88590 | 78 | RSSI = modemCom::getRSSI(); |
brunofgc | 1:0e0967c88590 | 79 | sdCardBuf.del(); |
brunofgc | 1:0e0967c88590 | 80 | sdCardBuf.fill("log{status:ip=255.255.255.255;csq=",34); |
brunofgc | 1:0e0967c88590 | 81 | |
brunofgc | 1:0e0967c88590 | 82 | sprintf(aux,"%d",RSSI); |
brunofgc | 1:0e0967c88590 | 83 | sdCardBuf.fill(aux,strlen(aux)); |
brunofgc | 1:0e0967c88590 | 84 | |
brunofgc | 1:0e0967c88590 | 85 | sprintf(aux,";imsi=%s}log",modemCom::status.MAC); |
brunofgc | 1:0e0967c88590 | 86 | sdCardBuf.fill(aux,strlen(aux)); |
brunofgc | 1:0e0967c88590 | 87 | |
brunofgc | 7:ae9c47f62946 | 88 | return sdCard::insereDadosArquivo(&sdCard::armazenamento,sdCardBuf.get(),sdCardBuf.getLength()); |
brunofgc | 1:0e0967c88590 | 89 | } |
brunofgc | 1:0e0967c88590 | 90 | |
brunofgc | 0:1c0a769988ee | 91 | bool modemCom::montaStatus(void) |
brunofgc | 0:1c0a769988ee | 92 | { |
brunofgc | 0:1c0a769988ee | 93 | if(sdCard::abreArquivo(&sdCard::status,"w")) { |
brunofgc | 0:1c0a769988ee | 94 | fprintf(sdCard::status.fp,"log{status:ip:255.255.255.255;csq:30;imsi:%s}log",modemCom::status.MAC); |
brunofgc | 0:1c0a769988ee | 95 | sdCard::fechaArquivo(&sdCard::status); |
brunofgc | 0:1c0a769988ee | 96 | sdCard::exibeArquivo(&sdCard::status); |
brunofgc | 0:1c0a769988ee | 97 | } else { |
brunofgc | 0:1c0a769988ee | 98 | return 0; |
brunofgc | 0:1c0a769988ee | 99 | } |
brunofgc | 0:1c0a769988ee | 100 | return true; |
brunofgc | 0:1c0a769988ee | 101 | } |
brunofgc | 0:1c0a769988ee | 102 | |
brunofgc | 0:1c0a769988ee | 103 | |
brunofgc | 0:1c0a769988ee | 104 | |
brunofgc | 0:1c0a769988ee | 105 | uint8_t modemCom::enviaDados(char tipoDado) |
brunofgc | 0:1c0a769988ee | 106 | { |
brunofgc | 0:1c0a769988ee | 107 | uint8_t result=0; |
brunofgc | 0:1c0a769988ee | 108 | modemCom::status.emComunicacao = true; |
brunofgc | 0:1c0a769988ee | 109 | |
brunofgc | 0:1c0a769988ee | 110 | switch(tipoDado) { |
brunofgc | 0:1c0a769988ee | 111 | case dadosArmazenados: |
brunofgc | 0:1c0a769988ee | 112 | if(!sdCard::preparaEnvio()){ |
brunofgc | 0:1c0a769988ee | 113 | pc.printf("Nao foi possivel preparar o envio.\n"); |
brunofgc | 2:55b7b466e742 | 114 | return false; |
brunofgc | 0:1c0a769988ee | 115 | } |
brunofgc | 7:ae9c47f62946 | 116 | sdCard::getFileTam(&sdCard::envio); |
brunofgc | 0:1c0a769988ee | 117 | pc.printf("Preparado o envio do arquivo com %lu bytes.\n",sdCard::envio.bytes); |
brunofgc | 0:1c0a769988ee | 118 | break; |
brunofgc | 0:1c0a769988ee | 119 | case dadosStatus: |
brunofgc | 0:1c0a769988ee | 120 | modemCom::montaStatus(); |
brunofgc | 0:1c0a769988ee | 121 | break; |
brunofgc | 0:1c0a769988ee | 122 | } |
brunofgc | 0:1c0a769988ee | 123 | |
brunofgc | 0:1c0a769988ee | 124 | switch(tipoDado) { |
brunofgc | 0:1c0a769988ee | 125 | case dadosArmazenados: |
brunofgc | 2:55b7b466e742 | 126 | result = modemCom::postFileCommandMode(serverAddress,"/drome/Parser/",&sdCard::envio); |
brunofgc | 0:1c0a769988ee | 127 | break; |
brunofgc | 0:1c0a769988ee | 128 | case dadosStatus: |
brunofgc | 2:55b7b466e742 | 129 | result = modemCom::postFileCommandMode(serverAddress,"/drome/Parser/",&sdCard::status); |
brunofgc | 0:1c0a769988ee | 130 | break; |
brunofgc | 0:1c0a769988ee | 131 | } |
brunofgc | 0:1c0a769988ee | 132 | |
brunofgc | 0:1c0a769988ee | 133 | if(!result){ |
brunofgc | 0:1c0a769988ee | 134 | return result; |
brunofgc | 0:1c0a769988ee | 135 | } |
brunofgc | 0:1c0a769988ee | 136 | |
brunofgc | 0:1c0a769988ee | 137 | //Envia uma string nula e faz o processador esperar a resposta do server! |
brunofgc | 0:1c0a769988ee | 138 | if(modemCom::status.ServerAck) { |
brunofgc | 0:1c0a769988ee | 139 | pc.printf("Enviado e recebido resposta do server (*ServerAck*).\n"); |
brunofgc | 0:1c0a769988ee | 140 | } else { |
brunofgc | 0:1c0a769988ee | 141 | pc.printf("Enviado mas nao recebido resposta do server (*ServerAck*).\n"); |
brunofgc | 0:1c0a769988ee | 142 | pc.printf("Conteudo de getRowBuffer <%s>.\n",bufModem.getRowBuffer()); |
brunofgc | 0:1c0a769988ee | 143 | } |
brunofgc | 0:1c0a769988ee | 144 | return result; |
brunofgc | 0:1c0a769988ee | 145 | } |
brunofgc | 0:1c0a769988ee | 146 | |
brunofgc | 0:1c0a769988ee | 147 | |
brunofgc | 0:1c0a769988ee | 148 | |
brunofgc | 0:1c0a769988ee | 149 | uint8_t modemCom::postFileCommandMode(char *host, char *uri, arquivoSD *arquivo) |
brunofgc | 0:1c0a769988ee | 150 | { |
brunofgc | 0:1c0a769988ee | 151 | #define maxRetentativasEnvioParteArquivo 20 |
brunofgc | 0:1c0a769988ee | 152 | uint32_t numeroDePartes; |
brunofgc | 0:1c0a769988ee | 153 | uint16_t restoDoEnvio; |
brunofgc | 0:1c0a769988ee | 154 | int32_t parteSendoEnviada; |
brunofgc | 0:1c0a769988ee | 155 | uint16_t numeroByteSendoEnviado; |
brunofgc | 0:1c0a769988ee | 156 | uint16_t maxTentativas=0; |
brunofgc | 0:1c0a769988ee | 157 | //unsigned int result; |
brunofgc | 0:1c0a769988ee | 158 | char header[512]; |
brunofgc | 0:1c0a769988ee | 159 | char aux[40]; |
brunofgc | 0:1c0a769988ee | 160 | uint16_t headerTam; |
brunofgc | 0:1c0a769988ee | 161 | uint32_t dataTam=0; |
brunofgc | 0:1c0a769988ee | 162 | char c; |
brunofgc | 0:1c0a769988ee | 163 | bool passaParaProximaParte = false; |
brunofgc | 0:1c0a769988ee | 164 | |
brunofgc | 0:1c0a769988ee | 165 | //Para garantir que não seja duvidoso proveniente de outra conexão. |
brunofgc | 0:1c0a769988ee | 166 | modemCom::status.ServerAck=0; |
brunofgc | 0:1c0a769988ee | 167 | modemCom::status.NO_CARRIER=0; |
brunofgc | 0:1c0a769988ee | 168 | |
brunofgc | 0:1c0a769988ee | 169 | if(arquivo->bytes==0){ |
brunofgc | 0:1c0a769988ee | 170 | |
brunofgc | 0:1c0a769988ee | 171 | if((!sdCard::getFileTam(arquivo))||(arquivo->bytes == 0)){ |
brunofgc | 7:ae9c47f62946 | 172 | if(arquivo->bytes == 0){ |
brunofgc | 7:ae9c47f62946 | 173 | sdCard::excluiArquivo(arquivo); |
brunofgc | 7:ae9c47f62946 | 174 | } |
brunofgc | 0:1c0a769988ee | 175 | return false; |
brunofgc | 0:1c0a769988ee | 176 | } |
brunofgc | 0:1c0a769988ee | 177 | } |
brunofgc | 0:1c0a769988ee | 178 | |
brunofgc | 0:1c0a769988ee | 179 | pc.printf("Abrindo o socket em <%s>.\n",host); |
brunofgc | 0:1c0a769988ee | 180 | sprintf(header,"AT+CIPSTART=1,\"TCP\",\"%s\",80\r\n",host); |
brunofgc | 7:ae9c47f62946 | 181 | if(modemCom::sendToModem(header,1,&modemCom::status.OK,NULL,20000,1,1)) { |
brunofgc | 0:1c0a769988ee | 182 | modemCom::status.NO_CARRIER=0; //Socket Aberto! |
brunofgc | 0:1c0a769988ee | 183 | pc.printf("Aberto o socket em command mode.\n"); |
brunofgc | 0:1c0a769988ee | 184 | pc.printf("Enviando arquivo com nome %s.\n",arquivo->nome); |
brunofgc | 0:1c0a769988ee | 185 | modemCom::status.serverConnected=1; |
brunofgc | 0:1c0a769988ee | 186 | modemCom::timeOutModem = 100; |
brunofgc | 0:1c0a769988ee | 187 | modemCom::status.emComunicacao = true; |
brunofgc | 0:1c0a769988ee | 188 | } else { |
brunofgc | 0:1c0a769988ee | 189 | pc.printf("Nao foi possivel Abrir o socket. Abortando tentativa.\n"); |
brunofgc | 0:1c0a769988ee | 190 | modemCom::timeOutModem = 10; |
brunofgc | 0:1c0a769988ee | 191 | return 0; |
brunofgc | 0:1c0a769988ee | 192 | } |
brunofgc | 0:1c0a769988ee | 193 | |
brunofgc | 0:1c0a769988ee | 194 | numeroDePartes = arquivo->bytes / numeroDeBytesPorEnvio; |
brunofgc | 0:1c0a769988ee | 195 | restoDoEnvio = arquivo->bytes % numeroDeBytesPorEnvio; |
brunofgc | 0:1c0a769988ee | 196 | |
brunofgc | 3:9598af355293 | 197 | |
brunofgc | 3:9598af355293 | 198 | pc.printf("Iniciando o envio do arquivo de %lu bytes\nnumeroDePartes = %lu\nrestoDoEnvio = %lu\n",arquivo->bytes,numeroDePartes,restoDoEnvio); |
brunofgc | 0:1c0a769988ee | 199 | |
brunofgc | 0:1c0a769988ee | 200 | if(!sdCard::abreArquivo(arquivo,"r")){ |
brunofgc | 7:ae9c47f62946 | 201 | pc.printf("Nao foi posssivel abrir o arquivo de dentro da funcao postFileCommandMode().\n"); |
brunofgc | 0:1c0a769988ee | 202 | modemCom::status.emComunicacao = false; |
brunofgc | 0:1c0a769988ee | 203 | return 0; |
brunofgc | 0:1c0a769988ee | 204 | } |
brunofgc | 0:1c0a769988ee | 205 | |
brunofgc | 4:13ff9c81dc10 | 206 | |
brunofgc | 0:1c0a769988ee | 207 | //Monta Header aqui! |
brunofgc | 4:13ff9c81dc10 | 208 | //application/octet-stream |
brunofgc | 4:13ff9c81dc10 | 209 | sprintf(header,"--xxBOUNDARYxx\r\nContent-Type: text/plain\r\nContent-Disposition: form-data; name=\"IMEI\"\r\n\r\n%s\r\n--xxBOUNDARYxx\r\nContent-Type: application/octet-stream\r\nContent-Disposition: form-data; name=\"file\"; filename=\"%s\"\r\n\r\n",modemCom::status.MAC,arquivo->nome); |
brunofgc | 0:1c0a769988ee | 210 | |
brunofgc | 0:1c0a769988ee | 211 | dataTam = strlen(header); |
brunofgc | 0:1c0a769988ee | 212 | dataTam+=arquivo->bytes; |
brunofgc | 4:13ff9c81dc10 | 213 | dataTam+= strlen("\r\n--xxBOUNDARYxx--"); |
brunofgc | 0:1c0a769988ee | 214 | //"/drome/parser/index.php" uri drome |
brunofgc | 4:13ff9c81dc10 | 215 | sprintf(header,"POST %s HTTP/1.1\r\nHost: %s\r\nContent-Type: multipart/form-data; boundary=xxBOUNDARYxx\r\nContent-Length: %lu\r\n\r\n",uri,host,dataTam); |
brunofgc | 0:1c0a769988ee | 216 | headerTam = strlen(header); //Pego o tamanho parcial para fazer a concatenaçao a fim de predizer o tamanho do post total |
brunofgc | 0:1c0a769988ee | 217 | |
brunofgc | 4:13ff9c81dc10 | 218 | sprintf(&header[headerTam],"--xxBOUNDARYxx\r\nContent-Type: text/plain\r\nContent-Disposition: form-data; name=\"IMEI\"\r\n\r\n%s\r\n--xxBOUNDARYxx\r\nContent-Type: application/octet-stream\r\nContent-Disposition: form-data; name=\"file\"; filename=\"%s\"\r\n\r\n",modemCom::status.MAC,arquivo->nome); |
brunofgc | 0:1c0a769988ee | 219 | headerTam = strlen(header); |
brunofgc | 0:1c0a769988ee | 220 | |
brunofgc | 0:1c0a769988ee | 221 | sprintf(aux,"AT+CIPSEND=1,%u\r\n",headerTam); |
brunofgc | 7:ae9c47f62946 | 222 | if(modemCom::sendToModem(aux,1,&modemCom::status.PROMPT_ENVIO_COMMAND_MODE,NULL,100,10,1)) { |
brunofgc | 7:ae9c47f62946 | 223 | if(!modemCom::sendToModem(header,1,&modemCom::status.SEND_OK,NULL,1000,1,1)) { |
brunofgc | 0:1c0a769988ee | 224 | sdCard::fechaArquivo(arquivo); |
brunofgc | 0:1c0a769988ee | 225 | modemCom::status.emComunicacao = false; |
brunofgc | 3:9598af355293 | 226 | pc.printf("Cabecalho http nao enviado.\n"); |
brunofgc | 0:1c0a769988ee | 227 | return 0; |
brunofgc | 0:1c0a769988ee | 228 | } else { |
brunofgc | 3:9598af355293 | 229 | pc.printf("Cabecalho http enviado.\n"); |
brunofgc | 0:1c0a769988ee | 230 | } |
brunofgc | 0:1c0a769988ee | 231 | } else { |
brunofgc | 0:1c0a769988ee | 232 | pc.printf("Erro enviando arquivo.\n"); |
brunofgc | 0:1c0a769988ee | 233 | sdCard::fechaArquivo(arquivo); |
brunofgc | 0:1c0a769988ee | 234 | modemCom::status.emComunicacao = false; |
brunofgc | 0:1c0a769988ee | 235 | return 0; |
brunofgc | 0:1c0a769988ee | 236 | } |
brunofgc | 0:1c0a769988ee | 237 | |
brunofgc | 0:1c0a769988ee | 238 | //Enviando as partes inteiras |
brunofgc | 0:1c0a769988ee | 239 | maxTentativas = maxRetentativasEnvioParteArquivo; |
brunofgc | 0:1c0a769988ee | 240 | for(parteSendoEnviada=0; parteSendoEnviada<numeroDePartes; parteSendoEnviada++) { |
brunofgc | 0:1c0a769988ee | 241 | //Aqui realizo o envio de cada parte. |
brunofgc | 0:1c0a769988ee | 242 | passaParaProximaParte = false; |
brunofgc | 0:1c0a769988ee | 243 | while((maxTentativas)&&(!passaParaProximaParte)) { |
brunofgc | 0:1c0a769988ee | 244 | sprintf(aux,"AT+CIPSEND=1,%u\r\n",numeroDeBytesPorEnvio); |
brunofgc | 7:ae9c47f62946 | 245 | if(modemCom::sendToModem(aux,1,&modemCom::status.PROMPT_ENVIO_COMMAND_MODE,NULL,100,20,1)) { |
brunofgc | 0:1c0a769988ee | 246 | if(maxTentativas!=maxRetentativasEnvioParteArquivo) { |
brunofgc | 0:1c0a769988ee | 247 | //Estou realizando reenvio desta parte logo preciso fazer fseek para a quantidade de bytes do envio anterior |
brunofgc | 0:1c0a769988ee | 248 | fseek(arquivo->fp,-numeroDeBytesPorEnvio,SEEK_CUR); |
brunofgc | 0:1c0a769988ee | 249 | } |
brunofgc | 3:9598af355293 | 250 | pc.printf("Numero de bytes por envio = %lu.\r\n",numeroDeBytesPorEnvio); |
brunofgc | 0:1c0a769988ee | 251 | for(numeroByteSendoEnviado=0; numeroByteSendoEnviado<(numeroDeBytesPorEnvio); numeroByteSendoEnviado++) { |
brunofgc | 0:1c0a769988ee | 252 | c = fgetc(arquivo->fp); |
brunofgc | 0:1c0a769988ee | 253 | modem.printf("%c",c); |
brunofgc | 0:1c0a769988ee | 254 | #ifdef xereta_modem |
brunofgc | 4:13ff9c81dc10 | 255 | pc.printf("%c",c); |
brunofgc | 0:1c0a769988ee | 256 | #endif |
brunofgc | 0:1c0a769988ee | 257 | } |
brunofgc | 0:1c0a769988ee | 258 | |
brunofgc | 7:ae9c47f62946 | 259 | if(modemCom::sendToModem(NULL,1,&modemCom::status.SEND_OK,NULL,5000,1,1)) { |
brunofgc | 0:1c0a769988ee | 260 | diversos::progressBar(parteSendoEnviada,numeroDePartes); |
brunofgc | 0:1c0a769988ee | 261 | //Preciso verificar se o buffer esvaziou. Se deu "flush" |
brunofgc | 0:1c0a769988ee | 262 | //if(modemCom::aguardaFlush(0)==1) { |
brunofgc | 0:1c0a769988ee | 263 | //passa pra proxima parte |
brunofgc | 0:1c0a769988ee | 264 | passaParaProximaParte = true; |
brunofgc | 0:1c0a769988ee | 265 | maxTentativas = maxRetentativasEnvioParteArquivo; |
brunofgc | 0:1c0a769988ee | 266 | } else { |
brunofgc | 0:1c0a769988ee | 267 | maxTentativas--; |
brunofgc | 0:1c0a769988ee | 268 | if(maxTentativas == 1) { |
brunofgc | 0:1c0a769988ee | 269 | pc.printf("MaxTentativas dentro de envio das partes == 1, vou aguardar o flush.\n"); |
brunofgc | 0:1c0a769988ee | 270 | } |
brunofgc | 0:1c0a769988ee | 271 | if(maxTentativas == 0) { |
brunofgc | 0:1c0a769988ee | 272 | sdCard::fechaArquivo(arquivo); |
brunofgc | 0:1c0a769988ee | 273 | modemCom::status.emComunicacao = false; |
brunofgc | 0:1c0a769988ee | 274 | return 0; |
brunofgc | 0:1c0a769988ee | 275 | } |
brunofgc | 0:1c0a769988ee | 276 | } |
brunofgc | 0:1c0a769988ee | 277 | } else { |
brunofgc | 0:1c0a769988ee | 278 | pc.printf("Erro enviando o arquivo nao recebeu a tentativa de envio de parte inteira.\n"); |
brunofgc | 0:1c0a769988ee | 279 | sdCard::fechaArquivo(arquivo); |
brunofgc | 0:1c0a769988ee | 280 | modemCom::status.emComunicacao = false; |
brunofgc | 0:1c0a769988ee | 281 | return 0; |
brunofgc | 0:1c0a769988ee | 282 | } |
brunofgc | 0:1c0a769988ee | 283 | } |
brunofgc | 0:1c0a769988ee | 284 | if(!passaParaProximaParte) { |
brunofgc | 0:1c0a769988ee | 285 | pc.printf("Erro enviando o arquivo nao recebeu a tentativa de envio de parte inteira.\n"); |
brunofgc | 0:1c0a769988ee | 286 | sdCard::fechaArquivo(arquivo); |
brunofgc | 0:1c0a769988ee | 287 | modemCom::status.emComunicacao = false; |
brunofgc | 0:1c0a769988ee | 288 | return 0; |
brunofgc | 0:1c0a769988ee | 289 | } |
brunofgc | 0:1c0a769988ee | 290 | } |
brunofgc | 0:1c0a769988ee | 291 | |
brunofgc | 0:1c0a769988ee | 292 | //Enviando o resto |
brunofgc | 0:1c0a769988ee | 293 | passaParaProximaParte = false; |
brunofgc | 0:1c0a769988ee | 294 | maxTentativas = maxRetentativasEnvioParteArquivo; |
brunofgc | 0:1c0a769988ee | 295 | while((maxTentativas)&&(!passaParaProximaParte)) { |
brunofgc | 4:13ff9c81dc10 | 296 | sprintf(aux,"AT+CIPSEND=1,%u\r\n",restoDoEnvio+strlen("\r\n--xxBOUNDARYxx--\r\n\r\n")); //Para incluir "\n--xxBOUNDARYxx--\n\n" no fim da msg http |
brunofgc | 7:ae9c47f62946 | 297 | if(modemCom::sendToModem(aux,1,&modemCom::status.PROMPT_ENVIO_COMMAND_MODE,NULL,100,3,1)) { |
brunofgc | 0:1c0a769988ee | 298 | for(numeroByteSendoEnviado=0; numeroByteSendoEnviado<restoDoEnvio; numeroByteSendoEnviado++) { |
brunofgc | 0:1c0a769988ee | 299 | //Envio os bytes aqui! Menos o ultimo para ter o check; |
brunofgc | 0:1c0a769988ee | 300 | c = fgetc(arquivo->fp); |
brunofgc | 0:1c0a769988ee | 301 | modem.printf("%c",c); |
brunofgc | 0:1c0a769988ee | 302 | #ifdef xereta_modem |
brunofgc | 0:1c0a769988ee | 303 | pc.printf("%c",c); |
brunofgc | 0:1c0a769988ee | 304 | #endif |
brunofgc | 0:1c0a769988ee | 305 | } |
brunofgc | 7:ae9c47f62946 | 306 | if(modemCom::sendToModem("\r\n--xxBOUNDARYxx--\r\n\r\n",1,&modemCom::status.SEND_OK,NULL,5000,1,1)) { |
brunofgc | 0:1c0a769988ee | 307 | if(numeroDePartes) { |
brunofgc | 0:1c0a769988ee | 308 | diversos::progressBar(parteSendoEnviada,numeroDePartes); |
brunofgc | 0:1c0a769988ee | 309 | } |
brunofgc | 0:1c0a769988ee | 310 | passaParaProximaParte = true; |
brunofgc | 0:1c0a769988ee | 311 | pc.printf("\nEnviado o resto do envio com fim de cabecalho!\n",parteSendoEnviada); |
brunofgc | 0:1c0a769988ee | 312 | modemCom::timeOutModem = 250; |
brunofgc | 0:1c0a769988ee | 313 | //} |
brunofgc | 0:1c0a769988ee | 314 | /*else { |
brunofgc | 0:1c0a769988ee | 315 | pc.printf("Erro enviando arquivo.\n"); |
brunofgc | 0:1c0a769988ee | 316 | fclose(fp); |
brunofgc | 0:1c0a769988ee | 317 | *aberto = false; |
brunofgc | 0:1c0a769988ee | 318 | return 0; |
brunofgc | 0:1c0a769988ee | 319 | }*/ |
brunofgc | 0:1c0a769988ee | 320 | } else { |
brunofgc | 0:1c0a769988ee | 321 | sdCard::fechaArquivo(arquivo); |
brunofgc | 0:1c0a769988ee | 322 | modemCom::status.emComunicacao = false; |
brunofgc | 0:1c0a769988ee | 323 | return 0; |
brunofgc | 0:1c0a769988ee | 324 | } |
brunofgc | 0:1c0a769988ee | 325 | } else { |
brunofgc | 0:1c0a769988ee | 326 | maxTentativas--; |
brunofgc | 7:ae9c47f62946 | 327 | modemCom::sendToModem("AT\r\n",1,&modemCom::status.OK,NULL,1000,100,1); |
brunofgc | 0:1c0a769988ee | 328 | } |
brunofgc | 0:1c0a769988ee | 329 | } |
brunofgc | 0:1c0a769988ee | 330 | |
brunofgc | 0:1c0a769988ee | 331 | if(!passaParaProximaParte) { |
brunofgc | 0:1c0a769988ee | 332 | pc.printf("Erro enviando o arquivo nao recebeu a tentativa de envio de parte inteira.\n"); |
brunofgc | 0:1c0a769988ee | 333 | sdCard::fechaArquivo(arquivo); |
brunofgc | 0:1c0a769988ee | 334 | modemCom::status.emComunicacao = false; |
brunofgc | 0:1c0a769988ee | 335 | return 0; |
brunofgc | 0:1c0a769988ee | 336 | } |
brunofgc | 0:1c0a769988ee | 337 | |
brunofgc | 0:1c0a769988ee | 338 | //Logica de timeout esperando confirmação modemCom::status.ServerAck 20 segundos |
brunofgc | 7:ae9c47f62946 | 339 | parteSendoEnviada = 200; |
brunofgc | 0:1c0a769988ee | 340 | while((parteSendoEnviada)&&(!modemCom::status.ServerAck)&&(!modemCom::status.NO_CARRIER)) { |
brunofgc | 0:1c0a769988ee | 341 | osDelay(100); |
brunofgc | 0:1c0a769988ee | 342 | parteSendoEnviada--; |
brunofgc | 0:1c0a769988ee | 343 | } |
brunofgc | 0:1c0a769988ee | 344 | |
brunofgc | 0:1c0a769988ee | 345 | modemCom::status.serverConnected=0; |
brunofgc | 0:1c0a769988ee | 346 | sdCard::fechaArquivo(arquivo); |
brunofgc | 0:1c0a769988ee | 347 | |
brunofgc | 0:1c0a769988ee | 348 | //modemCom::sendToModem("AT+CIPCLOSE=1\r\n",1,&modemCom::status.OK,500,3); |
brunofgc | 0:1c0a769988ee | 349 | modemCom::closeConnection(1); |
brunofgc | 0:1c0a769988ee | 350 | modemCom::status.emComunicacao = false; |
brunofgc | 0:1c0a769988ee | 351 | return modemCom::status.ServerAck; |
brunofgc | 0:1c0a769988ee | 352 | } |
brunofgc | 0:1c0a769988ee | 353 | |
brunofgc | 7:ae9c47f62946 | 354 | uint8_t modemCom::sendToModem(char *string,uint8_t confirmado,char *codigoConfirmacao,char *stringConfirmacao,uint16_t timeOut,uint8_t maxTentativas,uint16_t delayEntreTentativas) |
brunofgc | 0:1c0a769988ee | 355 | { |
brunofgc | 0:1c0a769988ee | 356 | uint16_t timeOutProcesso; |
brunofgc | 0:1c0a769988ee | 357 | uint16_t indexString,strLen; //Para exibir retentativas! |
brunofgc | 7:ae9c47f62946 | 358 | |
brunofgc | 0:1c0a769988ee | 359 | while(maxTentativas){ |
brunofgc | 0:1c0a769988ee | 360 | timeOutProcesso=timeOut; |
brunofgc | 7:ae9c47f62946 | 361 | |
brunofgc | 7:ae9c47f62946 | 362 | |
brunofgc | 7:ae9c47f62946 | 363 | bufModem.del(); //Deletando o buffer antes de iniciar envio |
brunofgc | 7:ae9c47f62946 | 364 | |
brunofgc | 0:1c0a769988ee | 365 | if(string!=NULL) { |
brunofgc | 7:ae9c47f62946 | 366 | modem.puts(string); |
brunofgc | 7:ae9c47f62946 | 367 | #ifdef xereta_modem |
brunofgc | 7:ae9c47f62946 | 368 | pc.printf("Sai pro modem <%s>\n",string); //Enviando na serial PC para debug |
brunofgc | 7:ae9c47f62946 | 369 | #endif |
brunofgc | 0:1c0a769988ee | 370 | } |
brunofgc | 7:ae9c47f62946 | 371 | if(confirmado==1){ |
brunofgc | 0:1c0a769988ee | 372 | modemCom::status.ERROR=0; |
brunofgc | 7:ae9c47f62946 | 373 | modemCom::status.busy=0; |
brunofgc | 7:ae9c47f62946 | 374 | |
brunofgc | 7:ae9c47f62946 | 375 | if(codigoConfirmacao!=NULL){ |
brunofgc | 7:ae9c47f62946 | 376 | *codigoConfirmacao=0; |
brunofgc | 7:ae9c47f62946 | 377 | while((timeOutProcesso)&&(!(*codigoConfirmacao))&&(!modemCom::status.ERROR)&&(!modemCom::status.busy)){ |
brunofgc | 7:ae9c47f62946 | 378 | osDelay(5); //timeOut em mS |
brunofgc | 7:ae9c47f62946 | 379 | timeOutProcesso--; |
brunofgc | 7:ae9c47f62946 | 380 | } |
brunofgc | 7:ae9c47f62946 | 381 | }else{ |
brunofgc | 7:ae9c47f62946 | 382 | while((timeOutProcesso)&&(!modemCom::status.ERROR)&&(!modemCom::status.busy)&&(!strstr(bufModem.getRowBuffer(),stringConfirmacao))) { |
brunofgc | 7:ae9c47f62946 | 383 | osDelay(1); //timeOut em mS |
brunofgc | 7:ae9c47f62946 | 384 | timeOutProcesso--; |
brunofgc | 7:ae9c47f62946 | 385 | } |
brunofgc | 7:ae9c47f62946 | 386 | } |
brunofgc | 7:ae9c47f62946 | 387 | if(modemCom::status.ERROR||modemCom::status.busy) { |
brunofgc | 7:ae9c47f62946 | 388 | osDelay(1000);//espera 1S |
brunofgc | 0:1c0a769988ee | 389 | } |
brunofgc | 7:ae9c47f62946 | 390 | |
brunofgc | 7:ae9c47f62946 | 391 | if(timeOutProcesso) { |
brunofgc | 0:1c0a769988ee | 392 | return maxTentativas; |
brunofgc | 7:ae9c47f62946 | 393 | } |
brunofgc | 7:ae9c47f62946 | 394 | |
brunofgc | 0:1c0a769988ee | 395 | strLen = strlen(string); |
brunofgc | 0:1c0a769988ee | 396 | for(indexString=0; indexString<(strLen-1); indexString++) { |
brunofgc | 0:1c0a769988ee | 397 | pc.putc(string[indexString]); |
brunofgc | 0:1c0a769988ee | 398 | } |
brunofgc | 7:ae9c47f62946 | 399 | #ifdef xereta_modem |
brunofgc | 7:ae9c47f62946 | 400 | pc.printf("> maxTentativas = %lu.\n",maxTentativas); |
brunofgc | 7:ae9c47f62946 | 401 | #endif |
brunofgc | 0:1c0a769988ee | 402 | } |
brunofgc | 7:ae9c47f62946 | 403 | maxTentativas--; |
brunofgc | 7:ae9c47f62946 | 404 | osDelay(delayEntreTentativas); |
brunofgc | 7:ae9c47f62946 | 405 | } |
brunofgc | 0:1c0a769988ee | 406 | return 0; |
brunofgc | 0:1c0a769988ee | 407 | } |
brunofgc | 0:1c0a769988ee | 408 | |
brunofgc | 0:1c0a769988ee | 409 | bool modemCom::closeConnection(uint8_t id){ |
brunofgc | 0:1c0a769988ee | 410 | char command[20]; |
brunofgc | 0:1c0a769988ee | 411 | sprintf(command,"AT+CIPCLOSE=%u\r\n",id); |
brunofgc | 7:ae9c47f62946 | 412 | return modemCom::sendToModem(command,1,&modemCom::status.OK,NULL,500,3,1); |
brunofgc | 0:1c0a769988ee | 413 | } |
brunofgc | 0:1c0a769988ee | 414 | |
brunofgc | 0:1c0a769988ee | 415 | char modemCom::cipSend(uint8_t connID,char *buffer,uint16_t len){ |
brunofgc | 0:1c0a769988ee | 416 | char aux[20]; |
brunofgc | 0:1c0a769988ee | 417 | uint16_t i; |
brunofgc | 0:1c0a769988ee | 418 | if(!len){ |
brunofgc | 0:1c0a769988ee | 419 | len = strlen(buffer); |
brunofgc | 0:1c0a769988ee | 420 | } |
brunofgc | 0:1c0a769988ee | 421 | printf("conteudo de cipsend.<"); |
brunofgc | 0:1c0a769988ee | 422 | sprintf(aux,"AT+CIPSENDEX=%u,%lu\r\n",connID,len); |
brunofgc | 7:ae9c47f62946 | 423 | if(sendToModem(aux,1,&modemCom::status.OK,NULL,500,1,1)){ |
brunofgc | 0:1c0a769988ee | 424 | for(i=0;i<len;i++){ |
brunofgc | 0:1c0a769988ee | 425 | modem.putc(buffer[i]); |
brunofgc | 0:1c0a769988ee | 426 | pc.putc(buffer[i]); |
brunofgc | 0:1c0a769988ee | 427 | } |
brunofgc | 0:1c0a769988ee | 428 | }else{ |
brunofgc | 0:1c0a769988ee | 429 | return 0; |
brunofgc | 0:1c0a769988ee | 430 | } |
brunofgc | 0:1c0a769988ee | 431 | printf(">\r\n"); |
brunofgc | 7:ae9c47f62946 | 432 | if(sendToModem("",1,&modemCom::status.SEND_OK,NULL,2000,1,1)){ |
brunofgc | 0:1c0a769988ee | 433 | return 1; |
brunofgc | 0:1c0a769988ee | 434 | } |
brunofgc | 0:1c0a769988ee | 435 | return 2; |
brunofgc | 0:1c0a769988ee | 436 | } |
brunofgc | 0:1c0a769988ee | 437 | |
brunofgc | 0:1c0a769988ee | 438 | void modemCom::webServer(void){ |
brunofgc | 0:1c0a769988ee | 439 | char buf[512]; |
brunofgc | 7:ae9c47f62946 | 440 | //char auxMsg[50]; |
brunofgc | 0:1c0a769988ee | 441 | bool encontrado=0; |
brunofgc | 7:ae9c47f62946 | 442 | //uint32_t aux_int; |
brunofgc | 0:1c0a769988ee | 443 | |
brunofgc | 0:1c0a769988ee | 444 | |
brunofgc | 0:1c0a769988ee | 445 | |
brunofgc | 2:55b7b466e742 | 446 | if(strstr(bufInPtr,"favicon.ico")){ |
brunofgc | 2:55b7b466e742 | 447 | //sprintf(buf,"<!DOCTYPE html><html><body><h1>DROME-Seu processo na palma da sua mao!</h1><img src='http://criandoriquezaimagens.s3.amazonaws.com/wp-content/uploads/2014/12/casaPropria.png' width='200' height='210'><br><p><a href='/lecomandocontrole.htm'>Ler comando do controle.</a></p><br></body></html>"); |
brunofgc | 2:55b7b466e742 | 448 | sprintf(buf,"<!DOCTYPE html><html><link rel=\"icon\" href=\"http://vitaesolucoes.com.br/drome/assets/local/images/favicon/favicon.ico\" type=\"image/x-icon\"></html>"); |
brunofgc | 2:55b7b466e742 | 449 | if(cipSend(connID,buf,0)==1){ |
brunofgc | 2:55b7b466e742 | 450 | printf("Requisicao enviada!\n"); |
brunofgc | 2:55b7b466e742 | 451 | }else{ |
brunofgc | 2:55b7b466e742 | 452 | printf("Requisicao nao enviada 1!!\n"); |
brunofgc | 2:55b7b466e742 | 453 | } |
brunofgc | 2:55b7b466e742 | 454 | encontrado = 1; |
brunofgc | 2:55b7b466e742 | 455 | } |
brunofgc | 2:55b7b466e742 | 456 | |
brunofgc | 0:1c0a769988ee | 457 | if(strstr(bufInPtr,"comandos.htm")){ |
brunofgc | 0:1c0a769988ee | 458 | |
brunofgc | 0:1c0a769988ee | 459 | //sprintf(buf,"<!DOCTYPE html><html><body><h1>DROME-Seu processo na palma da sua mao!</h1><img src='http://criandoriquezaimagens.s3.amazonaws.com/wp-content/uploads/2014/12/casaPropria.png' width='200' height='210'><br><p><a href='/lecomandocontrole.htm'>Ler comando do controle.</a></p><br></body></html>"); |
brunofgc | 0:1c0a769988ee | 460 | sprintf(buf,"<!DOCTYPE html><html><body><h1>DROME-Dispositivo Remoto de Operacao e Monitoramento de Equipamentos</h1><br><p><a href='/lecomandocontrole.htm'>Ler comando do controle.</a><a href='/configRede.htm'><br>Configurar rede.</a></p><br></body></html>"); |
brunofgc | 0:1c0a769988ee | 461 | if(cipSend(connID,buf,0)==1){ |
brunofgc | 0:1c0a769988ee | 462 | printf("Requisicao enviada!\n"); |
brunofgc | 0:1c0a769988ee | 463 | }else{ |
brunofgc | 0:1c0a769988ee | 464 | printf("Requisicao nao enviada 1!!\n"); |
brunofgc | 0:1c0a769988ee | 465 | } |
brunofgc | 0:1c0a769988ee | 466 | encontrado = 1; |
brunofgc | 0:1c0a769988ee | 467 | } |
brunofgc | 0:1c0a769988ee | 468 | |
brunofgc | 7:ae9c47f62946 | 469 | if(strstr(bufInPtr,"configRede.htm")){ |
brunofgc | 0:1c0a769988ee | 470 | //Pegando o IP |
brunofgc | 7:ae9c47f62946 | 471 | modemCom::leIP(); |
brunofgc | 0:1c0a769988ee | 472 | //sprintf(buf,"<script>window.location.href = 'http://www.vitaesolucoes.com.br/controladorawifi/config.php?ip=%s'</script>",modemStatus.STAIP); |
brunofgc | 0:1c0a769988ee | 473 | |
brunofgc | 0:1c0a769988ee | 474 | //sprintf(buf,"<!DOCTYPE html><form action='cwjap.htm' method='GET'><fieldset><legend>Parametros de Configuracao da rede:</legend>SSID WiFi:<br><input type='text' name='ssid' value=''><br>Senha WiFi:<br><input type='text' name='password' value=''><br>IP:(0 para DHCP)<br><input type='text' name='ip' value='0'><br>Porta:<br><input type='text' name='port' value=''><br><input type='submit' value='Configurar'></fieldset></form>"); |
brunofgc | 0:1c0a769988ee | 475 | sprintf(buf,"<!DOCTYPE html>IP Atual = %s.<br><form action='cwjap.htm' method='GET'><fieldset><legend>Parametros de Configuracao da rede:</legend>SSID WiFi:<br><input type='text' name='ssid' value='%s'><br>Senha WiFi:<br><input type='text' name='password' value='%s'><br>PORTA:<br><input type='text' name='porta' value='%s'><br><input type='submit' value='Configurar'></fieldset></form>",modemCom::status.STAIP,modemCom::status.ssid,modemCom::status.password,modemCom::status.port); |
brunofgc | 0:1c0a769988ee | 476 | |
brunofgc | 0:1c0a769988ee | 477 | cipSend(connID,buf,0); |
brunofgc | 0:1c0a769988ee | 478 | encontrado = 1; |
brunofgc | 0:1c0a769988ee | 479 | } |
brunofgc | 0:1c0a769988ee | 480 | |
brunofgc | 0:1c0a769988ee | 481 | if(strstr(bufInPtr,"cwjap.htm")){ |
brunofgc | 0:1c0a769988ee | 482 | //cwjap.htm?ssid=VSA&password=vitaeBBYP&ip=0&port=80 |
brunofgc | 0:1c0a769988ee | 483 | |
brunofgc | 0:1c0a769988ee | 484 | strtok(bufInPtr,"="); |
brunofgc | 3:9598af355293 | 485 | strcpy(modemCom::status.ssid,strtok(NULL,"&")); //pegando ssid |
brunofgc | 0:1c0a769988ee | 486 | |
brunofgc | 0:1c0a769988ee | 487 | strtok(NULL,"="); |
brunofgc | 0:1c0a769988ee | 488 | strcpy(modemCom::status.password,strtok(NULL,"&")); //pegando password |
brunofgc | 0:1c0a769988ee | 489 | |
brunofgc | 0:1c0a769988ee | 490 | //strtok(NULL,"="); |
brunofgc | 0:1c0a769988ee | 491 | //strcpy(modemStatus.STAIP,strtok(NULL,"&")); //pegando ip |
brunofgc | 0:1c0a769988ee | 492 | |
brunofgc | 0:1c0a769988ee | 493 | strtok(NULL,"="); |
brunofgc | 3:9598af355293 | 494 | strcpy(modemCom::status.port,strtok(NULL," ")); //pegando port |
brunofgc | 0:1c0a769988ee | 495 | |
brunofgc | 3:9598af355293 | 496 | pc.printf("Antes <%s>.\r\n",modemCom::status.ssid); |
brunofgc | 3:9598af355293 | 497 | diversos::strReplace(modemCom::status.ssid,"%20"," "); |
brunofgc | 3:9598af355293 | 498 | pc.printf("Tirado %%20 <%s>.\r\n",modemCom::status.ssid); |
brunofgc | 3:9598af355293 | 499 | diversos::strReplace(modemCom::status.ssid,"+"," "); |
brunofgc | 3:9598af355293 | 500 | pc.printf("Tirado + <%s>.\r\n",modemCom::status.ssid); |
brunofgc | 3:9598af355293 | 501 | |
brunofgc | 3:9598af355293 | 502 | if((modemCom::status.ssid[0]!=0) && (modemCom::status.password[0]!=0)){ |
brunofgc | 3:9598af355293 | 503 | sprintf(buf,"HTTP/1.1 200 OK\n\nACK"); |
brunofgc | 0:1c0a769988ee | 504 | mudaRede = true; |
brunofgc | 0:1c0a769988ee | 505 | }else{ |
brunofgc | 2:55b7b466e742 | 506 | sprintf(buf,"HTTP/1.1 200 OK\n\nNACK"); |
brunofgc | 3:9598af355293 | 507 | } |
brunofgc | 3:9598af355293 | 508 | cipSend(connID,buf,0); |
brunofgc | 0:1c0a769988ee | 509 | encontrado = 1; |
brunofgc | 0:1c0a769988ee | 510 | } |
brunofgc | 0:1c0a769988ee | 511 | |
brunofgc | 0:1c0a769988ee | 512 | /*if(strstr(bufInPtr,"leTemperaturas.htm")){ |
brunofgc | 0:1c0a769988ee | 513 | |
brunofgc | 0:1c0a769988ee | 514 | |
brunofgc | 0:1c0a769988ee | 515 | |
brunofgc | 0:1c0a769988ee | 516 | strtok(bufInPtr,"="); |
brunofgc | 0:1c0a769988ee | 517 | bufInPtr = strtok(NULL,NULL); |
brunofgc | 0:1c0a769988ee | 518 | //sprintf(buf,"%s",bufInPtr); |
brunofgc | 0:1c0a769988ee | 519 | aux_int = atoi(bufInPtr); |
brunofgc | 0:1c0a769988ee | 520 | aux_int--; |
brunofgc | 0:1c0a769988ee | 521 | */ |
brunofgc | 0:1c0a769988ee | 522 | |
brunofgc | 0:1c0a769988ee | 523 | /*if(num_temperatureSensors){ |
brunofgc | 0:1c0a769988ee | 524 | //Zerano conteudo de buff |
brunofgc | 0:1c0a769988ee | 525 | sprintf(buf,"callback({\"response\":\""); |
brunofgc | 0:1c0a769988ee | 526 | probe[0]->convertTemperature(true, DS1820::all_devices); //Start temperature conversion, wait until ready |
brunofgc | 0:1c0a769988ee | 527 | for (int i = 0; i<num_temperatureSensors; i++){ |
brunofgc | 0:1c0a769988ee | 528 | sprintf(auxMsg,"%3.1f;",probe[i]->temperature()); |
brunofgc | 0:1c0a769988ee | 529 | strcat(buf,auxMsg); |
brunofgc | 0:1c0a769988ee | 530 | } |
brunofgc | 0:1c0a769988ee | 531 | buf[strlen(buf)-1]=0; |
brunofgc | 0:1c0a769988ee | 532 | sprintf(auxMsg,"\"})"); |
brunofgc | 0:1c0a769988ee | 533 | strcat(buf,auxMsg); |
brunofgc | 0:1c0a769988ee | 534 | }else{ |
brunofgc | 0:1c0a769988ee | 535 | sprintf(buf,"callback({\"response\":\"Sensor_ERROR\"})"); |
brunofgc | 0:1c0a769988ee | 536 | } |
brunofgc | 0:1c0a769988ee | 537 | if(cipSend(connID,buf,0)==1){ |
brunofgc | 0:1c0a769988ee | 538 | printf("Requisicao enviada!\n"); |
brunofgc | 0:1c0a769988ee | 539 | }else{ |
brunofgc | 0:1c0a769988ee | 540 | printf("Requisicao nao enviada 1!!\n"); |
brunofgc | 0:1c0a769988ee | 541 | } |
brunofgc | 0:1c0a769988ee | 542 | encontrado = 1; |
brunofgc | 0:1c0a769988ee | 543 | }*/ |
brunofgc | 0:1c0a769988ee | 544 | |
brunofgc | 0:1c0a769988ee | 545 | |
brunofgc | 0:1c0a769988ee | 546 | |
brunofgc | 0:1c0a769988ee | 547 | if(strstr(bufInPtr,"lecomandocontrole.htm")){ |
brunofgc | 7:ae9c47f62946 | 548 | |
brunofgc | 0:1c0a769988ee | 549 | uint16_t timeout = 5000; |
brunofgc | 0:1c0a769988ee | 550 | IrDetect.reset(); |
brunofgc | 0:1c0a769988ee | 551 | IrDetect.start(); |
brunofgc | 0:1c0a769988ee | 552 | |
brunofgc | 0:1c0a769988ee | 553 | while((IrDetect.read_ms()<timeout) && (!detectaIRIn())); |
brunofgc | 0:1c0a769988ee | 554 | if(IrDetect.read_ms()<timeout){ |
brunofgc | 0:1c0a769988ee | 555 | serializaPacoteIR(connID); |
brunofgc | 0:1c0a769988ee | 556 | }else{ |
brunofgc | 2:55b7b466e742 | 557 | sprintf(buf,"erro"); |
brunofgc | 0:1c0a769988ee | 558 | if(cipSend(connID,buf,0)==1){ |
brunofgc | 0:1c0a769988ee | 559 | printf("Requisicao enviada!\n"); |
brunofgc | 0:1c0a769988ee | 560 | } |
brunofgc | 0:1c0a769988ee | 561 | } |
brunofgc | 6:d4ebbaaba295 | 562 | IrDetect.stop(); |
brunofgc | 7:ae9c47f62946 | 563 | encontrado = 1; |
brunofgc | 0:1c0a769988ee | 564 | } |
brunofgc | 0:1c0a769988ee | 565 | |
brunofgc | 2:55b7b466e742 | 566 | if(!encontrado){ |
brunofgc | 0:1c0a769988ee | 567 | sprintf(buf,"Pagina Nao Encontrada."); |
brunofgc | 0:1c0a769988ee | 568 | if(cipSend(connID,buf,0)==1){ |
brunofgc | 0:1c0a769988ee | 569 | printf("Requisicao enviada!\n"); |
brunofgc | 0:1c0a769988ee | 570 | }else{ |
brunofgc | 0:1c0a769988ee | 571 | printf("Requisicao nao enviada 3!!\n"); |
brunofgc | 0:1c0a769988ee | 572 | } |
brunofgc | 0:1c0a769988ee | 573 | } |
brunofgc | 0:1c0a769988ee | 574 | |
brunofgc | 2:55b7b466e742 | 575 | modemCom::closeConnection(connID); |
brunofgc | 2:55b7b466e742 | 576 | /*sprintf(buf,"AT+CIPCLOSE=%u\r\n",connID); |
brunofgc | 2:55b7b466e742 | 577 | sendToModem(buf,1,&modemCom::status.OK,300,1);*/ |
brunofgc | 0:1c0a769988ee | 578 | |
brunofgc | 0:1c0a769988ee | 579 | if(mudaRede){ |
brunofgc | 0:1c0a769988ee | 580 | mudaRede = false; |
brunofgc | 7:ae9c47f62946 | 581 | modemCom::sendToModem("AT+CIPSERVER=0\r\n",1,&modemCom::status.OK,NULL,2000,1,1); |
brunofgc | 0:1c0a769988ee | 582 | pc.printf("Mudando para rede:\r\nSSID:%s\r\nPASSWORD:%S\r\nPORTA:%s\r\n\r\n",modemCom::status.ssid,modemCom::status.password,modemCom::status.port); |
brunofgc | 0:1c0a769988ee | 583 | if(sdCard::abreArquivo(&sdCard::config,"w")){ |
brunofgc | 0:1c0a769988ee | 584 | pc.printf("Guardando config.\r\n"); |
brunofgc | 0:1c0a769988ee | 585 | fprintf(sdCard::config.fp,"SSID:%s\r\nPASSWORD:%s\r\nPORT:%s",modemCom::status.ssid,modemCom::status.password,modemCom::status.port); |
brunofgc | 0:1c0a769988ee | 586 | sdCard::fechaArquivo(&sdCard::config); |
brunofgc | 0:1c0a769988ee | 587 | modemCom::conectaWiFi(); |
brunofgc | 0:1c0a769988ee | 588 | } |
brunofgc | 0:1c0a769988ee | 589 | } |
brunofgc | 0:1c0a769988ee | 590 | } |
brunofgc | 1:0e0967c88590 | 591 | |
brunofgc | 7:ae9c47f62946 | 592 | bool modemCom::leIP(void){ |
brunofgc | 7:ae9c47f62946 | 593 | char *ptr; |
brunofgc | 7:ae9c47f62946 | 594 | sendToModem("AT+CIFSR\r\n",1,NULL,"+CIFSR:STAIP,",200,1,1); |
brunofgc | 7:ae9c47f62946 | 595 | ptr=strstr(modemCom::bufIn,"+CIFSR:STAIP,"); |
brunofgc | 7:ae9c47f62946 | 596 | if(ptr) { |
brunofgc | 7:ae9c47f62946 | 597 | ptr = strtok(ptr,"\""); |
brunofgc | 7:ae9c47f62946 | 598 | ptr = strtok(NULL,"\""); |
brunofgc | 7:ae9c47f62946 | 599 | strcpy(modemCom::status.STAIP,ptr); |
brunofgc | 7:ae9c47f62946 | 600 | } |
brunofgc | 7:ae9c47f62946 | 601 | if(strstr(modemCom::status.STAIP,"0.0.0.0")){return false;} |
brunofgc | 7:ae9c47f62946 | 602 | return true; |
brunofgc | 7:ae9c47f62946 | 603 | } |
brunofgc | 7:ae9c47f62946 | 604 | |
brunofgc | 1:0e0967c88590 | 605 | int16_t modemCom::getRSSI(void){ |
brunofgc | 2:55b7b466e742 | 606 | /*char *ptr; |
brunofgc | 1:0e0967c88590 | 607 | int16_t RSSI = 0; |
brunofgc | 2:55b7b466e742 | 608 | if(modemCom::sendToModem("AT+CWLAP\r\n",1,&modemCom::status.OK,1000,1)){ |
brunofgc | 1:0e0967c88590 | 609 | ptr = strstr(modemCom::bufIn,(const char*)modemCom::status.ssid); |
brunofgc | 1:0e0967c88590 | 610 | if(ptr){ |
brunofgc | 1:0e0967c88590 | 611 | ptr = strtok(ptr,","); |
brunofgc | 1:0e0967c88590 | 612 | ptr = strtok(NULL,","); |
brunofgc | 1:0e0967c88590 | 613 | RSSI = atoi(ptr); |
brunofgc | 1:0e0967c88590 | 614 | } |
brunofgc | 1:0e0967c88590 | 615 | } |
brunofgc | 2:55b7b466e742 | 616 | return RSSI;*/ |
brunofgc | 2:55b7b466e742 | 617 | return -50; |
brunofgc | 1:0e0967c88590 | 618 | } |
brunofgc | 7:ae9c47f62946 | 619 | |
brunofgc | 7:ae9c47f62946 | 620 | bool modemCom::verificaConexao(void){ |
brunofgc | 7:ae9c47f62946 | 621 | uint8_t i = 1; |
brunofgc | 7:ae9c47f62946 | 622 | while(i){ |
brunofgc | 7:ae9c47f62946 | 623 | if(!modemCom::sendToModem("AT+CWJAP?\r\n",1,NULL,"+CWJAP:",100,10,1000)){ |
brunofgc | 7:ae9c47f62946 | 624 | modemCom::conectaWiFi(); |
brunofgc | 7:ae9c47f62946 | 625 | i--; |
brunofgc | 7:ae9c47f62946 | 626 | }else{ |
brunofgc | 7:ae9c47f62946 | 627 | return true; |
brunofgc | 7:ae9c47f62946 | 628 | } |
brunofgc | 7:ae9c47f62946 | 629 | } |
brunofgc | 7:ae9c47f62946 | 630 | return false; |
brunofgc | 7:ae9c47f62946 | 631 | } |
brunofgc | 0:1c0a769988ee | 632 | |
brunofgc | 0:1c0a769988ee | 633 | void modemCom::conectaWiFi(void){ |
brunofgc | 0:1c0a769988ee | 634 | //Função para mudança de rede |
brunofgc | 0:1c0a769988ee | 635 | char *ptr; |
brunofgc | 0:1c0a769988ee | 636 | char aux[128]; |
brunofgc | 0:1c0a769988ee | 637 | if(sdCard::abreArquivo(&sdCard::config,"r")){ |
brunofgc | 0:1c0a769988ee | 638 | pc.printf("Abrindo arquivo de config para buscar dados de conexao.\r\n"); |
brunofgc | 0:1c0a769988ee | 639 | //"SSID:DROME\r\nPASSWORD:VITAE\r\nPORT:80" |
brunofgc | 0:1c0a769988ee | 640 | |
brunofgc | 0:1c0a769988ee | 641 | //Pegando SSID |
brunofgc | 0:1c0a769988ee | 642 | fgets(modemCom::status.ssid,40,sdCard::config.fp); |
brunofgc | 2:55b7b466e742 | 643 | ptr = strstr(modemCom::status.ssid,"SSID"); |
brunofgc | 2:55b7b466e742 | 644 | if(ptr!=NULL){ |
brunofgc | 2:55b7b466e742 | 645 | ptr = strtok(modemCom::status.ssid,":"); |
brunofgc | 2:55b7b466e742 | 646 | ptr = strtok(NULL,"\r\n"); |
brunofgc | 2:55b7b466e742 | 647 | strcpy(modemCom::status.ssid,ptr); |
brunofgc | 2:55b7b466e742 | 648 | |
brunofgc | 2:55b7b466e742 | 649 | //Pegando PASSWORD |
brunofgc | 2:55b7b466e742 | 650 | fgets(modemCom::status.password,50,sdCard::config.fp); |
brunofgc | 2:55b7b466e742 | 651 | ptr = strtok(modemCom::status.password,":"); |
brunofgc | 2:55b7b466e742 | 652 | ptr = strtok(NULL,"\r\n"); |
brunofgc | 2:55b7b466e742 | 653 | strcpy(modemCom::status.password,ptr); |
brunofgc | 2:55b7b466e742 | 654 | |
brunofgc | 2:55b7b466e742 | 655 | //Pegando PORT |
brunofgc | 2:55b7b466e742 | 656 | fgets(modemCom::status.port,20,sdCard::config.fp); |
brunofgc | 2:55b7b466e742 | 657 | ptr = strtok(modemCom::status.port,":"); |
brunofgc | 2:55b7b466e742 | 658 | ptr = strtok(NULL,"\r\n"); |
brunofgc | 2:55b7b466e742 | 659 | strcpy(modemCom::status.port,ptr); |
brunofgc | 2:55b7b466e742 | 660 | }else{ |
brunofgc | 2:55b7b466e742 | 661 | strcpy(modemCom::status.ssid,"VSA"); |
brunofgc | 2:55b7b466e742 | 662 | strcpy(modemCom::status.password,"vitaeBBYP"); |
brunofgc | 2:55b7b466e742 | 663 | strcpy(modemCom::status.port,"80"); |
brunofgc | 2:55b7b466e742 | 664 | } |
brunofgc | 0:1c0a769988ee | 665 | sdCard::fechaArquivo(&sdCard::config); |
brunofgc | 0:1c0a769988ee | 666 | pc.printf("Lidos os parametros de configuracao de wifi como sendo:\r\nSSID:<%s>\r\nPASSWORD:<%s>\r\nPORT:<%s>\r\n\r\n",modemCom::status.ssid,modemCom::status.password,modemCom::status.port); |
brunofgc | 2:55b7b466e742 | 667 | pc.printf("Iniciando conexao wifi.\r\n"); |
brunofgc | 2:55b7b466e742 | 668 | }else{ |
brunofgc | 7:ae9c47f62946 | 669 | strcpy(modemCom::status.ssid,"VSE"); |
brunofgc | 2:55b7b466e742 | 670 | strcpy(modemCom::status.password,"vitaeBBYP"); |
brunofgc | 7:ae9c47f62946 | 671 | strcpy(modemCom::status.port,"4000"); |
brunofgc | 2:55b7b466e742 | 672 | } |
brunofgc | 0:1c0a769988ee | 673 | |
brunofgc | 2:55b7b466e742 | 674 | //Mudando mode para AP e Station |
brunofgc | 7:ae9c47f62946 | 675 | if(modemCom::sendToModem("AT+CWMODE=3\r\n",1,&modemCom::status.OK,NULL,10000,1,1)) { |
brunofgc | 2:55b7b466e742 | 676 | pc.printf("Modo AP+STATION aceito.\n"); |
brunofgc | 2:55b7b466e742 | 677 | } |
brunofgc | 2:55b7b466e742 | 678 | |
brunofgc | 2:55b7b466e742 | 679 | //AT+CWJAP=\"\",\"\"\r\n |
brunofgc | 2:55b7b466e742 | 680 | sprintf(aux,"AT+CWJAP=\"%s\",\"%s\"\r\n",modemCom::status.ssid,modemCom::status.password); |
brunofgc | 7:ae9c47f62946 | 681 | if(modemCom::sendToModem(aux,1,&modemCom::status.OK,NULL,10000,1,1)) { |
brunofgc | 2:55b7b466e742 | 682 | pc.printf("WiFi Conectado.\n"); |
brunofgc | 2:55b7b466e742 | 683 | } |
brunofgc | 0:1c0a769988ee | 684 | |
brunofgc | 0:1c0a769988ee | 685 | //AT+CIPSERVER=1,80 |
brunofgc | 7:ae9c47f62946 | 686 | if(modemCom::sendToModem("AT+CIPMUX=1\r\n",1,&modemCom::status.OK,NULL,250,4,1)) { |
brunofgc | 0:1c0a769988ee | 687 | pc.printf("Recebeu o comando AT+CIPMUX=1.\n"); |
brunofgc | 0:1c0a769988ee | 688 | } |
brunofgc | 0:1c0a769988ee | 689 | |
brunofgc | 0:1c0a769988ee | 690 | //AT+CIPSERVER=1,80 |
brunofgc | 0:1c0a769988ee | 691 | sprintf(aux,"AT+CIPSERVER=1,%s\r\n",modemCom::status.port); |
brunofgc | 7:ae9c47f62946 | 692 | if(modemCom::sendToModem(aux,1,&modemCom::status.OK,NULL,250,4,1)) { |
brunofgc | 0:1c0a769988ee | 693 | pc.printf("Recebeu o comando <%s>.\n",aux); |
brunofgc | 0:1c0a769988ee | 694 | } |
brunofgc | 0:1c0a769988ee | 695 | |
brunofgc | 7:ae9c47f62946 | 696 | if(modemCom::sendToModem("AT+CIPSTO=60\r\n",1,&modemCom::status.OK,NULL,250,4,1)) { |
brunofgc | 0:1c0a769988ee | 697 | pc.printf("Recebeu o comando AT+CIPSTO=60.\n"); |
brunofgc | 2:55b7b466e742 | 698 | } |
brunofgc | 0:1c0a769988ee | 699 | |
brunofgc | 0:1c0a769988ee | 700 | sprintf(aux,"AT+CWSAP_CUR=\"DROME_WIFI_%s\",\"VsaDROME\",5,3\r\n",modemCom::status.MAC); |
brunofgc | 7:ae9c47f62946 | 701 | if(modemCom::sendToModem(aux,1,&modemCom::status.OK,NULL,250,4,1)) { |
brunofgc | 0:1c0a769988ee | 702 | pc.printf("Recebeu o comando %s.\n",aux); |
brunofgc | 0:1c0a769988ee | 703 | } |
brunofgc | 0:1c0a769988ee | 704 | |
brunofgc | 0:1c0a769988ee | 705 | |
brunofgc | 7:ae9c47f62946 | 706 | if(modemCom::sendToModem("AT+CIPAP_CUR=\"192.168.4.1\",\"192.168.4.1\",\"255.255.255.0\"\r\n",1,&modemCom::status.OK,NULL,250,4,1)) { |
brunofgc | 0:1c0a769988ee | 707 | pc.printf("Recebeu o comando AT+CIPAP_CUR=\"192.168.4.1\",\"192.168.4.1\",\"255.255.255.0\".\n"); |
brunofgc | 0:1c0a769988ee | 708 | } |
brunofgc | 0:1c0a769988ee | 709 | |
brunofgc | 7:ae9c47f62946 | 710 | if(modemCom::sendToModem("AT+CWDHCP_CUR=2,1\r\n",1,&modemCom::status.OK,NULL,250,4,1)) { |
brunofgc | 0:1c0a769988ee | 711 | pc.printf("Recebeu o comando AT+CWDHCP_CUR=2,1.\n"); |
brunofgc | 2:55b7b466e742 | 712 | } |
brunofgc | 0:1c0a769988ee | 713 | |
brunofgc | 0:1c0a769988ee | 714 | } |
brunofgc | 0:1c0a769988ee | 715 | |
brunofgc | 0:1c0a769988ee | 716 | void modemCom::processaPacote(void const *args){ |
brunofgc | 7:ae9c47f62946 | 717 | char *ptr,*scanPtr; |
brunofgc | 0:1c0a769988ee | 718 | uint16_t bufInLength = bufModem.getLength(); |
brunofgc | 7:ae9c47f62946 | 719 | uint16_t timeOut; |
brunofgc | 7:ae9c47f62946 | 720 | modemCom::bufIn = bufModem.get(); |
brunofgc | 3:9598af355293 | 721 | |
brunofgc | 7:ae9c47f62946 | 722 | modemCom::status.modemResponse=true; |
brunofgc | 7:ae9c47f62946 | 723 | |
brunofgc | 7:ae9c47f62946 | 724 | //pc.printf("Vem do modem <%s>.\r\n",modemCom::bufIn); |
brunofgc | 0:1c0a769988ee | 725 | |
brunofgc | 7:ae9c47f62946 | 726 | if(strstr(modemCom::bufIn,"WIFI")==NULL){ |
brunofgc | 7:ae9c47f62946 | 727 | //Buscando connect |
brunofgc | 7:ae9c47f62946 | 728 | if(!modemCom::atendendoWebServer){ |
brunofgc | 7:ae9c47f62946 | 729 | ptr=strstr(modemCom::bufIn,"CONNECT\r\n"); |
brunofgc | 7:ae9c47f62946 | 730 | //if(strstr(modemCom::bufIn,"WIFI")){ptr=NULL;} |
brunofgc | 7:ae9c47f62946 | 731 | if(ptr) { |
brunofgc | 7:ae9c47f62946 | 732 | ptr = strtok(modemCom::bufIn,","); |
brunofgc | 7:ae9c47f62946 | 733 | connID = atoi(ptr); |
brunofgc | 7:ae9c47f62946 | 734 | modemCom::status.serverConnected=1; |
brunofgc | 7:ae9c47f62946 | 735 | pc.printf("Recebido string CONNECT socket na conexao %u.\n",connID); |
brunofgc | 7:ae9c47f62946 | 736 | if(!modemCom::status.emComunicacao){ |
brunofgc | 7:ae9c47f62946 | 737 | pc.printf("Atendendo socket entrante na conexao %u.\n",connID); |
brunofgc | 7:ae9c47f62946 | 738 | modemCom::status.SRINGsockEntrante = true; |
brunofgc | 7:ae9c47f62946 | 739 | } |
brunofgc | 7:ae9c47f62946 | 740 | modemCom::status.emComunicacao=true; |
brunofgc | 7:ae9c47f62946 | 741 | modemCom::bufIn = strtok(NULL,""); |
brunofgc | 7:ae9c47f62946 | 742 | } |
brunofgc | 0:1c0a769988ee | 743 | } |
brunofgc | 0:1c0a769988ee | 744 | } |
brunofgc | 0:1c0a769988ee | 745 | |
brunofgc | 0:1c0a769988ee | 746 | IPDNumBytes = 0; |
brunofgc | 0:1c0a769988ee | 747 | ptr = strstr(modemCom::bufIn,"+IPD"); |
brunofgc | 0:1c0a769988ee | 748 | if(ptr!=NULL){ |
brunofgc | 0:1c0a769988ee | 749 | //+IPD,0,480:GET |
brunofgc | 0:1c0a769988ee | 750 | strtok(ptr,","); //Separando +IPD |
brunofgc | 0:1c0a769988ee | 751 | |
brunofgc | 0:1c0a769988ee | 752 | ptr = strtok(NULL,","); //Pegando o numero da conexão |
brunofgc | 0:1c0a769988ee | 753 | connID = atoi(ptr); |
brunofgc | 0:1c0a769988ee | 754 | |
brunofgc | 0:1c0a769988ee | 755 | ptr = strtok(NULL,":"); //Pegando o numero de caracteres IPD |
brunofgc | 0:1c0a769988ee | 756 | IPDNumBytes = atoi(ptr); |
brunofgc | 0:1c0a769988ee | 757 | |
brunofgc | 7:ae9c47f62946 | 758 | modemCom::bufIn = strtok(NULL,""); |
brunofgc | 0:1c0a769988ee | 759 | |
brunofgc | 7:ae9c47f62946 | 760 | //printf("Recebido pacote via conexao %u com %lu bytes.\n",connID,IPDNumBytes); |
brunofgc | 0:1c0a769988ee | 761 | |
brunofgc | 0:1c0a769988ee | 762 | } |
brunofgc | 0:1c0a769988ee | 763 | |
brunofgc | 7:ae9c47f62946 | 764 | //Reconhecendo GET HTTP e atendendo com um servidor web |
brunofgc | 7:ae9c47f62946 | 765 | if(strstr(modemCom::bufIn,"GET /") && strstr(modemCom::bufIn,"HTTP/1.1")){ |
brunofgc | 7:ae9c47f62946 | 766 | ptr=strstr(modemCom::bufIn,"GET /"); |
brunofgc | 7:ae9c47f62946 | 767 | if(ptr) { |
brunofgc | 7:ae9c47f62946 | 768 | ptr = strtok(ptr,"/"); |
brunofgc | 7:ae9c47f62946 | 769 | ptr = strtok(NULL," "); |
brunofgc | 7:ae9c47f62946 | 770 | bufInPtr = ptr; |
brunofgc | 7:ae9c47f62946 | 771 | modemCom::atendendoWebServer = true; |
brunofgc | 7:ae9c47f62946 | 772 | } |
brunofgc | 0:1c0a769988ee | 773 | } |
brunofgc | 0:1c0a769988ee | 774 | |
brunofgc | 0:1c0a769988ee | 775 | ptr=strstr(modemCom::bufIn,"*ServerAck*"); |
brunofgc | 7:ae9c47f62946 | 776 | if(ptr) { |
brunofgc | 7:ae9c47f62946 | 777 | modemCom::status.ServerAck=1; |
brunofgc | 0:1c0a769988ee | 778 | } |
brunofgc | 0:1c0a769988ee | 779 | |
brunofgc | 0:1c0a769988ee | 780 | ptr=strstr(modemCom::bufIn,"*ServerCommand*"); |
brunofgc | 7:ae9c47f62946 | 781 | if(ptr) { |
brunofgc | 0:1c0a769988ee | 782 | scanPtr = strtok (ptr,"\\"); |
brunofgc | 7:ae9c47f62946 | 783 | scanPtr = strtok (NULL,">"); |
brunofgc | 0:1c0a769988ee | 784 | strcpy(commands::buffer,scanPtr); |
brunofgc | 0:1c0a769988ee | 785 | pc.printf("Comandos <%s>.\n",commands::buffer); |
brunofgc | 0:1c0a769988ee | 786 | executaComandoServer = true; |
brunofgc | 0:1c0a769988ee | 787 | } |
brunofgc | 0:1c0a769988ee | 788 | |
brunofgc | 0:1c0a769988ee | 789 | //if(modemCom::status.recebendoArquivoDoServer){ |
brunofgc | 7:ae9c47f62946 | 790 | if(IPDNumBytes && modemCom::status.recebendoArquivoDoServer && (strstr(modemCom::bufIn,"CONNECT")==NULL) && (strstr(modemCom::bufIn,"*ServerCommand*")==NULL) ){ |
brunofgc | 0:1c0a769988ee | 791 | sdCard::insereDadosArquivo(&sdCard::tempFile,modemCom::bufIn,IPDNumBytes); |
brunofgc | 7:ae9c47f62946 | 792 | //modemCom::status.timeOut=100; |
brunofgc | 1:0e0967c88590 | 793 | |
brunofgc | 7:ae9c47f62946 | 794 | bufModem.del(); |
brunofgc | 0:1c0a769988ee | 795 | |
brunofgc | 0:1c0a769988ee | 796 | sprintf(modemCom::bufIn,"AT+CIPSEND=%u,%lu\r\n",connID,10); |
brunofgc | 0:1c0a769988ee | 797 | modem.puts(modemCom::bufIn); |
brunofgc | 7:ae9c47f62946 | 798 | timeOut = 5000; |
brunofgc | 7:ae9c47f62946 | 799 | while(timeOut && (strstr(bufModem.getRowBuffer(),">")==NULL)){ |
brunofgc | 7:ae9c47f62946 | 800 | osDelay(1); |
brunofgc | 7:ae9c47f62946 | 801 | timeOut--; |
brunofgc | 7:ae9c47f62946 | 802 | } |
brunofgc | 0:1c0a769988ee | 803 | modem.puts("sendData\r\n"); |
brunofgc | 0:1c0a769988ee | 804 | pc.printf("sendData\n"); |
brunofgc | 0:1c0a769988ee | 805 | return; |
brunofgc | 0:1c0a769988ee | 806 | } |
brunofgc | 0:1c0a769988ee | 807 | |
brunofgc | 0:1c0a769988ee | 808 | //Reconhecimento dos status |
brunofgc | 0:1c0a769988ee | 809 | modemCom::status.PROMPT_ENVIO_COMMAND_MODE=0; |
brunofgc | 0:1c0a769988ee | 810 | ptr=strstr(modemCom::bufIn,"> "); |
brunofgc | 0:1c0a769988ee | 811 | if(ptr) { |
brunofgc | 7:ae9c47f62946 | 812 | |
brunofgc | 0:1c0a769988ee | 813 | modemCom::status.serverConnected=1; |
brunofgc | 0:1c0a769988ee | 814 | modemCom::status.commandMode=1; |
brunofgc | 0:1c0a769988ee | 815 | modemCom::status.NO_CARRIER=0; |
brunofgc | 0:1c0a769988ee | 816 | modemCom::status.PROMPT_ENVIO_COMMAND_MODE=1; |
brunofgc | 0:1c0a769988ee | 817 | } |
brunofgc | 0:1c0a769988ee | 818 | |
brunofgc | 0:1c0a769988ee | 819 | modemCom::status.OK=0; |
brunofgc | 0:1c0a769988ee | 820 | ptr=strstr(modemCom::bufIn,"OK\r"); |
brunofgc | 7:ae9c47f62946 | 821 | if(ptr) { |
brunofgc | 0:1c0a769988ee | 822 | modemCom::status.OK=1; |
brunofgc | 7:ae9c47f62946 | 823 | } |
brunofgc | 0:1c0a769988ee | 824 | |
brunofgc | 0:1c0a769988ee | 825 | modemCom::status.busy=false; |
brunofgc | 0:1c0a769988ee | 826 | ptr=strstr(modemCom::bufIn,"busy s..."); |
brunofgc | 0:1c0a769988ee | 827 | if(ptr) { |
brunofgc | 7:ae9c47f62946 | 828 | |
brunofgc | 0:1c0a769988ee | 829 | modemCom::status.busy=1; |
brunofgc | 0:1c0a769988ee | 830 | } |
brunofgc | 0:1c0a769988ee | 831 | |
brunofgc | 0:1c0a769988ee | 832 | modemCom::status.SEND_OK=0; |
brunofgc | 0:1c0a769988ee | 833 | ptr=strstr(modemCom::bufIn,"SEND OK"); |
brunofgc | 0:1c0a769988ee | 834 | if(ptr) { |
brunofgc | 7:ae9c47f62946 | 835 | |
brunofgc | 0:1c0a769988ee | 836 | modemCom::status.SEND_OK=1; |
brunofgc | 0:1c0a769988ee | 837 | } |
brunofgc | 0:1c0a769988ee | 838 | |
brunofgc | 0:1c0a769988ee | 839 | modemCom::status.ERROR=0; |
brunofgc | 0:1c0a769988ee | 840 | ptr=strstr(modemCom::bufIn,"ERROR"); |
brunofgc | 0:1c0a769988ee | 841 | if(ptr) { |
brunofgc | 7:ae9c47f62946 | 842 | |
brunofgc | 0:1c0a769988ee | 843 | modemCom::status.ERROR=1; |
brunofgc | 0:1c0a769988ee | 844 | modemCom::status.OK=0; |
brunofgc | 7:ae9c47f62946 | 845 | } |
brunofgc | 7:ae9c47f62946 | 846 | ptr=strstr(modemCom::bufIn,"FAIL"); |
brunofgc | 7:ae9c47f62946 | 847 | if(ptr) { |
brunofgc | 7:ae9c47f62946 | 848 | |
brunofgc | 7:ae9c47f62946 | 849 | modemCom::status.ERROR=1; |
brunofgc | 7:ae9c47f62946 | 850 | modemCom::status.OK=0; |
brunofgc | 7:ae9c47f62946 | 851 | } |
brunofgc | 7:ae9c47f62946 | 852 | |
brunofgc | 7:ae9c47f62946 | 853 | //modemCom::status.CLOSED=false; |
brunofgc | 7:ae9c47f62946 | 854 | ptr=strstr(modemCom::bufIn,"CLOSED"); |
brunofgc | 7:ae9c47f62946 | 855 | if(ptr) { |
brunofgc | 7:ae9c47f62946 | 856 | |
brunofgc | 7:ae9c47f62946 | 857 | modemCom::status.CLOSED=1; |
brunofgc | 0:1c0a769988ee | 858 | } |
brunofgc | 0:1c0a769988ee | 859 | |
brunofgc | 0:1c0a769988ee | 860 | |
brunofgc | 0:1c0a769988ee | 861 | } |
brunofgc | 0:1c0a769988ee | 862 | |
brunofgc | 0:1c0a769988ee | 863 | uint8_t modemCom::inicializaModem(void) |
brunofgc | 0:1c0a769988ee | 864 | { |
brunofgc | 0:1c0a769988ee | 865 | //uint8_t maxTentativas=0; |
brunofgc | 0:1c0a769988ee | 866 | |
brunofgc | 0:1c0a769988ee | 867 | //char config[255]; |
brunofgc | 0:1c0a769988ee | 868 | //char *ptr; |
brunofgc | 7:ae9c47f62946 | 869 | //char *buf; |
brunofgc | 7:ae9c47f62946 | 870 | //buf = bufModem.getRowBuffer(); |
brunofgc | 0:1c0a769988ee | 871 | |
brunofgc | 0:1c0a769988ee | 872 | modemCom::timeOutModem = 10; |
brunofgc | 0:1c0a769988ee | 873 | modemCom::status.emComunicacao = true; |
brunofgc | 0:1c0a769988ee | 874 | |
brunofgc | 7:ae9c47f62946 | 875 | if(modemCom::sendToModem("ATE0\r\n",1,&modemCom::status.OK,NULL,1000,10,1)) { |
brunofgc | 0:1c0a769988ee | 876 | pc.printf("Recebeu o comando ATE0.\n"); |
brunofgc | 0:1c0a769988ee | 877 | } |
brunofgc | 0:1c0a769988ee | 878 | |
brunofgc | 7:ae9c47f62946 | 879 | modemCom::leMAC(); |
brunofgc | 0:1c0a769988ee | 880 | |
brunofgc | 0:1c0a769988ee | 881 | modemCom::status.emComunicacao = false; |
brunofgc | 0:1c0a769988ee | 882 | return 1; |
brunofgc | 0:1c0a769988ee | 883 | } |
brunofgc | 0:1c0a769988ee | 884 | |
brunofgc | 0:1c0a769988ee | 885 |