# Source code from earl@microcontrollerelectronics.com
#include <dht.h>
#include <UIPEthernet.h>
dht DHT;
#define DHT22_PIN 6
byte mac[] = { 0x54, 0x34, 0x41, 0x30, 0x30, 0x31 };
IPAddress ip(192, 168, 2, 77);
EthernetServer server(80);
double Fahrenheit(double celsius) {
return ((double)(9/5) * celsius) + 32;
}
double Kelvin(double celsius) {
return celsius + 273.15;
}
// dewPoint function NOAA
// reference (1) : http://wahiduddin.net/calc/density_algorithms.htm
// reference (2) : http://www.colorado.edu/geography/weather_station/Geog_site/about.htm
//
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);
}
// delta max = 0.6544 wrt dewPoint()
// 6.9 x 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*0.01);
double Td = (b * temp) / (a - temp);
return Td;
}
void setup() {
Serial.begin(115200);
Serial.println("DHT WEB SERVER ");
Serial.print("LIBRARY VERSION: ");
Serial.println(DHT_LIB_VERSION);
Serial.println();
Ethernet.begin(mac, ip);
server.begin();
Serial.print("IP Address: ");
Serial.println(Ethernet.localIP());
}
void loop() {
EthernetClient client = server.available();
if (client) {
Serial.println("-> New Connection");
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (c == '\n' && currentLineIsBlank) {
client.println("<!DOCTYPE html>");
client.println("<html xmlns='http://www.w3.org/1999/xhtml'>");
client.println("<head>\n<meta charset='UTF-8'>");
client.println("<title>DHT22 Weather Information</title>");
client.println("</head>\n<body>");
client.println("<H2>DHT22 Sensor</H2>");
client.println("<H3>Humidity / Temperature</H3>");
client.println("<pre>");
int chk = DHT.read22(DHT22_PIN);
switch (chk) {
case DHTLIB_OK:
client.print("Humidity (%) : ");
client.println((float)DHT.humidity, 2);
client.print("Temperature (°C) : ");
client.println((float)DHT.temperature, 2);
client.print("Temperature (°F) : ");
client.println(Fahrenheit(DHT.temperature), 2);
client.print("Temperature (°K) : ");
client.println(Kelvin(DHT.temperature), 2);
client.print("Dew Point (°C) : ");
client.println(dewPoint(DHT.temperature, DHT.humidity));
client.print("Dew PointFast (°C): ");
client.println(dewPointFast(DHT.temperature, DHT.humidity));
break;
case DHTLIB_ERROR_CHECKSUM:
client.print("\nChecksum error!");
break;
case DHTLIB_ERROR_TIMEOUT:
client.print("\nTime out error!");
break;
default:
client.print("\nUnknown error!");
break;
}
client.println("</pre>");
client.println("<H3>Powered by an Arduino Leonardo</H3>");
client.print("</body>\n</html>");
break;
}
if (c == '\n') currentLineIsBlank = true;
else if (c != '\r') {
currentLineIsBlank = false;
}
}
}
delay(10);
client.stop();
Serial.println(" Disconnected\n");
}
}
Recent Comments