Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By Fernando Gauenzi
#30730 Hello! I have a problem in this code to see if I can help .

I 'm sending from the ESP two pages . I GET hope the first browser and then hope the second GET to send the second page included in the first .

The String data page is stored in the Micro SD connected to the Arduino .

The problem I am having is that I will load the second page that does not reach the second GET, or rather ... maybe when I'm doing another process, the GET is over.

Or someone can help me find the problem ?


Code: Select all#include <SPI.h>
#include <SdFat.h>
#include <Wire.h>
#include <String.h>

////////////////////////////      defines         //////////////////////////////////////////////////////////////

 
#define DEBUG true
const int ledPin = 13;
const bool printReply = false;
const char line[] = "-----\n\r";
int loopCount=0;
int longitud;
char Client_ID;
char html[50];
char command[20];
char reply[500]; // you wouldn't normally do this
char ipAddress [20];
char name[30];
int lenHtml = 0;
char temp[5];
int t,j;
SdFat SD;
File dataFile;
#define SD_CS_PIN 7
#define PAG_INDEX "index.html"
#define PAG_DATOS "datos.html"
#define PAG_FAVICON "favicon.ico"

////////////////////////////      pages         //////////////////////////////////////////////////////////////


int CODE_M1=10;
int CODE_M2=15;

////////////////////////////         setup             //////////////////////////////////////////////////////////////

void setup()
{
  Serial.begin(115200);
  Serial3.begin(115200); // your esp's baud rate might be different

  SD_setup();

  pinMode(ledPin, OUTPUT);
  Serial.println("INICIANDO....");
  sendData("AT+RST\r\n",3000,DEBUG); // reset module
  sendData1("AT+CWMODE=2\r\n",1000,DEBUG); // configure as access point
  sendData1("AT+CIFSR\r\n",1000,DEBUG); // get ip address
  sendData1("AT+CIPMUX=1\r\n",1000,DEBUG); // configure for multiple connections
  sendData1("AT+CIPSERVER=1,80\r\n",1000,DEBUG); // turn on server on port 80
  sendData("AT+CWSAP=\"ESP-T-Motion\",\"hola\",1,0\r\n",2000,DEBUG); // reset module
 
  digitalWrite(ledPin, HIGH);   // set the LED on
}

////////////////////////////         loop             ///////////////////////////////////////////////////////////



void loop()
{boolean salida = true;

  transparente();
 
      if(Serial3.available()) // check if the ESO8266 is sending data
      {
       // char c = Serial3.read();
       //    if(c == 'c')
      if(Serial3.find("+IPD,"))
      {
        if(Serial3.available()) // check if the ESO8266 is sending data
          Client_ID = Serial3.read();
        Serial.print("Llego algo cliente: ");
        Serial.println(Client_ID);

          while( Serial3.available() == 0);
        Serial.print("+IPD,");
         

        while( salida )
        {
          while( Serial3.available() == 0);
         
          switch (char c = Serial3.read())
            {
              case 'G':
                        Serial.println("Trasmite");
                        elijopagina();
                        salida = false;
                        break;
             
              default:
                        Serial.print(c);
            }
        }
      Serial.println("listo de nuevo y escucho");
      // transparente();

      }
      }
 
      digitalWrite(ledPin, LOW);   // set the LED
      delay (100);
      digitalWrite(ledPin, HIGH);   // set the LED
      delay (100);
 
}


////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
void getReply1(int wait)
{
    int tempPos = 0;
    long int time = millis();
     while( (time + wait) > millis())
    {
        while(Serial3.available())
        {
            char c = Serial3.read();
            Serial.print(c);
            if (tempPos < 500) { reply[tempPos] = c; tempPos++;   }
        }
        reply[tempPos] = 0;
    }
 
    if (printReply) { Serial.println( reply );  Serial.println(line);     }
    else            {Serial.println("_______________________________________________________");}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////

void getReply(int wait)
{
  while(Serial3.find("OK") != true) {}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////

void enviapagina( String CODE[], int  CODE_M)
{
         
              // check to see if we have a name - loSerial3 for name=
              bool haveName = false;
              int nameStartPos = 0;
             
 
              digitalWrite(ledPin, LOW);   // set the LED on
              // start sending the HTML
              boolean algo=true;
              for(int i=0;i<CODE_M;i++)
              {
                // delay(200);
                Serial3.print("AT+CIPSEND=");
                Serial3.print(Client_ID);
                Serial3.print(",");
                longitud=(CODE[i].length())+2;
                Serial3.print(longitud);
                Serial3.print("\r\n");
               
                while(Serial3.find("OK") != true)
                      {
                        while(Serial3.find("bus") != true)
                           {
                            Serial.println("busy again!");
                            i--;
                            break;
                           }
                        break;
                      }
               
                Serial3.print(CODE[i]);         
                Serial3.print("\r\n");           
               
                Serial.print("Enviado cliente");
                Serial.print(Client_ID);
                Serial.println(longitud);
                Serial.println(CODE[i]);         

                while(Serial3.find("OK") != true)
                      {
                        while(Serial3.find("bus") != true)
                           {
                            Serial.println("busy again!");
                            i--;
                            break;
                           }
                        break;
                      }
                     
                Serial.println("-OK-");
              }
             
              delay(200);
              digitalWrite(ledPin, HIGH);   // set the LED on
             
 
          }


// ////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
String sendData(String command, const int timeout, boolean debug)
{
    String response = "";
   
    Serial3.print(command); // send the read character to the Serial3
   
    long int time = millis();
   
    while( (time+timeout) > millis())
    {
      while(Serial3.available())
      {
       
        // The esp has data so display its output to the serial window
        char c = Serial3.read(); // read the next character.
        response+=c;
       
      } 
    }
   
    if(debug)
    {
      Serial.print(response);
    }
   
    return response;
}
// ////////////////////////////////////////////////////////////////////////////////////////////////////////////

String sendData1(String command, const int timeout, boolean debug)
{
    String response = "";
   
    Serial3.print(command); // send the read character to the Serial3
   
    long int time = millis();
   
    while( (time+timeout) > millis())
    {
      while(Serial3.available())
      {
       
        // The esp has data so display its output to the serial window
        char c = Serial3.read(); // read the next character.
        response+=c;
        if(c == 'K')
        {Serial.println("<OK>") ; if(debug){Serial.print(response);} return response;}
      } 
    }
   
    if(debug)
    {
      Serial.print(response);
    }
   
    return response;
}

// ////////////////////////////////////////////////////////////////////////////////////////////////////

void elijopagina( void)
{char * acumulado="";
String cadena = "";
    while(Serial3.available())
    {
      char c = Serial3.read();
      cadena += c;
      Serial.print(c);
      if(cadena.lastIndexOf(" / ") >= 0)
      {
        cadena=""; 
        Serial.println("-Espero Blank-");
        esperoblank();
        Serial.println("-Envio index.html-");
        envio_SD(PAG_INDEX);
        return;
      }
      if(cadena.lastIndexOf("datos") >= 0)
      {
        cadena=""; 
        Serial.println("-Espero Blank-");
        esperoblank();
        Serial.println("-Envio datos.html-");
        envio_SD(PAG_DATOS);
        return;
      }
      if(cadena.lastIndexOf("favicon") >= 0)
      {
        cadena=""; 
        Serial.println("-Espero Blank-");
        esperoblank();
        Serial.println("-Envio favicon.ico-");
        envio_SD(PAG_FAVICON);
        return;
      }


      if(c == '\n' )
      cadena=""; 
    }
}

void esperoblank(void)
{String cadena = "";
  while(Serial3.available())
    {
      char c = Serial3.read();
      cadena += c;
      Serial.print(c);
      if(cadena.lastIndexOf("r/n/r/n") >= 0)
      {
        return;
      }
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
void SD_setup(void)
{
      // initialize SD card
    Serial.println("Initializing SD card...");
    if (!SD.begin(SD_CS_PIN,SPI_HALF_SPEED)) {Serial.println("ERROR - SD card initialization failed!"); return;}
       
    Serial.println("SUCCESS - SD card initialized.");
    // check for index.htm file
    if (!SD.exists(PAG_INDEX)) {Serial.println("ERROR - Can't find index.htm file!");return;}  // can't find index file
    Serial.println("SUCCESS - Found index.htm file.");

    return;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////

void envio_SD( char * archivo )
{

              // check to see if we have a name - loSerial3 for name=
              bool haveName = false;
              int nameStartPos = 0;
              digitalWrite(ledPin, LOW);   // set the LED on
              // start sending the HTML
              boolean algo=true;
             
               // re-open the file for reading:
              if (!SD.exists(archivo)) {Serial.println("ERROR - Can't find index.htm file!");return;}  // can't find index file
                  Serial.println("SUCCESS - Found x.htm file.");
               dataFile = SD.open(archivo);
               int Filesize = dataFile.size();
               if (dataFile)
                 {
                   String dataString = "";
                   Serial.print("-leyendo archivo de la SD-");
                   Serial.println(archivo);
                   while (dataFile.available())
                   {
                       char c = dataFile.read();
                       dataString += c;
                       //Serial.println(dataString);
                       Filesize--;
                     
                       if ( j==60 || Filesize == 0)
                      {
                         j=0;
                         delay(200);
                        Serial3.print("AT+CIPSEND=");
                        Serial3.print(Client_ID);
                        Serial3.print(",");
                        longitud=(dataString.length())+2;
                        Serial3.print(longitud);
                        Serial3.print("\r\n");
                        while(Serial3.find("OK") != true) Serial.print(Serial3.read());
                           
                         Serial.print("AT+CIPSEND= ");  // para ver en consola;
                         Serial.print(Client_ID);
                         Serial.print(", ");
                         Serial.println(longitud);
                         

                         Serial3.print(dataString);
                         Serial3.print("\r\n");   

                         while(Serial3.find("OK") != true) Serial.print(Serial3.read());;
                           
                         Serial.println(dataString);


                         Serial.println("<OK>");
                         dataString = "";
                         }
                        j++;
                 
                    }

              Serial.print( "AT+CIPCLOSE=" );
         
              Serial3.print( "AT+CIPCLOSE=0\r\n" );
             
              while(Serial3.find("OK") != true) ;
                      {
                        char c = Serial3.read();
                        Serial.print(c);
                        while(Serial3.find("bus") == true)
                           {
                            Serial.println("-busy again!-");
                            break;
                           }
                      }
              Serial.println("<termino de enviar de la sd>");

              digitalWrite(ledPin, HIGH);   // set the LED on
              }
              else {
                  // if the file didn't open, print an error:
                  Serial.println("error opening test.txt");
                    }
              dataFile.close();
              Serial.println("<Cerro el archivo>");

    return;
}           
               
 
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void transparente(void)
{
  while(1)
      {
        if(Serial3.available())
          {char c=Serial3.read();
            Serial.print(c);
          }
        if(Serial.available())
          {char c=Serial.read();
            Serial3.print(c);
          }
      }
}