Chat freely about anything...

User avatar
By Fernando Gauenzi
#33815 Hello everyone !
I commented that I'm sending a HTTP page from the ESP into the browser .
In the Http code I generated an automatic refresh .
The problem I am having is that randomly , when I send the CIPSEND command , he responds with Busy inet .... and it stops automatically refresh due to error.

I attached the code to see if I can help detect what is wrong .Also I attached a picture with the fault.

They can help me?

esspwifi.jpg
ESPWIFI


Code: Select all

/*
Start up a very mini web server
 */

#define TIMEOUT      5000 // mS
#define ESP_RESET    22
#define BAUDRATE     115200

//*** Literals and vars used in the message queue ***
#define QUE_SIZE     8
#define HTML_VISOR   1     // En vez de ver Case 1: de esta manera veo Case HTML_VISOR
#define FAVICON_REQUEST  3

#define QUE_INTERVAL    500
int QueIn=0;
int QueOut=0;
int CommandQue[QUE_SIZE];
int CommandQueIPD_CH[QUE_SIZE];
float LastCommandSent=0;
float LastQueEntered=0;
String CIPSendString="";


void setup() 
{
 
  pinMode(ESP_RESET,OUTPUT);
  Serial.println("loading..");
  digitalWrite(ESP_RESET,HIGH);
  Serial.begin(115200);         // Manual interface port
  Serial3.begin(BAUDRATE);        // Port to ESP8266 Wifi Module

  InitWifiModule();
}

void loop(){
 
   
  String IncomingString="";
  char SingleChar;
  boolean StringReady = false;


  //*** Handle each character that is coming in from the ESP8266 ****
  while(Serial3.available())
  {
    IncomingChar (Serial3.read ());
  } 
 
  //*** Allow manual commands to be given during run time. ***
  while(Serial.available())
  {
    Serial3.write(Serial.read());
  }
 
  if (QueIn != QueOut)
  {
    if ((millis()-LastCommandSent > QUE_INTERVAL) && (millis()-LastQueEntered > QUE_INTERVAL))
      {
          ProcessQueCommand(QueOut);
                if(QueOut!=QueIn){QueOut++;}
          LastCommandSent = millis();
                if (QueOut==QUE_SIZE)
                  {QueOut=0;
                   Serial.println("Resetting QueOut");}
        }
       
   }


}

//***** Formulate command string and send *****
void ProcessQueCommand(int QueOut){

String HTTPHeader1;
String HTMLCode1; 

  switch (CommandQue[QueOut]){
    case HTML_VISOR:
    float CIPSendTime1;
     
      //*** Build the HTML code ***
      //Note: Need this first since HTTP Header needs length of content
     
     
      HTMLCode1   =    "<HTML><HEAD>"
                      "<meta http-equiv=refresh content=10>"
                      "<TITLE>Dates Online</TITLE>"
                      "<BODY><H2>MEDITION"
                         "<H3><form action=\"\" method=\"get\">"
                         "<fieldset>"
                            "<legend>Dates</legend>"
                            "ESP WIFI\r\n"
                           
                         "</fieldset>"
                     
                       "</form></H3>"
                       "</BODY></HTML>";
                       
                                     
      //*** Build HTTP Header ***
      HTTPHeader1 = "HTTP/1.0 200 OK \r\n"
                      "Date: Fri, 31 Dec 1999 23:59:59 GMT \r\n"
                      "Content-Type: text/html\r\n"
                      "Content-Length: " + String(HTMLCode1.length()) + "\r\n";
                     
                     
                 

      //*** Sen0d the CIPSEND command for the HTTPHeader string ***
      Serial3.println("AT+CIPSEND=" + String(CommandQueIPD_CH[QueOut]) + ","+  String(HTTPHeader1.length()+2));
      CIPSendTime1 = millis();
      while ((millis() - CIPSendTime1)<QUE_INTERVAL){
        //*** Handle each character that is coming in from the ESP8266 ***
        //*** Need to do this here also to not miss any incoming messages ***
        while(Serial3.available())
        {
          IncomingChar (Serial3.read ());
        } 
      }
      if (QueOut==QueIn) break;        // There must have been a reset

      //*** Send the HTTPHeader String ***
      Serial3.println(HTTPHeader1);
   
      CIPSendTime1 = millis();
      while ((millis() - CIPSendTime1)<QUE_INTERVAL){
        //*** Handle each character that is coming in from the ESP8266 ***
        //*** Need to do this here also to not miss any incoming messages ***
        while(Serial3.available())
        {
          IncomingChar (Serial3.read ());
        }
      } 
      if (QueOut==QueIn) break;        // There must have been a reset

      //*** Send the CIPSEND command for the HTML page ***       
      Serial3.println("AT+CIPSEND=" + String(CommandQueIPD_CH[QueOut]) + ","+  String(HTMLCode1.length()+2));
      CIPSendTime1 = millis();
      while ((millis() - CIPSendTime1)<QUE_INTERVAL){
        //*** Handle each character that is coming in from the ESP8266 ***
        //*** Need to do this here also to not miss any incoming messages ***
        while(Serial3.available())
        {
          IncomingChar (Serial3.read ());
        }
      } 
      if (QueOut==QueIn) break;        // There must have been a reset

      //*** Send out the HTML code ***
      Serial3.println(HTMLCode1);   
      CIPSendTime1 = millis();
      while ((millis() - CIPSendTime1)<QUE_INTERVAL){
        //*** Handle each character that is coming in from the ESP8266 ***
        //*** Need to do this here also to not miss any incoming messages ***
        while(Serial3.available())
        {
          IncomingChar (Serial3.read ());
        }
      } 
     
      break;
   
   
     case FAVICON_REQUEST:
      //*** Send the CIPSEND command for Close ***   
      Serial3.println("AT+CIPCLOSE=" + String(CommandQueIPD_CH[QueOut]));
     
      break;
     
    default:
      //Nothing here yet 
      Serial.println("Should never see this");
  }


//***** Group up characters until end of line ****
void IncomingChar (const byte InChar)
{
  static char InLine [500];    //Hope we don't get more than that in one line
  static unsigned int Position = 0;

  switch (InChar)
  {
  case '\r':   // Don't care about carriage return so throw away.
    break;
   
  case '\n': 
    InLine [Position] = 0; 
    ProcessCommand(String(InLine));
    Position = 0; 
    break;

  default:
      InLine [Position++] = InChar;
  } 
}


void ProcessCommand(String InLine){
  Serial.println("InLine: " + InLine);
 
  if (InLine.startsWith("+IPD,")){
    CommandQueIPD_CH[QueIn]=InLine.charAt(5)-48;    // The value returned is ASCII code.  48 is ASCII code for 0
    Serial.println("******** IPD found: " + String(CommandQueIPD_CH[QueIn]));
  }
 
  if(InLine.indexOf("GET / ") != -1) {CommandQue[QueIn++]=HTML_VISOR;}
   
 
  if (InLine.indexOf("favicon.ico") != -1) { 
    CommandQue[QueIn++]=FAVICON_REQUEST;}
 
 
  if (InLine.indexOf("System Ready") != -1) {
    Serial.println("The ESP8266 Reset for some reason");
    InitWifiModule();

  }
   
  if (InLine.indexOf("busy s...") != -1) {
    Serial.println("dead with busy s...   HW Reset");
    QueOut=QueIn;                      //Clear out the command que
    digitalWrite(ESP_RESET,LOW);
    delay(500);
    digitalWrite(ESP_RESET,HIGH);   
    // Note: Parser should see the reset and start the InitWifiModule routine
  }
  if (InLine.indexOf("link is not") != -1) {
    Serial.println("AHAHAHAHAHAAHAHAHAHAHAH***********************************");
  } 
 
  if (QueIn==QUE_SIZE){
    Serial.println("Resetting QueIn");delay(200);
    QueIn=0;
  }

  LastQueEntered = millis();

}

//***** This initializes the Wifi Module as a server  *****
void InitWifiModule(){
  SendCommand("AT+RST", "Ready", true);
  delay(200);
  SendCommand("AT+CIFSR", "OK", true);
  SendCommand("AT+CIPMUX=1","OK",true);
  SendCommand("AT+CWMODE=3","no change",true);

  SendCommand("AT+CIPSERVER=1,80","OK",true);
  delay(50);
  SendCommand("AT+CWSAP=\"ESP-Wifi\",\"hola\",1,0\r\n","OK",true); // reset module
 
  //----------------------------------------------------
}

//************** This section specific to simple AT command with no need to parse the response ************
//  Warning: Will drop any characters coming in while waiting for the indicated response.
boolean SendCommand(String cmd, String ack, boolean halt_on_fail)
{
  Serial3.println(cmd); // Send command to module

  // Otherwise wait for ack.
  if (!echoFind(ack)) // timed out waiting for ack string
    if (halt_on_fail)
      errorHalt(cmd+" failed");// Critical failure halt.
    else
      return false; // Let the caller handle it.
  return true; // ack blank or ack found
}

// Read characters from WiFi module and echo to serial until keyword occurs or timeout.
boolean echoFind(String keyword)
{
  byte current_char   = 0;
  byte keyword_length = keyword.length();

  // Fail if the target string has not been sent by deadline.
  long deadline = millis() + TIMEOUT;
  while(millis() < deadline)
  {
    if (Serial3.available())
    {
      char ch = Serial3.read();
      Serial.write(ch);
      if (ch == keyword[current_char])
        if (++current_char == keyword_length)
        {
          Serial.println();
          return true;
        }
    }
  }
  return false;  // Timed out
}

// Print error message and loop stop.
void errorHalt(String msg)
{
  Serial.println(msg);
  Serial.println("HALT");
  while(true){};
}