2009-09-13

Arduino Home Monitor

Last week I received my Arduino-Ethernet-shield, although I couldn't use my Decimillia with it (due to a compiler problem), I went ahead with my old NG in order to try it out.
I've build a simple system with a LM35 temperature sensor connected to analog input 0(zero) through a low pass filter (100K/ 100nF) , to measure the ambient temperature.
To measure the ambient lighting I connected to the analog input 1 to the middle point of a simple LDR circuit (LDR in series with a 10K resistor, middle point connected to analog input).

The ethernet shield was the first one issued, based on the W5100 ethernet TCP adaptor. Some of the TCP stack is processed in the W5100 so the Arduino spends less time processing the packets and has some more time to do what you want him to do. Also the libraries included in the Arduino IDE work "out of the box". I added my "breadboard" shield on top of the ethernet shield plug in and went on to try it out.


My program is based on the original "ethernet/analog input" example but I had to do some calibration and conversion from the sensors.
I also tried to change the analogReference during runtime, but I didn't get good results. The LM35CZ output will change from 200mV to 300mV, for 20 degrees centigrade to 30 C respectively, therefore I would get the best of the ADC resolution if I used the INTERNAL reference of 1.1V. For some reason the values were not stable, so I abandoned it and used the 5V reference (DEFAULT) for both measures.
I also tried to "correct" the HTML output so that it could be read by a friend's Freecom MusicPal, but we never made it. I need to learn a bit more of HTML parsing and building the page.
Here is the code:

/*
* Web Server
*
* A simple web server that shows the value of the analog input pins.
*/

#include <ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 2, 253 };
//byte gateway[] = { 192, 168, 2, 254 };
//byte subnet[] = { 255, 255, 255, 0 };

Server server(80);

void setup()
{
Ethernet.begin(mac, ip);
server.begin();
}

void loop()
{
long tmp;

Client client = server.available();
if (client) {
// an http request ends with a blank line
boolean current_line_is_blank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
// if we've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so we can send a reply
if (c == '\n' && current_line_is_blank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("<HTML>");
client.println("<title>Arduino Home monitor</title>");
client.println("<BODY>");
// output the value of each analog input pin

client.print("Temperature is ");
analogReference(DEFAULT);
delay(10);
tmp=((long)analogRead(0)*5000)/1024;
client.print(tmp / 10);
client.print(".");
client.print(tmp % 10);
client.println("
");

analogReference(DEFAULT);
delay(10);
client.print("Light at home is ");
tmp=(long)analogRead(1)*1000/1024;
client.print(tmp/10);
client.print("%");
client.println("
");

client.println("<BODY>");
client.println("<HTML>");
break;
}
if (c == '\n') {
// we're starting a new line
current_line_is_blank = true;
} else if (c != '\r') {
// we've gotten a character on the current line
current_line_is_blank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
client.stop();
}
}

P.S. I had to change the > and < by the html code & g t ; and & l t ; so I don't know if the above code will cut/paste correctly.
Most of my problems were in fact related to the my provider's ADSL access box, it has only one fixed IP address available x.x.x.253 (it must be outside the DHCP range, which I cannot change), and then enabling the routing of port 80 from the outside world to the Arduino in the inside network.
With my IP address I can check the temperature and light levels (?) at home from any computer!(possibly later I could use a free dynamicdns)...
All this in the same weekend we place a new kitchen floor...
Uf... I though it never ended...

1 comment:

Unknown said...

So cool!! I will look forward to see more of it ;)