DHT Library/Sketches

Hello, how can I put a Fahrenheit formula into this DHT library? I haven't really tried updating libraries. Also, after that is put into that library, how could these sketches be updated to include the dew point reading? I'd really appreciate any help. I have attached the library file. Here is the dew point formula that I would like to add:

(243.04*(log(h/100)+((17.625t)/(243.04+t)))/(17.625-log(h/100)-((17.625t)/(243.04+t)))*1.8+32)

Here are the 2 sketches I'd like to update:

// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain

#include "DHT.h"

#define DHTPIN 7     // what digital pin we're connected to

// Uncomment whatever type you're using!
#define DHTTYPE DHT11   // DHT 11
//#define DHTTYPE DHT22  //DHT 22 (AM2302)

// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors.  This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  Serial.println("DHTxx test!");

  dht.begin();
}

void loop() {
  // Wait a few seconds between measurements.
  delay(60000);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(t, h, false);

  Serial.print(f);
  Serial.print(" \t");
  Serial.print(h);
  Serial.print(" \t");
  Serial.print(hif);
  Serial.print(" \t");
  Serial.println(243.04*(log(h/100)+((17.625*t)/(243.04+t)))/(17.625-log(h/100)-((17.625*t)/(243.04+t)))*1.8+32);
  }

And sketch 2:

#include <LowPower.h>
#include <BH1750.h>
#include <SFE_BMP180.h>
#include <DHT.h>
#include <Wire.h>
#include "Sodaq_DS3231.h"
#include <SD.h>
#include <SPI.h>


#define DHTPIN 2
#define DHTTYPE DHT22   // DHT 22  (AM2302)

#define ALTITUDE 216.0 // Altitude in Sparta, Greece
char TEMPERATURE = 'C' ;  //Change it to F to log degrees Fahrenheit
int INTERVAL = 1; //interval of measurements in minutes
DateTime dt(2015, 12, 14, 9, 28, 40, 1);  //Date and time

int CS_PIN = 10;
int ledPin = 7;
int POWERPIN = 3;

File file;
DHT dht(DHTPIN,DHTTYPE);
SFE_BMP180 pressureSensor;
BH1750 lightMeter;
int id = 0;

void setup () 
{
   pinMode(POWERPIN, OUTPUT);
   digitalWrite(POWERPIN,HIGH);
   pinMode(ledPin, OUTPUT);
   initializeSD();
   
    //rtc.setDateTime(dt); //Adjust date-time as defined 'dt' above 
}

void loop () 
{
    String temperature;
    String humidity;
    String light;
    String pressure;
    String entryId;
    String dateEntry;
    id++;
    
    digitalWrite(POWERPIN,HIGH);
    digitalWrite(ledPin,HIGH);
    
    delay(2000);
    initSensors();
    delay(10000);
    
    entryId = String(id);
    humidity = String(dht.readHumidity());
    if(TEMPERATURE =='F')
    {
      temperature = String(dht.readTemperature(true));
    }else
    {
      temperature = String(dht.readTemperature());   
    }
    
    pressure = readPressure();
    light = readLight();
    dateEntry = DateLogEntry();

    String entry = entryId+","+dateEntry+","+temperature+","+humidity+","+pressure+","+light;    
    writeEntryToFile(entry);
    delay(2000);
    sleepForMinutes(INTERVAL);
    
}

void initSensors()
{
   
   Wire.begin();
   rtc.begin();
   dht.begin();
   pressureSensor.begin();
   lightMeter.begin();
}

void sleepForMinutes(int interval)
{
  int i=0;
  int seconds = interval*60;
  int iterations = seconds/8;
    for (byte i = 0; i <= A5; i++)
    {
    pinMode (i, OUTPUT);    // changed as per below
    digitalWrite (i, LOW);  //     ditto
    }  
  
  digitalWrite(POWERPIN,LOW);
  for(i=0;i<iterations;i++)
  {
    LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
  }  
}


float readPressureFromSensor()
{
  char status;
  double T,P,p0,a;

  status = pressureSensor.startTemperature();
  if (status != 0)
  {
    delay(status);
    status = pressureSensor.getTemperature(T);
    if (status != 0)
    { 
      status = pressureSensor.startPressure(3);
      if (status != 0)
      {
        delay(status);
        status = pressureSensor.getPressure(P,T);
        if (status != 0)
        {
          p0 = pressureSensor.sealevel(P,ALTITUDE);       
          return p0;
        }
      }
    }
  }
}

String DateLogEntry()
{
  
  String dateEntry;
  String year;
  String month;
  String day;
  String hour;
  String minute;
  
  DateTime now = rtc.now();
  year = String(now.year());
  month = String(now.month());
  day = String(now.date());

  if(now.hour()<10)
  {
    hour = "0"+String(now.hour());
  }else
  {
    hour = String(now.hour());
  }

    if(now.minute()<10)
  {
    minute = "0"+String(now.minute());
  }else
  {
    minute = String(now.minute());
  }
 
  dateEntry = month+"/"+day+"/"+year+" "+hour+":"+minute;
  return dateEntry;
}

String readLight()
{
  uint16_t lux = lightMeter.readLightLevel();
  return String(lux);
}

String readPressure()
{
  String pressure;
  pressure = String(readPressureFromSensor());
  return pressure;
}

void initializeSD()
{
  pinMode(10, OUTPUT); // change this to 53 on a mega  // don't follow this!!
  digitalWrite(10, HIGH); // Add this line
  if (SD.begin(CS_PIN))
  {
    digitalWrite(ledPin,LOW);
  } else
  {
    digitalWrite(ledPin,LOW);
    delay(1000);
    digitalWrite(ledPin,HIGH);
    delay(1000);
     digitalWrite(ledPin,LOW);
    delay(1000);
    digitalWrite(ledPin,HIGH);
    return;
  }
}

int openFileToWrite(char filename[])
{
  file = SD.open(filename, FILE_WRITE);

  if (file)
  {
    digitalWrite(ledPin,HIGH);
    delay(200);
    digitalWrite(ledPin,LOW);
    delay(200);
    digitalWrite(ledPin,HIGH);
    delay(200);
    digitalWrite(ledPin,LOW);
    return 1;
  } else
  {
    return 0;
  }
}

int writeToFile(String text)
{
  if (file)
  {
    file.println(text);
    return 1;
  } else
  {
    return 0;
  }
}

void closeFile()
{
  if (file)
  {
    file.close();
  }
}

void writeEntryToFile(String entry)
{
  openFileToWrite("log.txt");
  writeToFile(entry);
  closeFile();
}

DHT.txt (8.35 KB)

Hello, how can I put a Fahrenheit formula into this DHT library?

Why do you need to? If I'm not mistaken (though I could be, since you didn't provide a link to the library), that library has a method to return the temperature in Fahrenheit.

If not, it is trivial to convert Celcius to Fahrenheit in the sketch.

I see you attached the code. There is a method in the library to convert C to F or F to C.

That formula has a lot of "magic numbers" in it. So the conversion from C to F is tricky. It's a great example of why you should never code magic numbers. But if you do some research on the web, the dew point formula is available in it's full scientific glory which makes it amenable to any kind of conversions you want to do.

Edit - I now see that it contains an obvious C to F conversion. So it is easy to adapt to either system.

Thank you all. Paul, I meant the dew point formula in Fahrenheit was the one I'd like to include in that library. You're right, the library can already compute the temperature in Fahrenheit, however, it does not have a computation to calculate dew point. I found the dew point formula in the first post online, and it works very well in that first sketch when using the serial port. I'm planning on using the second sketch away from a computer to log temperature data though. If it's easier, how could I add that formula to the second sketch so it will write the dew point data to an SD Card? Thank you.

If you are just adding a class method, it's a better practice to extend a library by inheritance, than to modify the library.

I'm not sure exactly what that means, sorry, even after looking it up. I'd like to use that Fahrenheit dew formula from earlier and put it in sketch 2 to write those values to an SD Card.

GoBraves21:
I'm not sure exactly what that means, sorry, even after looking it up. I'd like to use that Fahrenheit dew formula from earlier and put it in sketch 2 to write those values to an SD Card.

Well, it's intermediate level OO programming, which you are forgiven for not knowing. But I don't see what is difficult about putting a formula into a sketch. Drop it in. It calculates based on 2 variables, h and t. You just need to assign values to those and assign the expression to a result. Not difficult, it's programming 101.

Also, you were given the rest of the answer in the double post that you started on this subject.

Hello
could somebody give an example for inheritance?

My one does not work:

Here is my header ths_export.hpp

#ifndef THS_INC_THS_EXPORT_HPP_
#define THS_INC_THS_EXPORT_HPP_

#include "DHT.h"

//////////////////////////////
// DHT21 / AMS2301 is at GPIO2
//////////////////////////////
#define DHTPIN 2

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302)
#define DHTTYPE DHT21 // DHT 21 (AM2301)

// init DHT; 3rd parameter = 16 works for ESP8266@80MHz

class THS: public DHT
{
public:
// THS();
THS(): DHT(2, DHT21, 16)
{
minValue = 2.0;
}
char* getString();

private:
float maxValue = 100.9;
float minValue = 1.8;
};

#endif /* THS_INC_THS_EXPORT_HPP_ */

I get the following error when compiling:

ths_export.hpp:29: undefined reference to `DHT::DHT(unsigned char, unsigned char, unsigned char)'