我正在使用ESP8266模块。我已经成功创建了一个草图,它将HTTP请求发送到我的本地服务器。服务器发送响应,但是,我无法在Arduino IDE的串行监视器中显示它。我在互联网上找不到任何地方,如何显示收到的消息。我唯一能够显示的是整个GET请求。
甚至可以从服务器获取,解析和显示响应吗?如果是这样,任何人都可以提供代码示例吗?
答案 0 :(得分:0)
HTTPClient http;
USE_SERIAL.print("[HTTP] begin...\n");
// configure traged server and url
//http.begin("https://192.168.1.12/test.html", "7a 9c f4 db 40 d3 62 5a 6e 21 bc 5c cc 66 c8 3e a1 45 59 38"); //HTTPS
http.begin("http://192.168.1.12/test.html"); //HTTP
USE_SERIAL.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();
// httpCode will be negative on error
if(httpCode) {
// HTTP header has been send and Server response header has been handled
USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server
if(httpCode == HTTP_CODE_OK) {
String payload = http.getString();
// print only part of the string
USE_SERIAL.println(payload.substring(0,200));
}
} else {
USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
这对你有用吗? 关键是:payload.substring(0,200)
HTH