【问题标题】:How to get data from web by NodeMCUNodeMCU如何从网络获取数据
【发布时间】:2017-10-14 11:27:16
【问题描述】:

我正在使用 NodeMCU v3,并且已经可以在其服务器上发送一些信息。

但是我怎样才能从其他网页接收一些信息,比如说开始,一个纯文本?

【问题讨论】:

    标签: c++ arduino microcontroller nodemcu


    【解决方案1】:

    您需要一个 HttpClient 来与 网络服务器 通信。

    开始的好方法是使用 HttpClient 示例 -> ReuseConnection。

    这将使您请求不止一个

    您可以在 Arduino IDE 的串行监视器中查看请求的响应。

    示例代码:

    注意:将“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);
    }
    

    【讨论】:

    • 我可以使用 https 吗?
    猜你喜欢
    • 1970-01-01
    • 2011-11-02
    • 1970-01-01
    • 2021-01-06
    • 2013-05-04
    • 1970-01-01
    • 2014-07-10
    • 1970-01-01
    相关资源
    最近更新 更多