Arduino Ethernet Shield - can't read file from SD card

Hello!

I have no idea why my sketch doesn't work correctly. I want to build a server to get analog inputs values from my Arduino (with AJAX request). I have Ethernet Shield with SD reader. On my SD card i have "index.htm" file with content of my web page (it contains HTML and JavaScript with AJAX request sending function). This is my code(pasebin because of char limits on forum):

http://pastebin.com/B6C3sLvQ

In the setup() function I check if there is "index.htm" file on SD card and then I open it to print its content to Serial. I get the following result on COM8:

Initializing SD card...
SUCCESS - SD card initialized.
SUCCESS - Found index.htm fileeee.
SETUP: Reading file...
<!doctype html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>Arduino odczyt</title>
</head>
    <body>
    ...
    </body>
</html>SETUP: End of file reading - OK
server is at 192.168.1.101

when ... is my code. I also begin server in setup(). This is OK.

In loop() I wait for a client. If there's a client connected I read his request char by char:

char c = client.read(); // read 1 byte (character) from client
HttpReq += c;

When there's end of request (if (c == '\n' && currentLineIsBlank)) server starts to response. First it checks if it was "ajax_down" request: if (HttpReq.indexOf("ajax_down") > -1) and here I have my first problem: When HttpReq have no "ajax_down" string it returns 0 when it should return -1 (indexOf() - Arduino Reference). COM8 output when going to 192.168.1.101 on web browser:

New client:
GET / HTTP/1.1
Host: 192.168.1.101
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4

Position of ajax_down: 0
!!!!! Here should be ajax_down string:
GET / HTTP/1.1
Host: 192.168.1.101
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Ge
!!!!! END
Getting results...OK
New client:
GET /favicon.ico HTTP/1.1
Host: 192.168.1.101
Connection: keep-alive
Accept: */*
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4

Position of ajax_down: 0
!!!!! Here should be ajax_down string:
GET /favicon.ico HTTP/1.1
Host: 192.168.1.101
Connection: keep-alive
Accept: */*
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-La
!!!!! END
Getting results...OK

It behave as if there was "ajax_down" at the begin of HttpReq, but there isn't! Then it call GetResults(client); so in my browser window I can see two numbers (analog input values). But OK, let assume that it don't count from 0 but from 1, so when I change

if (HttpReq.indexOf("ajax_down") > -1)

for

if (HttpReq.indexOf("ajax_down") > 0)

It will get 'else' here:

else //it wasn't ajax_down request

and there is reading a file from SD card (the same code as in setup()!!!) but now I can't read the file (but first reading at setup() works). COM8:

New client:
GET / HTTP/1.1
Host: 192.168.1.101
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4

Position of ajax_down: 0
Can't read file!

And I get "Can't get results!" in my browser :confused:

I don't know why I can't open the file second time (it doesn't work also when I delete reading from setup()). Maybe it's caused by some memory leak? Binary size of the sketch is 24 568 bytes (max: 32 256) but I don't know if it have something to say in this case.

ServerSketch.ino (4.96 KB)

This is my code(pasebin because of char limits on forum):

I don't do rubbish bin. Below the gray box you typed in, there is a link, Additional Options...

Click on that to get the ability to add your code as an attachment.

HttpReq += c;

Using Strings is not advised.

PaulS, is clicking your mouse ONE additional time to view a piece of code THAT HARD?

PaulS:

HttpReq += c;

Using Strings is not advised.

Not advised for what? To remember a content of request? Because request can be long?

vulture2600:
PaulS, is clicking your mouse ONE additional time to view a piece of code THAT HARD?

Don't off top, please :slight_smile:

PaulS, is clicking your mouse ONE additional time to view a piece of code THAT HARD?

As a matter of fact, it's not just hard. It's impossible. My employer employs a proxy server that makes getting to rubbish bin, while at work, impossible.

I have had the same problem playing with eth_websrv_SD. I have found that by changing the file ending from .htm to .TXT everything works OK. It seems as if the arduino ethernet card does not like .htm.

The ethernet shield has no problem with any file type. I have tested most types with this code.
http://playground.arduino.cc/Code/WebServerST

Here are the file types it will handle:
HTM
PHP (edit: doesn't do the PHP part, just serves the file as a html doc)
TXT
CSS
GIF
JPG
JS
ICO
PNG
PDF
ZIP

You can add more if you need more. It is just a file server.