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):
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
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)