从 ESP8266 Arduino 开发板向服务器发送 JSON 对象的问题

Issues posting a JSON object to a server from an ESP8266 Arduino board

我正在尝试从 ESP8266 开发板 post 将 JSON 对象发送到部署在 Heroku 上的服务器。

我的问题是我没有 posting 任何东西,或者我 posting 了一个错误的 JSON 对象,因为 Heroku 日志显示 500 错误并且我得到一个空的串行监视器上的对象。

我的理解是我正确地访问了服务器,但是 JSON 对象的格式错误或者我的 post 方法中遗漏了一些东西。

能否请您检查我的代码并帮助我找到解决方案?

这是我的代码:

#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecure.h> 
#include <WiFiClient.h> 

const char *host = "appname.herokuapp.com";
const char* serverName = "https://appname.herokuapp.com";
const char* ssid = "*******"; 
const char* password = "*******";

void setup() {
  Serial.begin(9600);

  WiFi.begin(ssid, password);
  Serial.println("Connecting");
  while(WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());
}

void loop() {

  if(WiFi.status()== WL_CONNECTED) {
      std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure);
      client->setInsecure();
     
      HTTPClient https;

      DynamicJsonDocument doc(512);
      JsonObject object = doc.to<JsonObject>();
      object["temperature"] = 30;
      String data;
      serializeJsonPretty(object, data);

      Serial.println(data);

      https.addHeader("Accept:", "application/json");
      https.addHeader("Host:", host);

      if (https.begin(*client, serverName)) {
          Serial.print("[HTTPS] POST...\n");
          int httpCode = https.POST(data);

          if (httpCode > 0) {
            Serial.printf("[HTTPS] POST... code: %d\n", httpCode);
            
            if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
              String payload = https.getString();
              Serial.println(payload);

            } else {
              Serial.printf("[HTTPS] POST... failed, error: %s\n", https.errorToString(httpCode).c_str());
            }

          } else  {
            Serial.printf("[HTTPS] POST... failed, error: %s\n", https.errorToString(httpCode).c_str());
          }
          https.end();

    } else {
      Serial.printf("[HTTPS] Unable to connect\n");
    } 
  }
  delay(5000);
}

问题是将 post 方法嵌套在 if 语句中,而不是在添加 headers 之后再使用它。 我已经修改了代码,现在可以使用了。

void loop() {

  if(WiFi.status()== WL_CONNECTED) {

    std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure);
    client->setInsecure();
 
    HTTPClient https;

    DynamicJsonDocument doc(512);
    JsonObject object = doc.to<JsonObject>();
      
    object["temperature"] = 32;

    String data;
    serializeJsonPretty(object, data);

    Serial.println(data);

    https.begin(*client, serverName);
    https.addHeader("Content-Type", "application/json; charset=UTF-8");       
      
    int httpCode = https.POST(data);

    if (httpCode == 200) {
      Serial.println("POST succeeded with code:");
      Serial.println(httpCode);
      
    } else if (httpCode != 200) {
      Serial.println("POST failed with code:");
      Serial.println(httpCode);

    } else {
      Serial.println("Unknown error");
    }

    String payload = https.getString();
    Serial.println(payload);

    https.end();
  }
  delay(10000);
}