How to use DHT22 in a function?

Library Example code works:

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

#include <DHT.h>

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

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

// Connect pin 1 (on the left) of the sensor to +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

DHT dht(DHTPIN, DHTTYPE);

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

dht.begin();
}

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

// 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
float t = dht.readTemperature();
// Read temperature as Fahrenheit
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!");
}

// Compute heat index
// Must send in temp in Fahrenheit!
float hi = dht.computeHeatIndex(f, h);

double VaporPressureValue = h * 0.01 * 6.112 * exp((17.62 * t) / (t + 243.12));
double Numerator = 243.12 * log(VaporPressureValue) - 440.1;
double Denominator = 19.43 - (log(VaporPressureValue));
double DewPoint = (Numerator / Denominator);

Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C ");
Serial.print(f);
Serial.print(" *F\t");
Serial.print("Heat index: ");
Serial.print(hi);
Serial.println(" *F");
Serial.print("Dewpoint ");
Serial.print(DewPoint);
Serial.print(" *C ");
Serial.print(((9.0 / 5.0) * DewPoint) + 32);
Serial.println(" *F\n");

}

When I try this code with the getDHT22; function I get no return values:

// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain, Dew Point added by W0FMS
// Modified by Techno500

#include <DHT.h>

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

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

// Connect pin 1 (on the left) of the sensor to +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

DHT dht(DHTPIN, DHTTYPE);

float h;
float t;
float f;
float hi;
float DewPoint;

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

dht.begin();
}

void loop()
{

getDHT22();
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C ");
Serial.print(f);
Serial.print(" *F\t");
Serial.print("Heat index: ");
Serial.print(hi);
Serial.println(" *F");
Serial.print("Dewpoint ");
Serial.print(DewPoint);
Serial.print(" *C ");
Serial.print(((9.0 / 5.0) * DewPoint) + 32);
Serial.println(" *F\n");
}

float getDHT22() {
// Wait a few seconds between measurements.
delay(2000);

// 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
float t = dht.readTemperature();
// Read temperature as Fahrenheit
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!");
}

// Compute heat index
// Must send in temp in Fahrenheit!
float hi = dht.computeHeatIndex(f, h);

double VaporPressureValue = h * 0.01 * 6.112 * exp((17.62 * t) / (t + 243.12));
double Numerator = 243.12 * log(VaporPressureValue) - 440.1;
double Denominator = 19.43 - (log(VaporPressureValue));
double DewPoint = (Numerator / Denominator);

return(h,t,f,hi,DewPoint);
}

I get all zero values for return variables.

Is it possible to have a function like getDHT22 and return valid variable values?

This is the origin of the DHT library used: GitHub - adafruit/DHT-sensor-library: Arduino library for DHT11, DHT22, etc Temperature & Humidity Sensors

William

  return (h, t, f, hi, DewPoint);You can only return a single value from a function.

You can actually have a function return multiple values indirectly using a struct.

struct MyReadings
{
   float humidity;
   float temperature;
};

MyReadings m = GetReadings();
Serial.println(m.temperature);

struct MyReadings GetReadings()
{
  MyReadings r;
  r.temperature = 0;
  return r;
}

@b4ip

Thank you for the reply. My first time using struct; still do not understand how to apply it to the DHT22 function. Here is what I attempted:

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


//My failed attempt to use struct.

#include <DHT.h>

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

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

// Connect pin 1 (on the left) of the sensor to +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

DHT dht(DHTPIN, DHTTYPE);

float h;
float t;
float f;
float hi;
float DewPoint;

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

dht.begin();
}

void loop()
{

getDHT22();
Serial.print("Humidity: ");
Serial.print(m.h);
Serial.print(" %t");
Serial.print("Temperature: ");
Serial.print(m.t);
Serial.print(" *C ");
Serial.print(m.f);
Serial.print(" *Ft");
Serial.print("Heat index: ");
Serial.print(m.hi);
Serial.println(" *F");
Serial.print("Dewpoint ");
Serial.print(m.DewPoint);
Serial.print(" *C ");
Serial.print(((9.0 / 5.0) * DewPoint) + 32);
Serial.println(" *Fn");
}

float getDHT22() 
{
 // Wait a few seconds between measurements.
 delay(2000);

 // 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
 float t = dht.readTemperature();
 // Read temperature as Fahrenheit
 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!");
 }

 // Compute heat index
 // Must send in temp in Fahrenheit!
 float hi = dht.computeHeatIndex(f, h);

 double VaporPressureValue = h * 0.01 * 6.112 * exp((17.62 * t) / (t + 243.12));
 double Numerator = 243.12 * log(VaporPressureValue) - 440.1;
 double Denominator = 19.43 - (log(VaporPressureValue));
 double DewPoint = (Numerator / Denominator);
 
 MyReadings m = GetReadings();
}
 
 
struct MyReadings GetReadings()
{
 MyReadings r;
 r.h = 0;
 r.t = 0;
 r.f = 0;
 r.hi = 0;
 r.DewPoint = 0;
  
    return r;
}

Need help understanding how to apply struct to return five float variables.

William

At some point you will find that “delay” is a bad way to code. This will lead you to having to use millis timers to get updates from the dht sensor. In the end you will find that storing the readings as a global works best (easiest) so you could skip a few steps and just do that.

try this example

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

#include <DHT.h>

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

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

// Connect pin 1 (on the left) of the sensor to +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

DHT dht(DHTPIN, DHTTYPE);
float h = 0;
float t = 0;
float f = 0;
float hi = 0;
double DewPoint = 0;
unsigned long previousMillis = 0;

void setup() {
  Serial.begin(9600);
  Serial.println("DHTxx test!");
  Serial.println("waiting first read");
  dht.begin();
}

void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis > 2000)
  { //every 2 seconds this is true and readings are updated
    takeReadings();
    showReadings();
    previousMillis = currentMillis;
  }
}

void takeReadings() {
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  h = dht.readHumidity();
  // Read temperature as Celsius
  t = dht.readTemperature();
  // Read temperature as Fahrenheit
  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!");
  }

  // Compute heat index
  // Must send in temp in Fahrenheit!
  hi = dht.computeHeatIndex(f, h);

  double VaporPressureValue = h * 0.01 * 6.112 * exp((17.62 * t) / (t + 243.12));
  double Numerator = 243.12 * log(VaporPressureValue) - 440.1;
  double Denominator = 19.43 - (log(VaporPressureValue));
  DewPoint = (Numerator / Denominator);
}
void showReadings() {
  Serial.println("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.print(" *C ");
  Serial.print(f);
  Serial.print(" *F\t");
  Serial.print("Heat index: ");
  Serial.print(hi);
  Serial.println(" *F");
  Serial.print("Dewpoint ");
  Serial.print(DewPoint);
  Serial.print(" *C ");
  Serial.print(((9.0 / 5.0) * DewPoint) + 32);
  Serial.println(" *F\n");
}

@gpop1

Example presented does not compile. Was this the code you intended for me to try?

I am unable to figure out how to get multiple float variables out of function. Function is "getDHT22()" which produces the readings (global variables). I need the five variables in two other functions; one for logging and one to for Ethernet function.

@b4ip

You can actually have a function return multiple values indirectly using a "struct."

I am struggling to understand the use of "struct" for the purpose of returning multiple values. So far, my attempts have failed to compile.

Function is "getDHT22()" which produces the readings (global variables).

Since the function writes to global variables, there is no need for the function to return anything.

Except they are not, You are creating local variables in the function with the same names as the global variables. Stop doing that.

I am unable to figure out how to get multiple float variables out of function.

Either make the variables globals, or make them part of a struct returned by the function.

Example presented does not compile

Not a helpful description

My apologies… Global variables worked without needing struct!

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

#include "DHT.h"

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

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

// 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 for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);

float h;
float t;
float f;
float hi;
double DewPoint;

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

  dht.begin();
}

void loop()
{
  getDHT22();
  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(f);
  Serial.print(" *F\t");
  Serial.print("Heat index: ");
  Serial.print(hi);
  Serial.print(" *F ");
  Serial.print("Dew Point: ");
  Serial.print((DewPoint) + (9/5 + 32));
  Serial.println(" *F");
}

void getDHT22()
{
  // Wait a few seconds between measurements.
  delay(2000);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  h = dht.readHumidity();
  // Read temperature as Celsius
  t = dht.readTemperature();
  // Read temperature as Fahrenheit
  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!");
  }

  // Compute heat index
  // Must send in temp in Fahrenheit!
  hi = dht.computeHeatIndex(f, h);
   
  double VaporPressureValue = h * 0.01 * 6.112 * exp((17.62 * t) / (t + 243.12));
  double Numerator =243.12 * log(VaporPressureValue) - 440.1;
  double Denominator = 19.43 - (log(VaporPressureValue));
  DewPoint = Numerator / Denominator;
}

Thanks to all that have read and those that replied.
William