teste de publish

Dependencies:   DS1820 HighSpeedAnalogIn devices mbed

Committer:
brunofgc
Date:
Wed Aug 30 18:42:18 2017 +0000
Revision:
11:631bea162800
Parent:
10:263c093f8977
Child:
12:95eb36d5e82d
Ultima vers?o sem kick em watch dog timer dentro do processo de envio de dados.

Who changed what in which revision?

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