最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

arduino - Incomplete JSON Response from API on Controllino Mega When Using Postman - Stack Overflow

programmeradmin1浏览0评论

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:

  1. 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?

  2. 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.

  3. 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: /

发布评论

评论列表(0)

  1. 暂无评论