teste de publish

Dependencies:   DS1820 HighSpeedAnalogIn devices mbed

Revision:
33:a60abda630f7
Parent:
31:a1ba855c6e8a
Child:
34:348991c54ff3
--- a/modem.cpp	Sat May 26 14:17:55 2018 +0000
+++ b/modem.cpp	Wed May 30 04:24:55 2018 +0000
@@ -6,6 +6,7 @@
 bool modemCom::atendendoWebServer;
 bool modemCom::exibeEntradaPacote;
 int16_t numeroDeBytesPorEnvio;
+uint16_t modemCom::timeOutEnvioDados;
 uint8_t connID;
 uint16_t IPDNumBytes;
 bool enviaSendData;
@@ -19,7 +20,9 @@
             if(modemCom::status.emComunicacao){
                 modemCom::cipSend(conId,"ocupado",7);
                 pc.printf("Fechando socket chave 1.\r\n");
-                modemCom::closeConnection(conId);
+                modemCom::closeConnection(&modemCom::status.connIDServerCommand);
+                modemCom::closeConnection(&modemCom::status.connIDWebServer);
+                modemCom::closeConnection(&modemCom::status.connIDSendData);
             }
             
             modemCom::status.socketEntranteAtivo=true;            
@@ -28,7 +31,7 @@
             
             //pc.printf("Entrei em modemCom::atendeSRING.\r\n");
             
-            while(modemCom::status.timeOut||modemCom::status.NO_CARRIER){
+            while(modemCom::status.timeOut){
              if(modemCom::status.timeOut){modemCom::status.timeOut--;}
              /*//Aqui devo perguntar e pedir os dados em buffer
              */             
@@ -43,7 +46,9 @@
                         
             //Fechando o socket         
             pc.printf("Fechando socket chave 2.\r\n");               
-            modemCom::closeConnection(conId);
+            modemCom::closeConnection(&modemCom::status.connIDServerCommand);
+            modemCom::closeConnection(&modemCom::status.connIDWebServer);
+            modemCom::closeConnection(&modemCom::status.connIDSendData);
             modemCom::status.socketEntranteAtivo=false;
             modemCom::status.emComunicacao = false;
             modemCom::timeOutModem = 10;
@@ -95,8 +100,10 @@
     sprintf(aux,"%d",RSSI);
     sdCardBuf.fill(aux,strlen(aux));
     
-    /*sprintf(aux,";firmVer=%s;rstCause=%s",firmVersion,resetCause);    
-    sdCardBuf.fill(aux,strlen(aux));*/
+    
+    
+    sprintf(aux,";fv=%s;rc=%s",firmVersion,resetCauses);    
+    sdCardBuf.fill(aux,strlen(aux));
     
     sprintf(aux,";imsi=%s}log",modemCom::status.MAC);    
     sdCardBuf.fill(aux,strlen(aux));
@@ -209,7 +216,7 @@
 
     //Para garantir que não seja duvidoso proveniente de outra conexão.
     modemCom::status.ServerAck=0;
-    modemCom::status.NO_CARRIER=0;
+
     modemCom::status.dnsError=0;
 
     pc.printf("Iniciando envio de arquivo.\r\n");
@@ -226,9 +233,10 @@
     modemCom::status.ALREADY_CONNECTED=0;
     sprintf(header,"AT+CIPSTART=1,\"TCP\",\"%s\",80\r\n",serverIP);
     if(modemCom::sendToModem(header,1,&modemCom::status.OK,NULL,20000,3,1000)){
-        modemCom::status.NO_CARRIER=0; //Socket Aberto!
+
         pc.printf("Aberto o socket em command mode.\n");
         pc.printf("Enviando arquivo com nome %s.\n",nomeArquivo);
+        modemCom::status.connIDSendData=1;
         modemCom::status.serverConnected=1;
         modemCom::timeOutModem = 50;
         modemCom::status.emComunicacao = true;
@@ -236,7 +244,7 @@
         pc.printf("Nao foi possivel Abrir o socket. Abortando tentativa.\n");
         if(modemCom::status.ALREADY_CONNECTED){
             //Socket aberto, fecho-o antes de partir.
-            modemCom::closeConnection(1);    
+            modemCom::closeConnection(&modemCom::status.connIDSendData);    
         }
         modemCom::timeOutModem = 20;
         modemCom::status.emComunicacao = false;
@@ -402,15 +410,15 @@
     sdCard::fechaArquivo(arquivo);
 
     //Logica de timeout esperando confirmação modemCom::status.ServerAck 20 segundos
-    parteSendoEnviada = 300;    
-    while((parteSendoEnviada)&&(!modemCom::status.ServerAck)&&(!modemCom::status.NO_CARRIER)) {
+    parteSendoEnviada = 300; //Reaproveitando variável
+    while((parteSendoEnviada)&&(!modemCom::status.ServerAck)) {
         osDelay(100);
         parteSendoEnviada--;
     }
 
     modemCom::status.serverConnected=0;    
     //modemCom::sendToModem("AT+CIPCLOSE=1\r\n",1,&modemCom::status.OK,500,3);    
-    modemCom::closeConnection(1);
+    modemCom::closeConnection(&modemCom::status.connIDSendData);
     modemCom::status.emComunicacao = false;    
     
     return modemCom::status.ServerAck;
@@ -483,12 +491,18 @@
     return 0;
 }
 
-bool modemCom::closeConnection(uint8_t id){
+char modemCom::closeConnection(uint8_t *id){
     char command[20];
-    if(debug){pc.printf("Lido %u como sendo o id em closeConnection.\r\n",id);}
-    
-    sprintf(command,"AT+CIPCLOSE=%u\r\n",id);
-    return modemCom::sendToModem(command,1,NULL,",CLOSED",1000,3,300);
+    if(debug){pc.printf("Lido %u como sendo o id em closeConnection.\r\n",*id);}
+    if((*id)==255){
+        return 2;
+    }
+    sprintf(command,"AT+CIPCLOSE=%u\r\n",*id);
+    if(modemCom::sendToModem(command,1,NULL,",CLOSED",1000,3,300)){
+        *id = 255;
+        return 1;
+    }
+    return 0;
 }
 
 char modemCom::cipSend(uint8_t idConnection,char *buffer,uint16_t len){
@@ -515,7 +529,7 @@
 }
 
 void modemCom::webServer(uint8_t id){    
-    char aux[50];
+    //char aux[50];
     bool encontrado=0;
     char buf[1024];
     char *strOutput;
@@ -651,7 +665,7 @@
         }         
     }    
     if(debug){pc.printf("Fechando socket chave 3.\r\n");}
-    modemCom::closeConnection(id);    
+    modemCom::closeConnection(&modemCom::status.connIDWebServer);
     if(mudaRede){            
         mudaRede = false;
         modemCom::sendToModem("AT+CIPSERVER=0\r\n",1,&modemCom::status.OK,NULL,2000,1,1);        
@@ -726,7 +740,7 @@
     //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,5000,2,5000)) {
+    if(modemCom::sendToModem(aux,1,&modemCom::status.wifi_connected,NULL,5000,2,5000)) {
         if(debug){pc.printf("WiFi Conectado.\n");}
         return true;
     }else{
@@ -752,7 +766,7 @@
         
         modemCom::status.serverConnected=1;
         modemCom::status.commandMode=1;
-        modemCom::status.NO_CARRIER=0;
+        
         modemCom::status.PROMPT_ENVIO_COMMAND_MODE=1;
     }
 
@@ -802,7 +816,13 @@
         modemCom::status.ERROR=1;
         modemCom::status.OK=0;
     }    
-   
+    
+    ptr=strstr(modemCom::bufIn,"WIFI CONNECTED");
+    if(ptr) {
+        modemCom::status.wifi_connected=1;
+        
+    }        
+    
     ptr=strstr(modemCom::bufIn,"ALREADY CONNECTED");
     if(ptr) {
         modemCom::status.ALREADY_CONNECTED=1;
@@ -885,7 +905,7 @@
         
         modem.puts("sendData\r\n");        
         diversos::wdt.kick();
-        modemCom::status.recebendoArquivoDoServer = 600;
+        modemCom::status.recebendoArquivoDoServer = 250;
         return;
     }
         
@@ -1034,9 +1054,8 @@
     char aux[255];    
     modemCom::timeOutModem = 10;
     modemCom::status.emComunicacao = true;    
-    if(modemCom::sendToModem("ATE0\r\n",1,&modemCom::status.OK,NULL,1000,10,100)) {
-        pc.printf("Recebeu o comando ATE0.\n");
-    }
+    
+    serialModem::configBaud();
 
     modemCom::leMAC();