I currently am using a DHT11 sensor to display the humidity, temperature, and dew point via a webserver (through the arduino), serial, and an LCD readout.
The problem I am having is probably with some basic coding. It doesn't output to serial and LCD until I view the results via the web.
Any thoughts?
// Temperature Probe
// Include for Webserver
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xAA, 0xBB, 0xCC, 0xDD, 0xFE, 0x00 };
IPAddress ip(172,31,11, 191);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
// Include for DTHT11
#include <dht11.h>
dht11 DHT11;
#define DHT11PIN 8
// Include for LCD Display
#include <LiquidCrystal.h>
LiquidCrystal lcd(7,6,5,4,3,2);
void setup()
{
// DHT11 Setup
Serial.begin(9600);
Serial.println("DHT11 TEST PROGRAM ");
Serial.print("LIBRARY VERSION: ");
Serial.println(DHT11LIB_VERSION);
Serial.println();
// LCD Setup
lcd.begin(16,2);
// Webserver Setup
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
void loop()
{
// Create variables used for LCD output
int intHum = (float)DHT11.humidity;
double dblHum = (float)DHT11.humidity;
double dblCTemp = (float)DHT11.temperature;
double dblFTemp = Fahrenheit(DHT11.temperature);
double dblKTemp = Kelvin(DHT11.temperature);
double dblFDew = dewPoint(DHT11.temperature, DHT11.humidity);
double dblCDew = dewPoint2(DHT11.temperature, DHT11.humidity);
double dblFDewFast = dewPointFast(DHT11.temperature, DHT11.humidity);
double dblCDewFast = dewPointFast2(DHT11.temperature, DHT11.humidity);
// 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.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connnection: close");
client.println();
client.println("");
client.println("");
// add a meta refresh tag, so the browser pulls again every 5 seconds:
client.println("<meta http-equiv="refresh" content="5">");
// output the value of each analog input pin
client.print("Humidity: ");
client.print (intHum);
client.print ("
");
client.print("Temperature: ");
client.print (dblFTemp);
client.print ("/");
client.print (dblCTemp);
client.print ("
");
client.print("Dew Point: ");
client.print (dblFDew);
client.print ("/");
client.print (dblCDew);
client.print ("
");
client.println("
");
client.println("");
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 disonnected");
// DHT11 Output to serial
Serial.println("\n");
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;
}
Serial.print("Humidity (%): ");
Serial.println((float)DHT11.humidity, 2);
Serial.print("Temperature (oC): ");
Serial.println((float)DHT11.temperature, 2);
Serial.print("Temperature (oF): ");
Serial.println(Fahrenheit(DHT11.temperature), 2);
Serial.print("Temperature (K): ");
Serial.println(Kelvin(DHT11.temperature), 2);
Serial.print("Dew Point (oF): ");
Serial.println(dewPoint(DHT11.temperature, DHT11.humidity));
Serial.print("Dew Point (oC): ");
Serial.println(dewPoint2(DHT11.temperature, DHT11.humidity));
Serial.print("Dew PointFast (oF): ");
Serial.println(dewPointFast(DHT11.temperature, DHT11.humidity));
Serial.print("Dew PointFast (oC): ");
Serial.println(dewPointFast2(DHT11.temperature, DHT11.humidity));
// Output variabled to LCD screen
lcd.setCursor(0,0);
lcd.print("Temp: ");
lcd.print(dblFTemp);
lcd.print(" ");
lcd.print((char)223);
lcd.print("F");
lcd.setCursor(0,2);
lcd.print("Humidity: ");
lcd.print(intHum);
lcd.print("%");
// Delay
delay (2000);
}
}/* --(end main loop )-- */
/-----( Declare User-written Functions )-----/
//
//Celsius to Fahrenheit conversion
double Fahrenheit(double celsius)
{
return 1.8 * celsius + 32;
}
//Celsius to Kelvin conversion
double Kelvin(double celsius)
{
return celsius + 273.15;
}
// dewPoint function NOAA
// reference: Algorithms - Schlatter and Baker
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
double TT = (241.88 * T) / (17.558-T);
return 1.8 * TT + 32;
}
double dewPoint2(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: Dew point - Wikipedia
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 1.8 * Td + 32;
}
double dewPointFast2(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;
}