简体   繁体   中英

Arduino & ESP8266 HTTP GET Response handling

I'm successfully connecting my server but I can't handle the response data. esp8266.find("+PID,") doesn't work. My loop code is below:

void loop() {
  String cmd = "AT+CIPSTART=\"TCP\",\""; //make this command: AT+CPISTART="TCP","192.168.88.35",80
  cmd += DST_IP;
  cmd += "\",80\r\n";
  esp8266.print(cmd);
  //wait a little while for 'Linked'
  delay(3000);

  //This is our HTTP GET Request change to the page and server you want to load.
  cmd = "GET /~atolye/wifiModule.php?id=123 HTTP/1.0\r\n";
  cmd += "Host: 159.122.115.70\r\n\r\n";
  esp8266.print("AT+CIPSEND=63\r\n");
  if(esp8266.find(">")) {
    //Send our http GET request
    Serial.println("SENDING THE REQUEST");
    esp8266.print(cmd);
    if(esp8266.find("+IPD,")) {
      Serial.println("\r\nIPD FOUND");
      if(esp8266.find("\"1")) {
        int value = esp8266.read()-48;
        Serial.println("\r\nVALUE FOUND");
        Serial.println(value);
        esp8266.println(value);
      }
    } else {
      Serial.println("\r\n!! IPD NOT FOUND");
    }
  }
  else {
    Serial.println("\r\nERROR > DOESN'T EXIST");
  //Something didn't work...
    esp8266.print("AT+CIPCLOSE\r\n");
  }

}

And this is the output: As you can notice, IPD NOT FOUND executes before RESPONSE arrives. What should I do to catch that data? Thanks in advance.

AT+CIPSTART="TCP","159.122.115.70",80

CONNECT

OK
SENDING THE REQUEST

!! IPD NOT FOUND
START="TCP","159.122.115.70",80

busy s...

Recv 103 bytes

SEND OK

+IPD,205:HTTP/1.1 200 OK
Date: Thu, 16 Jun 2016 08:33:19 GMT
Server: Apache/2.4.16 (Unix) OpenSSL/1.0.1e-fips mod_bwlimited/1.4
X-Powered-By: PHP/5.5.30
Connection: close
Content-Type: application/json

"12"CLOSED

The problem was not about response time. Assume there is a TCP connection opened via AT+CIPSTART, and a request which is 42 chars long, as shown below:

GET / HTTP/1.0\r\nHost: 77.223.129.195\r\n

Before executing one should set CIPSEND to char count. Problem is, \\r\\n are counted as 1 byte each, shouldn't be counted 2 chars. Request consists of 4 chars 42-4 = 38.

AT+CIPSEND=38\r\n

Excluding 2 bytes solved the problem and successfully displayed before the code checks if there are any "+PID,"s.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM