Introduction: Temperature and Humidity Datalogger + Webserver

In this instructable (my first instructable) we will create a temperature and humidity datalogger adding the posibility to access the data by web.

The new arduino ethernet makes everything sooooooo easy that it looks like magic. In the same board you get: an ATMega 328, ethernet conectivity, sd card

Only need a RTC (Real Time Clock) in order to have a full datalogger. I will plan to make another instructable about how to conect a RTC to this board.

You can connect to my own arduino server in order to see my server room temperature.

Step 1: The Basic Stuff


Lets me show how easy is to make a datalogger that saves sensor data in a SD card and allow remote access by web using any browser.

We will use three DHT11 sensor as source of data. Every DHT11 give us temperature and humidity data. They are really cheap (2€ iin ebay) and more or less acurrate ;-) : 2 Celsius degree and 5% of humidity. In the case you want more acurrate data, the DHT22 could be your election.

I apologize for using Celsius degrees. That is because I radicate in Spain. Changing the units to Fahrenheit degrees it is really easy.

Step 2: Connect Arduino to the Sensor

The sensor use a proper protocol, using one wire transmisition, mudulating the value in time, first you get the temperature, then the humidity value. It's something similar to pwm. The orthers used pins are Vdd and Gnd.

In order to facilitate the code we are going to use a library for DHT11 comunication. You can download the library from arduino-info.

To connect the sensors, you only have to connect all the Vdd pins to the 5+ and the Gnd pins to Gnd of arduino. In this example the data of every sensor is connected to the pins 2,3 and 4.

A basic program could be this one:

#include

dht11 DHT11;


void setup()
{
Serial.begin(9600);
}

void getdata(int iPuerto)
{
int chk = DHT11.read(iPuerto);

Serial.print("Sensor ");
Serial.print(iPuerto);
Serial.print(" ");
switch (chk)
{
case 0:
Serial.print((float)DHT11.humidity, 2);
Serial.print(" % ");
Serial.print((float)DHT11.temperature, 2);
Serial.println(" o C");
break;
case -1: Serial.println(" Checksum error"); break;
case -2: Serial.println(" Time out error"); break;
default: Serial.println(" Unknown error"); break;
}


}

void loop()
{
getdata(2);
getdata(3);
getdata(4);

delay(200);
}

I have trouble downloading the program using the current Arduino IDE 0022 under ubuntu. In order to solve then I have to modify the boards.txt file. An bug have been reported to the arduino comunity.

Step 3: Connect Arduino to Internet

Using the webserver example it is really easy to put your data on internet.

The previous Sketch modified could be in order to acept http request:


#include
#include
#include


dht11 DHT11;

#define nSensores 3
int puertos[nSensores];
float fHumedades[nSensores];
float fTemperaturas[nSensores];

int iNVisitas=0;

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xEE, 0xEE };
byte ip[] = { 192,168,1, 177 };

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
Server server(666);
void setup()
{
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();

Serial.begin(115200);
puertos={2,3,4};

}

void getdata(int iIndice)
{
int chk = DHT11.read(puertos[iIndice]);
fHumedades[iIndice]=-1;
fTemperaturas[iIndice]=-1;

Serial.print("Sensor ");
Serial.print(iIndice);
Serial.print(" ");
switch (chk)
{
case 0:
fHumedades[iIndice]=(float)DHT11.humidity;
Serial.print(fHumedades[iIndice], 2);
Serial.print(" % ");
fTemperaturas[iIndice]=(float)DHT11.temperature;
Serial.print(fTemperaturas[iIndice], 2);
Serial.println(" o C");
break;
case -1: Serial.println(" Checksum error"); break;
case -2: Serial.println(" Time out error"); break;
default: Serial.println(" Unknown error"); break;
}


}


void loop()
{
// listen for incoming clients
Client client = server.available();
if (client) {
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {

char c = client.read();
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == ' ' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
// output the value of each analog input pin
for(int i=0;i // for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
client.print("sensor ");
client.print(i);
client.print(": ");
if(fHumedades[i]==-1)
client.print(" error leyendo el sensor");
else
{
client.print(fHumedades[i], 2);
client.print(" % ");
client.print(fTemperaturas[i], 2);
client.println(" o C");
}
client.println("
");
}
client.print((iNVisitas++)/2);
client.println(" visitas
");

break;
}
if (c == ' ') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != ' ') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
}
else
{
for(int i=0;i getdata(i);
delay(200);
}
}

Where I am using my proper network configuration. In order to use your configuration, only change the mac and ip values.

You can connect to my own arduino server in order to see my server room temperature

Step 4: Save Data to a Sd Card

Saving the data to a sd card is really easy for the ehternet arduino.

The SD must be fat16 or fat32 formated

Only need to Including the sd library, initialize the sd library and open and write to a file

Here is the code:

#include
#include
#include
#include


dht11 DHT11;

#define nSensores 3
int puertos[nSensores];
float fHumedades[nSensores];
float fTemperaturas[nSensores];

int nFilas=0;
int nFiles=0;

File file;

int iNVisitas=0;

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xEE, 0xEE };
byte ip[] = { 192,168,1, 177 };

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
Server server(666);
void setup()
{
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();

Serial.begin(115200);
puertos={2,3,4};

pinMode(10, OUTPUT);
if (!SD.begin(4))
{
Serial.println("Error inicializando SD");
nFiles=-1;
}
else
{
nFiles=0;
Serial.println("SD initializada.");
}

}

void getdata(int iIndice)
{
int chk = DHT11.read(puertos[iIndice]);
fHumedades[iIndice]=-1;
fTemperaturas[iIndice]=-1;

Serial.print("Sensor ");
Serial.print(iIndice);
Serial.print(" ");
switch (chk)
{
case 0:
fHumedades[iIndice]=(float)DHT11.humidity;
Serial.print(fHumedades[iIndice], 2);
Serial.print(" % ");
fTemperaturas[iIndice]=(float)DHT11.temperature;
Serial.print(fTemperaturas[iIndice], 2);
Serial.println(" o C");
break;
case -1: Serial.println(" Checksum error"); break;
case -2: Serial.println(" Time out error"); break;
default: Serial.println(" Unknown error"); break;
}


}


void loop()
{
// listen for incoming clients
Client client = server.available();
if (client) {
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {

char c = client.read();
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == ' ' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
// output the value of each analog input pin
for(int i=0;i // for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
client.print("sensor ");
client.print(i);
client.print(": ");
if(fHumedades[i]==-1)
client.print(" error leyendo el sensor");
else
{
client.print(fHumedades[i], 2);
client.print(" % ");
client.print(fTemperaturas[i], 2);
client.println(" o C");
}
client.println("
");
}
client.print((iNVisitas++)/2);
client.println(" visitas
");

break;
}
if (c == ' ') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != ' ') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
}
else
{
//if(nFiles>=0 && file)
{
file= SD.open("datalog.txt", FILE_WRITE);
}
String data="";
for(int i=0;i {
getdata(i);

data+=String(nFilas)+";"+String(i)+";"+String((int)fHumedades[i])+";"+String((int)fTemperaturas[i])+" ";
}
if(file)
{
file.print(data);
Serial.print(data);
file.close();
nFilas++;
}
delay(200);
}
}