teste de publish

Dependencies:   DS1820 HighSpeedAnalogIn devices mbed

Committer:
brunofgc
Date:
Wed Aug 30 19:13:19 2017 +0000
Revision:
12:95eb36d5e82d
Parent:
11:631bea162800
Child:
13:b9183b4bc049
Vers?o Instalada UCSal 30/08/2017 Com KICK de watchdog em postFileCommandMode

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