teste de publish

Dependencies:   DS1820 HighSpeedAnalogIn devices mbed

Committer:
brunofgc
Date:
Wed Aug 09 20:38:10 2017 +0000
Revision:
9:cf406384efd9
Parent:
8:9f8ad3dbeb87
Child:
10:263c093f8977
Ultima versao com enviaDados na thread comunicacoes;

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