Hi everybody,
Im trying to build a webserver with data from temperature sensors. For the web server I used code from this page
As it is it works fine on my arduino with ethernet shield. Then I start to add my code for reading sensors (works perfectly on its own when information are send only to serial monitor).
Here is my code:
#include <SPI.h>
#include <Ethernet.h>
// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 5); // IP address, may need to change depending on network
EthernetServer server(80); // create a server at port 80
String HTTP_req; // stores the HTTP request
#include <dht11.h> //library for DHT11
#define DHT11PIN 2 //digital pin to read DHT11 ( # way of defining constants takes no memory)
dht11 DHT11; //define variable DHT11 of type dht11
#define LMPin A0 // analog pin to read LM35
#define numReadings 50 //number of readings for LM35
float lm35tmp = 100;
float mintemp[8], maxtemp[8]; //arrays to keep extremas
long meastimer; //timer
int measint = 2000;
void setup()
{
Ethernet.begin(mac, ip); // initialize Ethernet device
server.begin(); // start to listen for clients
Serial.begin(9600); // for diagnostics
analogReference(INTERNAL); //changes Aref to 1.1V for better accuracy on LM35
// for (int i = 0; i < 8; i++) { mintemp[i] = 100; maxtemp[i] = 0;}
meastimer =millis();
delay(measint); // give the sensor and Ethernet shield time to set up
}
void DHT11temperature() { // function to obtain temperature and humidity from DHT11 sensor
int chk = DHT11.read(DHT11PIN);
// Serial.print("Read sensor: ");
switch (chk)
{
case DHTLIB_OK:
Serial.print("DHT11...");
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.println("DHT11 Checksum error...");
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.println("DHT11 Time out error...");
break;
default:
Serial.println("DHT11 Unknown error...");
break;
}
}
//function that returns temperature of LM35 in degrees C - takes 50ms
float LM35temperature()
{ float LMtotal =0;
for (int index =0; index < numReadings; index++)
{
LMtotal= LMtotal + analogRead(LMPin);
delay(1);
}
// calculate the average:
float LMaverage = LMtotal / numReadings;
//compute average voltage on LM35
float LMtemp = (LMaverage /1024.0)*111 ;
Serial.print("LM35...");
return LMtemp;
}
void loop()
{
// check for a reading no more than once a second.
if (millis() - meastimer > measint){
Serial.print("Reading temperatures...");
// DHT11temperature();
// lm35tmp = LM35temperature();
Serial.println("done");
meastimer = millis();
}
// listen for incoming Ethernet connections:
listenForEthernetClients();
}
void listenForEthernetClients() {
EthernetClient client = server.available(); // try to get client
if (client) { // got 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
HTTP_req += c; // save the HTTP request 1 char at a time
// last line of client request is blank and ends with \n
// respond to client only after last line received
if (c == '\n' && currentLineIsBlank) {
Serial.println(HTTP_req);
Serial.println(HTTP_req.indexOf("ajax_switch"));
// 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();
// AJAX request for switch state
if (HTTP_req.indexOf("ajax_switch") > -1) {
// read switch state and send appropriate paragraph text
Serial.println("ajax_switch contained");
GetSwitchState(client);
}
else { // HTTP request for web page
Serial.println("ajax_switch NOT contained");
// send web page - contains JavaScript with AJAX calls
client.println("<!DOCTYPE html>");
client.println("<html>");
client.println("<head>");
client.println("<title>Arduino Web Page</title>");
client.println("<script>");
client.println("function GetSwitchState() {");
client.println("nocache = \"&nocache=\"\
+ Math.random() * 1000000;");
client.println("var request = new XMLHttpRequest();");
client.println("request.onreadystatechange = function() {");
client.println("if (this.readyState == 4) {");
client.println("if (this.status == 200) {");
client.println("if (this.responseText != null) {");
client.println("document.getElementById(\"switch_txt\")\
.innerHTML = this.responseText;");
client.println("}}}}");
client.println(
"request.open(\"GET\", \"ajax_switch\" + nocache, true);");
//client.println("request.open(\"GET\", \"ajax_switch\", true);");
client.println("request.send(null);");
client.println("}");
client.println("</script>");
client.println("</head>");
client.println("<body>");
client.println("<h1>Arduino AJAX Switch Status</h1>");
client.println(
"<p id=\"switch_txt\">Switch state: Not requested...</p>");
client.println("<button type=\"button\"\
onclick=\"GetSwitchState()\">Get Switch State</button>");
client.println("</body>");
client.println("</html>");
}
// display received HTTP request on serial port
//Serial.print(HTTP_req);
HTTP_req = ""; // finished with request, empty string
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)
}
// send the state of the switch to the web browser
void GetSwitchState(EthernetClient cl)
{
cl.println(millis());
}