In a previous post titled Arduino Leonardo and SPI Communications, I documented how to connect the Arduino Leonardo to the ENC28J60 Ethernet module. This post takes things a bit further and adds a DHT22 Sensor. The Arduino Leonardo is used as a WEB Server to Display Temperature and Humidity information from the DHT22 sensor.
The UIPEternet library is used to create a WEB server at IP Address 192.168.2.77 (which needs changed to whatever address would be appropriate for your LAN). The DHT library is used to communicate with the DHT22 sensor.
Here is the wiring graphic:
You can get the Fritzing.org code to create that image here. When you visit the WEB page in a browser, here is what the page looks like:
Here is the DHT22_ENC28J60_WEB.ino source code to make it all happen:
# 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"); } }
7 comments
Skip to comment form
Can you use an sd to record the raw data as well as do a live display?
I’ve been trying to get both going but not successful with my current coding.
Also, can a Ethernet shield using the w5500 series be used?
Author
Hi Matthew,
For your reference https://github.com/UIPEthernet/UIPEthernet/issues/19
To use the w5500 you will need a different library. The UIPEthernet is for the ENC28j60.
Earl
Thanks for the info. Also I did purchase the same hardware as what is posted for your circuit today but when trying to compile it states
“dht.h: No such file or directory” Even though I have downloaded/installed multiple libraries of this.
Can you link the DHT library you used for this. I have tried finding it but there are so many versions.
I did finally find one I think is close but I get the following error.
exit status 1
‘dht’ does not name a type
Author
Hi Matthew,
The link (https://playground.arduino.cc/Main/DHTLib/) is actualy in my post where it references the DHT library. If you take a look at that link, it has the code for the DHT ‘library’ which you need to cut and paste. It tells you how in the Notes section.
Earl
Author
Hi Matthew, I’ve not done anything with an SD card on an arduino but others have with great success. There is lots on info around on it.
Earl