'class DHT' has no member named 'temperature'

Hi
I’m new to programming and I’m starting off with sending a temperature and humidity values to the particle cloud. I’m using DHT22.

I’m getting an error when I compile: ‘class DHT’ has no member named ‘temperature’ and same with the humidity. It’s probably a simple fix, hope someone can help. This has been done before and I think even with my minor modifications I’m not too far off.

Setup of the DHT22 has a 10kOhm resistor as the pullup.

Here’s the code:

/* this program is made to read temp and hum from DHT22 and send the values to the particle console
see https://openhomeautomation.net/cloud-data-logger-particle-photon for setup

*/
// This #include statement was automatically added by the Particle IDE.
#include <Adafruit_DHT.h>

#define DHTPIN 3
#define DHTTYPE 22

// define variables

double temperature = 0;
double humidity = 0;

DHT dht(DHTPIN, DHTTYPE);

void setup() {

    // Start the DHT sensor
    dht.begin();
    
    //Allow the sensor to stabilise
    delay(5000);
    
    Particle.variable("temperature", temperature); // api variables
    Particle.variable("humidity", humidity);
}



void loop() {

    // wait a few seconds between measurements
    delay(5000);
    // get the temperature value from the sensor and put in "temperature"
    temperature = dht.temperature();
    
    //get the humidity value from the sensor and put in "humidity"
    humidity = dht.humidity();
    
    // Publish the data to the particle console
    Spark.publish("temperature", String(temperature) + " °C");
    delay(5000);
    Spark.publish("humidity", String(humidity) + "%");
    delay(5000);

}

Welcome to the community.
In order to properly advise it’s always good to clarify what device you are building for and what device OS version you are using.

If you get an error message about functions not being declared, the first step would be to check the library header file and see what is and what isn’t declared there.

Looking here …
https://build.particle.io/libs/Adafruit_DHT/0.0.4/tab/Adafruit_DHT.h

… you’ll notice that the functions are actually called getTempCelsius(), getTempFarenheit(), getTempKelvin() and getHumidity().

Thanks ScruffR - Tried changing those functions to the getTempCelsius(); etc. Now it says “‘getTempCelsius’ was not declared in this scope” when verified - likewise with Humidity.

The device is Particle Photon with OS 0.6.3 as the target and 0.6.3 on the device. Is this the information needed?

Yes, that is it.

Can you show your updated code?

However, since I don’t see any overly specific about your code, why did you not just go with the library example for the start?
https://build.particle.io/libs/Adafruit_DHT/0.0.4/tab/example/dht-test.ino

In that all the required syntax is there for the pick and you’d just need to use USE THIS EXAMPLE button and build to get immediate feedback whether the library still builds or not.

1 Like

Thanks for responding. Have tried what you suggested - using the code in the link. Still won’t publish to the console stream. Tried including "Publish.particle(“t”) as below. Compiles fine, flashes success. I expect to see the temperature displayed in the console but only get a weird string of letters & numbers in the value column.

Here’s the new code:


// This #include statement was automatically added by the Particle IDE.
#include <Adafruit_DHT.h>


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

#define DHTPIN 3     // 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("DHT22 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.getHumidity();
// Read temperature as Celsius
	float t = dht.getTempCelcius();
// Read temperature as Farenheit
	float f = dht.getTempFarenheit();
  
// 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
// Must send in temp in Fahrenheit!
	float hi = dht.getHeatIndex();
	float dp = dht.getDewPoint();
	float k = dht.getTempKelvin();
	Particle.publish("t");

	Serial.print("Humid: "); 
	Serial.print(h);
	Serial.print("% - ");
	Serial.print("Temp: "); 
	Serial.print(t);
	Serial.print("*C ");
	Serial.print(f);
	Serial.print("*F ");
	Serial.print(k);
	Serial.print("*K - ");
	Serial.print("DewP: ");
	Serial.print(dp);
	Serial.print("*C - ");
	Serial.print("HeatI: ");
	Serial.print(hi);
	Serial.println("*C");
	Serial.println(Time.timeStr());
}

Take a look at the syntax and examples here you are missing the flag value (PUBLIC or PRIVATE)

Thanks for that. I’ve got it outputting to the console which is what I wanted. Only problem is that it’s giving “Null”. I guess it’s a hardware problem so I’ll work on that. thanks.

Thanks everyone. What I failed to understand is that I needed some room to put in the value from the sensor. Did that with a "char tempString[30] = “”; then float t = dht.getTempCelcius(); sprintf(tempString, %f, t); Particle,publish(“t,tempString, PRIVATE”);

Final code below, with some extra code like a counter for troubleshooting. I’m having issues with the serial which is why I’m relying on the console (will investigate later). I’m also getting the sensor returning “nan” for temp and 6553 for humidity which I’ll post on another thread soon after I’ve done some reading.
Thanks!


// This #include statement was automatically added by the Particle IDE.
#include <Adafruit_DHT.h>


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

#define DHTPIN 2     // 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

// tell the dht what it is
DHT dht(DHTPIN, DHTTYPE);

int counter = 0;
char counterString[30] = "";
char tempString[30] = "";
char humString[30] = "";
float t = 0;
float h = 0;


void setup() {
    Particle.publish("initializing serial connection", PRIVATE);
    Particle.publish("finished setup", PRIVATE);
    
    //initiate dht
    dht.begin();
    
    // let it settle, petal
    delay(2000);
    counter = 0;
    
}

void loop(){
    
    // puts values into t and h
    float t = dht.getTempCelcius();
    float h = dht.getHumidity();
    
    //put t and h into strings
    sprintf(counterString, "%d", counter);
    sprintf(tempString, "%f", t);
    sprintf(humString, "%f", h);
    
    // publish the strings to the console
    Particle.publish("counter", counterString, PRIVATE);
    counter = counter + 1;
    Particle.publish("t", tempString, PRIVATE);
    Particle.publish("h", humString, PRIVATE);
   //counter = counter + 1;
    delay(5000);


}

Try this

  char data[64];
  // e.g. "24.5°C 32.1% (0007)"
  snprintf(data, sizeof(data), "%.1f°C %.1f%% (%04d)", t, h, counter++);
  Particle.publish("EnvData", data, PRIVATE);

Just wrap all your data into one single event.

BTW, there is no need for your global t and h variables as you are never using them. You hide them by redefining local versions with the same name.

Thanks.
I get it re: the globals but sometimes if I don’t put them there, it gives an error (I’m thinking my troubleshooting migh have been inadequate). I’ve taken them out of the final code and it works ok with what you’ve provided. Thanks for the tip. Thanks for your help.

1 Like