2017-05-13 235 views
0

我一直試圖在我的ESP8266上獲取天氣信息以在OLED顯示屏上顯示它們。ESP8266 Arduino IDE JSON HTTP請求

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

回答

0

我固定它在某種程度上此代碼爲大家誰了相同的錯誤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; 
    }