Arduino& ESP8266 HTTP GET响应处理

时间:2016-06-16 08:43:04

标签: c arduino http-get esp8266

我已成功连接我的服务器,但我无法处理响应数据。 esp8266.find(" + PID,")不起作用。我的循环代码如下:

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");
  }

}

这是输出:正如您所注意到的,IPD NOT FOUND在RESPONSE到达之前执行。我该怎么做来捕获这些数据?提前谢谢。

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

1 个答案:

答案 0 :(得分:0)

问题不在于响应时间。假设有一个通过AT + CIPSTART打开的TCP连接,以及一个42个字符长的请求,如下所示:

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

在执行之前,应将CIPSEND设置为字数。问题是,\ r \ n 每个计为1个字节,不应计为2个字符。请求包含4个字符42-4 = 38。

AT+CIPSEND=38\r\n

排除2个字节解决了问题,并在我的代码检查是否有任何" + PID," s之前成功显示。