Hi guys !
So i'm working on a arduino project including multiples sensors, LCD screen etc... And i'm trying to display the values of the sensors on a webserver hosted by my arduino (with a shield RJ45). I got a SD card plugged in my shield which contains basically 2 files :
- index.htm containing the webpage which displays the values contained in data.js.
- data.js containing data of the sensors in JSON format collected by index.htm (using JSON.parse).
The HTML page collect the JSON object correctly when i run those files on wampserver.
But when i open index.htm on my arduino webserver it shows this error message on the console section of the webpage : "Failed to load resource: net::ERR_EMPTY_RESPONSE".
I really dont understand why it doesn't read the file...
the coded part of the index.htm file reading the json object from data.js:
<script>
function readJson(){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function(){
if(this.readyState==4 && this.status==200){
var temp,humidity,pressure,luminosity;
var data = JSON.parse(this.responseText);
console.log(data);
temp = data.temperature;
humidity = data.humidity;
pressure = data.pressure;
luminosity = data.luminosity;
document.getElementById("temp").innerHTML=temp;
document.getElementById("humidity").innerHTML=humidity;
document.getElementById("pressure").innerHTML=pressure;
document.getElementById("luminosity").innerHTML=luminosity;
}
};
xhttp.open("GET","DATA.JS",true)
xhttp.send();
console.log("readJson called");
}
window.onload=readJson;
</script>
the coded part of the webserver.ino file (in the loop function) launching the index.htm file on the server
EthernetClient client = server.available(); // try to get client
if (client) { // got client?
Serial.println("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
// buffer first part of HTTP request in HTTP_req array (string)
// leave last element in array as 0 to null terminate string (REQ_BUF_SZ - 1)
if (req_index < (REQ_BUF_SZ - 1)) {
HTTP_req[req_index] = c; // save HTTP request character
req_index++;
}
// print HTTP request character to serial monitor
Serial.print(c);
// last line of client request is blank and ends with \n
// respond to client only after last line received
if (c == '\n' && currentLineIsBlank) {
// open requested web page file
if (StrContains(HTTP_req, "GET / ")
|| StrContains(HTTP_req, "GET /index.htm")) {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connnection: close");
client.println();
webFile = SD.open("index.htm"); // open web page file
}
else if (StrContains(HTTP_req, "GET /data.js")) {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connnection: close");
client.println();
webFile = SD.open("data.js"); // open web page file
}
if (webFile) {
while(webFile.available()) {
client.write(webFile.read()); // send web page to client
}
webFile.close();
}
// reset buffer index and all buffer elements to 0
req_index = 0;
StrClear(HTTP_req, REQ_BUF_SZ);
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)
I dont have the best english, sorry if there is any missundertstanding ! If you got some questions about the project you can feel free to ask them ![]()
Thanks in advance for your (potential) help !