Go Down

Topic: Need help for Ethernet ENC28J60 and small buffer size optimization (Read 1 time) previous topic - next topic

Zabuel

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 :


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

Any idea ?

Thank you all.
Z@buel

Bainesbunch

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.
EmbeddedAT .. From Concept to Prototype to Production
Electronics and firmware design and project mentoring
I do answer personal requests for help when accompanied with a PayPal receipt ;)

Zabuel

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

 :)
Z@buel

patduino

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.
There are 10 types of people in the world, those who understand binary, and those that don't.

Zabuel

Thank you Patduino.

I will try this evening and give some news.
Z@buel

Zabuel

I finally fixed my problem.

The use of "PString" library was helpful .

Thank you all.

I can go ahead now. :)
Z@buel

Go Up