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).
http://www.atasoyweb.net/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 :
aà
ÀgäqàÇ4+©ðo`ààÄTÝO¶øÞ etc....
Any idea ?
Thank you all.