Can't get double from DHT11 to print to LCD

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

Anyone have an opinion on which is the better option?

if you just do a lcd.print(DHT.fahrenheit(), 2) ; or lcd.print( (int) DHT.fahrenheit() ) ;

does that work?

maybe you just dont get a value - does the serial print meaningful values? => Use another lib for the DHT this one works for DHT11, 21, 22 - http://playground.arduino.cc/Main/DHTLib -

Have you tried printing dummy values on the LCD?

robtillaart: if you just do a lcd.print(DHT.fahrenheit(), 2) ; or lcd.print( (int) DHT.fahrenheit() ) ;

does that work?

maybe you just dont get a value - does the serial print meaningful values? => Use another lib for the DHT this one works for DHT11, 21, 22 - http://playground.arduino.cc/Main/DHTLib -

Have you tried printing dummy values on the LCD?

Printing it to serial gives me meaningful values. Just trying to do a lcd.print(DHT.fahrenheit()) doesn't display anything either. The LCDPrintDouble function was an attempt to get it working based on other code samples I found. Once I get home, I will try the DHT lib you linked to.

maybe the lcd.print does not support floats? can you convert DHT.Fahrenheit to an integer explicitly?

This is the way I do it:

dtostrf(DHT.temperature,2,1,Temp);
lcd.setCursor(whatever);
lcd.print(Temp);

Using http://playground.arduino.cc/Main/DHTLib worked for me. Next step is to only display the IP address on the LCD when a user has no connected or has not hit a button disabling it. I will figure that out eventually.

Thanks for the help.

Next step is to only display the IP address on the LCD when a user has no connected or has not hit a button disabling it

The IP address of what?

PaulS:

Next step is to only display the IP address on the LCD when a user has no connected or has not hit a button disabling it

The IP address of what?

The Arduino. It is currently running how I want it until I get my RTC module, DHT22 and possibly a 20x4 LCD.

The LCD displays the IP of the Arduino until either a client connects to the web server or a users presses a button. Overall, I am happy with my progress & I will be posting a little tutorial on my blog in the near future.

Welcome, good tutorials are always welcome

smccloud:

PaulS:

Next step is to only display the IP address on the LCD when a user has no connected or has not hit a button disabling it

The IP address of what?

The Arduino. It is currently running how I want it until I get my RTC module, DHT22 and possibly a 20x4 LCD.

The LCD displays the IP of the Arduino until either a client connects to the web server or a users presses a button. Overall, I am happy with my progress & I will be posting a little tutorial on my blog in the near future.

The IP address is simply 4 bytes. Displaying the values of 4 bytes on the LCD should be trivial.

PaulS:

smccloud:

PaulS:

Next step is to only display the IP address on the LCD when a user has no connected or has not hit a button disabling it

The IP address of what?

The Arduino. It is currently running how I want it until I get my RTC module, DHT22 and possibly a 20x4 LCD.

The LCD displays the IP of the Arduino until either a client connects to the web server or a users presses a button. Overall, I am happy with my progress & I will be posting a little tutorial on my blog in the near future.

The IP address is simply 4 bytes. Displaying the values of 4 bytes on the LCD should be trivial.

Yep, it was. I have a button set to disable the IP display & switch to Humidity & Temp Display when its pulled down to ground. I’m happy with the result so far. Next step is to get a larger LCD (probably I2C enabled), swap my DHT11 for the DHT22 still on its way & finally get my I2C RTC in my hands and plugged in.