我正在使用 NodeMCU v3,并且已经可以在其服务器上发送一些信息。
但是我如何从其他网页接收一些信息,比如说开始,纯文本?
最佳答案
您需要一个HttpClient来与网络服务器通信。
开始的好方法是使用 HttpClient 示例 -> ReuseConnection。
这将允许您发出多个请求。
您可以在 Arduino IDE 的 Serial Monitor 中查看请求的响应。
示例代码:
注意:将“http://:/someroute ”替换为您想要获取的http页面。
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#define USE_SERIAL Serial
ESP8266WiFiMulti WiFiMulti;
HTTPClient http;
void setup() {
USE_SERIAL.begin(115200);
// USE_SERIAL.setDebugOutput(true);
USE_SERIAL.println();
USE_SERIAL.println();
USE_SERIAL.println();
for(uint8_t t = 4; t > 0; t--) {
USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
USE_SERIAL.flush();
delay(1000);
}
WiFiMulti.addAP("SSID", "PASSWORD");
// allow reuse (if server supports it)
http.setReuse(true);
}
void loop() {
// wait for WiFi connection
if((WiFiMulti.run() == WL_CONNECTED)) {
http.begin("http://<IP>:<Port>/someroute");
int httpCode = http.GET();
if(httpCode > 0) {
USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server
if(httpCode == HTTP_CODE_OK) {
String payload = http.getString();
USE_SERIAL.println(payload);
}
} else {
USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
}
delay(3000);
}
关于c++ - 如何通过NodeMCU从网络获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43983535/