"deserializeJson() failed: NoMemory" Error with my NodeMcu-Mx with ESP8266

3.4k views Asked by At

Im making a little project with my NodeMCU Mx with ESP8266, but ArduinoJson lib tells me there's an error. I just want to fetch the data inside my json file and use the data as variable, in order to print it on a LCD display. It was working at the beginning, but now its always makes the same error, even if i recompile and transfert it into my nodemcu.

The code :

#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
}

And here's the error:

deserializeJson() failed: NoMemory

I don't really understand, can someone help me please? Thanks!

1

There are 1 answers

0
stevieb On

You've specified 96 bytes as the size of the object, so the JSON you're retrieving must be larger than you're expecting.

From Deserialization Errors:

DeserializationError::NoMemory

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

Copy the JSON data into your clipboard, and head on over to the ArduinoJSON Assistant. Select your processor, then paste in the JSON that you're expecting. The assistant will tell you the object size you need to send into the StaticJsonDocument<96> doc; line.