如何解决“deserializeJson() 失败:NoMemory”我的 NodeMcu-Mx 与 ESP8266 错误
我正在用我的 NodeMCU Mx 和 ESP8266 制作一个小项目,但 ArduinoJson 库告诉我有一个错误。 我只想获取我的 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
我真的不明白,有人可以帮我吗? 谢谢!
解决方法
您已指定 96 字节作为对象的大小,因此您要检索的 JSON 必须大于您的预期。
DeserializationError::NoMemory
The JsonDocument is too small; you need to increase its capacity.
将 JSON 数据复制到剪贴板,然后转到 ArduinoJSON Assistant。选择您的处理器,然后粘贴您期望的 JSON。助手会告诉您需要发送到 StaticJsonDocument<96> doc;
行的对象大小。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。