teste de publish

Dependencies:   DS1820 HighSpeedAnalogIn devices mbed

Committer:
brunofgc
Date:
Thu Nov 09 12:53:41 2017 +0000
Revision:
14:c0162ab2a951
Parent:
13:b9183b4bc049
Child:
15:0f78bf9c13ec
vers?o com bootloader

Who changed what in which revision?

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