Arduino web server showing DHT11 data

2,661

In this example we will use a DHT11 sensor and display the humidity and temperature on a web page. You can use any temperature sensor but the DHT11 is fairly low cost and there are libraries written for it, it comes in modules and even if you don’t have a modular version it is a through hole component so is easier to work with than some of the smt components that are available.

Layout

Here is a layout showing the DHT11 sensor connected to an Etehrnet shield which would be fitted to an Arduino Uno

ethernet and dht11_bb

 

Code

You will need to add the DHT library

#include <SPI.h>
#include <Ethernet.h>
#include <dht11.h>
#include <Wire.h>
 
 
#define DHT11PIN 2  // The Temperature/Humidity sensor pin
 
// 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, 0xFE, 0xED };
 
IPAddress ip(192,168,0, 177);
 
// Initialize the Ethernet server library
EthernetServer server(80);
 
dht11 DHT11;  //The Sensor Object
/*-----( Declare Variables )-----*/
 
 
 
void setup()
{
  // Open serial communications
  Serial.begin(9600);
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}
 
 
void loop()
{
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) 
  {
    Serial.println("new client");
    boolean currentLineIsBlank = true;
    while (client.connected()) 
    {
      if (client.available()) 
      {
        char c = client.read();
        Serial.write(c);
        if (c == '\n' && currentLineIsBlank) 
        {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connnection: close");
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
          client.println("<meta http-equiv=\"refresh\" content=\"5\">");
          client.println("<br />");    
 
          int chk = DHT11.read(DHT11PIN);
 
          Serial.print("Read sensor: ");
          switch (chk)
          {
          case 0: 
            Serial.println("OK"); 
            break;
          case -1: 
            Serial.println("Checksum error"); 
            break;
          case -2: 
            Serial.println("Time out error"); 
            break;
          default: 
            Serial.println("Unknown error"); 
            break;
          }  
 
          client.print("Temperature (C): ");
          client.println((float)DHT11.temperature, 1);  
          client.println("<br />");  
 
 
          client.print("Humidity (%): ");
          client.println((float)DHT11.humidity, 0);  
          client.println("<br />");   
 
          client.println("</html>");
          break;
        }
        if (c == '\n') 
        {
          currentLineIsBlank = true;
        } 
        else if (c != '\r') 
        {
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disonnected");
  }
}

 

Testing

Navigate to the IP address you specified in the code, you shoudl see something like the following, the temperature and humidity will change every 5 seconds. This can also be changed in the code as well

Temperature (C): 24.0
Humidity (%): 36

 

Links

You might also like More from author

Comments are closed, but trackbacks and pingbacks are open.