Big http body can't be read by nano 33 IoT

Hi, so I'm having trouble reading the json body from an http request on my Arduino nano 33 IoT (1.5.0). I am able to receive bodies of size 550 characters but anything greater than this is impossible to read and my Arduino just crashes/stops.
The Arduino was able to read 17000 characters at some point with the same code but ins't able to anymore and I can't get my head around to why.

Would anyone know what is happening ?

This is my very simplified code:

#include <WiFiNINA.h>
#include <ArduinoHttpClient.h>
#include <ArduinoJson.h>

WiFiClient wifi;
HttpClient client = HttpClient(wifi, serverAddress, port);

void setup() {
  Serial.begin(115200);
  WiFi.setHostname("arduino_name");
  WiFi.begin(SECRET_SSID, SECRET_PASS);

  delay(5000); // I actually check if I have connection and it connects successfully ...
}

void loop() {
  //checks if wifi connection is still up ...

  //doing get request
  client.beginRequest();
  client.get(getaddress);
  client.sendBasicAuth(apikey, ""); // send the username and password for authentication
  client.endRequest();
  
  //get body
  int statusCode = client.responseStatusCode();
  const char* response = client.responseBody().c_str(); //crashes here
  Serial.println(strlen(response));
  Serial.println(response);

  // Parsing Json body
  StaticJsonDocument<24576> doc;
  DeserializationError error = deserializeJson(doc, response);

  //Then use the Json Document to extract data ...

  delay(15000);
}

Try using online parsing (parse as you read, without reading it into memory first) is it possible to do like this?

void loop() {
  // ... Your existing setup code ...

  client.beginRequest();
  client.get(getaddress);
  client.sendBasicAuth(apikey, ""); // send the username and password for authentication
  client.endRequest();

  //get body
  int statusCode = client.responseStatusCode();
  if (statusCode != 200) {
    Serial.println("HTTP request failed");
    return;
  }

  // Create a StaticJsonDocument with a size to suit your response.
  StaticJsonDocument<24576> doc;

  // Parse the JSON in a streaming fashion.
  DeserializationError error = deserializeJson(doc, client);
  if (error) {
    Serial.print(F("deserializeJson() failed: "));
    Serial.println(error.c_str());
    return;
  }

  // Use the JsonDocument to extract data ...

  delay(15000);
}
2 Likes

Solved So I've completely redone my code from scratch and I am able to receive my json body again but it stops working as soon as I add the
DeserializationError error = deserializeJson(doc, client);
or
DeserializationError error = deserializeJson(doc, response);

Even the print that is called before it stops working for some reason.

-- This could be due to the large size of the doc file, and indeed it is, the Arduino nano 33 IoT only has a maximum of 32768 bytes for local memory, so calling a 24000 bytes doc + the body string + other local variables fills that quickly -> therefore crashing.
I've never had to think about memory but now I do haha, thank you for your help !
I'll try and implement the deserialising for stream. :slightly_smiling_face:

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