Hello,
I'm working on a simple project to have a DHT11 (soon to be DHT22) sensor read by my Uno R3 & output to a web page (working) and a LCD. I have an aftermarket ethernet shield, but I do not believe that is the issue. I can print my Uno's IP address on both lines of my 16x2 LCD just fine but if I try to print the Fahrenheit temperature from the DHT11 on the LCD it displays nothing. My code is as follows. The time line as millis() is a placeholder while I wait for my I2C RTC module to arrive.
#include <SPI.h>
#include <Ethernet.h>
#include <dht11.h>
#include <RTClib.h>
#include <LiquidCrystal.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
LiquidCrystal lcd(4, 5, 6, 7, 8, 9);
EthernetServer server(80);
dht11 DHT = dht11(14);
void setup()
{
Serial.begin(9600);
lcd.begin(16, 2);
Ethernet.begin(mac);
server.begin();
}
void loop()
{
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
lcd.clear();
for (byte thisByte = 0; thisByte < 4; thisByte++)
{
lcd.print(Ethernet.localIP()[thisByte], DEC);
if (thisByte < 3)
{
lcd.print(".");
}
}
lcd.setCursor(0, 1);
boolean DHTgood = CheckDHT();
EthernetClient client = server.available();
if (client)
{
Serial.println("new client");
boolean currentLineIsBlank = true;
while (client.connected())
{
if (client.available())
{
char c = client.read();
Serial.write(c);
if (c == '\n' && currentLineIsBlank)
{
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println("Refresh: 5");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
if (DHTgood)
{
client.print("DHT11
\nHumidity: ");
client.print((float)DHT.humidity, 2);
client.print("%\n
\nTemperature: ");
client.print(DHT.fahrenheit(), 2);
LCDPrintDouble(DHT.fahrenheit(), 2, 2);
client.print("F\n
\nDew Point: ");
client.print(DHT.dewPoint() * 9 / 5 + 32, 2);
client.print("F\n
\nDew PointFast: ");
client.print(DHT.dewPointFast() * 9 / 5 + 32, 2);
client.print("F\n
");
}
client.print("\nTime: ");
client.print(millis());
client.print("\n</html>");
break;
}
if (c == '\n')
{
currentLineIsBlank = true;
}
else if (c != '\r')
{
currentLineIsBlank = false;
}
}
}
delay(1);
client.stop();
Serial.println("client disonnected");
}
delay(2000);
}
boolean CheckDHT()
{
boolean retVal = false;
int chk = DHT.read();
Serial.print("Read DHT Sensor: ");
switch (chk)
{
case 0:
Serial.println("OK");
retVal = true;
break;
case -1:
Serial.println("Checksum error");
break;
case -2:
Serial.println("Time out error");
break;
default:
Serial.println("Unknown error");
break;
}
return retVal;
}
void LCDPrintDouble(double val, byte precision, int precision2)
{
if (val < 0.0)
{
lcd.print("-");
val = -val;
lcd.print(int(val));
if (precision > 0)
{
lcd.print(".");
unsigned long frac;
unsigned long mult = 1;
byte padding = precision - 1;
while (precision--)
{
mult *= 10;
}
if (val >= 0)
{
frac = (val - int(val)) * mult;
}
else
{
frac = (int(val) - val) * mult;
}
unsigned long frac1 = frac;
padding--;
while(padding--)
{
lcd.print("0");
}
lcd.print(frac, precision2);
}
}
}
Other than my complete lack of comments (which I do at my job as a .NET developer a lot too), does anyone see any major issues?
Also, due to the horrible number of connections to my Arduino, I'm thinking of getting
- Two TinkerKit LCDs
- An I2C/SPI LCD Backpack and a 20x4 LCD I know is supported by it
Anyone have an opinion on which is the better option?