Arduino & ESP8266 HTTP GET 响应处理
Arduino & ESP8266 HTTP GET Response handling
我已成功连接服务器,但无法处理响应数据。 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
问题不在于响应时间。假设有一个通过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”之前成功显示。
我已成功连接服务器,但无法处理响应数据。 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
问题不在于响应时间。假设有一个通过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”之前成功显示。