teste de publish

Dependencies:   DS1820 HighSpeedAnalogIn devices mbed

Revision:
21:b9315cdd9275
Parent:
20:da1b8d80ba00
Child:
22:cb832a9bc704
diff -r da1b8d80ba00 -r b9315cdd9275 modem.cpp
--- a/modem.cpp	Mon Jan 29 14:22:38 2018 +0000
+++ b/modem.cpp	Tue Feb 06 16:33:51 2018 +0000
@@ -402,7 +402,7 @@
 uint8_t modemCom::sendToModem(char *string,uint8_t confirmado,char *codigoConfirmacao,char *stringConfirmacao,uint16_t timeOut,uint8_t maxTentativas,uint16_t delayEntreTentativas)
 {
     uint16_t timeOutProcesso;
-    uint16_t indexString,strLen; //Para exibir retentativas!
+    uint16_t indexString,strLen; //Para exibir retentativas!    
     
     while(maxTentativas){
         timeOutProcesso=timeOut;
@@ -441,7 +441,12 @@
                return 0;
             }    
             
-            if(timeOutProcesso) {                
+            if(timeOutProcesso) {
+                if(confirmado==1){
+                    if(!(*codigoConfirmacao)){return 0;}
+                }else{
+                    if(!strstr(bufModem.getRowBuffer(),stringConfirmacao)){return 0;}
+                 }
                 return maxTentativas;                
             }                                    
             
@@ -521,9 +526,8 @@
         
     if(strstr(webServerBuff,"configRede.htm")&&(!encontrado)){        
         //Pegando o IP
-        modemCom::leIP();
-        //sprintf(buf,"<!DOCTYPE html><form action='cwjap.htm' method='GET'><fieldset><legend>Parametros de Configuracao da rede:</legend>SSID WiFi:<br><input type='text' name='ssid' value=''><br>Senha WiFi:<br><input type='text' name='password' value=''><br>IP:(0 para DHCP)<br><input type='text' name='ip' value='0'><br>Porta:<br><input type='text' name='port' value=''><br><input type='submit' value='Configurar'></fieldset></form>");
-        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>IP Atual = %s.<br><form action='cwjap.htm' method='GET'><fieldset><legend>Parametros de Configuracao da rede:</legend>SSID WiFi:<br><input type='text' name='ssid' value='%s'><br>Senha WiFi:<br><input type='text' name='password' value='%s'><br>PORTA:<br><input type='text' name='porta' value='%s'><br>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><input type='submit' value='Configurar'></fieldset></form></body>",modemCom::status.STAIP,modemCom::status.ssid,modemCom::status.password,modemCom::status.port,modemCom::status.serverIP,modemCom::status.host,modemCom::status.periodo);        
+        modemCom::leIP();        
+        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><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);
         cipSend(id,buf,0);
         encontrado = 1;
     }
@@ -535,7 +539,7 @@
         IrDetect.start();
 
         diversos::wdt.kick(360.0);        
-
+        ledUsoGeral = true;
         while((IrDetect.read_ms()<timeout) && (!detectaIRIn()));
         if(IrDetect.read_ms()<timeout){
             serializaPacoteIR(modemCom::status.connIDWebServer);            
@@ -545,13 +549,12 @@
                 printf("Requisicao enviada!\n");    
             }
          }
-        IrDetect.stop();        
+        IrDetect.stop();   
+        ledUsoGeral = false;
         encontrado = 1;        
     }
     
     if(strstr(webServerBuff,"cwjap.htm")&&(!encontrado)){
-        //cwjap.htm?ssid=VSE&password=vitaeBBYP&porta=4010&serverIP=191.252.142.51&host=www.vitaesolucoes.com.br&periodo=900
-        
         pc.printf("Lido de cwjap <%s>.\r\n\r\n",webServerBuff);
         
         strtok(webServerBuff,"=");
@@ -570,15 +573,40 @@
         strcpy(modemCom::status.host,strtok(NULL,"&")); //pegando host
         
         strtok(NULL,"=");
-        strcpy(buf,strtok(NULL," ")); //pegando periodo
+        strcpy(buf,strtok(NULL,"&")); //pegando periodo
         modemCom::status.periodo = atoi(buf);
         modemCom::status.periodoConfiguracao = modemCom::status.periodo;
         
+        //*************************IP ou DHCP****************************
+        
+        strtok(NULL,"=");
+        strcpy(modemCom::status.STAIP,strtok(NULL,"&")); //pegando IP/DHCP
+        
+        strtok(NULL,"=");
+        strcpy(modemCom::status.subnetmask,strtok(NULL,"&")); //pegando subnetmask
+        
+        strtok(NULL,"=");
+        strcpy(modemCom::status.gateway,strtok(NULL," ")); //pegando gateway
+        
+        
+        
         pc.printf("Antes <%s>.\r\n",modemCom::status.ssid);
-        diversos::strReplace(modemCom::status.ssid,"%20"," ");
+        diversos::strReplace(modemCom::status.ssid,"%%20"," ");
         pc.printf("Tirado %%20 <%s>.\r\n",modemCom::status.ssid);
+        pc.printf("Antes <%s>.\r\n",modemCom::status.ssid);
+        diversos::strReplace(modemCom::status.ssid,"%%40","@");
+        pc.printf("Tirado %%40 <%s>.\r\n",modemCom::status.ssid);
         diversos::strReplace(modemCom::status.ssid,"+"," ");
-        pc.printf("Tirado + <%s>.\r\n",modemCom::status.ssid);        
+        pc.printf("Tirado + <%s>.\r\n",modemCom::status.ssid);
+        
+        pc.printf("Antes <%s>.\r\n",modemCom::status.password);
+        diversos::strReplace(modemCom::status.password,"%%20"," ");
+        pc.printf("Tirado %%20 <%s>.\r\n",modemCom::status.password);
+        pc.printf("Antes <%s>.\r\n",modemCom::status.password);
+        diversos::strReplace(modemCom::status.password,"%%40","@");
+        pc.printf("Tirado %%40 <%s>.\r\n",modemCom::status.password);
+        diversos::strReplace(modemCom::status.password,"+"," ");
+        pc.printf("Tirado + <%s>.\r\n",modemCom::status.password);        
         
         if((modemCom::status.ssid[0]!=0) && (modemCom::status.password[0]!=0)){
             sprintf(buf,"HTTP/1.1 200 OK\n\nACK");                      
@@ -603,10 +631,16 @@
     if(mudaRede){            
         mudaRede = false;
         modemCom::sendToModem("AT+CIPSERVER=0\r\n",1,&modemCom::status.OK,NULL,2000,1,1);        
-        pc.printf("Mudando para rede:\r\nSSID:%s\r\nPASSWORD:%S\r\nPORTA:%s\r\nserverIP:%s\r\nHOST:%s\r\nPERIODO:%lu\r\n\r\n",modemCom::status.ssid,modemCom::status.password,modemCom::status.port,modemCom::status.serverIP,modemCom::status.host,modemCom::status.periodo);
+        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\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);
+        
+        if(strstr(modemCom::status.gateway,"dhcp")){
+            strcpy(modemCom::status.STAIP,"dhcp");
+            strcpy(modemCom::status.subnetmask,"dhcp");
+        }
+        
         if(sdCard::abreArquivo(&sdCard::config,"w")){
             pc.printf("Guardando config.\r\n");
-            fprintf(sdCard::config.fp,"SSID:%s\r\nPASSWORD:%s\r\nPORT:%s\r\nserverIP:%s\r\nHOST:%s\r\nPERIODO:%lu",modemCom::status.ssid,modemCom::status.password,modemCom::status.port,modemCom::status.serverIP,modemCom::status.host,modemCom::status.periodo);
+            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\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);
             sdCard::fechaArquivo(&sdCard::config);
             modemCom::inicializaModem();
             modemCom::conectaWiFi();
@@ -665,7 +699,7 @@
     //Função para mudança de rede
     
     char aux[128];    
-    modemCom::leParametrosConexaoSDCard();    
+    //modemCom::leParametrosConexaoSDCard();    
     //AT+CWJAP=\"\",\"\"\r\n
     sprintf(aux,"AT+CWJAP=\"%s\",\"%s\"\r\n",modemCom::status.ssid,modemCom::status.password);
     if(modemCom::sendToModem(aux,1,&modemCom::status.OK,NULL,10000,1,1)) {
@@ -868,14 +902,14 @@
             strcpy(modemCom::status.port,ptr);            
             
             //Pegando serverIP
-            fgets(modemCom::status.host,50,sdCard::config.fp);  //Usando host como buffer
-            ptr = strtok(modemCom::status.host,":");            //Usando host como buffer
+            fgets(aux,50,sdCard::config.fp);
+            ptr = strtok(aux,":");
             ptr = strtok(NULL,"\r\n");
             strcpy(modemCom::status.serverIP,ptr);
                         
             //Pegando host
-            fgets(modemCom::status.host,50,sdCard::config.fp);
-            ptr = strtok(modemCom::status.host,":");
+            fgets(aux,50,sdCard::config.fp);
+            ptr = strtok(aux,":");
             ptr = strtok(NULL,"\r\n");
             strcpy(modemCom::status.host,ptr);
             
@@ -885,16 +919,40 @@
             ptr = strtok(NULL,"\r\n");        
             modemCom::status.periodo = atoi(ptr);
             modemCom::status.periodoConfiguracao = modemCom::status.periodo;
+            
+            //*************IP ou DHCP*****************
+            
+            //Pegando STAIP
+            fgets(aux,50,sdCard::config.fp);
+            ptr = strtok(aux,":");
+            ptr = strtok(NULL,"\r\n");
+            strcpy(modemCom::status.STAIP,ptr);
+            
+            //Pegando subnetmask
+            fgets(aux,50,sdCard::config.fp);
+            ptr = strtok(aux,":");
+            ptr = strtok(NULL,"\r\n");
+            strcpy(modemCom::status.subnetmask,ptr);
+            
+            //Pegando gateway
+            fgets(aux,50,sdCard::config.fp);
+            ptr = strtok(aux,":");
+            ptr = strtok(NULL,"\r\n");
+            strcpy(modemCom::status.gateway,ptr);
+            
+            
         }else{
             strcpy(modemCom::status.ssid,"VSE");
             strcpy(modemCom::status.password,"vitaeBBYP");
             strcpy(modemCom::status.port,"80");
             strcpy(modemCom::status.serverIP,"191.252.142.51");
             strcpy(modemCom::status.host,"www.vitaesolucoes.com.br");
+            strcpy(modemCom::status.gateway,"dhcp");
             modemCom::status.periodo = 900;
+            modemCom::status.periodoConfiguracao = modemCom::status.periodo;
          }
         sdCard::fechaArquivo(&sdCard::config);
-        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\n",modemCom::status.ssid,modemCom::status.password,modemCom::status.port,modemCom::status.serverIP,modemCom::status.host,modemCom::status.periodo);
+        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\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);
         pc.printf("Iniciando conexao wifi.\r\n");
         return true;        
     }else{
@@ -903,6 +961,7 @@
             strcpy(modemCom::status.port,"80");
             strcpy(modemCom::status.serverIP,"191.252.142.51");
             strcpy(modemCom::status.host,"www.vitaesolucoes.com.br");
+            strcpy(modemCom::status.gateway,"dhcp");
             modemCom::status.periodo = 900;
             modemCom::status.periodoConfiguracao = modemCom::status.periodo;
             return false;
@@ -944,7 +1003,7 @@
         pc.printf("Recebeu o comando AT+CIPSTO=60.\n");
     }        
     
-    sprintf(aux,"AT+CWSAP_CUR=\"DROME_WIFI_%s\",\"VsaDROME\",5,3\r\n",modemCom::status.MAC);
+    sprintf(aux,"AT+CWSAP_CUR=\"DROME_WIFI_%s\",\"VseDROME\",5,3\r\n",modemCom::status.MAC);
     if(modemCom::sendToModem(aux,1,&modemCom::status.OK,NULL,250,4,1)) {
         pc.printf("Recebeu o comando %s.\n",aux);
     }
@@ -954,10 +1013,23 @@
         pc.printf("Recebeu o comando AT+CIPAP_CUR=\"192.168.4.1\",\"192.168.4.1\",\"255.255.255.0\".\n");
     }    
     
-    if(modemCom::sendToModem("AT+CWDHCP_CUR=2,1\r\n",1,&modemCom::status.OK,NULL,250,4,1)) {
-        pc.printf("Recebeu o comando AT+CWDHCP_CUR=2,1.\n");
-    }
+    //Aqui decido se uso DHCP ou não.
+    if(strstr(modemCom::status.gateway,"dhcp")){
+        //usando dhcp
+        if(modemCom::sendToModem("AT+CWDHCP_CUR=2,1\r\n",1,&modemCom::status.OK,NULL,250,4,1)) {
+            pc.printf("Recebeu o comando AT+CWDHCP_CUR=2,1.\n");
+        }
+    }else{
+        if(modemCom::sendToModem("AT+CWDHCP_CUR=2,0\r\n",1,&modemCom::status.OK,NULL,250,4,1)) {
+            pc.printf("Recebeu o comando AT+CWDHCP_CUR=2,0.\n");
+        }
+        //AT+CIPSTA_DEF="192.168.6.100","192.168.6.1","255.255.255.0"
+        sprintf(aux,"AT+CIPSTA_CUR=\"%s\",\"%s\",\"%s\"\r\n",modemCom::status.STAIP,modemCom::status.gateway,modemCom::status.subnetmask);
+        if(modemCom::sendToModem(aux,1,&modemCom::status.OK,NULL,250,4,1)) {
+            pc.printf("Recebeu o comando <%s>.\n",aux);
+        }
         
+     }
     modemCom::status.emComunicacao = false;
     return 1;
 }