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:
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,101); // IP address, may need to change depending on network
EthernetServer server(80); // create a server at port 80
File pageFile;
String HttpReq = String();
void setup()
{
Serial.begin(9600);
// initialize SD card
Serial.println("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("ERROR - SD card initialization failed!");
return; // init failed
}
Serial.println("SUCCESS - SD card initialized.");
// check for index.htm file
if (!SD.exists("index.htm")) {
Serial.println("ERROR - Can't find index.htm file!");
return; // can't find index file
}
Serial.println("SUCCESS - Found index.htm fileeee.");
pageFile = SD.open("index.htm", FILE_READ);
if (pageFile)
{
Serial.println("SETUP: Reading file...");
char c;
while(pageFile.available())
{
c = pageFile.read();
Serial.print(c);
}
Serial.println("SETUP: End of file reading - OK");
} else Serial.println("SETUP: Can't read file");
pageFile.close();
Ethernet.begin(mac, ip); // initialize Ethernet device
server.begin(); // start to listen for clients
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
void loop()
{
EthernetClient client = server.available(); // try to get client
if (client)
{ // got client?
Serial.println("New client:");
boolean currentLineIsBlank = true;
while (client.connected())
{
if (client.available())
{ // client data available to read
char c = client.read(); // read 1 byte (character) from client
// last line of client request is blank and ends with \n
// respond to client only after last line received
HttpReq += c;
Serial.print(c);
if (c == '\n' && currentLineIsBlank) //end of client request, now have to send server response
{
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: keep-alive");
client.println();
// send web page
Serial.print("Position of ajax_down: ");
Serial.println(HttpReq.indexOf("ajax_down"));
if (HttpReq.indexOf("ajax_down") > -1)
{
Serial.println("!!!!! Here should be ajax_down string:");
Serial.println(HttpReq);
Serial.println("!!!!! END");
GetResults(client);
}
else //it wasn't ajax_down request
{
pageFile = SD.open("index.htm", FILE_READ);
if (pageFile)
{
Serial.println("Reading...");
while(pageFile.available())
{
char c = pageFile.read();
client.print(c);
Serial.print(c);
}
pageFile.close();
}
else
{
Serial.println("Can't read file!");
client.print("Can't get results!");
}
}
HttpReq = "";
break;
}
// every line of text received from the client ends with \r\n
if (c == '\n') {
// last character on line of received text
// starting new line with next character read
currentLineIsBlank = true;
}
else if (c != '\r') {
// a text character was received from client
currentLineIsBlank = false;
}
} // end if (client.available())
} // end while (client.connected())
delay(1); // give the web browser time to receive the data
client.stop(); // close the connection
} // end if (client)
}
void GetResults(EthernetClient cl)
{
Serial.print("Getting results...");
int temperatury = 0;
for (int i=0; i<20; i++)
{
temperatury += analogRead(0);
}
int odczytTemperatury = (temperatury/20)*(5000/1023);
int odczytSwiatla = analogRead(5);
cl.print(odczytTemperatury);
cl.print(" ");
cl.print(odczytSwiatla);
Serial.println("OK");
}
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 (http://arduino.cc/en/Reference/StringIndexOf).
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!**
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
**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.