bblanchon / ArduinoJson

📟 JSON library for Arduino and embedded C++. Simple and efficient.
https://arduinojson.org
MIT License
6.69k stars 1.12k forks source link

ESP32: NoMemory error using ArduinoJSON assistant #1640

Closed tobozo closed 3 years ago

tobozo commented 3 years ago

Hey @bblanchon thanks for your great library!

I'm getting a NoMemory error even though I've used the assistant.

  DynamicJsonDocument jsonAppBuffer(8192);
  String payload = http.getString();
  DeserializationError error = deserializeJson(jsonAppBuffer, payload );
  // DeserializationError error = deserializeJson(jsonAppBuffer, payload.c_str(), payload.length() );

  if (error) {
    Serial.printf("JSON Parsing failed! (err=%s)\n", error.c_str() );
  }

Serial output:

[118188][D][HTTPClient.cpp:1260] handleHeaderResponse(): size: 3095
[118189][D][HTTPClient.cpp:603] sendRequest(): sendRequest code=200
[118219][D][HTTPClient.cpp:1396] writeToStreamDataBlock(): connection closed or file end (written: 3095).
[118220][D][HTTPClient.cpp:385] disconnect(): tcp keep open for reuse
JSON Parsing failed! (err=NoMemory)

here's my software stack:

Using library ESP32-Chimera-Core at version 1.2.0 in folder: ~/Arduino/libraries/ESP32-Chimera-Core 
Using library Wire at version 2.0.0 in folder: ~/.arduino15/packages/esp32/hardware/esp32/2.0.0-rc2/libraries/Wire 
Using library SPI at version 2.0.0 in folder: ~/.arduino15/packages/esp32/hardware/esp32/2.0.0-rc2/libraries/SPI 
Using library FS at version 2.0.0 in folder: ~/.arduino15/packages/esp32/hardware/esp32/2.0.0-rc2/libraries/FS 
Using library SD at version 2.0.0 in folder: ~/.arduino15/packages/esp32/hardware/esp32/2.0.0-rc2/libraries/SD 
Using library LovyanGFX at version 0.4.2 in folder: ~/Arduino/libraries/LovyanGFX 
Using library Preferences at version 2.0.0 in folder: ~/.arduino15/packages/esp32/hardware/esp32/2.0.0-rc2/libraries/Preferences 
Using library M5Stack-SD-Updater at version 1.1.0 in folder: ~/Arduino/libraries/M5Stack-SD-Updater 
Using library Update at version 2.0.0 in folder: ~/.arduino15/packages/esp32/hardware/esp32/2.0.0-rc2/libraries/Update 
Using library ArduinoJson at version 6.18.3 in folder: ~/Arduino/libraries/ArduinoJson 
Using library WiFi at version 2.0.0 in folder: ~/.arduino15/packages/esp32/hardware/esp32/2.0.0-rc2/libraries/WiFi 
Using library HTTPClient at version 2.0.0 in folder: ~/.arduino15/packages/esp32/hardware/esp32/2.0.0-rc2/libraries/HTTPClient 
Using library WiFiClientSecure at version 2.0.0 in folder: ~/.arduino15/packages/esp32/hardware/esp32/2.0.0-rc2/libraries/WiFiClientSecure 
bblanchon commented 3 years ago

Hi @tobozo,

I'm pretty confident the Assistant computes the right capacity; however, some settings can affect the result:

  1. Input type, in step 1, should be String or const char*.
  2. Deduplicate values when measuring the capacity, in step 3, should be unchecked, most likely.
  3. Deduplicate keys when measuring the capacity, in step 3, it should be unchecked if the keys are dynamic or if you used placeholders.

It's also possible that the DynamicJsonDocument fails to allocate the memory pool. Please check the value of JsonDocument::capacity() to confirm that the allocation succeeded.

If the problem persists, please provide an MCVE.

Best regards, Benoit

tobozo commented 3 years ago

Hello @bblanchon

Thanks for hinting at capacity(), it's returning 0 as you probably suspected.

Making the DynamicJsonDocument jsonAppBuffer(8192); declaration global seemed to change the game as I got a memory error on the https connection instead:

[ 65150][E][ssl_client.cpp:36] _handle_error(): [start_ssl_client():253]: (-10368) X509 - Allocation of memory failed

Using http.end() after collecting the https payload and before feeding the DynamicJsonDocument seemed to solve my issue.

Having 55771 bytes of heap free seems enough to decode 8Kb of json, or at least it was with esp32-arduino-core 1.x.x.

My guess is arduino-core-2.0.0-rc2 has just become a huge memory sink when it comes to use TLS, and the free heap reported isn't exactly free.

I'm closing this as it's not really an ArduinoJSON issue, but feel free to ask if you need me to test this edgy case with some experimental changes.

Thanks again for your availability an patience on this marvelous project :+1:

bblanchon commented 3 years ago

You're welcome, @tobozo. Maybe the reported free heap is accurate, but the heap is highly fragmented... BTW, the ArduinoJson Troubleshooter could have helped you diagnose this issue.