teste de publish

Dependencies:   DS1820 HighSpeedAnalogIn devices mbed

Committer:
brunofgc
Date:
Fri May 25 23:46:10 2018 +0000
Revision:
30:8a06a85d8807
Parent:
29:823a9da3696b
Child:
31:a1ba855c6e8a
Concertado ROM e RAM para n?o resetar.

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