Thanks, it is working almost fine, following the code:
char buffer[500];
int charCnt;
file.open(&root, "arduino.htm", O_READ);
while ((charCnt = file.read(buffer, 499)) > 0) {
buffer[charCnt] = '\0';
client.write(buffer);
}
I also changed the sketch to another that fill the memory for 23000 bytes compared with previous of 29000.
I used the sketch in this page Arduino Tutorials - Ethernet+SD, but I'm experiencing an issue when I have to respond a request of json string, I post the code:
#include "SPI.h"
#include "Ethernet.h"
#include "SD.h"
#define NAMELEN 32
#define VALUELEN 32
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 210 };
EthernetServer server(80);
char buffer[500];
int charCnt;
Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;
typedef enum URLPARAM_RESULT { URLPARAM_OK, URLPARAM_NAME_OFLO, URLPARAM_VALUE_OFLO, URLPARAM_BOTH_OFLO,URLPARAM_EOS };
// store error strings in flash to save RAM
#define error(s) error_P(PSTR(s))
void setup() {
Serial.begin(9600);
PgmPrint("Free RAM: ");
Serial.println(FreeRam());
// initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
// breadboards. use SPI_FULL_SPEED for better performance.
pinMode(10, OUTPUT); // set the SS pin as an output (necessary!)
digitalWrite(10, HIGH); // but turn off the W5100 chip!
if (!card.init(SPI_HALF_SPEED, 4)) error("card.init failed!");
// initialize a FAT volume
if (!volume.init(&card)) error("vol.init failed!");
PgmPrint("Volume is FAT");
Serial.println(volume.fatType(),DEC);
Serial.println();
if (!root.openRoot(&volume)) error("openRoot failed");
// list file in root with date and size
PgmPrintln("Files found in root:");
root.ls(LS_DATE | LS_SIZE);
Serial.println();
// Recursive list of all directories
PgmPrintln("Files found in all dirs:");
root.ls(LS_R);
Serial.println();
PgmPrintln("Done");
// Debugging complete, we start the server!
Ethernet.begin(mac, ip);
server.begin();
}
void loop()
{
char clientline[BUFSIZ];
int index = 0;
char name[NAMELEN];
char value[VALUELEN];
EthernetClient client = server.available();
if (client) {
// an http request ends with a blank line
boolean current_line_is_blank = true;
// reset the input buffer
index = 0;
while (client.connected()) {
if (client.available()) {
char c = client.read();
// If it isn't a new line, add the character to the buffer
if (c != '\n' && c != '\r') {
clientline[index] = c;
index++;
// are we too big for the buffer? start tossing out data
if (index >= BUFSIZ)
index = BUFSIZ -1;
// continue to read more data!
continue;
}
// got a \n or \r new line, which means the string is done
clientline[index] = 0;
// Print it out for debugging
//Serial.println(clientline);
// Look for substring such as a request to get the root file
if (strstr(clientline, "GET / ") != 0) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
file.open(&root, "arduino.htm", O_READ);
while ((charCnt = file.read(buffer, 499)) > 0) {
// uncomment the serial to debug (slow!)
//Serial.print((char)c);
buffer[charCnt] = '\0';
client.write(buffer);
}
file.close();
} else if (strstr(clientline, "GET /") != 0) {
char *filename;
char *url_param;
url_param = strstr (clientline,"?") + 1;
(strstr(url_param, " HTTP"))[0] = 0;
// filename = clientline + 16;
// (strstr(clientline, " HTTP"))[0] = 0;
int rc;
char parametri[4];
if (strlen(url_param))
{
int j = 0;
while (strlen(url_param))
{
rc = decodeURLparam(&url_param, name, NAMELEN, value, VALUELEN);
if (rc != 0)
{
parametri[j] = value[0];
j = j + 1;
}
}
}
writeIO(parametri[0], (int)parametri[1], (int)parametri[2]);
**********| THIS PART SEEMS TO HAVE SOME PROBLEM |**********************
//client.println("HTTP/1.1 200 OK");
//client.println("Content-Type: text/html");
//client.println();
client.println("{di:[{pin:2,v:0},{pin:3,v:1},{pin:4,v:1}]}");
*************************************************************
} else {
// everything else is a 404
client.println("HTTP/1.1 404 Not Found");
client.println("Content-Type: text/html");
client.println();
client.println("<h2>File Not Found!</h2>");
}
break;
}
}
// give the web browser time to receive the data
delay(1);
client.stop();
}
}
When I request the page by browser I receive the right response, but from an AJAX call ajax script cannot connect to that page. If I copy the page content in a text file and try from ajax to connect is working fine. In my opinion the content type of the response should be configured properly but if I remove the comments from the following lines
lient.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
and I try to connect by browser I don't receive anything, blank page.
I tryed to change the content-type (text/html, text/json, application/json, text/plain), nothing change.