gpt4 book ai didi

http - ESP8266 WiFiClient 简单 HTTP GET

转载 作者:可可西里 更新时间:2023-11-01 15:13:03 26 4
gpt4 key购买 nike

我正在研究使用 ESP8266 和 ESP8266WiFi library 读取网页的简单问题.

我只更改了示例中的几行,不知道是什么问题。那是我的代码:

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 背后发生的事情。

关于http - ESP8266 WiFiClient 简单 HTTP GET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34078497/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com