teste de publish
Dependencies: DS1820 HighSpeedAnalogIn devices mbed
modem.cpp@6:d4ebbaaba295, 2017-06-15 (annotated)
- Committer:
- brunofgc
- Date:
- Thu Jun 15 12:50:58 2017 +0000
- Revision:
- 6:d4ebbaaba295
- Parent:
- 5:7801f913384e
- Child:
- 7:ae9c47f62946
Ultima vers?o antes da adi??o de Exce??es de calend?rio e persist?ncias
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 | 0:1c0a769988ee | 45 | if(modemCom::sendToModem(aux,1,&modemCom::status.PROMPT_ENVIO_COMMAND_MODE,2000,10)) { |
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 | 0:1c0a769988ee | 52 | numeroByteSendoEnviado = modemCom::sendToModem(NULL,1,&modemCom::status.SEND_OK,5000,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 | 0:1c0a769988ee | 62 | if(modemCom::sendToModem("AT+CIPSTAMAC?\r\n",1,&modemCom::status.OK,250,20)){ |
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 | 0:1c0a769988ee | 73 | bool modemCom::getSocketInfo(uint8_t sId){ |
brunofgc | 0:1c0a769988ee | 74 | bool retorno=false; |
brunofgc | 0:1c0a769988ee | 75 | sId++; //Para transformar de indice C para o índice usado no modem; |
brunofgc | 0:1c0a769988ee | 76 | switch(sId){ |
brunofgc | 0:1c0a769988ee | 77 | case 1: |
brunofgc | 0:1c0a769988ee | 78 | retorno=modemCom::sendToModem("AT#SI=1\r",1,&modemCom::status.OK,300,1); |
brunofgc | 0:1c0a769988ee | 79 | break; |
brunofgc | 0:1c0a769988ee | 80 | case 2: |
brunofgc | 0:1c0a769988ee | 81 | retorno=modemCom::sendToModem("AT#SI=2\r",1,&modemCom::status.OK,300,1); |
brunofgc | 0:1c0a769988ee | 82 | break; |
brunofgc | 0:1c0a769988ee | 83 | } |
brunofgc | 0:1c0a769988ee | 84 | return retorno; |
brunofgc | 0:1c0a769988ee | 85 | } |
brunofgc | 0:1c0a769988ee | 86 | |
brunofgc | 1:0e0967c88590 | 87 | bool modemCom::writeStatusToSD(void){ |
brunofgc | 1:0e0967c88590 | 88 | char aux[30]; |
brunofgc | 1:0e0967c88590 | 89 | int16_t RSSI; |
brunofgc | 1:0e0967c88590 | 90 | RSSI = modemCom::getRSSI(); |
brunofgc | 1:0e0967c88590 | 91 | sdCardBuf.del(); |
brunofgc | 1:0e0967c88590 | 92 | sdCardBuf.fill("log{status:ip=255.255.255.255;csq=",34); |
brunofgc | 1:0e0967c88590 | 93 | |
brunofgc | 1:0e0967c88590 | 94 | sprintf(aux,"%d",RSSI); |
brunofgc | 1:0e0967c88590 | 95 | sdCardBuf.fill(aux,strlen(aux)); |
brunofgc | 1:0e0967c88590 | 96 | |
brunofgc | 1:0e0967c88590 | 97 | sprintf(aux,";imsi=%s}log",modemCom::status.MAC); |
brunofgc | 1:0e0967c88590 | 98 | sdCardBuf.fill(aux,strlen(aux)); |
brunofgc | 1:0e0967c88590 | 99 | |
brunofgc | 2:55b7b466e742 | 100 | if(!sdCard::arquivoAberto()){ |
brunofgc | 1:0e0967c88590 | 101 | sdCard::insereDadosArquivo(&sdCard::armazenamento,sdCardBuf.get(),sdCardBuf.getLength()); |
brunofgc | 1:0e0967c88590 | 102 | } |
brunofgc | 1:0e0967c88590 | 103 | } |
brunofgc | 1:0e0967c88590 | 104 | |
brunofgc | 0:1c0a769988ee | 105 | bool modemCom::montaStatus(void) |
brunofgc | 0:1c0a769988ee | 106 | { |
brunofgc | 0:1c0a769988ee | 107 | if(sdCard::abreArquivo(&sdCard::status,"w")) { |
brunofgc | 0:1c0a769988ee | 108 | fprintf(sdCard::status.fp,"log{status:ip:255.255.255.255;csq:30;imsi:%s}log",modemCom::status.MAC); |
brunofgc | 0:1c0a769988ee | 109 | sdCard::fechaArquivo(&sdCard::status); |
brunofgc | 0:1c0a769988ee | 110 | sdCard::exibeArquivo(&sdCard::status); |
brunofgc | 0:1c0a769988ee | 111 | } else { |
brunofgc | 0:1c0a769988ee | 112 | return 0; |
brunofgc | 0:1c0a769988ee | 113 | } |
brunofgc | 0:1c0a769988ee | 114 | return true; |
brunofgc | 0:1c0a769988ee | 115 | } |
brunofgc | 0:1c0a769988ee | 116 | |
brunofgc | 0:1c0a769988ee | 117 | |
brunofgc | 0:1c0a769988ee | 118 | |
brunofgc | 0:1c0a769988ee | 119 | uint8_t modemCom::enviaDados(char tipoDado) |
brunofgc | 0:1c0a769988ee | 120 | { |
brunofgc | 0:1c0a769988ee | 121 | uint8_t result=0; |
brunofgc | 0:1c0a769988ee | 122 | modemCom::status.emComunicacao = true; |
brunofgc | 0:1c0a769988ee | 123 | |
brunofgc | 0:1c0a769988ee | 124 | switch(tipoDado) { |
brunofgc | 0:1c0a769988ee | 125 | case dadosArmazenados: |
brunofgc | 0:1c0a769988ee | 126 | if(!sdCard::preparaEnvio()){ |
brunofgc | 0:1c0a769988ee | 127 | pc.printf("Nao foi possivel preparar o envio.\n"); |
brunofgc | 2:55b7b466e742 | 128 | return false; |
brunofgc | 0:1c0a769988ee | 129 | } |
brunofgc | 0:1c0a769988ee | 130 | if(sdCard::envio.bytes==0) { |
brunofgc | 0:1c0a769988ee | 131 | sdCard::getFileTam(&sdCard::envio); |
brunofgc | 0:1c0a769988ee | 132 | } |
brunofgc | 0:1c0a769988ee | 133 | pc.printf("Preparado o envio do arquivo com %lu bytes.\n",sdCard::envio.bytes); |
brunofgc | 0:1c0a769988ee | 134 | break; |
brunofgc | 0:1c0a769988ee | 135 | case dadosStatus: |
brunofgc | 0:1c0a769988ee | 136 | modemCom::montaStatus(); |
brunofgc | 0:1c0a769988ee | 137 | break; |
brunofgc | 0:1c0a769988ee | 138 | } |
brunofgc | 0:1c0a769988ee | 139 | |
brunofgc | 0:1c0a769988ee | 140 | switch(tipoDado) { |
brunofgc | 0:1c0a769988ee | 141 | case dadosArmazenados: |
brunofgc | 2:55b7b466e742 | 142 | result = modemCom::postFileCommandMode(serverAddress,"/drome/Parser/",&sdCard::envio); |
brunofgc | 0:1c0a769988ee | 143 | break; |
brunofgc | 0:1c0a769988ee | 144 | case dadosStatus: |
brunofgc | 2:55b7b466e742 | 145 | result = modemCom::postFileCommandMode(serverAddress,"/drome/Parser/",&sdCard::status); |
brunofgc | 0:1c0a769988ee | 146 | break; |
brunofgc | 0:1c0a769988ee | 147 | } |
brunofgc | 0:1c0a769988ee | 148 | |
brunofgc | 0:1c0a769988ee | 149 | if(!result){ |
brunofgc | 0:1c0a769988ee | 150 | return result; |
brunofgc | 0:1c0a769988ee | 151 | } |
brunofgc | 0:1c0a769988ee | 152 | |
brunofgc | 0:1c0a769988ee | 153 | //Envia uma string nula e faz o processador esperar a resposta do server! |
brunofgc | 0:1c0a769988ee | 154 | if(modemCom::status.ServerAck) { |
brunofgc | 0:1c0a769988ee | 155 | pc.printf("Enviado e recebido resposta do server (*ServerAck*).\n"); |
brunofgc | 0:1c0a769988ee | 156 | } else { |
brunofgc | 0:1c0a769988ee | 157 | pc.printf("Enviado mas nao recebido resposta do server (*ServerAck*).\n"); |
brunofgc | 0:1c0a769988ee | 158 | pc.printf("Conteudo de getRowBuffer <%s>.\n",bufModem.getRowBuffer()); |
brunofgc | 0:1c0a769988ee | 159 | } |
brunofgc | 0:1c0a769988ee | 160 | return result; |
brunofgc | 0:1c0a769988ee | 161 | } |
brunofgc | 0:1c0a769988ee | 162 | |
brunofgc | 0:1c0a769988ee | 163 | |
brunofgc | 0:1c0a769988ee | 164 | |
brunofgc | 0:1c0a769988ee | 165 | uint8_t modemCom::postFileCommandMode(char *host, char *uri, arquivoSD *arquivo) |
brunofgc | 0:1c0a769988ee | 166 | { |
brunofgc | 0:1c0a769988ee | 167 | #define maxRetentativasEnvioParteArquivo 20 |
brunofgc | 0:1c0a769988ee | 168 | uint32_t numeroDePartes; |
brunofgc | 0:1c0a769988ee | 169 | uint16_t restoDoEnvio; |
brunofgc | 0:1c0a769988ee | 170 | int32_t parteSendoEnviada; |
brunofgc | 0:1c0a769988ee | 171 | uint16_t numeroByteSendoEnviado; |
brunofgc | 0:1c0a769988ee | 172 | uint16_t maxTentativas=0; |
brunofgc | 0:1c0a769988ee | 173 | //unsigned int result; |
brunofgc | 0:1c0a769988ee | 174 | char header[512]; |
brunofgc | 0:1c0a769988ee | 175 | char aux[40]; |
brunofgc | 0:1c0a769988ee | 176 | uint16_t headerTam; |
brunofgc | 0:1c0a769988ee | 177 | uint32_t dataTam=0; |
brunofgc | 0:1c0a769988ee | 178 | char c; |
brunofgc | 0:1c0a769988ee | 179 | bool passaParaProximaParte = false; |
brunofgc | 0:1c0a769988ee | 180 | |
brunofgc | 0:1c0a769988ee | 181 | //Para garantir que não seja duvidoso proveniente de outra conexão. |
brunofgc | 0:1c0a769988ee | 182 | modemCom::status.ServerAck=0; |
brunofgc | 0:1c0a769988ee | 183 | modemCom::status.NO_CARRIER=0; |
brunofgc | 0:1c0a769988ee | 184 | |
brunofgc | 0:1c0a769988ee | 185 | if(arquivo->bytes==0){ |
brunofgc | 0:1c0a769988ee | 186 | |
brunofgc | 0:1c0a769988ee | 187 | if((!sdCard::getFileTam(arquivo))||(arquivo->bytes == 0)){ |
brunofgc | 0:1c0a769988ee | 188 | return false; |
brunofgc | 0:1c0a769988ee | 189 | } |
brunofgc | 0:1c0a769988ee | 190 | } |
brunofgc | 0:1c0a769988ee | 191 | |
brunofgc | 0:1c0a769988ee | 192 | pc.printf("Abrindo o socket em <%s>.\n",host); |
brunofgc | 0:1c0a769988ee | 193 | sprintf(header,"AT+CIPSTART=1,\"TCP\",\"%s\",80\r\n",host); |
brunofgc | 0:1c0a769988ee | 194 | if(modemCom::sendToModem(header,1,&modemCom::status.OK,20000,2)) { |
brunofgc | 0:1c0a769988ee | 195 | modemCom::status.NO_CARRIER=0; //Socket Aberto! |
brunofgc | 0:1c0a769988ee | 196 | pc.printf("Aberto o socket em command mode.\n"); |
brunofgc | 0:1c0a769988ee | 197 | pc.printf("Enviando arquivo com nome %s.\n",arquivo->nome); |
brunofgc | 0:1c0a769988ee | 198 | modemCom::status.serverConnected=1; |
brunofgc | 0:1c0a769988ee | 199 | modemCom::timeOutModem = 100; |
brunofgc | 0:1c0a769988ee | 200 | modemCom::status.emComunicacao = true; |
brunofgc | 0:1c0a769988ee | 201 | } else { |
brunofgc | 0:1c0a769988ee | 202 | pc.printf("Nao foi possivel Abrir o socket. Abortando tentativa.\n"); |
brunofgc | 0:1c0a769988ee | 203 | modemCom::timeOutModem = 10; |
brunofgc | 0:1c0a769988ee | 204 | return 0; |
brunofgc | 0:1c0a769988ee | 205 | } |
brunofgc | 0:1c0a769988ee | 206 | |
brunofgc | 0:1c0a769988ee | 207 | numeroDePartes = arquivo->bytes / numeroDeBytesPorEnvio; |
brunofgc | 0:1c0a769988ee | 208 | restoDoEnvio = arquivo->bytes % numeroDeBytesPorEnvio; |
brunofgc | 0:1c0a769988ee | 209 | |
brunofgc | 3:9598af355293 | 210 | |
brunofgc | 3:9598af355293 | 211 | pc.printf("Iniciando o envio do arquivo de %lu bytes\nnumeroDePartes = %lu\nrestoDoEnvio = %lu\n",arquivo->bytes,numeroDePartes,restoDoEnvio); |
brunofgc | 0:1c0a769988ee | 212 | |
brunofgc | 0:1c0a769988ee | 213 | if(!sdCard::abreArquivo(arquivo,"r")){ |
brunofgc | 0:1c0a769988ee | 214 | pc.printf("Nao foi posssivel abrir o arquivo de dentro da funcao sendFileCommandMode().\n"); |
brunofgc | 0:1c0a769988ee | 215 | modemCom::status.emComunicacao = false; |
brunofgc | 0:1c0a769988ee | 216 | return 0; |
brunofgc | 0:1c0a769988ee | 217 | } |
brunofgc | 0:1c0a769988ee | 218 | |
brunofgc | 4:13ff9c81dc10 | 219 | |
brunofgc | 0:1c0a769988ee | 220 | //Monta Header aqui! |
brunofgc | 4:13ff9c81dc10 | 221 | //application/octet-stream |
brunofgc | 4:13ff9c81dc10 | 222 | 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 | 223 | |
brunofgc | 0:1c0a769988ee | 224 | dataTam = strlen(header); |
brunofgc | 0:1c0a769988ee | 225 | dataTam+=arquivo->bytes; |
brunofgc | 4:13ff9c81dc10 | 226 | dataTam+= strlen("\r\n--xxBOUNDARYxx--"); |
brunofgc | 0:1c0a769988ee | 227 | //"/drome/parser/index.php" uri drome |
brunofgc | 4:13ff9c81dc10 | 228 | 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 | 229 | headerTam = strlen(header); //Pego o tamanho parcial para fazer a concatenaçao a fim de predizer o tamanho do post total |
brunofgc | 0:1c0a769988ee | 230 | |
brunofgc | 4:13ff9c81dc10 | 231 | 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 | 232 | headerTam = strlen(header); |
brunofgc | 0:1c0a769988ee | 233 | |
brunofgc | 0:1c0a769988ee | 234 | sprintf(aux,"AT+CIPSEND=1,%u\r\n",headerTam); |
brunofgc | 0:1c0a769988ee | 235 | if(modemCom::sendToModem(aux,1,&modemCom::status.PROMPT_ENVIO_COMMAND_MODE,100,10)) { |
brunofgc | 0:1c0a769988ee | 236 | if(!modemCom::sendToModem(header,1,&modemCom::status.SEND_OK,1000,1)) { |
brunofgc | 0:1c0a769988ee | 237 | sdCard::fechaArquivo(arquivo); |
brunofgc | 0:1c0a769988ee | 238 | modemCom::status.emComunicacao = false; |
brunofgc | 3:9598af355293 | 239 | pc.printf("Cabecalho http nao enviado.\n"); |
brunofgc | 0:1c0a769988ee | 240 | return 0; |
brunofgc | 0:1c0a769988ee | 241 | } else { |
brunofgc | 3:9598af355293 | 242 | pc.printf("Cabecalho http enviado.\n"); |
brunofgc | 0:1c0a769988ee | 243 | } |
brunofgc | 0:1c0a769988ee | 244 | } else { |
brunofgc | 0:1c0a769988ee | 245 | pc.printf("Erro enviando arquivo.\n"); |
brunofgc | 0:1c0a769988ee | 246 | sdCard::fechaArquivo(arquivo); |
brunofgc | 0:1c0a769988ee | 247 | modemCom::status.emComunicacao = false; |
brunofgc | 0:1c0a769988ee | 248 | return 0; |
brunofgc | 0:1c0a769988ee | 249 | } |
brunofgc | 0:1c0a769988ee | 250 | |
brunofgc | 0:1c0a769988ee | 251 | //Enviando as partes inteiras |
brunofgc | 0:1c0a769988ee | 252 | maxTentativas = maxRetentativasEnvioParteArquivo; |
brunofgc | 0:1c0a769988ee | 253 | for(parteSendoEnviada=0; parteSendoEnviada<numeroDePartes; parteSendoEnviada++) { |
brunofgc | 0:1c0a769988ee | 254 | //Aqui realizo o envio de cada parte. |
brunofgc | 0:1c0a769988ee | 255 | passaParaProximaParte = false; |
brunofgc | 0:1c0a769988ee | 256 | while((maxTentativas)&&(!passaParaProximaParte)) { |
brunofgc | 0:1c0a769988ee | 257 | sprintf(aux,"AT+CIPSEND=1,%u\r\n",numeroDeBytesPorEnvio); |
brunofgc | 0:1c0a769988ee | 258 | if(modemCom::sendToModem(aux,1,&modemCom::status.PROMPT_ENVIO_COMMAND_MODE,100,20)) { |
brunofgc | 0:1c0a769988ee | 259 | if(maxTentativas!=maxRetentativasEnvioParteArquivo) { |
brunofgc | 0:1c0a769988ee | 260 | //Estou realizando reenvio desta parte logo preciso fazer fseek para a quantidade de bytes do envio anterior |
brunofgc | 0:1c0a769988ee | 261 | fseek(arquivo->fp,-numeroDeBytesPorEnvio,SEEK_CUR); |
brunofgc | 0:1c0a769988ee | 262 | } |
brunofgc | 3:9598af355293 | 263 | pc.printf("Numero de bytes por envio = %lu.\r\n",numeroDeBytesPorEnvio); |
brunofgc | 0:1c0a769988ee | 264 | for(numeroByteSendoEnviado=0; numeroByteSendoEnviado<(numeroDeBytesPorEnvio); numeroByteSendoEnviado++) { |
brunofgc | 0:1c0a769988ee | 265 | c = fgetc(arquivo->fp); |
brunofgc | 0:1c0a769988ee | 266 | modem.printf("%c",c); |
brunofgc | 0:1c0a769988ee | 267 | #ifdef xereta_modem |
brunofgc | 4:13ff9c81dc10 | 268 | pc.printf("%c",c); |
brunofgc | 0:1c0a769988ee | 269 | #endif |
brunofgc | 0:1c0a769988ee | 270 | } |
brunofgc | 0:1c0a769988ee | 271 | |
brunofgc | 0:1c0a769988ee | 272 | if(modemCom::sendToModem(NULL,1,&modemCom::status.SEND_OK,5000,1)) { |
brunofgc | 0:1c0a769988ee | 273 | diversos::progressBar(parteSendoEnviada,numeroDePartes); |
brunofgc | 0:1c0a769988ee | 274 | //Preciso verificar se o buffer esvaziou. Se deu "flush" |
brunofgc | 0:1c0a769988ee | 275 | //if(modemCom::aguardaFlush(0)==1) { |
brunofgc | 0:1c0a769988ee | 276 | //passa pra proxima parte |
brunofgc | 0:1c0a769988ee | 277 | passaParaProximaParte = true; |
brunofgc | 0:1c0a769988ee | 278 | maxTentativas = maxRetentativasEnvioParteArquivo; |
brunofgc | 0:1c0a769988ee | 279 | } else { |
brunofgc | 0:1c0a769988ee | 280 | maxTentativas--; |
brunofgc | 0:1c0a769988ee | 281 | if(maxTentativas == 1) { |
brunofgc | 0:1c0a769988ee | 282 | pc.printf("MaxTentativas dentro de envio das partes == 1, vou aguardar o flush.\n"); |
brunofgc | 0:1c0a769988ee | 283 | } |
brunofgc | 0:1c0a769988ee | 284 | if(maxTentativas == 0) { |
brunofgc | 0:1c0a769988ee | 285 | sdCard::fechaArquivo(arquivo); |
brunofgc | 0:1c0a769988ee | 286 | modemCom::status.emComunicacao = false; |
brunofgc | 0:1c0a769988ee | 287 | return 0; |
brunofgc | 0:1c0a769988ee | 288 | } |
brunofgc | 0:1c0a769988ee | 289 | } |
brunofgc | 0:1c0a769988ee | 290 | } else { |
brunofgc | 0:1c0a769988ee | 291 | pc.printf("Erro enviando o arquivo nao recebeu a tentativa de envio de parte inteira.\n"); |
brunofgc | 0:1c0a769988ee | 292 | sdCard::fechaArquivo(arquivo); |
brunofgc | 0:1c0a769988ee | 293 | modemCom::status.emComunicacao = false; |
brunofgc | 0:1c0a769988ee | 294 | return 0; |
brunofgc | 0:1c0a769988ee | 295 | } |
brunofgc | 0:1c0a769988ee | 296 | } |
brunofgc | 0:1c0a769988ee | 297 | if(!passaParaProximaParte) { |
brunofgc | 0:1c0a769988ee | 298 | pc.printf("Erro enviando o arquivo nao recebeu a tentativa de envio de parte inteira.\n"); |
brunofgc | 0:1c0a769988ee | 299 | sdCard::fechaArquivo(arquivo); |
brunofgc | 0:1c0a769988ee | 300 | modemCom::status.emComunicacao = false; |
brunofgc | 0:1c0a769988ee | 301 | return 0; |
brunofgc | 0:1c0a769988ee | 302 | } |
brunofgc | 0:1c0a769988ee | 303 | } |
brunofgc | 0:1c0a769988ee | 304 | |
brunofgc | 0:1c0a769988ee | 305 | //Enviando o resto |
brunofgc | 0:1c0a769988ee | 306 | passaParaProximaParte = false; |
brunofgc | 0:1c0a769988ee | 307 | maxTentativas = maxRetentativasEnvioParteArquivo; |
brunofgc | 0:1c0a769988ee | 308 | while((maxTentativas)&&(!passaParaProximaParte)) { |
brunofgc | 4:13ff9c81dc10 | 309 | 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 | 0:1c0a769988ee | 310 | if(modemCom::sendToModem(aux,1,&modemCom::status.PROMPT_ENVIO_COMMAND_MODE,100,3)) { |
brunofgc | 0:1c0a769988ee | 311 | for(numeroByteSendoEnviado=0; numeroByteSendoEnviado<restoDoEnvio; numeroByteSendoEnviado++) { |
brunofgc | 0:1c0a769988ee | 312 | //Envio os bytes aqui! Menos o ultimo para ter o check; |
brunofgc | 0:1c0a769988ee | 313 | c = fgetc(arquivo->fp); |
brunofgc | 0:1c0a769988ee | 314 | modem.printf("%c",c); |
brunofgc | 0:1c0a769988ee | 315 | #ifdef xereta_modem |
brunofgc | 0:1c0a769988ee | 316 | pc.printf("%c",c); |
brunofgc | 0:1c0a769988ee | 317 | #endif |
brunofgc | 0:1c0a769988ee | 318 | } |
brunofgc | 4:13ff9c81dc10 | 319 | if(modemCom::sendToModem("\r\n--xxBOUNDARYxx--\r\n\r\n",1,&modemCom::status.SEND_OK,5000,1)) { |
brunofgc | 0:1c0a769988ee | 320 | if(numeroDePartes) { |
brunofgc | 0:1c0a769988ee | 321 | diversos::progressBar(parteSendoEnviada,numeroDePartes); |
brunofgc | 0:1c0a769988ee | 322 | } |
brunofgc | 0:1c0a769988ee | 323 | passaParaProximaParte = true; |
brunofgc | 0:1c0a769988ee | 324 | pc.printf("\nEnviado o resto do envio com fim de cabecalho!\n",parteSendoEnviada); |
brunofgc | 0:1c0a769988ee | 325 | modemCom::timeOutModem = 250; |
brunofgc | 0:1c0a769988ee | 326 | //} |
brunofgc | 0:1c0a769988ee | 327 | /*else { |
brunofgc | 0:1c0a769988ee | 328 | pc.printf("Erro enviando arquivo.\n"); |
brunofgc | 0:1c0a769988ee | 329 | fclose(fp); |
brunofgc | 0:1c0a769988ee | 330 | *aberto = false; |
brunofgc | 0:1c0a769988ee | 331 | return 0; |
brunofgc | 0:1c0a769988ee | 332 | }*/ |
brunofgc | 0:1c0a769988ee | 333 | } else { |
brunofgc | 0:1c0a769988ee | 334 | sdCard::fechaArquivo(arquivo); |
brunofgc | 0:1c0a769988ee | 335 | modemCom::status.emComunicacao = false; |
brunofgc | 0:1c0a769988ee | 336 | return 0; |
brunofgc | 0:1c0a769988ee | 337 | } |
brunofgc | 0:1c0a769988ee | 338 | } else { |
brunofgc | 0:1c0a769988ee | 339 | maxTentativas--; |
brunofgc | 0:1c0a769988ee | 340 | modemCom::sendToModem("AT\r\n",1,&modemCom::status.OK,1000,100); |
brunofgc | 0:1c0a769988ee | 341 | } |
brunofgc | 0:1c0a769988ee | 342 | } |
brunofgc | 0:1c0a769988ee | 343 | |
brunofgc | 0:1c0a769988ee | 344 | if(!passaParaProximaParte) { |
brunofgc | 0:1c0a769988ee | 345 | pc.printf("Erro enviando o arquivo nao recebeu a tentativa de envio de parte inteira.\n"); |
brunofgc | 0:1c0a769988ee | 346 | sdCard::fechaArquivo(arquivo); |
brunofgc | 0:1c0a769988ee | 347 | modemCom::status.emComunicacao = false; |
brunofgc | 0:1c0a769988ee | 348 | return 0; |
brunofgc | 0:1c0a769988ee | 349 | } |
brunofgc | 0:1c0a769988ee | 350 | |
brunofgc | 0:1c0a769988ee | 351 | //Logica de timeout esperando confirmação modemCom::status.ServerAck 20 segundos |
brunofgc | 0:1c0a769988ee | 352 | parteSendoEnviada = 600; |
brunofgc | 0:1c0a769988ee | 353 | while((parteSendoEnviada)&&(!modemCom::status.ServerAck)&&(!modemCom::status.NO_CARRIER)) { |
brunofgc | 0:1c0a769988ee | 354 | osDelay(100); |
brunofgc | 0:1c0a769988ee | 355 | parteSendoEnviada--; |
brunofgc | 0:1c0a769988ee | 356 | } |
brunofgc | 0:1c0a769988ee | 357 | |
brunofgc | 0:1c0a769988ee | 358 | modemCom::status.serverConnected=0; |
brunofgc | 0:1c0a769988ee | 359 | sdCard::fechaArquivo(arquivo); |
brunofgc | 0:1c0a769988ee | 360 | |
brunofgc | 0:1c0a769988ee | 361 | //modemCom::sendToModem("AT+CIPCLOSE=1\r\n",1,&modemCom::status.OK,500,3); |
brunofgc | 0:1c0a769988ee | 362 | modemCom::closeConnection(1); |
brunofgc | 0:1c0a769988ee | 363 | modemCom::status.emComunicacao = false; |
brunofgc | 0:1c0a769988ee | 364 | return modemCom::status.ServerAck; |
brunofgc | 0:1c0a769988ee | 365 | } |
brunofgc | 0:1c0a769988ee | 366 | |
brunofgc | 0:1c0a769988ee | 367 | uint8_t modemCom::sendToModem(char *string,uint8_t confirmado,char *codigoConfirmacao,uint16_t timeOut,uint8_t maxTentativas) |
brunofgc | 0:1c0a769988ee | 368 | { |
brunofgc | 0:1c0a769988ee | 369 | uint16_t timeOutProcesso; |
brunofgc | 0:1c0a769988ee | 370 | uint16_t indexString,strLen; //Para exibir retentativas! |
brunofgc | 0:1c0a769988ee | 371 | led3=1; |
brunofgc | 0:1c0a769988ee | 372 | while(maxTentativas){ |
brunofgc | 0:1c0a769988ee | 373 | timeOutProcesso=timeOut; |
brunofgc | 0:1c0a769988ee | 374 | //UARTSend(2,string,strlen(string)); |
brunofgc | 0:1c0a769988ee | 375 | if(string!=NULL) { |
brunofgc | 0:1c0a769988ee | 376 | modem.puts(string); |
brunofgc | 0:1c0a769988ee | 377 | #ifdef xereta_modem |
brunofgc | 0:1c0a769988ee | 378 | //DebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebug |
brunofgc | 0:1c0a769988ee | 379 | pc.printf("Sai pro modem <%s>\n",string); //Enviando na serial PC para debug |
brunofgc | 0:1c0a769988ee | 380 | #endif |
brunofgc | 0:1c0a769988ee | 381 | } |
brunofgc | 0:1c0a769988ee | 382 | if(confirmado==1){ |
brunofgc | 0:1c0a769988ee | 383 | *codigoConfirmacao=0; |
brunofgc | 0:1c0a769988ee | 384 | modemCom::status.ERROR=0; |
brunofgc | 0:1c0a769988ee | 385 | while((timeOutProcesso)&&(!(*codigoConfirmacao))&&(!modemCom::status.ERROR)) { |
brunofgc | 0:1c0a769988ee | 386 | osDelay(5); //timeOut em mS |
brunofgc | 0:1c0a769988ee | 387 | timeOutProcesso--; |
brunofgc | 0:1c0a769988ee | 388 | } |
brunofgc | 0:1c0a769988ee | 389 | if(modemCom::status.ERROR||modemCom::status.busy) { |
brunofgc | 0:1c0a769988ee | 390 | osDelay(1000);//espera 0.1S |
brunofgc | 0:1c0a769988ee | 391 | } |
brunofgc | 0:1c0a769988ee | 392 | |
brunofgc | 0:1c0a769988ee | 393 | #ifdef xereta_modem |
brunofgc | 0:1c0a769988ee | 394 | //DebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebugDebug |
brunofgc | 0:1c0a769988ee | 395 | pc.printf("Vem do modem <%s>\n",bufModem.getRowBuffer()); |
brunofgc | 0:1c0a769988ee | 396 | #endif |
brunofgc | 0:1c0a769988ee | 397 | if(*codigoConfirmacao==1) { |
brunofgc | 0:1c0a769988ee | 398 | led3=0; |
brunofgc | 0:1c0a769988ee | 399 | return maxTentativas; |
brunofgc | 0:1c0a769988ee | 400 | } |
brunofgc | 0:1c0a769988ee | 401 | strLen = strlen(string); |
brunofgc | 0:1c0a769988ee | 402 | pc.printf("Erro em sendToModem codigo <%s> Retentativa de envio da string <",bufModem.getRowBuffer()); |
brunofgc | 0:1c0a769988ee | 403 | for(indexString=0; indexString<(strLen-1); indexString++) { |
brunofgc | 0:1c0a769988ee | 404 | pc.putc(string[indexString]); |
brunofgc | 0:1c0a769988ee | 405 | } |
brunofgc | 0:1c0a769988ee | 406 | pc.printf("> maxTentativas = %lu.\n",maxTentativas); |
brunofgc | 0:1c0a769988ee | 407 | } |
brunofgc | 0:1c0a769988ee | 408 | maxTentativas--; |
brunofgc | 0:1c0a769988ee | 409 | } |
brunofgc | 0:1c0a769988ee | 410 | led3=0; |
brunofgc | 0:1c0a769988ee | 411 | return 0; |
brunofgc | 0:1c0a769988ee | 412 | } |
brunofgc | 0:1c0a769988ee | 413 | |
brunofgc | 0:1c0a769988ee | 414 | bool modemCom::closeConnection(uint8_t id){ |
brunofgc | 0:1c0a769988ee | 415 | char command[20]; |
brunofgc | 0:1c0a769988ee | 416 | sprintf(command,"AT+CIPCLOSE=%u\r\n",id); |
brunofgc | 3:9598af355293 | 417 | return modemCom::sendToModem(command,1,&modemCom::status.CLOSED,500,5); |
brunofgc | 0:1c0a769988ee | 418 | } |
brunofgc | 0:1c0a769988ee | 419 | |
brunofgc | 0:1c0a769988ee | 420 | char modemCom::cipSend(uint8_t connID,char *buffer,uint16_t len){ |
brunofgc | 0:1c0a769988ee | 421 | char aux[20]; |
brunofgc | 0:1c0a769988ee | 422 | uint16_t i; |
brunofgc | 0:1c0a769988ee | 423 | if(!len){ |
brunofgc | 0:1c0a769988ee | 424 | len = strlen(buffer); |
brunofgc | 0:1c0a769988ee | 425 | } |
brunofgc | 0:1c0a769988ee | 426 | printf("conteudo de cipsend.<"); |
brunofgc | 0:1c0a769988ee | 427 | sprintf(aux,"AT+CIPSENDEX=%u,%lu\r\n",connID,len); |
brunofgc | 0:1c0a769988ee | 428 | if(sendToModem(aux,1,&modemCom::status.OK,500,1)){ |
brunofgc | 0:1c0a769988ee | 429 | for(i=0;i<len;i++){ |
brunofgc | 0:1c0a769988ee | 430 | modem.putc(buffer[i]); |
brunofgc | 0:1c0a769988ee | 431 | pc.putc(buffer[i]); |
brunofgc | 0:1c0a769988ee | 432 | } |
brunofgc | 0:1c0a769988ee | 433 | }else{ |
brunofgc | 0:1c0a769988ee | 434 | return 0; |
brunofgc | 0:1c0a769988ee | 435 | } |
brunofgc | 0:1c0a769988ee | 436 | printf(">\r\n"); |
brunofgc | 0:1c0a769988ee | 437 | if(sendToModem("",1,&modemCom::status.SEND_OK,2000,1)){ |
brunofgc | 0:1c0a769988ee | 438 | return 1; |
brunofgc | 0:1c0a769988ee | 439 | } |
brunofgc | 0:1c0a769988ee | 440 | return 2; |
brunofgc | 0:1c0a769988ee | 441 | } |
brunofgc | 0:1c0a769988ee | 442 | |
brunofgc | 0:1c0a769988ee | 443 | void modemCom::webServer(void){ |
brunofgc | 0:1c0a769988ee | 444 | char buf[512]; |
brunofgc | 0:1c0a769988ee | 445 | char auxMsg[50]; |
brunofgc | 0:1c0a769988ee | 446 | bool encontrado=0; |
brunofgc | 0:1c0a769988ee | 447 | uint32_t aux_int; |
brunofgc | 0:1c0a769988ee | 448 | |
brunofgc | 0:1c0a769988ee | 449 | |
brunofgc | 0:1c0a769988ee | 450 | |
brunofgc | 2:55b7b466e742 | 451 | if(strstr(bufInPtr,"favicon.ico")){ |
brunofgc | 2:55b7b466e742 | 452 | //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 | 453 | 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 | 454 | if(cipSend(connID,buf,0)==1){ |
brunofgc | 2:55b7b466e742 | 455 | printf("Requisicao enviada!\n"); |
brunofgc | 2:55b7b466e742 | 456 | }else{ |
brunofgc | 2:55b7b466e742 | 457 | printf("Requisicao nao enviada 1!!\n"); |
brunofgc | 2:55b7b466e742 | 458 | } |
brunofgc | 2:55b7b466e742 | 459 | encontrado = 1; |
brunofgc | 2:55b7b466e742 | 460 | } |
brunofgc | 2:55b7b466e742 | 461 | |
brunofgc | 0:1c0a769988ee | 462 | if(strstr(bufInPtr,"comandos.htm")){ |
brunofgc | 0:1c0a769988ee | 463 | |
brunofgc | 0:1c0a769988ee | 464 | //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 | 465 | 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 | 466 | if(cipSend(connID,buf,0)==1){ |
brunofgc | 0:1c0a769988ee | 467 | printf("Requisicao enviada!\n"); |
brunofgc | 0:1c0a769988ee | 468 | }else{ |
brunofgc | 0:1c0a769988ee | 469 | printf("Requisicao nao enviada 1!!\n"); |
brunofgc | 0:1c0a769988ee | 470 | } |
brunofgc | 0:1c0a769988ee | 471 | encontrado = 1; |
brunofgc | 0:1c0a769988ee | 472 | } |
brunofgc | 0:1c0a769988ee | 473 | |
brunofgc | 0:1c0a769988ee | 474 | if(strstr(bufInPtr,"configRede.htm")){ |
brunofgc | 0:1c0a769988ee | 475 | |
brunofgc | 0:1c0a769988ee | 476 | |
brunofgc | 0:1c0a769988ee | 477 | //Pegando o IP |
brunofgc | 0:1c0a769988ee | 478 | sendToModem("AT+CIFSR\r\n",1,&modemCom::status.OK,200,1); |
brunofgc | 0:1c0a769988ee | 479 | //sprintf(buf,"<script>window.location.href = 'http://www.vitaesolucoes.com.br/controladorawifi/config.php?ip=%s'</script>",modemStatus.STAIP); |
brunofgc | 0:1c0a769988ee | 480 | |
brunofgc | 0:1c0a769988ee | 481 | //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 | 482 | 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 | 483 | |
brunofgc | 0:1c0a769988ee | 484 | cipSend(connID,buf,0); |
brunofgc | 0:1c0a769988ee | 485 | encontrado = 1; |
brunofgc | 0:1c0a769988ee | 486 | } |
brunofgc | 0:1c0a769988ee | 487 | |
brunofgc | 0:1c0a769988ee | 488 | if(strstr(bufInPtr,"cwjap.htm")){ |
brunofgc | 0:1c0a769988ee | 489 | //cwjap.htm?ssid=VSA&password=vitaeBBYP&ip=0&port=80 |
brunofgc | 0:1c0a769988ee | 490 | |
brunofgc | 0:1c0a769988ee | 491 | strtok(bufInPtr,"="); |
brunofgc | 3:9598af355293 | 492 | strcpy(modemCom::status.ssid,strtok(NULL,"&")); //pegando ssid |
brunofgc | 0:1c0a769988ee | 493 | |
brunofgc | 0:1c0a769988ee | 494 | strtok(NULL,"="); |
brunofgc | 0:1c0a769988ee | 495 | strcpy(modemCom::status.password,strtok(NULL,"&")); //pegando password |
brunofgc | 0:1c0a769988ee | 496 | |
brunofgc | 0:1c0a769988ee | 497 | //strtok(NULL,"="); |
brunofgc | 0:1c0a769988ee | 498 | //strcpy(modemStatus.STAIP,strtok(NULL,"&")); //pegando ip |
brunofgc | 0:1c0a769988ee | 499 | |
brunofgc | 0:1c0a769988ee | 500 | strtok(NULL,"="); |
brunofgc | 3:9598af355293 | 501 | strcpy(modemCom::status.port,strtok(NULL," ")); //pegando port |
brunofgc | 0:1c0a769988ee | 502 | |
brunofgc | 3:9598af355293 | 503 | pc.printf("Antes <%s>.\r\n",modemCom::status.ssid); |
brunofgc | 3:9598af355293 | 504 | diversos::strReplace(modemCom::status.ssid,"%20"," "); |
brunofgc | 3:9598af355293 | 505 | pc.printf("Tirado %%20 <%s>.\r\n",modemCom::status.ssid); |
brunofgc | 3:9598af355293 | 506 | diversos::strReplace(modemCom::status.ssid,"+"," "); |
brunofgc | 3:9598af355293 | 507 | pc.printf("Tirado + <%s>.\r\n",modemCom::status.ssid); |
brunofgc | 3:9598af355293 | 508 | |
brunofgc | 3:9598af355293 | 509 | if((modemCom::status.ssid[0]!=0) && (modemCom::status.password[0]!=0)){ |
brunofgc | 3:9598af355293 | 510 | sprintf(buf,"HTTP/1.1 200 OK\n\nACK"); |
brunofgc | 0:1c0a769988ee | 511 | mudaRede = true; |
brunofgc | 0:1c0a769988ee | 512 | }else{ |
brunofgc | 2:55b7b466e742 | 513 | sprintf(buf,"HTTP/1.1 200 OK\n\nNACK"); |
brunofgc | 3:9598af355293 | 514 | } |
brunofgc | 3:9598af355293 | 515 | cipSend(connID,buf,0); |
brunofgc | 0:1c0a769988ee | 516 | encontrado = 1; |
brunofgc | 0:1c0a769988ee | 517 | } |
brunofgc | 0:1c0a769988ee | 518 | |
brunofgc | 0:1c0a769988ee | 519 | /*if(strstr(bufInPtr,"leTemperaturas.htm")){ |
brunofgc | 0:1c0a769988ee | 520 | |
brunofgc | 0:1c0a769988ee | 521 | |
brunofgc | 0:1c0a769988ee | 522 | |
brunofgc | 0:1c0a769988ee | 523 | strtok(bufInPtr,"="); |
brunofgc | 0:1c0a769988ee | 524 | bufInPtr = strtok(NULL,NULL); |
brunofgc | 0:1c0a769988ee | 525 | //sprintf(buf,"%s",bufInPtr); |
brunofgc | 0:1c0a769988ee | 526 | aux_int = atoi(bufInPtr); |
brunofgc | 0:1c0a769988ee | 527 | aux_int--; |
brunofgc | 0:1c0a769988ee | 528 | */ |
brunofgc | 0:1c0a769988ee | 529 | |
brunofgc | 0:1c0a769988ee | 530 | /*if(num_temperatureSensors){ |
brunofgc | 0:1c0a769988ee | 531 | //Zerano conteudo de buff |
brunofgc | 0:1c0a769988ee | 532 | sprintf(buf,"callback({\"response\":\""); |
brunofgc | 0:1c0a769988ee | 533 | probe[0]->convertTemperature(true, DS1820::all_devices); //Start temperature conversion, wait until ready |
brunofgc | 0:1c0a769988ee | 534 | for (int i = 0; i<num_temperatureSensors; i++){ |
brunofgc | 0:1c0a769988ee | 535 | sprintf(auxMsg,"%3.1f;",probe[i]->temperature()); |
brunofgc | 0:1c0a769988ee | 536 | strcat(buf,auxMsg); |
brunofgc | 0:1c0a769988ee | 537 | } |
brunofgc | 0:1c0a769988ee | 538 | buf[strlen(buf)-1]=0; |
brunofgc | 0:1c0a769988ee | 539 | sprintf(auxMsg,"\"})"); |
brunofgc | 0:1c0a769988ee | 540 | strcat(buf,auxMsg); |
brunofgc | 0:1c0a769988ee | 541 | }else{ |
brunofgc | 0:1c0a769988ee | 542 | sprintf(buf,"callback({\"response\":\"Sensor_ERROR\"})"); |
brunofgc | 0:1c0a769988ee | 543 | } |
brunofgc | 0:1c0a769988ee | 544 | if(cipSend(connID,buf,0)==1){ |
brunofgc | 0:1c0a769988ee | 545 | printf("Requisicao enviada!\n"); |
brunofgc | 0:1c0a769988ee | 546 | }else{ |
brunofgc | 0:1c0a769988ee | 547 | printf("Requisicao nao enviada 1!!\n"); |
brunofgc | 0:1c0a769988ee | 548 | } |
brunofgc | 0:1c0a769988ee | 549 | encontrado = 1; |
brunofgc | 0:1c0a769988ee | 550 | }*/ |
brunofgc | 0:1c0a769988ee | 551 | |
brunofgc | 0:1c0a769988ee | 552 | |
brunofgc | 0:1c0a769988ee | 553 | |
brunofgc | 0:1c0a769988ee | 554 | if(strstr(bufInPtr,"lecomandocontrole.htm")){ |
brunofgc | 0:1c0a769988ee | 555 | led2=1; |
brunofgc | 0:1c0a769988ee | 556 | uint16_t timeout = 5000; |
brunofgc | 0:1c0a769988ee | 557 | IrDetect.reset(); |
brunofgc | 0:1c0a769988ee | 558 | IrDetect.start(); |
brunofgc | 0:1c0a769988ee | 559 | |
brunofgc | 0:1c0a769988ee | 560 | while((IrDetect.read_ms()<timeout) && (!detectaIRIn())); |
brunofgc | 0:1c0a769988ee | 561 | if(IrDetect.read_ms()<timeout){ |
brunofgc | 0:1c0a769988ee | 562 | serializaPacoteIR(connID); |
brunofgc | 0:1c0a769988ee | 563 | }else{ |
brunofgc | 2:55b7b466e742 | 564 | sprintf(buf,"erro"); |
brunofgc | 0:1c0a769988ee | 565 | if(cipSend(connID,buf,0)==1){ |
brunofgc | 0:1c0a769988ee | 566 | printf("Requisicao enviada!\n"); |
brunofgc | 0:1c0a769988ee | 567 | } |
brunofgc | 0:1c0a769988ee | 568 | } |
brunofgc | 6:d4ebbaaba295 | 569 | IrDetect.stop(); |
brunofgc | 6:d4ebbaaba295 | 570 | |
brunofgc | 0:1c0a769988ee | 571 | encontrado = 1; |
brunofgc | 0:1c0a769988ee | 572 | led2=0; |
brunofgc | 0:1c0a769988ee | 573 | } |
brunofgc | 0:1c0a769988ee | 574 | |
brunofgc | 2:55b7b466e742 | 575 | if(!encontrado){ |
brunofgc | 0:1c0a769988ee | 576 | sprintf(buf,"Pagina Nao Encontrada."); |
brunofgc | 0:1c0a769988ee | 577 | if(cipSend(connID,buf,0)==1){ |
brunofgc | 0:1c0a769988ee | 578 | printf("Requisicao enviada!\n"); |
brunofgc | 0:1c0a769988ee | 579 | }else{ |
brunofgc | 0:1c0a769988ee | 580 | printf("Requisicao nao enviada 3!!\n"); |
brunofgc | 0:1c0a769988ee | 581 | } |
brunofgc | 0:1c0a769988ee | 582 | } |
brunofgc | 0:1c0a769988ee | 583 | |
brunofgc | 2:55b7b466e742 | 584 | modemCom::closeConnection(connID); |
brunofgc | 2:55b7b466e742 | 585 | /*sprintf(buf,"AT+CIPCLOSE=%u\r\n",connID); |
brunofgc | 2:55b7b466e742 | 586 | sendToModem(buf,1,&modemCom::status.OK,300,1);*/ |
brunofgc | 0:1c0a769988ee | 587 | |
brunofgc | 0:1c0a769988ee | 588 | if(mudaRede){ |
brunofgc | 0:1c0a769988ee | 589 | mudaRede = false; |
brunofgc | 0:1c0a769988ee | 590 | modemCom::sendToModem("AT+CIPSERVER=0\r\n",1,&modemCom::status.OK,2000,1); |
brunofgc | 0:1c0a769988ee | 591 | 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 | 592 | if(sdCard::abreArquivo(&sdCard::config,"w")){ |
brunofgc | 0:1c0a769988ee | 593 | pc.printf("Guardando config.\r\n"); |
brunofgc | 0:1c0a769988ee | 594 | fprintf(sdCard::config.fp,"SSID:%s\r\nPASSWORD:%s\r\nPORT:%s",modemCom::status.ssid,modemCom::status.password,modemCom::status.port); |
brunofgc | 0:1c0a769988ee | 595 | sdCard::fechaArquivo(&sdCard::config); |
brunofgc | 0:1c0a769988ee | 596 | modemCom::conectaWiFi(); |
brunofgc | 0:1c0a769988ee | 597 | } |
brunofgc | 0:1c0a769988ee | 598 | } |
brunofgc | 0:1c0a769988ee | 599 | } |
brunofgc | 1:0e0967c88590 | 600 | |
brunofgc | 1:0e0967c88590 | 601 | int16_t modemCom::getRSSI(void){ |
brunofgc | 2:55b7b466e742 | 602 | /*char *ptr; |
brunofgc | 1:0e0967c88590 | 603 | int16_t RSSI = 0; |
brunofgc | 2:55b7b466e742 | 604 | if(modemCom::sendToModem("AT+CWLAP\r\n",1,&modemCom::status.OK,1000,1)){ |
brunofgc | 1:0e0967c88590 | 605 | ptr = strstr(modemCom::bufIn,(const char*)modemCom::status.ssid); |
brunofgc | 1:0e0967c88590 | 606 | if(ptr){ |
brunofgc | 1:0e0967c88590 | 607 | ptr = strtok(ptr,","); |
brunofgc | 1:0e0967c88590 | 608 | ptr = strtok(NULL,","); |
brunofgc | 1:0e0967c88590 | 609 | RSSI = atoi(ptr); |
brunofgc | 1:0e0967c88590 | 610 | } |
brunofgc | 1:0e0967c88590 | 611 | } |
brunofgc | 2:55b7b466e742 | 612 | return RSSI;*/ |
brunofgc | 2:55b7b466e742 | 613 | return -50; |
brunofgc | 1:0e0967c88590 | 614 | } |
brunofgc | 0:1c0a769988ee | 615 | |
brunofgc | 0:1c0a769988ee | 616 | void modemCom::conectaWiFi(void){ |
brunofgc | 0:1c0a769988ee | 617 | //Função para mudança de rede |
brunofgc | 0:1c0a769988ee | 618 | char *ptr; |
brunofgc | 0:1c0a769988ee | 619 | char aux[128]; |
brunofgc | 0:1c0a769988ee | 620 | if(sdCard::abreArquivo(&sdCard::config,"r")){ |
brunofgc | 0:1c0a769988ee | 621 | pc.printf("Abrindo arquivo de config para buscar dados de conexao.\r\n"); |
brunofgc | 0:1c0a769988ee | 622 | //"SSID:DROME\r\nPASSWORD:VITAE\r\nPORT:80" |
brunofgc | 0:1c0a769988ee | 623 | |
brunofgc | 0:1c0a769988ee | 624 | //Pegando SSID |
brunofgc | 0:1c0a769988ee | 625 | fgets(modemCom::status.ssid,40,sdCard::config.fp); |
brunofgc | 2:55b7b466e742 | 626 | ptr = strstr(modemCom::status.ssid,"SSID"); |
brunofgc | 2:55b7b466e742 | 627 | if(ptr!=NULL){ |
brunofgc | 2:55b7b466e742 | 628 | ptr = strtok(modemCom::status.ssid,":"); |
brunofgc | 2:55b7b466e742 | 629 | ptr = strtok(NULL,"\r\n"); |
brunofgc | 2:55b7b466e742 | 630 | strcpy(modemCom::status.ssid,ptr); |
brunofgc | 2:55b7b466e742 | 631 | |
brunofgc | 2:55b7b466e742 | 632 | //Pegando PASSWORD |
brunofgc | 2:55b7b466e742 | 633 | fgets(modemCom::status.password,50,sdCard::config.fp); |
brunofgc | 2:55b7b466e742 | 634 | ptr = strtok(modemCom::status.password,":"); |
brunofgc | 2:55b7b466e742 | 635 | ptr = strtok(NULL,"\r\n"); |
brunofgc | 2:55b7b466e742 | 636 | strcpy(modemCom::status.password,ptr); |
brunofgc | 2:55b7b466e742 | 637 | |
brunofgc | 2:55b7b466e742 | 638 | //Pegando PORT |
brunofgc | 2:55b7b466e742 | 639 | fgets(modemCom::status.port,20,sdCard::config.fp); |
brunofgc | 2:55b7b466e742 | 640 | ptr = strtok(modemCom::status.port,":"); |
brunofgc | 2:55b7b466e742 | 641 | ptr = strtok(NULL,"\r\n"); |
brunofgc | 2:55b7b466e742 | 642 | strcpy(modemCom::status.port,ptr); |
brunofgc | 2:55b7b466e742 | 643 | }else{ |
brunofgc | 2:55b7b466e742 | 644 | strcpy(modemCom::status.ssid,"VSA"); |
brunofgc | 2:55b7b466e742 | 645 | strcpy(modemCom::status.password,"vitaeBBYP"); |
brunofgc | 2:55b7b466e742 | 646 | strcpy(modemCom::status.port,"80"); |
brunofgc | 2:55b7b466e742 | 647 | } |
brunofgc | 0:1c0a769988ee | 648 | sdCard::fechaArquivo(&sdCard::config); |
brunofgc | 0:1c0a769988ee | 649 | 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 | 650 | pc.printf("Iniciando conexao wifi.\r\n"); |
brunofgc | 2:55b7b466e742 | 651 | }else{ |
brunofgc | 2:55b7b466e742 | 652 | strcpy(modemCom::status.ssid,"VSA"); |
brunofgc | 2:55b7b466e742 | 653 | strcpy(modemCom::status.password,"vitaeBBYP"); |
brunofgc | 2:55b7b466e742 | 654 | strcpy(modemCom::status.port,"80"); |
brunofgc | 2:55b7b466e742 | 655 | } |
brunofgc | 0:1c0a769988ee | 656 | |
brunofgc | 2:55b7b466e742 | 657 | //Mudando mode para AP e Station |
brunofgc | 2:55b7b466e742 | 658 | if(modemCom::sendToModem("AT+CWMODE=3\r\n",1,&modemCom::status.OK,10000,1)) { |
brunofgc | 2:55b7b466e742 | 659 | pc.printf("Modo AP+STATION aceito.\n"); |
brunofgc | 2:55b7b466e742 | 660 | } |
brunofgc | 2:55b7b466e742 | 661 | |
brunofgc | 2:55b7b466e742 | 662 | //AT+CWJAP=\"\",\"\"\r\n |
brunofgc | 2:55b7b466e742 | 663 | sprintf(aux,"AT+CWJAP=\"%s\",\"%s\"\r\n",modemCom::status.ssid,modemCom::status.password); |
brunofgc | 2:55b7b466e742 | 664 | if(modemCom::sendToModem(aux,1,&modemCom::status.OK,10000,1)) { |
brunofgc | 2:55b7b466e742 | 665 | pc.printf("WiFi Conectado.\n"); |
brunofgc | 2:55b7b466e742 | 666 | } |
brunofgc | 0:1c0a769988ee | 667 | |
brunofgc | 0:1c0a769988ee | 668 | //AT+CIPSERVER=1,80 |
brunofgc | 0:1c0a769988ee | 669 | if(modemCom::sendToModem("AT+CIPMUX=1\r\n",1,&modemCom::status.OK,250,4)) { |
brunofgc | 0:1c0a769988ee | 670 | pc.printf("Recebeu o comando AT+CIPMUX=1.\n"); |
brunofgc | 0:1c0a769988ee | 671 | } |
brunofgc | 0:1c0a769988ee | 672 | |
brunofgc | 0:1c0a769988ee | 673 | //AT+CIPSERVER=1,80 |
brunofgc | 0:1c0a769988ee | 674 | sprintf(aux,"AT+CIPSERVER=1,%s\r\n",modemCom::status.port); |
brunofgc | 0:1c0a769988ee | 675 | if(modemCom::sendToModem(aux,1,&modemCom::status.OK,250,4)) { |
brunofgc | 0:1c0a769988ee | 676 | pc.printf("Recebeu o comando <%s>.\n",aux); |
brunofgc | 0:1c0a769988ee | 677 | } |
brunofgc | 0:1c0a769988ee | 678 | |
brunofgc | 0:1c0a769988ee | 679 | if(modemCom::sendToModem("AT+CIPSTO=60\r\n",1,&modemCom::status.OK,250,4)) { |
brunofgc | 0:1c0a769988ee | 680 | pc.printf("Recebeu o comando AT+CIPSTO=60.\n"); |
brunofgc | 2:55b7b466e742 | 681 | } |
brunofgc | 0:1c0a769988ee | 682 | |
brunofgc | 0:1c0a769988ee | 683 | sprintf(aux,"AT+CWSAP_CUR=\"DROME_WIFI_%s\",\"VsaDROME\",5,3\r\n",modemCom::status.MAC); |
brunofgc | 0:1c0a769988ee | 684 | if(modemCom::sendToModem(aux,1,&modemCom::status.OK,250,4)) { |
brunofgc | 0:1c0a769988ee | 685 | pc.printf("Recebeu o comando %s.\n",aux); |
brunofgc | 0:1c0a769988ee | 686 | } |
brunofgc | 0:1c0a769988ee | 687 | |
brunofgc | 0:1c0a769988ee | 688 | |
brunofgc | 0:1c0a769988ee | 689 | if(modemCom::sendToModem("AT+CIPAP_CUR=\"192.168.4.1\",\"192.168.4.1\",\"255.255.255.0\"\r\n",1,&modemCom::status.OK,250,4)) { |
brunofgc | 0:1c0a769988ee | 690 | pc.printf("Recebeu o comando AT+CIPAP_CUR=\"192.168.4.1\",\"192.168.4.1\",\"255.255.255.0\".\n"); |
brunofgc | 0:1c0a769988ee | 691 | } |
brunofgc | 0:1c0a769988ee | 692 | |
brunofgc | 0:1c0a769988ee | 693 | if(modemCom::sendToModem("AT+CWDHCP_CUR=2,1\r\n",1,&modemCom::status.OK,250,4)) { |
brunofgc | 0:1c0a769988ee | 694 | pc.printf("Recebeu o comando AT+CWDHCP_CUR=2,1.\n"); |
brunofgc | 2:55b7b466e742 | 695 | } |
brunofgc | 0:1c0a769988ee | 696 | |
brunofgc | 0:1c0a769988ee | 697 | } |
brunofgc | 0:1c0a769988ee | 698 | |
brunofgc | 0:1c0a769988ee | 699 | void modemCom::processaPacote(void const *args){ |
brunofgc | 0:1c0a769988ee | 700 | char *ptr,*scanPtr; |
brunofgc | 0:1c0a769988ee | 701 | bool naoCompreendido=true; |
brunofgc | 0:1c0a769988ee | 702 | uint16_t bufInLength = bufModem.getLength(); |
brunofgc | 0:1c0a769988ee | 703 | modemCom::bufIn = bufModem.get(); |
brunofgc | 0:1c0a769988ee | 704 | |
brunofgc | 5:7801f913384e | 705 | //pc.printf("Entrada do modem<\n\n%s\n\n>\n",modemCom::bufIn); |
brunofgc | 3:9598af355293 | 706 | |
brunofgc | 0:1c0a769988ee | 707 | /* |
brunofgc | 0:1c0a769988ee | 708 | if(modemCom::exibeEntradaPacote){ |
brunofgc | 0:1c0a769988ee | 709 | modemCom::exibeEntradaPacote=false; |
brunofgc | 0:1c0a769988ee | 710 | pc.printf("Lido entrada Pacote modem<%s>.\n",modemCom::bufIn); |
brunofgc | 0:1c0a769988ee | 711 | } |
brunofgc | 0:1c0a769988ee | 712 | */ |
brunofgc | 0:1c0a769988ee | 713 | |
brunofgc | 0:1c0a769988ee | 714 | //Reconhecendo GET HTTP e atendendo com um servidor web |
brunofgc | 0:1c0a769988ee | 715 | if(strstr(modemCom::bufIn,"GET /") && strstr(modemCom::bufIn,"HTTP/1.1")){ |
brunofgc | 0:1c0a769988ee | 716 | ptr=strstr(modemCom::bufIn,"GET /"); |
brunofgc | 0:1c0a769988ee | 717 | if(ptr) { |
brunofgc | 0:1c0a769988ee | 718 | naoCompreendido=false; |
brunofgc | 0:1c0a769988ee | 719 | ptr = strtok(ptr,"/"); |
brunofgc | 0:1c0a769988ee | 720 | ptr = strtok(NULL," "); |
brunofgc | 0:1c0a769988ee | 721 | bufInPtr = ptr; |
brunofgc | 0:1c0a769988ee | 722 | modemCom::atendendoWebServer = true; |
brunofgc | 0:1c0a769988ee | 723 | //osSignalSet(idThreadComunicacoes, 0x1); |
brunofgc | 0:1c0a769988ee | 724 | |
brunofgc | 0:1c0a769988ee | 725 | } |
brunofgc | 0:1c0a769988ee | 726 | } |
brunofgc | 0:1c0a769988ee | 727 | |
brunofgc | 0:1c0a769988ee | 728 | //Buscando connect |
brunofgc | 0:1c0a769988ee | 729 | if(!modemCom::atendendoWebServer){ |
brunofgc | 0:1c0a769988ee | 730 | ptr=strstr(modemCom::bufIn,"CONNECT\r\n"); |
brunofgc | 0:1c0a769988ee | 731 | if(ptr) { |
brunofgc | 0:1c0a769988ee | 732 | ptr = strtok(modemCom::bufIn,","); |
brunofgc | 0:1c0a769988ee | 733 | connID = atoi(ptr); |
brunofgc | 0:1c0a769988ee | 734 | naoCompreendido=false; |
brunofgc | 0:1c0a769988ee | 735 | modemCom::status.serverConnected=1; |
brunofgc | 0:1c0a769988ee | 736 | pc.printf("Recebido string CONNECT socket na conexao %u.\n",connID); |
brunofgc | 0:1c0a769988ee | 737 | if(!modemCom::status.emComunicacao){ |
brunofgc | 0:1c0a769988ee | 738 | //Preciso saber se é decorrente de uma chamada minha ou se é uma entrada de socket. |
brunofgc | 0:1c0a769988ee | 739 | pc.printf("Atendendo socket entrante na conexao %u.\n",connID); |
brunofgc | 0:1c0a769988ee | 740 | modemCom::status.SRINGsockEntrante = true; |
brunofgc | 0:1c0a769988ee | 741 | } |
brunofgc | 0:1c0a769988ee | 742 | modemCom::status.emComunicacao=true; |
brunofgc | 0:1c0a769988ee | 743 | modemCom::bufIn = strtok(NULL,NULL); |
brunofgc | 0:1c0a769988ee | 744 | } |
brunofgc | 0:1c0a769988ee | 745 | } |
brunofgc | 0:1c0a769988ee | 746 | |
brunofgc | 0:1c0a769988ee | 747 | IPDNumBytes = 0; |
brunofgc | 0:1c0a769988ee | 748 | ptr = strstr(modemCom::bufIn,"+IPD"); |
brunofgc | 0:1c0a769988ee | 749 | if(ptr!=NULL){ |
brunofgc | 0:1c0a769988ee | 750 | //+IPD,0,480:GET |
brunofgc | 0:1c0a769988ee | 751 | strtok(ptr,","); //Separando +IPD |
brunofgc | 0:1c0a769988ee | 752 | |
brunofgc | 0:1c0a769988ee | 753 | ptr = strtok(NULL,","); //Pegando o numero da conexão |
brunofgc | 0:1c0a769988ee | 754 | connID = atoi(ptr); |
brunofgc | 0:1c0a769988ee | 755 | |
brunofgc | 0:1c0a769988ee | 756 | ptr = strtok(NULL,":"); //Pegando o numero de caracteres IPD |
brunofgc | 0:1c0a769988ee | 757 | IPDNumBytes = atoi(ptr); |
brunofgc | 0:1c0a769988ee | 758 | |
brunofgc | 0:1c0a769988ee | 759 | modemCom::bufIn = strtok(NULL,NULL); |
brunofgc | 0:1c0a769988ee | 760 | |
brunofgc | 0:1c0a769988ee | 761 | printf("Recebido pacote via conexao %u com %lu bytes.\n",connID,IPDNumBytes); |
brunofgc | 0:1c0a769988ee | 762 | |
brunofgc | 0:1c0a769988ee | 763 | } |
brunofgc | 0:1c0a769988ee | 764 | |
brunofgc | 0:1c0a769988ee | 765 | if(modemCom::atendendoWebServer){ |
brunofgc | 0:1c0a769988ee | 766 | return; |
brunofgc | 0:1c0a769988ee | 767 | } |
brunofgc | 0:1c0a769988ee | 768 | |
brunofgc | 0:1c0a769988ee | 769 | //printf("Sobrou pacote <%s>.\n\n",modemCom::bufIn); |
brunofgc | 0:1c0a769988ee | 770 | //uint8_t i; |
brunofgc | 0:1c0a769988ee | 771 | |
brunofgc | 0:1c0a769988ee | 772 | |
brunofgc | 0:1c0a769988ee | 773 | |
brunofgc | 0:1c0a769988ee | 774 | ptr=strstr(modemCom::bufIn,"*ServerAck*"); |
brunofgc | 0:1c0a769988ee | 775 | if(ptr) { |
brunofgc | 0:1c0a769988ee | 776 | naoCompreendido=false; |
brunofgc | 0:1c0a769988ee | 777 | modemCom::status.ServerAck=1; |
brunofgc | 0:1c0a769988ee | 778 | pc.printf("SERVER RESPONSE\n%s\n\n",modemCom::bufIn); |
brunofgc | 0:1c0a769988ee | 779 | } |
brunofgc | 0:1c0a769988ee | 780 | |
brunofgc | 0:1c0a769988ee | 781 | ptr=strstr(modemCom::bufIn,"*ServerCommand*"); |
brunofgc | 0:1c0a769988ee | 782 | if(ptr) { |
brunofgc | 0:1c0a769988ee | 783 | naoCompreendido=false; |
brunofgc | 0:1c0a769988ee | 784 | scanPtr = strtok (ptr,"\\"); |
brunofgc | 0:1c0a769988ee | 785 | scanPtr = strtok (NULL,">"); |
brunofgc | 0:1c0a769988ee | 786 | //modemCom::status.ServerCommand=1; |
brunofgc | 0:1c0a769988ee | 787 | strcpy(commands::buffer,scanPtr); |
brunofgc | 0:1c0a769988ee | 788 | pc.printf("Comandos <%s>.\n",commands::buffer); |
brunofgc | 0:1c0a769988ee | 789 | executaComandoServer = true; |
brunofgc | 0:1c0a769988ee | 790 | //Se devo executar comando tenho que sair imediatamente. |
brunofgc | 0:1c0a769988ee | 791 | return; |
brunofgc | 0:1c0a769988ee | 792 | } |
brunofgc | 0:1c0a769988ee | 793 | |
brunofgc | 0:1c0a769988ee | 794 | //if(modemCom::status.recebendoArquivoDoServer){ |
brunofgc | 0:1c0a769988ee | 795 | if(IPDNumBytes && modemCom::status.recebendoArquivoDoServer && (strstr(modemCom::bufIn,"CONNECT")==NULL) && (strstr(modemCom::bufIn,"*ServerCommand*")==NULL) ){ |
brunofgc | 0:1c0a769988ee | 796 | //Appenda bufIn no devicescfg |
brunofgc | 0:1c0a769988ee | 797 | //sdCard::insereDadosArquivo(&sdCard::devices,modemCom::bufIn,bufInLength); |
brunofgc | 0:1c0a769988ee | 798 | sdCard::insereDadosArquivo(&sdCard::tempFile,modemCom::bufIn,IPDNumBytes); |
brunofgc | 0:1c0a769988ee | 799 | osDelay(250); |
brunofgc | 1:0e0967c88590 | 800 | |
brunofgc | 0:1c0a769988ee | 801 | |
brunofgc | 0:1c0a769988ee | 802 | modemCom::status.timeOut=100; |
brunofgc | 0:1c0a769988ee | 803 | |
brunofgc | 0:1c0a769988ee | 804 | sprintf(modemCom::bufIn,"AT+CIPSEND=%u,%lu\r\n",connID,10); |
brunofgc | 0:1c0a769988ee | 805 | modem.puts(modemCom::bufIn); |
brunofgc | 0:1c0a769988ee | 806 | osDelay(100); |
brunofgc | 0:1c0a769988ee | 807 | modem.puts("sendData\r\n"); |
brunofgc | 0:1c0a769988ee | 808 | pc.printf("sendData\n"); |
brunofgc | 0:1c0a769988ee | 809 | return; |
brunofgc | 0:1c0a769988ee | 810 | } |
brunofgc | 0:1c0a769988ee | 811 | |
brunofgc | 0:1c0a769988ee | 812 | //Reconhecimento dos status |
brunofgc | 0:1c0a769988ee | 813 | modemCom::status.PROMPT_ENVIO_COMMAND_MODE=0; |
brunofgc | 0:1c0a769988ee | 814 | |
brunofgc | 0:1c0a769988ee | 815 | ptr=strstr(modemCom::bufIn,"> "); |
brunofgc | 0:1c0a769988ee | 816 | if(ptr) { |
brunofgc | 0:1c0a769988ee | 817 | naoCompreendido=false; |
brunofgc | 0:1c0a769988ee | 818 | modemCom::status.serverConnected=1; |
brunofgc | 0:1c0a769988ee | 819 | modemCom::status.commandMode=1; |
brunofgc | 0:1c0a769988ee | 820 | modemCom::status.NO_CARRIER=0; |
brunofgc | 0:1c0a769988ee | 821 | modemCom::status.PROMPT_ENVIO_COMMAND_MODE=1; |
brunofgc | 0:1c0a769988ee | 822 | } |
brunofgc | 0:1c0a769988ee | 823 | |
brunofgc | 0:1c0a769988ee | 824 | modemCom::status.OK=0; |
brunofgc | 0:1c0a769988ee | 825 | |
brunofgc | 0:1c0a769988ee | 826 | ptr=strstr(modemCom::bufIn,"OK\r"); |
brunofgc | 0:1c0a769988ee | 827 | if(ptr) { |
brunofgc | 0:1c0a769988ee | 828 | naoCompreendido=false; |
brunofgc | 0:1c0a769988ee | 829 | modemCom::status.OK=1; |
brunofgc | 0:1c0a769988ee | 830 | } |
brunofgc | 0:1c0a769988ee | 831 | |
brunofgc | 0:1c0a769988ee | 832 | |
brunofgc | 0:1c0a769988ee | 833 | ptr=strstr(modemCom::bufIn,"+CIFSR:STAIP,"); |
brunofgc | 0:1c0a769988ee | 834 | if(ptr) { |
brunofgc | 0:1c0a769988ee | 835 | naoCompreendido=false; |
brunofgc | 0:1c0a769988ee | 836 | ptr = strtok(ptr,"\""); |
brunofgc | 0:1c0a769988ee | 837 | ptr = strtok(NULL,"\""); |
brunofgc | 0:1c0a769988ee | 838 | strcpy(modemCom::status.STAIP,ptr); |
brunofgc | 0:1c0a769988ee | 839 | } |
brunofgc | 0:1c0a769988ee | 840 | |
brunofgc | 0:1c0a769988ee | 841 | modemCom::status.busy=false; |
brunofgc | 0:1c0a769988ee | 842 | ptr=strstr(modemCom::bufIn,"busy s..."); |
brunofgc | 0:1c0a769988ee | 843 | if(ptr) { |
brunofgc | 0:1c0a769988ee | 844 | naoCompreendido=false; |
brunofgc | 0:1c0a769988ee | 845 | modemCom::status.busy=1; |
brunofgc | 0:1c0a769988ee | 846 | } |
brunofgc | 0:1c0a769988ee | 847 | |
brunofgc | 0:1c0a769988ee | 848 | modemCom::status.SEND_OK=0; |
brunofgc | 0:1c0a769988ee | 849 | ptr=strstr(modemCom::bufIn,"SEND OK"); |
brunofgc | 0:1c0a769988ee | 850 | if(ptr) { |
brunofgc | 0:1c0a769988ee | 851 | naoCompreendido=false; |
brunofgc | 0:1c0a769988ee | 852 | modemCom::status.SEND_OK=1; |
brunofgc | 0:1c0a769988ee | 853 | } |
brunofgc | 0:1c0a769988ee | 854 | |
brunofgc | 0:1c0a769988ee | 855 | ptr=strstr(modemCom::bufIn,"#SGACT: 1,"); |
brunofgc | 0:1c0a769988ee | 856 | if(ptr) { |
brunofgc | 0:1c0a769988ee | 857 | naoCompreendido=false; |
brunofgc | 0:1c0a769988ee | 858 | if(ptr[10]=='1') { |
brunofgc | 0:1c0a769988ee | 859 | modemCom::status.associated=1; |
brunofgc | 0:1c0a769988ee | 860 | } |
brunofgc | 0:1c0a769988ee | 861 | if(ptr[10]=='0') { |
brunofgc | 0:1c0a769988ee | 862 | modemCom::status.associated=0; |
brunofgc | 0:1c0a769988ee | 863 | } |
brunofgc | 0:1c0a769988ee | 864 | } else { |
brunofgc | 0:1c0a769988ee | 865 | |
brunofgc | 0:1c0a769988ee | 866 | ptr=strstr(modemCom::bufIn,"#SGACT: "); |
brunofgc | 0:1c0a769988ee | 867 | if(ptr) { |
brunofgc | 0:1c0a769988ee | 868 | naoCompreendido=false; |
brunofgc | 0:1c0a769988ee | 869 | modemCom::status.associated=1; |
brunofgc | 0:1c0a769988ee | 870 | scanPtr = strtok (ptr," "); |
brunofgc | 0:1c0a769988ee | 871 | scanPtr = strtok (NULL,"\r"); |
brunofgc | 0:1c0a769988ee | 872 | if(scanPtr) { |
brunofgc | 0:1c0a769988ee | 873 | strcpy(modemCom::status.modemIP,scanPtr); |
brunofgc | 0:1c0a769988ee | 874 | } |
brunofgc | 0:1c0a769988ee | 875 | } |
brunofgc | 0:1c0a769988ee | 876 | } |
brunofgc | 0:1c0a769988ee | 877 | |
brunofgc | 0:1c0a769988ee | 878 | |
brunofgc | 0:1c0a769988ee | 879 | ptr=strstr(modemCom::bufIn,"+CGPADDR: 1,"); |
brunofgc | 0:1c0a769988ee | 880 | if(ptr) { |
brunofgc | 0:1c0a769988ee | 881 | naoCompreendido=false; |
brunofgc | 0:1c0a769988ee | 882 | scanPtr = strtok (ptr,"\""); |
brunofgc | 0:1c0a769988ee | 883 | scanPtr = strtok (NULL,"\""); |
brunofgc | 0:1c0a769988ee | 884 | if(scanPtr) { |
brunofgc | 0:1c0a769988ee | 885 | strcpy(modemCom::status.modemIP,scanPtr); |
brunofgc | 0:1c0a769988ee | 886 | } |
brunofgc | 0:1c0a769988ee | 887 | } |
brunofgc | 0:1c0a769988ee | 888 | |
brunofgc | 0:1c0a769988ee | 889 | |
brunofgc | 0:1c0a769988ee | 890 | //#PING: 01,"187.45.195.188",5,54 |
brunofgc | 0:1c0a769988ee | 891 | ptr=strstr(modemCom::bufIn,"#PING: "); |
brunofgc | 0:1c0a769988ee | 892 | if(ptr) { |
brunofgc | 0:1c0a769988ee | 893 | naoCompreendido=false; |
brunofgc | 0:1c0a769988ee | 894 | scanPtr = strtok (ptr,"\""); |
brunofgc | 0:1c0a769988ee | 895 | scanPtr = strtok (NULL,"\""); |
brunofgc | 0:1c0a769988ee | 896 | if(scanPtr) { |
brunofgc | 0:1c0a769988ee | 897 | strcpy(modemCom::status.serverIP,scanPtr); |
brunofgc | 0:1c0a769988ee | 898 | modemCom::status.serverIP[16]=1; |
brunofgc | 0:1c0a769988ee | 899 | modemCom::status.ping=1; |
brunofgc | 0:1c0a769988ee | 900 | } |
brunofgc | 0:1c0a769988ee | 901 | } |
brunofgc | 0:1c0a769988ee | 902 | |
brunofgc | 0:1c0a769988ee | 903 | |
brunofgc | 0:1c0a769988ee | 904 | //SocketBufferPendent? |
brunofgc | 0:1c0a769988ee | 905 | //#SI: 1,123,400,10,50 |
brunofgc | 0:1c0a769988ee | 906 | |
brunofgc | 0:1c0a769988ee | 907 | ptr=strstr(modemCom::bufIn,"#SI: "); |
brunofgc | 0:1c0a769988ee | 908 | if(ptr) { |
brunofgc | 0:1c0a769988ee | 909 | //uint8_t connId; |
brunofgc | 0:1c0a769988ee | 910 | naoCompreendido=false; |
brunofgc | 0:1c0a769988ee | 911 | |
brunofgc | 0:1c0a769988ee | 912 | //Descartando "#SI: " |
brunofgc | 0:1c0a769988ee | 913 | scanPtr = strtok (ptr," "); |
brunofgc | 0:1c0a769988ee | 914 | |
brunofgc | 0:1c0a769988ee | 915 | //Capturando connId |
brunofgc | 0:1c0a769988ee | 916 | scanPtr = strtok (NULL,","); |
brunofgc | 0:1c0a769988ee | 917 | connID = atoi(scanPtr); |
brunofgc | 0:1c0a769988ee | 918 | connID--; //Para transformar em indice de linguagem C que começa em zero; |
brunofgc | 0:1c0a769988ee | 919 | |
brunofgc | 0:1c0a769988ee | 920 | //capturando sent |
brunofgc | 0:1c0a769988ee | 921 | scanPtr = strtok (NULL,","); |
brunofgc | 0:1c0a769988ee | 922 | modemCom::status.socketInfo[connID].sent = atoi(scanPtr); |
brunofgc | 0:1c0a769988ee | 923 | |
brunofgc | 0:1c0a769988ee | 924 | //capturando received |
brunofgc | 0:1c0a769988ee | 925 | scanPtr = strtok (NULL,","); |
brunofgc | 0:1c0a769988ee | 926 | modemCom::status.socketInfo[connID].received = atoi(scanPtr); |
brunofgc | 0:1c0a769988ee | 927 | |
brunofgc | 0:1c0a769988ee | 928 | //capturando buff_in |
brunofgc | 0:1c0a769988ee | 929 | scanPtr = strtok (NULL,","); |
brunofgc | 0:1c0a769988ee | 930 | modemCom::status.socketInfo[connID].buff_in = atoi(scanPtr); |
brunofgc | 0:1c0a769988ee | 931 | |
brunofgc | 0:1c0a769988ee | 932 | //capturando ack_waiting |
brunofgc | 0:1c0a769988ee | 933 | scanPtr = strtok (NULL,","); |
brunofgc | 0:1c0a769988ee | 934 | modemCom::status.socketInfo[connID].ack_waiting = atoi(scanPtr); |
brunofgc | 0:1c0a769988ee | 935 | } |
brunofgc | 0:1c0a769988ee | 936 | |
brunofgc | 0:1c0a769988ee | 937 | |
brunofgc | 0:1c0a769988ee | 938 | modemCom::status.ERROR=0; |
brunofgc | 0:1c0a769988ee | 939 | ptr=strstr(modemCom::bufIn,"ERROR"); |
brunofgc | 0:1c0a769988ee | 940 | if(ptr) { |
brunofgc | 0:1c0a769988ee | 941 | naoCompreendido=false; |
brunofgc | 0:1c0a769988ee | 942 | modemCom::status.ERROR=1; |
brunofgc | 0:1c0a769988ee | 943 | modemCom::status.OK=0; |
brunofgc | 0:1c0a769988ee | 944 | } |
brunofgc | 0:1c0a769988ee | 945 | |
brunofgc | 0:1c0a769988ee | 946 | ptr=strstr(modemCom::bufIn,"HTTP/1.1"); |
brunofgc | 0:1c0a769988ee | 947 | if(ptr) { |
brunofgc | 0:1c0a769988ee | 948 | naoCompreendido=false; |
brunofgc | 0:1c0a769988ee | 949 | pc.printf("HTTP RESPONSE\n%s\n\n",modemCom::bufIn); |
brunofgc | 0:1c0a769988ee | 950 | } |
brunofgc | 0:1c0a769988ee | 951 | |
brunofgc | 0:1c0a769988ee | 952 | ptr=strstr(modemCom::bufIn,"SRING: 1"); |
brunofgc | 0:1c0a769988ee | 953 | if(ptr) { |
brunofgc | 0:1c0a769988ee | 954 | naoCompreendido=false; |
brunofgc | 0:1c0a769988ee | 955 | modem.printf("AT#SRECV=1,1500\r"); |
brunofgc | 0:1c0a769988ee | 956 | } |
brunofgc | 0:1c0a769988ee | 957 | |
brunofgc | 0:1c0a769988ee | 958 | ptr=strstr(modemCom::bufIn,"SRING: 2"); |
brunofgc | 0:1c0a769988ee | 959 | if(ptr){ |
brunofgc | 0:1c0a769988ee | 960 | naoCompreendido=false; |
brunofgc | 0:1c0a769988ee | 961 | if(!modemCom::status.socketEntranteAtivo){ |
brunofgc | 0:1c0a769988ee | 962 | modemCom::status.SRINGsockEntrante=1; |
brunofgc | 0:1c0a769988ee | 963 | osSignalSet(idThreadComunicacoes, 0x1); |
brunofgc | 0:1c0a769988ee | 964 | }else{ |
brunofgc | 0:1c0a769988ee | 965 | //modem.printf("AT#SRECV=2,1500\r"); |
brunofgc | 0:1c0a769988ee | 966 | } |
brunofgc | 0:1c0a769988ee | 967 | } |
brunofgc | 0:1c0a769988ee | 968 | |
brunofgc | 0:1c0a769988ee | 969 | |
brunofgc | 0:1c0a769988ee | 970 | ptr=strstr(modemCom::bufIn,"NO CARRIER"); |
brunofgc | 0:1c0a769988ee | 971 | if(ptr) { |
brunofgc | 0:1c0a769988ee | 972 | naoCompreendido=false; |
brunofgc | 0:1c0a769988ee | 973 | modemCom::status.serverConnected=0; |
brunofgc | 0:1c0a769988ee | 974 | modemCom::status.commandMode=1; |
brunofgc | 0:1c0a769988ee | 975 | modemCom::status.NO_CARRIER=1; |
brunofgc | 0:1c0a769988ee | 976 | pc.printf("NO CARRIER!!\n"); |
brunofgc | 0:1c0a769988ee | 977 | } |
brunofgc | 0:1c0a769988ee | 978 | |
brunofgc | 0:1c0a769988ee | 979 | modemCom::status.CLOSED=false; |
brunofgc | 0:1c0a769988ee | 980 | |
brunofgc | 0:1c0a769988ee | 981 | ptr=strstr(modemCom::bufIn,"CLOSED"); |
brunofgc | 0:1c0a769988ee | 982 | if(ptr) { |
brunofgc | 0:1c0a769988ee | 983 | naoCompreendido=false; |
brunofgc | 0:1c0a769988ee | 984 | modemCom::status.CLOSED=1; |
brunofgc | 0:1c0a769988ee | 985 | } |
brunofgc | 0:1c0a769988ee | 986 | |
brunofgc | 0:1c0a769988ee | 987 | if(naoCompreendido) { |
brunofgc | 0:1c0a769988ee | 988 | pc.printf("Mensagem nao compreendida vinda do modem \n ->{\n\n%s\n\n}<-\n",modemCom::bufIn); |
brunofgc | 0:1c0a769988ee | 989 | } |
brunofgc | 0:1c0a769988ee | 990 | |
brunofgc | 3:9598af355293 | 991 | //if(modemCom::status.exibeBufModem){ |
brunofgc | 3:9598af355293 | 992 | // modemCom::status.exibeBufModem=false; |
brunofgc | 3:9598af355293 | 993 | //pc.printf("Entrada do modem<\n\n%s\n\n>\n",modemCom::bufIn); |
brunofgc | 3:9598af355293 | 994 | //} |
brunofgc | 0:1c0a769988ee | 995 | } |
brunofgc | 0:1c0a769988ee | 996 | |
brunofgc | 0:1c0a769988ee | 997 | uint8_t modemCom::inicializaModem(void) |
brunofgc | 0:1c0a769988ee | 998 | { |
brunofgc | 0:1c0a769988ee | 999 | //uint8_t maxTentativas=0; |
brunofgc | 0:1c0a769988ee | 1000 | |
brunofgc | 0:1c0a769988ee | 1001 | //char config[255]; |
brunofgc | 0:1c0a769988ee | 1002 | //char *ptr; |
brunofgc | 0:1c0a769988ee | 1003 | char *buf; |
brunofgc | 0:1c0a769988ee | 1004 | buf = bufModem.getRowBuffer(); |
brunofgc | 0:1c0a769988ee | 1005 | |
brunofgc | 0:1c0a769988ee | 1006 | modemCom::timeOutModem = 10; |
brunofgc | 0:1c0a769988ee | 1007 | modemCom::status.emComunicacao = true; |
brunofgc | 0:1c0a769988ee | 1008 | |
brunofgc | 0:1c0a769988ee | 1009 | if(modemCom::sendToModem("ATE0\r\n",1,&modemCom::status.OK,1000,10)) { |
brunofgc | 0:1c0a769988ee | 1010 | pc.printf("Recebeu o comando ATE0.\n"); |
brunofgc | 0:1c0a769988ee | 1011 | } |
brunofgc | 0:1c0a769988ee | 1012 | |
brunofgc | 0:1c0a769988ee | 1013 | modemCom::leMAC(); |
brunofgc | 0:1c0a769988ee | 1014 | |
brunofgc | 0:1c0a769988ee | 1015 | |
brunofgc | 0:1c0a769988ee | 1016 | |
brunofgc | 0:1c0a769988ee | 1017 | /* |
brunofgc | 0:1c0a769988ee | 1018 | if(modemCom::sendToModem("\r",1,&modemCom::status.OK,250,4)) { |
brunofgc | 0:1c0a769988ee | 1019 | pc.printf("Recebeu o comando .\n"); |
brunofgc | 0:1c0a769988ee | 1020 | } |
brunofgc | 0:1c0a769988ee | 1021 | */ |
brunofgc | 0:1c0a769988ee | 1022 | |
brunofgc | 0:1c0a769988ee | 1023 | modemCom::status.emComunicacao = false; |
brunofgc | 0:1c0a769988ee | 1024 | return 1; |
brunofgc | 0:1c0a769988ee | 1025 | } |
brunofgc | 0:1c0a769988ee | 1026 | |
brunofgc | 0:1c0a769988ee | 1027 |