Error sending array > around 255 bytes

Dr pepper
Posts: 5
Joined: Tue Oct 15, 2019 9:15 am

Error sending array > around 255 bytes

Postby Dr pepper » Tue Oct 15, 2019 9:22 am

I modded the Espwho code so that it has an extra page that draws a google chart, the data for the chart is on a another page in the form of a json.
The chart auto updates with jquery every minute, the data table gets another value every minute too.
When the table gets around 255 bytes long the send command causes the Esp to crash, telling me the cause was httpd.
The data is stored in a char array.
The line I use to send the reply is:
httpd_resp_send(req, buf, strlen(buf));
Buf is a char array.
Any pointers?, I notice on some examples that a uint_t is sometimes used as the data type.

boarchuz
Posts: 559
Joined: Tue Aug 21, 2018 5:28 am

Re: Error sending array > around 255 bytes

Postby boarchuz » Tue Oct 15, 2019 5:13 pm

Some code would be helpful. From what you've given it sounds like buf is not null terminated so strlen is crapping out.

Dr pepper
Posts: 5
Joined: Tue Oct 15, 2019 9:15 am

Re: Error sending array > around 255 bytes

Postby Dr pepper » Wed Oct 16, 2019 4:34 pm

I was using string, and the code was a kludge.
I modded the code and did this, but the esp still crashes.
Just before I try to send the json i print it to the serial port, and it looks Ok, I added a '0' to the end of the json.

Code: Select all

static esp_err_t graph_data_handler (httpd_req_t *req) {

  char htmlResponse[35000];
  char *html = htmlResponse;
  html+=sprintf(html, "%s", "{\"time\":[");
  
  for (int i = 0 ; i < counters; i++) {
  html+=sprintf(html, "%ld", timeStamp[i]);
  if (i + 1 != counters) html+=sprintf(html, "%s", ',');
  }

  html+=sprintf(html, "%s", "],\"Itemp\":[");

  for (int i = 0 ; i < counters ; i++) {
  html+=sprintf(html, "%.2f", tempCdata[i]);
  if (i + 1 != counters) html+=sprintf(html, "%s",  ",");
  }
  
  html+=sprintf(html, "%s", "],\"lux\":[");

  for (int i = 0 ; i < counters ; i++) {
  html+=sprintf(html, "%.2f", ldata[i]);
  if (i + 1 != counters) html+=sprintf(html, "%s", ",");
  }
  
  html+=sprintf(html, "%s", "],\"press\":[");
  
  for (int i = 0 ; i < counters ; i++) {
  html+=sprintf(html, "%.2f", pressData[i]);
  if (i + 1 != counters) html+=sprintf(html, "%s", ",");
  }
  
  html+=sprintf(html, "%s", "],\"hum\":[");
  
  for (int i = 0 ; i < counters ; i++) {
  html+=sprintf(html, "%.2f", fdata[i]);
  if (i + 1 != counters) html+=sprintf(html, "%s", ",");
  }
  
  html+=sprintf(html, "%s", "]}\0");

  Serial.println (htmlResponse);
  
  httpd_resp_set_type(req, "application/json");
  httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
  httpd_resp_send(req, htmlResponse, strlen(htmlResponse));   //html.length());
  Serial.println ("Chart data sent");
  
}
And here is the serial including the errors:

{"time":[11],"Itemp":[0.00],"lux":[-2.00],"press":[0.00],"hum":[0.00]}
***ERROR*** A stack overflow in task httpd has been detected.
abort() was called at PC 0x400903f0 on core 1

Backtrace: 0x400901a8:0x3ffd0280 0x400903d9:0x3ffd02a0 0x400903f0:0x3ffd02c0 0x40093857:0x3ffd02e0 0x40095388:0x3ffd0300 0x4009533e:0xfefefefe

Rebooting...

The line with 'time' in it is the json repsonse, it looks Ok to me.

boarchuz
Posts: 559
Joined: Tue Aug 21, 2018 5:28 am

Re: Error sending array > around 255 bytes

Postby boarchuz » Thu Oct 17, 2019 7:16 am

char htmlResponse[35000];
Do you really need 35k for that little json?
Make it much much smaller or allocate dynamically.

Dr pepper
Posts: 5
Joined: Tue Oct 15, 2019 9:15 am

Re: Error sending array > around 255 bytes

Postby Dr pepper » Thu Oct 17, 2019 5:53 pm

Continuing from my last post.
I've been reading up on arduino json 5, and came up with this code that builds a json response in a buffer.
It prints to the serial port, and what I get is what I expect.

Code: Select all

#include <ArduinoJson.h>

  DynamicJsonBuffer jbuffer;
  JsonObject& jsonResponse = jbuffer.createObject();
  JsonArray& torqueArray = jbuffer.createArray();
  JsonArray& powerArray = jbuffer.createArray();

void setup() {
  Serial.begin(9600);
}

void loop() {
  if (Serial.read() == 'j') {

  torqueArray.add(1);
  torqueArray.add(2);
  powerArray.add(3);
  powerArray.add(4);

  printJson ();
  }
}

void printJson () {
  
  jsonResponse["Torque"] = torqueArray;
  jsonResponse["Power Kw"] = powerArray;
  jsonResponse.printTo(Serial);
  Serial.println();
}
And the serial:

{"Torque":[1,2],"Power Kw":[3,4]}

How do I get the server to send this as a json response, maybe - httpd_resp_send(req, jsonResponse, -1);

Thanks for your help.

Who is online

Users browsing this forum: No registered users and 61 guests