teste de publish

Dependencies:   DS1820 HighSpeedAnalogIn devices mbed

Committer:
brunofgc
Date:
Wed Dec 27 13:10:09 2017 +0000
Revision:
15:0f78bf9c13ec
Parent:
14:c0162ab2a951
Child:
16:9a19ac33957e
Vers?o otimizada quanto ? resposta do webbrowser e demais funcionalidades executadas via "*ServerCommand*/". Bootloader com problema. Vers?o instalada no HSR.

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