teste de publish

Dependencies:   DS1820 HighSpeedAnalogIn devices mbed

Committer:
brunofgc
Date:
Mon Apr 16 12:30:29 2018 +0000
Revision:
25:a6da63ed025b
Parent:
23:f3c87afb6548
Child:
29:823a9da3696b
Vers?o 16/04/2018 com modbus otimizado para 100 leituras

Who changed what in which revision?

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