[英]ESP8266 Arduino IDE JSON HTTP REQUEST
我一直在嘗試在ESP8266上獲取天氣信息,以將其顯示在OLED顯示屏上。
if (!client.connect("api.apixu.com", 80)) {
Serial.println("connection failed");
return;}
client.print("GET " + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Content-Type: application/json\r\n" +
"Connection: close\r\n\r\n");
char c[1024];
// Read all the lines of the reply from server and print them to Serial
while(client.available()){
c[0] = client.read();Serial.print(c);}
這是我用於請求和讀取JSON內容的代碼。 但是無論我如何嘗試,代碼都不會進入while循環。
有人可以發現錯誤嗎?
先感謝您! :d
我以某種方式修復了此代碼,該代碼適用於所有具有與available()
始終為0的錯誤相同的錯誤的人。
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.