Go Down

Topic: Serve graphics with Eithernet Uno? (Read 1 time) previous topic - next topic

TheNorm

Should I be able to serve .jpg files? I can send text no sweat but jpg files just grind and grind and eventually time-out.

PaulS

Quote
Should I be able to serve .jpg files?

But of course. A jpg file is just a binary data file. Nothing magic about it.

Quote
I can send text no sweat but jpg files just grind and grind and eventually time-out.

So, you can store text in SRAM and send it. Where are you storing this jpg file? There is only 2K of SRAM on most Arduinos. Hardly enough to store any data that will take any time at all to send.

Most likely, its a coding issue. Hint, hint.

TheNorm

#2
Apr 11, 2012, 10:10 pm Last Edit: Apr 11, 2012, 10:20 pm by TheNorm Reason: 1
I'm using the SD card. No shortage of room.

I send an html header & file and it works fine. Send a jpg header and file and it doesn't work.

Nick Gammon

Better take PaulS's hint and show your code, huh?

TheNorm

#4
Apr 12, 2012, 02:48 am Last Edit: Apr 17, 2012, 09:49 am by TheNorm Reason: 1
Ok, I snipped out the relevant stuff.

The jpg is 35.5 KB.

Here's the calling code...
 if (strcmp(ext,"jpg")==0){
   sendHeader("jpg.hdr", client;
   sendFile(parameter, client);
   return;
 }


Here's the text from the file jpg.hdr
HTTP/1.1 200 OK
Content-Type: image/jpeg


Code to send header...
void sendHeader(char *fileName, EthernetClient *client){  // no CGI parsing
 DEBUG_PRINTLN("sending header " << fileName << "\n\n" );
 sendFile(fileName,client);
 client->print ("\n\n");  // terminator for http headers
}


Code to actually send...
void sendFile(char *fileName, EthernetClient *client){
 DEBUG_PRINTLN("sending file " << fileName << "\n" );
 File source = openFile(fileName);
 while (source.available()) {
   byte byt = source.read();
   client->write(byt);
 }
 source.close();
}




TheNorm

Is this thread dead for some reason?

Nick Gammon

1. What does the debugging show?

2. Can you use code tags to post code? You can do that by hitting the # button above the posting area.

3. After this line:

Code: [Select]
 File source = openFile(fileName);

You don't check if the file opened OK. If not (and source is not open) then the program will likely hang.

TheNorm


1. What does the debugging show?

2. Can you use code tags to post code? You can do that by hitting the # button above the posting area.

3. After this line:

Code: [Select]
 File source = openFile(fileName);

You don't check if the file opened OK. If not (and source is not open) then the program will likely hang.


"openFile(fileName)" has error checking built in. Currently it prints an error msg and loops forever.

Code: [Select]
Ok. I'll do this in the future.

As far as I can tell this code is ok and it sends html just fine.

The debug output just says "sending header  htm.hdr" and  "sending file index.htm"  and then when the client asks for the graphics... ""sending header  jpg.hdr" and  "sending file test.jpg"  but the graphics never arrive.

Nick Gammon

Hmmm- are you base64 encoding the graphics file?

FRiC

You might be sending too fast and overflowing the buffer. Try putting in a delay inside the client write loop.

TheNorm


Hmmm- are you base64 encoding the graphics file?


It's just a jpg file created in Photoshop that I copied to the SD card. I didn't do anything to it. I don't understand the question. It sounds like I may be missing a whole topic here. What's base64 encoding? Why would anyone do anything in base 64? Is there something I should have read?


TheNorm


You might be sending too fast and overflowing the buffer. Try putting in a delay inside the client write loop.


Tried that.
Thanks

Nick Gammon


What's base64 encoding? Why would anyone do anything in base 64? Is there something I should have read?


Yesss.

http://en.wikipedia.org/wiki/Base64

Your original question:

Quote
Should I be able to serve .jpg files?


... was perhaps a bit vaguely worded. You can send any file you like, but the HTTP transfer protocol is basically text. You can send other things, but they are usually base-64 encoded, which effectively turns them into text.

I suppose we assumed that since you are generating HTML pages you might know that. Ah well.

Basically you run the file (in batches) through the base64-encoder, which makes it 4/3 times larger. I think you usually do the data in batches of around 57 bytes which become 76 bytes when encoded (ie. the length of a line in an email message).

You will also need to look at the header, I think you need to tell the browser that the text is base64-encoded.

Something like this line in addition to what you have:

Code: [Select]
Content-Transfer-Encoding: base64

An example, from an email I got:

Code: [Select]
Content-Type: image/png;
name="image001.png"
Content-Transfer-Encoding: base64
Content-ID: <image001.png@01CD20C6.92DC0A80>

iVBORw0KGgoAAAANSUhEUgAAAoAAAAPACAYAAACl4sj7AAAAAXNSR0IArs4c6QAAAARnQU1BAACx
jwv8YQUAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAA/45JREFU
eF7sfQWYXdW5dv7/v6XtvXWlSItDaYFCW2rU/daNGhUoUqS4exIsBEhCQoQQd3d318lkfDIT9yBB


Something roughly along those lines. Look it up, there will be lots of references. And you should find a base-64 encoding function easily enough.

TheNorm

Thanks Nick.

Sigh. I think I'll just eliminate graphics from my server. It's not critical and the thing is already too fat. More code is not the way to go and I don't care for the idea of pre-converting the files.

Chagrin

Nick, image files are not base64 encoded in normal HTTP transmissions. Mail messages with MIME attachments are a completely different animal.

Example for a random 1x1 pixel image on yahoo.com; the image data is on the last line and the "GIF89a" header present in any gif image is clearly visible, unencoded.

root@zygot:~# telnet l.yimg.com 80
Trying 98.137.80.49...
Connected to fo-anyycs-l.ay1.b.yahoodns.net.
Escape character is '^]'.
GET /a/i/mntl/ww/events/p.gif HTTP/1.0
Host: l.yimg.com

HTTP/1.0 200 OK
Date: Wed, 25 Apr 2012 19:02:39 GMT
Cache-Control: max-age=315360000
Expires: Sat, 23 Apr 2022 19:02:39 GMT
Last-Modified: Tue, 05 Oct 2010 18:34:21 GMT
Accept-Ranges: bytes
Content-Length: 43
Content-Type: image/gif
Age: 14636
Server: YTS/1.20.14

GIF89aÀÀÀ!ù,D;

Go Up