Need help for Ethernet ENC28J60 and small buffer size optimization

Hi,

Due to a very small amount of SRAM on arduino UNO, I try to implement this upgrade of the "etherCard Library" by "Yayın tarihi" in order to manage big pages with small buffers.

It's OK for receiving large pages.

But I do not succeed sending a JSON string (in response of a $.ajax request targeting the arduino).

Modifying the EtherCard Library To Handle Large TCP Packets.

The following code is working fine but this is a static page, I would like to add dynamic values.

char *page = (char *)PSTR("HTTP/1.0 200 OK\r\n"
"Content-Type: application/json;charset=utf-8\r\n"
"Access-Control-Allow-Origin: *\r\n"
"Server: Arduino\r\n"
"Pragma: no-cache\r\n"
"Connnection: close\r\n"
"\r\n"
"{"led":"0","
""door":"0","
""lux":"500","
""time":"8:12","
""date":"1/5/2016","
""temp":"19"}");

ether.fillAndSend(page, sizeof(page));
ether.finalizeConn();

This code fails :

// LECTURE DES CAPTEURS ET ETATS
short state = digitalRead(LED_PIN); // Lecture état de la led
short valeurLux = analogRead(LUX_PIN); // Lecture capteur luminosité
short temp = DS3231_get_treg(); // Lecture température module RTC

// OBTIENT L'HEURE COURANTE
struct ts t;
DS3231_get(&t);

Serial.println(F(""));
Serial.println(F("RETURNED..."));
Serial.println(F("
"));

const char *chaine = PSTR("HTTP/1.0 200 OK\r\n"
"Content-Type: application/json;charset=utf-8\r\n"
"Access-Control-Allow-Origin: *\r\n"
"Server: Arduino\r\n"
"Pragma: no-cache\r\n"
"Connnection: close\r\n"
"\r\n"
"{"led":"%d","
""door":"%d","
""lux":"%d","
""time":"%d:%d","
""date":"%d/%d/%d","
""temp":"%d"}");

size_t sz = snprintf_P(NULL, 0, chaine, state, etatFinCourse, valeurLux, t.hour, t.min, t.mday, t.mon, t.year, temp);
char *buf = (char *)malloc(sz + 1);
snprintf_P(buf, sz + 1, chaine, state, etatFinCourse, valeurLux, t.hour, t.min, t.mday, t.mon, t.year, temp);

Serial.println(buf);
ether.fillAndSend(buf, sizeof(buf));
ether.finalizeConn();
free(buf);
buf = NULL;

When I look for the response in "Chrome", I can see strange caracters instead of my Json page :


Àgäqàǔ4‰+©ð”o`à‚àÄTÝO¶ø”Þ etc....

Any idea ?

Thank you all.

EssaiEthernet.ino (7.79 KB)

one of the options that you have is to split the string and send it in parts. You can then use the Progmem to store the fixed strings and only build the part that is dynamic.

You could also consider using the PString library to build up the buffer contents instead of using snprintf_P

Cheers Pete.

Thank you for your reactivity Bainesbunch. I will have a look at the "Pstring" library.

:slight_smile:

Hi Zabuel,

Also look, as Bainesbunch recommends, into storing your constant / fixed strings in PROGMEM. The Arduino site has a lot of good references and tutorials on it (PROGMEM). It's pretty easy and can save a lot of your precious RAM.

Pat.

Thank you Patduino.

I will try this evening and give some news.

I finally fixed my problem.

The use of "PString" library was helpful .

Thank you all.

I can go ahead now. :slight_smile: