View Single Post
Old 01-03-16, 02:24 PM   #20
Acuario
Apprentice EcoRenovator
 
Join Date: May 2011
Location: Tortosa, Spain
Posts: 221
Thanks: 2
Thanked 81 Times in 46 Posts
Default

6. DHT11.ino
#include <dht.h>
#include <math.h>

dht DHT11;

NIL_THREAD(threadReadDHT11, arg) {
while (TRUE) {
int chk = DHT11.read11(DHT11PIN);

//Serial.print("Read sensor: ");
switch (chk)
{
case DHTLIB_OK:
humidity = (float)DHT11.humidity;
dht11Temperature = (float)DHT11.temperature;
dewPointTemp = dewPoint(DHT11.temperature, DHT11.humidity);
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.println("DHT11 Checksum error");
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.println("DHT11 Time out error");
break;
default:
Serial.println("DHT11 Unknown error");
break;
}

//Serial.print("Humidity (%): ");
//Serial.println(humidity, 2);

//Serial.print("Temperature (°C): ");
//Serial.println(dht11Temperature, 2);

//Serial.print("Dew Point (°C): ");
//Serial.println(dewPointTemp);

// Sleep for 10 seconds.
nilThdSleepSeconds(10);
}
}

// dewPoint function NOAA
// reference (1) : Algorithms - Schlatter and Baker
// reference (2) : About the Weather Station
//
double dewPoint(double celsius, double humidity)
{
// (1) Saturation Vapor Pressure = ESGG(T)
double RATIO = 373.15 / (273.15 + celsius);
double RHS = -7.90298 * (RATIO - 1);
RHS += 5.02808 * log10(RATIO);
RHS += -1.3816e-7 * (pow(10, (11.344 * (1 - 1 / RATIO))) - 1);
RHS += 8.1328e-3 * (pow(10, (-3.49149 * (RATIO - 1))) - 1);
RHS += log10(1013.246);

// factor -3 is to adjust units - Vapor Pressure SVP * humidity
double VP = pow(10, RHS - 3) * humidity;

// (2) DEWPOINT = F(Vapor Pressure)
double T = log(VP / 0.61078); // temp var
return (241.88 * T) / (17.558 - T);
}
Acuario is offline   Reply With Quote