The people's republic of Massachusetts
Offline
Full Member
Karma: 0
Posts: 121
|
 |
« on: January 22, 2012, 10:05:22 pm » |
I am working with a Ethernet shield and want to add some variables to lines of text and HTML / Java Code. This is what I am doing and it works but... client.println("<p>The phone number of John Smith is "); client.println(phone_number); client.println("</p>"); In PHP I would do this like so... So how do I do it with Arduino? client.println("<p>The phone number of John Smith is ".$phone_number."</p> "); Can it be done?
|
|
|
|
|
Logged
|
|
|
|
|
Austin, TX
Offline
Faraday Member
Karma: 41
Posts: 5176
CMiYC
|
 |
« Reply #1 on: January 22, 2012, 10:17:22 pm » |
you need to use sprintf() and a pre-defined character buffer.
However, keep in mind the limited amount of RAM of the arduino makes strings like you suggest problematic. Large char const* like those get copied into RAM and cause problems.
|
|
|
|
|
Logged
|
|
|
|
|
The people's republic of Massachusetts
Offline
Full Member
Karma: 0
Posts: 121
|
 |
« Reply #2 on: January 22, 2012, 10:21:07 pm » |
So I should stick with what I have sine I have LOT more other lines of code also? I don't mind doing it like I show in the first example but it does make editing the HTML code a PITA. 
|
|
|
|
|
Logged
|
|
|
|
|
Austin, TX
Offline
Faraday Member
Karma: 41
Posts: 5176
CMiYC
|
 |
« Reply #3 on: January 22, 2012, 10:25:03 pm » |
That's not at all what I'm saying. Having a lot of lines like: client.println("<p>The phone number of John Smith is "); Consumes RAM, unless you are very careful. That line is 40 bytes of the 2048 of RAM you have available...
|
|
|
|
|
Logged
|
|
|
|
|
The people's republic of Massachusetts
Offline
Full Member
Karma: 0
Posts: 121
|
 |
« Reply #4 on: January 22, 2012, 10:28:29 pm » |
I under stand what you are saying about the RAM, does it free it back up after it prints the line or save it till it does the entire code. If it does the entire code I will not be able to do it since the HTML code is several hundred lines and most of them will have variables.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
God Member
Karma: 0
Posts: 503
|
 |
« Reply #5 on: January 22, 2012, 10:31:12 pm » |
Any label you generate occupies memory, it could be forced into the program space using PROGMEM then read out of the program memoryspace.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
God Member
Karma: 0
Posts: 503
|
 |
« Reply #6 on: January 22, 2012, 10:32:51 pm » |
I too have had to get used to the thimble sized memory here that I had in masses in windows
|
|
|
|
|
Logged
|
|
|
|
|
The people's republic of Massachusetts
Offline
Full Member
Karma: 0
Posts: 121
|
 |
« Reply #7 on: January 22, 2012, 10:38:41 pm » |
Ok I will keep the memory problem in mind, but my issue at the moment is how to stick a variable in line wit the other code. As shown above, Anyone got any way to do that?
|
|
|
|
|
Logged
|
|
|
|
|
Austin, TX
Offline
Faraday Member
Karma: 41
Posts: 5176
CMiYC
|
 |
« Reply #8 on: January 22, 2012, 10:55:02 pm » |
Ok I will keep the memory problem in mind, but my issue at the moment is how to stick a variable in line wit the other code. As shown above, Anyone got any way to do that?
uh? I already told you. sprintf()
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 219
Posts: 13896
Lua rocks!
|
 |
« Reply #9 on: January 22, 2012, 10:57:47 pm » |
I could tell you that (and James already did) but bear in mind you probably need to print literals differently from variables for the memory reasons discussed. ... does it free it back up after it prints the line or save it till it does the entire code ... String literals (like "foo") are copied from the program memory into RAM. They are never freed. However if you use the PROGMEM library, you can copy them a byte at a time, and only therefore use a total of one byte of RAM to hold them.
|
|
|
|
|
Logged
|
|
|
|
|
The people's republic of Massachusetts
Offline
Full Member
Karma: 0
Posts: 121
|
 |
« Reply #10 on: January 22, 2012, 11:04:10 pm » |
Ok, I get it now  Now I understand why you were bringing up the RAM problem, I did not look into how sprintf() solved the problem till after my last post. Once I did I see why it will not work for what I need (in total) Thanks!
|
|
|
|
|
Logged
|
|
|
|
|
West Des Moines, Iowa USA
Offline
Sr. Member
Karma: 2
Posts: 429
|
 |
« Reply #11 on: January 22, 2012, 11:50:59 pm » |
One viable approach might be to do something like the following: void putline(unsigned fmt,...) { static char format[MAXFMT]; static char buffer[MAXLIN];
/* Retrieve format string fmt from FLASH into 'format' */
/* Assemble print line in 'buffer' */
Serial.println(buffer); } if you made '#' the substitution character, fmt == 0 corresponds to "<p>The phone number of # is #</p>", name = "John Smith", and phone = "123-456-7890", then the call putline(0,name,phone); would result in retrieving the appropriate format string, performing the two substitutions, and outputting the text line you want, ie: <p>The phone number of John Smith is 123-456-7890</p>
|
|
|
|
|
Logged
|
There's always a better way!
|
|
|
|
Seattle, WA
Offline
God Member
Karma: 4
Posts: 673
|
 |
« Reply #12 on: January 23, 2012, 01:04:30 am » |
Or you could just set up printf, and use the normal idioms! int serial_putc( char c, FILE * ) { Serial.write( c ); return c; }
void setup(void) { Serial.begin(57600); fdevopen( &serial_putc, 0 ); printf("Lots of %u numbers %u everywhere %S!",1,2,PSTR("And strings too!")); }
Easy to extend this to printing out to the Ethernet client too.
|
|
|
|
|
Logged
|
|
|
|
|
West Des Moines, Iowa USA
Offline
Sr. Member
Karma: 2
Posts: 429
|
 |
« Reply #13 on: January 23, 2012, 03:26:29 am » |
I took a couple of minutes to play with 02660's problem and wrote this test code to run on my desktop:
(non-working code deleted)
I thought I was being more helpful than I was. I got help with the problems I was having (in a separate thread: Compiling data into program memory...) and posted the working version of the deleted code there.
|
|
|
|
« Last Edit: January 23, 2012, 03:21:44 pm by Morris Dovey »
|
Logged
|
There's always a better way!
|
|
|
|
|