我对 HTTPClient 库的 POST 请求有 JSON 问题

I have a JSON problem with HTTPClient library's POST request

我无法正确使用 POST() 功能或不知道如何使用

我收到此错误消息

no matching function for call to 'HTTPClient::POST(ArduinoJson::JsonObject&)

当我尝试在 POST() 函数中发送一个 JSON 对象时,我收到以下 return 消息:

{
  "error": "ParseError",
  "description": "Errors found in incoming JSON buffer"
}

当我尝试在 POST() 函数中简单地将 JSON 写为字符串并尝试以这种方式发送它时

#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h> //v5.13.5

const char* ssid = "**";
const char* password = "**";


void setup() {
  Serial.begin(115200);
  delay(4000);
  WiFi.begin(ssid,password);

  while (WiFi.status() != WL_CONNECTED) { //Check for the connection
    delay(1000);
    Serial.println("Connecting to WiFi..");
  }

  Serial.println("Connection established!");
  Serial.print("IP address:\t");
  Serial.println(WiFi.localIP());
  Serial.println("Connected to the WiFi network");


}

void loop() {
  if(WiFi.status() == WL_CONNECTED){
    HTTPClient http;

    http.begin("http://172.20.10.13:1026/v2/entities"); //My docker
    http.addHeader("Content-Type", "application/json");

    StaticJsonBuffer<200> jsonBuffer;
    JsonObject& root = jsonBuffer.createObject();
    root["id"] = "urn:ngsi-ld:Sensor:001";
    root["type"] = "motion";
    root["value"] = "No";
    root.printTo(Serial);
    /*int httpResponseCode = http.POST("{\n\t\"id\":\"urn:ngsi-ld:Sensor:001\", \"type\":\"MotionSensor\",\n\t\"value\":\"NO\"\n}"); */
    int httpResponseCode = http.POST(root);
    if(httpResponseCode > 0){
      String response = http.getString();

      Serial.println(httpResponseCode);
      Serial.println(response);
    }
    else{
      Serial.print("Error on sending POST: ");
      Serial.println(httpResponseCode);
    }
    http.end();
  }
  else{
    Serial.println("Error in WiFi connection");
  }
  delay(300000);
}

结果应该是对 docker 的正常 POST 请求和存储的数据,而我可以在我的 VM

终端中使用 GET 命令读取该数据

HTTPClient.POST is Arduino String (or a C string) and you can't simply pass your JSON object. You need to use prettyPrintTo 的参数,用于将您的 json 对象转换为标准 JSON 字符串,然后将其传递给 POST 函数。

#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h> //v5.13.5

const char *ssid = "**";
const char *password = "**";

void setup()
{
    Serial.begin(115200);
    delay(4000);
    WiFi.begin(ssid, password);

    while (WiFi.status() != WL_CONNECTED)
    { //Check for the connection
        delay(1000);
        Serial.println("Connecting to WiFi..");
    }

    Serial.println("Connection established!");
    Serial.print("IP address:\t");
    Serial.println(WiFi.localIP());
    Serial.println("Connected to the WiFi network");
}

void loop()
{
    if (WiFi.status() == WL_CONNECTED)
    {
        HTTPClient http;

        http.begin("http://172.20.10.13:1026/v2/entities"); //My docker
        http.addHeader("Content-Type", "application/json");

        StaticJsonBuffer<200> jsonBuffer;
        JsonObject &root = jsonBuffer.createObject();
        root["id"] = "urn:ngsi-ld:Sensor:001";
        root["type"] = "motion";
        root["value"] = "No";
        root.printTo(Serial);
        /*int httpResponseCode = http.POST("{\n\t\"id\":\"urn:ngsi-ld:Sensor:001\", \"type\":\"MotionSensor\",\n\t\"value\":\"NO\"\n}"); */
        char json_str[100];
        root.prettyPrintTo(json_str, sizeof(json_str));
        int httpResponseCode = http.POST(json_str);
        if (httpResponseCode > 0)
        {
            String response = http.getString();

            Serial.println(httpResponseCode);
            Serial.println(response);
        }
        else
        {
            Serial.print("Error on sending POST: ");
            Serial.println(httpResponseCode);
        }
        http.end();
    }
    else
    {
        Serial.println("Error in WiFi connection");
    }
    delay(300000);
}