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