Temperature and Humidity Datalogger + Webserver

87,453

80

15

Intro: 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);
}
}

Share

    Recommendations

    • Electronics Tips & Tricks Challenge

      Electronics Tips & Tricks Challenge
    • Plastics Contest

      Plastics Contest
    • Audio Contest 2018

      Audio Contest 2018

    15 Discussions

    0
    None
    mmo6

    5 years ago on Step 4

    is there a way that file savings are made to different files and max size ~1M?

    0
    None
    javacasm

    5 years ago on Step 4

    I'm it almost worked. The delay could be due to some config or hardwareissues. Please, check the ip and the ethernet wire.

    0
    None
    mmo6

    5 years ago on Step 4

    thnx now its working:) i added to the end some sd card lines , now its saving data to sd card but web browsing is very very slow or noting at all.. , where should i put some delay s for that browser can makes connections in time ?




    // close the connection:
    client.stop();
    }
    else

    {
    //if(nFiles>=0 && file)
    {
    file= SD.open("datalog.txt", FILE_WRITE);
    }
    String data="";
    for(int i=0;i
    }


    if(file)
    {
    file.println(data);
    Serial.println(data);
    file.close();
    nFilas++;
    }
    delay(1000);

    for(int i=0;i getdataDHT(i);
    delay(1000);
    getdataOneWire();
    }
    }

    0
    None
    javacasm

    5 years ago on Step 4

    I'll try to reproduce again the circuit and test again the code.
    This is the code I'm using now in a bit more complex project. You can remove the OneWire part
    I hope this helps

    #include <SPI.h>
    #include <Ethernet.h>
    #include <OneWire.h>
    #include <dht11.h>  // Descargamos la librería desde http://arduino-info.wikispaces.com/DHT11-Humidity-TempSensor

    dht11 DHT11; 
    #define nSensoresDHT 6
    #define nSensoresOneWire 3
    int puertosDHT[]={2,3,5,6,7,8}; // ¿Por qué nos saltamos el 4?
    int puertoOneWire=9;
    OneWire  ds(puertoOneWire); 

    float fHumedades[nSensoresDHT];
    float fTemperaturas[nSensoresDHT+nSensoresOneWire];

    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):
    EthernetServer server(666);
    void setup()
    {
      // start the Ethernet connection and the server:
      Ethernet.begin(mac, ip);
      server.begin();

      Serial.begin(115200);


    }
    float tMin=100;
    float tMax=0;
    float tMedia=0;
    int contador=0;
    void getdataOneWire()
    {
      byte i;
      byte present = 0;
      byte type_s;
      byte data[12];
      byte addr[8];
      float celsius;
     
      if ( !ds.search(addr)) {
        Serial.print(contador);
        Serial.println(" DS18x20 encontrados");
        Serial.print("Tmax=");
        Serial.print(tMax);
        Serial.print(" Tmin=");
        Serial.print(tMin);
        Serial.print(" tmedia=");
        Serial.println( tMedia/contador);
        ds.reset_search();
        contador=0;
        tMedia=0;
        delay(250);
        return;
      }

      Serial.print("ROM =");
      for( i = 0; i < 8; i++) {
       
        if(addr[i]<16)
          Serial.print(" 0");
        else
          Serial.write(' ');
        Serial.print(addr[i], HEX);
      }

      if (OneWire::crc8(addr, 7) != addr[7]) {
          Serial.println("CRC is not valid!");
          return;
      }
    // Serial.println();

      // the first ROM byte indicates which chip
      switch (addr[0]) {
        case 0x10:
          Serial.print(" DS18S20");  // or old DS1820
          type_s = 1;
          break;
        case 0x28:
          Serial.print(" DS18B20");
          type_s = 0;
          break;
        case 0x22:
          Serial.print(" DS1822");
          type_s = 0;
          break;
        default:
          Serial.print("Not a DS18x20");
          return;
      }

      ds.reset();
      ds.select(addr);
      ds.write(0x44,1);         // start conversion, with parasite power on at the end
     
      delay(1000);     // maybe 750ms is enough, maybe not
      // we might do a ds.depower() here, but the reset will take care of it.
     
      present = ds.reset();
      ds.select(addr);   
      ds.write(0xBE);         // Read Scratchpad

      for ( i = 0; i < 9; i++) {           // we need 9 bytes
        data[i] = ds.read();

      }


      // convert the data to actual temperature

      unsigned int raw = (data[1] << 8) | data[0];
      if (type_s) {
        raw = raw << 3; // 9 bit resolution default
        if (data[7] == 0x10) {
          // count remain gives full 12 bit resolution
          raw = (raw & 0xFFF0) + 12 - data[6];
        }
      } else {
        byte cfg = (data[4] & 0x60);
        if (cfg == 0x00) raw = raw << 3;  // 9 bit resolution, 93.75 ms
        else if (cfg == 0x20) raw = raw << 2; // 10 bit res, 187.5 ms
        else if (cfg == 0x40) raw = raw << 1; // 11 bit res, 375 ms
        // default is 12 bit resolution, 750 ms conversion time
      }
      celsius = (float)raw / 16.0;
      fTemperaturas[nSensoresDHT+contador]=celsius;
      if(tMin>celsius)
        tMin=celsius;
      if(tMax<celsius)
        tMax=celsius;
      tMedia+=celsius;
      Serial.print("  Temp.= ");
      Serial.print(celsius);
      Serial.println("C");
      contador++;
    }


    void getdataDHT(int iIndice)
    {
      int chk = DHT11.read(puertosDHT[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
      EthernetClient client = server.available();
      String sURL;
      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 == '\n' && currentLineIsBlank) {
              sURL=sURL.substring(0,9);
              Serial.println(sURL);
              if(sURL.endsWith("csv "))
              {
                for(int i=0;i<nSensoresDHT;i++){
                  client.print(fHumedades[i], 2);
                  client.print(";");
                  client.print(fTemperaturas[i], 2);
                  client.print(";");

                }
                for(int i=0;i<nSensoresOneWire;i++){
                  client.print(fTemperaturas[nSensoresDHT+i]);             
                  client.print(";");
                }
                client.println("");      
              }
              else
              {
                // send a standard http response header
                client.println("HTTP/1.1 200 OK");
                client.println("Content-Type: text/html");
                client.println();
                // client.println("<meta http-equiv=\"refresh\" content=\"1\" />"); //Autorefresh de la página
                // output the value of each analog input pin
                for(int i=0;i<nSensoresDHT;i++){
                  client.print("sensorDHT ");
                  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(" C");
                  }
                  client.println("<br />");
                }
                for(int i=0;i<nSensoresOneWire;i++){
                  client.print("sensorOneWire ");
                  client.print(i);
                  client.print(": ");
                  client.print(fTemperaturas[nSensoresDHT+i]);
                  client.println(" C<br />");
                }         
                client.print((iNVisitas++)/2);
                client.println(" visitas <br />");
              }
              break;
            }
            if (c == '\n') {
              // you're starting a new line
              currentLineIsBlank = true;
            }
            else if (c != '\r') {
              // you've gotten a character on the current line
              sURL+=c;
              currentLineIsBlank = false;

            }
          }
        }
        // give the web browser time to receive the data
        delay(1);
        // close the connection:
        client.stop();
      }
      else
      {
        for(int i=0;i<nSensoresDHT;i++)
          getdataDHT(i);
        delay(100);
        getdataOneWire();
      }
    }

    0
    None
    mmo6

    5 years ago on Step 4

    its ok now in program , no errors but there is no readings from sensors
    sensor 0: error leyendo el sensor
    sensor 1: error leyendo el sensor
    sensor 2: error leyendo el sensor

    i think there are much more differences between old and new versions

    0
    None
    mmo6

    5 years ago on Step 4

    i am using newer version yes. there are some other things that must be changed like "Server" is now "EthernetServer" and Client is EthernetClient, but i cant understand what is wrong with this "puertos={2,3,4}; "

    1 reply
    0
    None
    javacasmmmo6

    Reply 5 years ago on Step 4

    I don't know why doesn't work in 1.0 IDE

    You can fix it changing the declaration of puertos with this one

    int puertos[]={2,3,4};

    I hope this helps

    0
    None
    mmo6

    5 years ago on Step 4

    hi
    can you say why does line
    puertos={2,3,4};
    give me an error?
    expected primary expression before { token

    1 reply
    0
    None
    javacasmmmo6

    Reply 5 years ago on Step 4

    Which arduino version are you using?
    Maybe my code need a revision for arduino 1.0

    0
    None
    jabujavi

    5 years ago on Introduction

    Gracias tio! me ha venido de lujo.
    No serás forococheros verdad? te encontrarias un monton de cosas en la plataforma...

    0
    None
    saraca_92

    6 years ago on Step 4

    i like it.
    so nice.
    i can try =)