簡體   English   中英

ESP8266 WiFiClient 簡單的 HTTP GET

[英]ESP8266 WiFiClient simple HTTP GET

我正在研究使用 ESP8266 和ESP8266WiFi 庫閱讀網頁的簡單問題。

我在示例中只更改了幾行,不知道是什么問題。 那是我的代碼:

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

我在串行監視器中看到的是:

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

我正在查看heroku的日志,但那里沒有顯示任何內容。 在此先感謝您的任何幫助。

您一定一直在關注https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/examples/WiFiClient/WiFiClient.ino 上的示例

但是,您錯過了一個關鍵部分。 host值不得以 URI 樣式的方案開頭,例如http://https:// 再次查看示例並使用

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

反而。

如果在您的控制台中運行curl -v http://pure-caverns-1350.herokuapp.com/stan ,您可以非常清楚地看到 HTTP 引擎蓋下發生的事情。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM