Skip navigation
>

DHT22 temp and humidity over the internet

Description:

How to use a DHT22 temperature and Humidity sensor with an arduino and provide the readings to a web browser on either an ENC or Wiznet network board


Code below:-


#include <SPI.h>

#include <Wire.h>

#include <Ethernet.h>

//#include <UIPEthernet.h>

#include <dht.h>

#include <stdio.h>

dht DHT;

#define DHT22_PIN 2

 

 

float combined_temp_C;

float combined_temp_F;  //create a new variable

byte TempHi;              // Variable hold data high byte

byte TempLo;              // Variable hold data low byte

boolean P_N;              // Bit flag for Positive and Negative

 

 

unsigned int Decimal;     // Variable hold decimal value

 

 

char * strTempC = NULL;

char * strTempF = NULL;

 

 

byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x4E, 0x12 };  // MAC address 84.85.88.16.0.36

byte ip[]  = { 192, 168, 1, 202 };                     // ip-address, please change to fit your network

byte mydns[] = { 192, 168, 1, 1 };          

byte gateway[] = { 192, 168, 1, 1 };           

byte subnet[] = { 255,255,255,0 };

EthernetServer server(80);

 

 

static char output[300] = "";

 

 

void setup() {

  Serial.begin(9600);

  //ethernet

   Ethernet.begin(mac, ip, mydns,gateway,subnet);

   server.begin();

   Serial.print("server is at ");

   Serial.println(Ethernet.localIP());

}

 

 

char headerHTML[] = "HTTP/1.1 200 OK\r\n"

          "Content-Type: text/html\r\n"

          "Connection: close\r\n"

          "Refresh: 5\r\n"

          "\r\n"

          "<!DOCTYPE HTML>"

          "<Title>RBBS Server</Title>"

          "<html>";

        

char footerHTML[] = "</html>" ;

 

char * TimeElapsed() { // Was Home Page

  long t = millis() / 1000;

  word h = t / 3600;

  byte m = (t / 60) % 60;

  byte s = t % 60;

  sprintf(output, "<h1>%d%d:%d%d:%d%d</h1>" , h/10, h%10, m/10, m%10, s/10, s%10);

  return output;

}

void sendTempToNetwork(EthernetClient myClient)

{

      // now humidity / temp sensor

    int chk = DHT.read22(DHT22_PIN);

    myClient.print("Humidity=");

    myClient.print(DHT.humidity,0);

    myClient.print("%");

    myClient.print("<BR/>");

    myClient.print("Temperature=");

    myClient.print(DHT.temperature, 0);

    myClient.write(" ");

    myClient.print("C");

    myClient.print("<BR/>");

    myClient.print("Dewpoint=");

    myClient.print(dewPoint(DHT.temperature,DHT.humidity), 0);

    myClient.write(" ");

    myClient.print("C");

    myClient.print("<BR/><BR/>");

}

void sendAnalogToNetwork(EthernetClient myClient)

{

    // output the value of each analog input pin for good measure

    for (int analogChannel = 0; analogChannel < 6; analogChannel++) {

      int sensorReading = analogRead(analogChannel);

      myClient.print("analog input ");

      myClient.print(analogChannel);

      myClient.print(" is ");

      myClient.print(sensorReading);

      myClient.println("<br />");     

    }

}

/*******************************************************************************

* Main Loop

*******************************************************************************/

void loop()

{

 

 

  // listen for incoming clients

  EthernetClient client = server.available();

  if (client) {

    Serial.println("new client");

    // an http request ends with a blank line

    boolean currentLineIsBlank = true;

    while (client.connected()) {

      if (client.available()) {

        char c = client.read();

        Serial.write(c);

        // 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) {

          // send a standard http response header

          client.print(headerHTML);

          // now send the stuff we want

          client.print(TimeElapsed()); // your old code here

          // do some more stuff

          sendTempToNetwork(client);

          sendAnalogToNetwork(client);

           // were done sending so now send the footer to close the page

          client.println(footerHTML);

          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

          currentLineIsBlank = false;

        }

      }

    }

    // give the web browser time to receive the data

    delay(1);

    // close the connection:

    client.stop();

    Serial.println("client disconnected");

  }

  delay(2000);

}

 

 

void Cal_Temp()

{

  if (TempHi&0x80)    // If bit7 of the TempHi is HIGH then the temperature is negative

    P_N = 0;

  else    // Else the temperature is positive

  P_N = 1;

 

 

  TempHi = TempHi & 0x7F;   // Remove sign

  TempLo = TempLo & 0xF0;   // Filter out last nibble

  TempLo = TempLo >>4; // Shift right 4 times

  Decimal = TempLo;

  Decimal = Decimal * 625;  // Each bit = 0.0625 degree C

  combined_temp_C= TempHi + TempLo*.0625;

  combined_temp_F = combined_temp_C*1.8+32;

   sprintf(strTempC, "%f", combined_temp_C);

   sprintf(strTempF, "%f", combined_temp_F);

}

////Celsius to Kelvin conversion

//double Kelvin(double celsius)

//{

//        return celsius + 273.15;

//}

 

 

// dewPoint function NOAA

// reference: http://wahiduddin.net/calc/density_algorithms.htm

double dewPoint(double celsius, double humidity)

{

        double A0= 373.15/(273.15 + celsius);

        double SUM = -7.90298 * (A0-1);

        SUM += 5.02808 * log10(A0);

        SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;

        SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;

        SUM += log10(1013.246);

        double VP = pow(10, SUM-3) * humidity;

        double T = log(VP/0.61078);   // temp var

        return (241.88 * T) / (17.558-T);

}

 

 

// delta max = 0.6544 wrt dewPoint()

// 5x faster than dewPoint()

// reference: http://en.wikipedia.org/wiki/Dew_point

double dewPointFast(double celsius, double humidity)

{

        double a = 17.271;

        double b = 237.7;

        double temp = (a * celsius) / (b + celsius) + log(humidity/100);

        double Td = (b * temp) / (a - temp);

        return Td;

}

Comments