2017-05-13 1 views
0

J'ai essayé d'obtenir des informations météorologiques sur mon ESP8266 pour les afficher sur un écran OLED.ESP8266 Demande HTTP IDE JSON Arduino

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

C'est mon code pour demander et lire le contenu JSON. Mais quoi que j'essaie, le code n'entre pas dans la boucle while.

Quelqu'un peut-il repérer l'erreur?

Merci d'avance! : D

Répondre

0

Je l'ai fixé en quelque sorte ce code fonctionne pour tous ceux qui avaient la même erreur que available() était toujours 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; 
    }