1

我有一个 NodeMCU WiFi 模块,我在 Arduino IDE 中为它编写代码。我在发送一些东西时遇到问题,使用 POST 方法然后阅读页面。我有一个返回两个变量的 PHP 页面。我的代码如下:

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

void loop() {
    // wait for WiFi connection

    if ((WiFiMulti.run() == WL_CONNECTED)) {
        HTTPClient http;
        USE_SERIAL.print("[HTTP] begin...\n");
        // configure traged server and url
        http.begin("http://server/test.php"); // HTTP

        http.addHeader("Content-Type", "application/x-www-form-urlencoded");
        http.POST("id=s1&api=123456789");
        http.writeToStream(&Serial);
        http.end();

        USE_SERIAL.print("[HTTP] GET...\n");
        // start connection and send HTTP header
        int httpCode = http.GET();

        // httpCode will be negative on error
        if (httpCode > 0) {
            // HTTP header has been send and Server response header has been handled
            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(10000);
}

当我运行它时,我得到一个 411 错误代码。这里有什么问题?我该如何解决?
我在 Postman 中测试了这些论点,它们没问题。

4

0 回答 0