teste de publish

Dependencies:   DS1820 HighSpeedAnalogIn devices mbed

Committer:
brunofgc
Date:
Tue Jun 05 17:39:37 2018 +0000
Revision:
36:fd239680e427
Parent:
34:348991c54ff3
Child:
37:0e95c85f0160
Ultima vers?o com WDT reset cause informando quando houve (timestamp) wdt

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