I'm developing an API on my Controllino Mega (an Arduino Mega variant) that handles GET and POST requests. The GET request should return a JSON object containing data for multiple groups of pins, but when I call the API using Postman, I'm noticing that some of the expected data is missing from the response.
The GET request response looks something like this in Postman:
{
"lamp": [
{
"header": "Digital Output",
"pins": [
{ "name": "", "pin": 2, "value": 1 },
{ "name": "", "pin": 3, "value": 1 },
// ... more pins
]
},
// ... other groups
]
}
Here’s a simplified version of my code:
#include <SPI.h>
#include <Ethernet.h>
#include <Controllino.h>
#include <ArduinoJson.h>
#define ARRAY_COUNT(x) (sizeof(x)/sizeof(x[0]))
struct PinRange {
int startPin;
int endPin;
};
struct PinGroup {
const char* name;
PinRange* ranges;
int rangeCount;
};
PinRange digitalOutputRanges[] = { {2, 13}, {42, 49}, {77, 80} };
PinRange analogPinRanges[] = { {54, 69}, {38, 40}, {18, 19} };
// ... other pin range definitions
PinGroup pinGroups[] = {
{"Digital Output", digitalOutputRanges, ARRAY_COUNT(digitalOutputRanges)},
{"Analog Pins", analogPinRanges, ARRAY_COUNT(analogPinRanges)},
// ... other groups
};
EthernetServer server(80);
void handleGETRequest(EthernetClient &client) {
StaticJsonDocument<8192> doc;
JsonArray lampArray = doc.createNestedArray("lamp");
// Iterate through each pin group.
for (int g = 0; g < ARRAY_COUNT(pinGroups); g++) {
JsonObject groupObj = lampArray.createNestedObject();
groupObj["header"] = pinGroups[g].name;
JsonArray pinsArray = groupObj.createNestedArray("pins");
// For each range in the group
for (int r = 0; r < pinGroups[g].rangeCount; r++) {
for (int p = pinGroups[g].ranges[r].startPin; p <= pinGroups[g].ranges[r].endPin; p++) {
JsonObject pinObj = pinsArray.createNestedObject();
pinObj["name"] = "";
pinObj["pin"] = p;
pinObj["value"] = digitalRead(p);
}
}
}
// Send response with JSON data
client.print("HTTP/1.1 200 OK\r\nContent-Type: application/json; charset=utf-8\r\nConnection: keep-alive\r\n\r\n");
serializeJson(doc, client);
}
My questions are:
Could the incomplete JSON response be due to memory limitations? I'm using a StaticJsonDocument of size 8192. Is that sufficient, or could it be cutting off data if the JSON payload becomes too large?
Is there any issue with the way I'm reading from the client or sending the response that might result in truncated data? I use a basic loop to read the HTTP request line and then process the GET/POST accordingly.
Are there any best practices for managing such large JSON documents on an Arduino/Controllino Mega? Any advice or pointers on debugging such issues would be very helpful.
I've tried debugging by printing out serial logs to verify that all iterations run as expected, but the issue only appears in the JSON response sent to Postman.
Any insights or suggestions on how to get a complete JSON response would be greatly appreciated!
Additional Info of the controllino Mega: /