ArduinoJson Lockup

Hi Community

Would you kindly take a look at this piece of code which locks up my mkr 1010.

In essence I am querying OpenWeatherMap and decoding the returned json using JsonArduino 6.17.2. The json blob is approx. 6kB in size and is initially decoded just fine. However upon the 5th execution the mkr hangs. This is regardless of the interval between executions.

I narrowed it down to the exact line of code. So it must either be deserializeJson() or
httpClient or String

Any recommendations as to the use of dynamic vs. static JsonDocument are also greatly appreciated.

#include <Arduino.h>
#include <WiFiNINA.h>
#include <ArduinoHttpClient.h>
#include <RTCZero.h>
#include <ArduinoJson.h>
#include <math.h>


DynamicJsonDocument jDoc(10000);
queryOW(now(), jDoc);

[...]

bool CWeatherman::queryOW(uint32_t dt, DynamicJsonDocument &jsonDoc) {
  char qry[256]; 
  snprintf(qry, 255, "/data/2.5/onecall/timemachine?lat=%f&lon=%f&dt=%lu&appid=f39d26xxx",m_latitude,m_longitude,dt);
   
  WiFiSSLClient wifiCl;
  HttpClient httpCl = HttpClient(wifiCl, m_OWServer, 443);
  httpCl.get(qry); 
  
  int httpStat = httpCl.responseStatusCode();
  if (httpStat != 200) {
      Serial.println("Request failed");
      return false;
  }
  //## Parse Response => Json
  Serial.print("works");
  DeserializationError err = deserializeJson(jsonDoc, httpCl.responseBody().c_str());
  Serial.print("not reached after 5th execution");
  
  if (err) {
    Serial.print(F("deserializeJson() failed: "));
    return false;
  }
  return true;
}

Using a microcontroller with a total of 32kB RAM you use a JSON buffer of 10kB and get a 6kB JSON document. The memory probably gets fragmented too fast.

Use a full-fledged computer (p.e. Raspberry Pi) if you want to handle such documents or avoid libraries that use dynamic memory allocation and optimize the complete program for memory (p.e. parse the JSON document yourself and just keep the values you’re interested in).

Hi everybody

I found the solution.
It was heap fragmentation - again :slight_smile:

ArduinoJson’s DynamicJsonDcument is locking its allocated heap space thus preventing others from reusing it even when the DynamicJsonDocument goes out of scope.

I dropped ArduinoJson completely for its memory footprint and switched to an on-the-fly json parser without any memory consumption.

Thanks

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.