teste de publish

Dependencies:   DS1820 HighSpeedAnalogIn devices mbed

Committer:
brunofgc
Date:
Thu Mar 30 19:54:09 2017 +0000
Revision:
1:0e0967c88590
Parent:
0:1c0a769988ee
Child:
2:55b7b466e742
Antes de corrigir Aloca??o Din?mica Mal feita

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