0

我用我的 NodeMCU Mx 和 ESP8266 做了一个小项目,但是 ArduinoJson lib 告诉我有一个错误。我只想在我的 json 文件中获取数据并将数据用作变量,以便将其打印在 LCD 显示器上。它一开始可以工作,但现在它总是会犯同样的错误,即使我重新编译并将其传输到我的 nodemcu 中。

编码 :

#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
String payload = "";

void setup()
{
  //CONNEXION AU WIFI
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);
  WiFi.begin("test", "testtest");
  while (WiFi.status() != WL_CONNECTED) {
    digitalWrite(LED_BUILTIN, LOW);
    delay(100);
    digitalWrite(LED_BUILTIN, HIGH);
    delay(100);
  }
  Serial.println("Connected !");
  delay(1000);
  Serial.print("Local IP:");
  Serial.println((WiFi.localIP().toString()));
  Serial.print("Mac adress :");
  Serial.println((WiFi.macAddress().c_str()));
  Serial.print("Hostname :");
  Serial.println((WiFi.hostname()));

  //LED ALWAYS ON
  digitalWrite(LED_BUILTIN, LOW);
}

void loop() {
  if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status

    HTTPClient http; //Declare an object of class HTTPClient

    http.begin("http://latin-american-brea.000webhostapp.com/dan.json"); //Specify request destination
    http.addHeader("Content-Type", "text/html");
    int httpCode = http.GET(); //Send the request

    if (httpCode > 0) { //Check the returning code

      String payload = http.getString();   //Get the request response payload
      Serial.println(payload);             //Print the response payload

      // Stream& input;

      StaticJsonDocument<96> doc;

      DeserializationError error = deserializeJson(doc, payload);

      if (error) {
        Serial.print(F("deserializeJson() failed: "));
        Serial.println(error.f_str());
        return;
      }

      const char* fruit = doc["fruit"]; // "Apple"
      const char* sizz = doc["sizz"];   // "Large"
      const char* color = doc["color"]; // "Red"
      Serial.println(fruit);

    } else {
      Serial.println("Marche pas");
    }

    http.end(); //Close connection
  }

  Serial.println(payload);
  delay(30000); //Send a request every 30 seconds
}

这是错误:

deserializeJson() failed: NoMemory

我真的不明白,请有人帮助我吗?谢谢!

4

1 回答 1

1

您已将 96 字节指定为对象的大小,因此您检索的 JSON 必须比您预期的要大。

反序列化错误

DeserializationError::NoMemory

The JsonDocument is too small; you need to increase its capacity.

将 JSON 数据复制到剪贴板,然后转到ArduinoJSON 助手。选择您的处理器,然后粘贴您期望的 JSON。助手会告诉您需要发送到StaticJsonDocument<96> doc;线路中的对象大小。

于 2021-05-14T01:20:19.033 回答