2015-12-03 18 views
6

Sto lavorando su un semplice problema di lettura di una pagina web utilizzando ESP8266 e ESP8266WiFi library.ESP8266 WiFiClient simple HTTP GET

Ho modificato solo poche righe nell'esempio e non so qual è il problema. Quello è il mio codice:

include <ESP8266WiFi.h> 

const char* ssid  = "WiwoNET"; 
const char* password = "xxxxxxx"; 

const char* host = "https://pure-caverns-1350.herokuapp.com"; 

void setup() { 
    Serial.begin(115200); 
    delay(10); 

    // We start by connecting to a WiFi network 

    Serial.println(); 
    Serial.println(); 
    Serial.print("Connecting to "); 
    Serial.println(ssid); 

    WiFi.begin(ssid, password); 

    while (WiFi.status() != WL_CONNECTED) { 
    delay(500); 
    Serial.print("."); 
    } 

    Serial.println(""); 
    Serial.println("WiFi connected"); 
    Serial.println("IP address: "); 
    Serial.println(WiFi.localIP()); 
} 

int value = 0; 

void loop() { 
    delay(5000); 
    ++value; 

    Serial.print("connecting to "); 
    Serial.println(host); 

    // Use WiFiClient class to create TCP connections 
    WiFiClient client; 
    const int httpPort = 80; 
    if (!client.connect(host, httpPort)) { 
    Serial.println("connection failed"); 
    return; 
    } 

    // We now create a URI for the request 
    String url = "/stan"; 

    Serial.print("Requesting URL: "); 
    Serial.println(url); 

    // This will send the request to the server 
    client.print(String("GET ") + url + " HTTP/1.1\r\n" + 
       "Host: " + host + "\r\n" + 
       "Connection: close\r\n\r\n"); 
    delay(10); 

    // Read all the lines of the reply from server and print them to Serial 
    Serial.println("Respond:"); 
    while(client.available()){ 
    String line = client.readStringUntil('\r'); 
    Serial.print(line); 
    } 

    Serial.println(); 
    Serial.println("closing connection"); 
} 

E quello che vedo sul monitor di serie è:

Connecting to WiwoNET 
....... 
WiFi connected 
IP address: 
192.168.0.111 
connecting to https://pure-caverns-1350.herokuapp.com 
Requesting URL: /stan 
Informacja zwrotna: 
HTTP/1.1 400 Bad Request 
Connection: close 
Server: Cowboy 
Date: Thu, 03 Dec 2015 23:38:59 GMT 
Content-Length: 0 


closing connection 

Stavo guardando i log di Heroku e niente sta mostrando là. Grazie in anticipo per qualsiasi tipo di aiuto.

risposta

8

Si deve essere stato seguendo l'esempio qui: https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/examples/WiFiClient/WiFiClient.ino

c'è un pezzo fondamentale vi siete persi, però. Il valore host non deve essere anteposto a uno schema in stile URI come http:// o https://. Guarda nuovamente l'esempio e usa

const char* host = "pure-caverns-1350.herokuapp.com"; 

.

Si può vedere molto bene cosa sta succedendo sotto il cofano di HTTP se eseguito nella console curl -v http://pure-caverns-1350.herokuapp.com/stan.

+0

Ciao, c'è qualche link aggiornato su quel gitub che hai menzionato? – shamsulfakhar

+0

Aggiornato il collegamento. –