Using an object (adafruits DHT sensor library) inside a class

I'm rewriting my code from a bunch of functions, to a (imho) more coherent c++ class for an Arduino sketch.

However I'm having difficutly getting Adafruits DHT library to work within my class. I don't understand how to declare the object in my .h file, intialize it in my .cpp file....

Normally, I'd use it like the below snippet and that works.

#include "Arduino.h"
#include <DHT.h>

#define  DHT_PIN                 8
#define  DHT_TYPE                DHT11

DHT dht(DHT_PIN, DHT_TYPE);

void setup() {
  // Begin dht sensor
  dht.begin();
  Serial.begin(9600);
}

void loop() {
  Serial.print(checkTemp());
  delay(5000);
}

float checkTemp() {
  return dht.readTemperature();
}

But now I'm converting over to a class and it's throwing errors about DHT not naming a type

.h file:

#ifndef GREEN_HOUSE_H
#define GREEN_HOUSE_H

#include <DHT.h>

class Greenhouse {
    public:
        Greenhouse(
                int temp_low = 26, int temp_high = 33, 
                int humidity_low = 80, int light_thresh = 400);
        void begin();
        void run_tasks();
        void run_sensor_check();
        bool get_light_status();
        bool get_heater_status();
        float get_temp();
        float get_humidity();
        int get_light_level();
        int get_soil_moisture_1();
        int get_soil_moisture_2();

    private:
        float _temp;
        float _humidity;
        int _light_level;
        int _soil_moisture_1;
        int _soil_moisture_2;
        bool _is_light_on;
        bool _is_heater_on;

        int _temp_low;
        int _temp_high;
        int _humidity_low;
        int _light_thresh;
        DHT _dht;     // This is the dht declaration that doesn't work.

        float check_temp();
        float check_humidity();
        int check_light_level();
        void water_pump_on_off(bool on_off);
        int check_soil_moisture_1();
        int check_soil_moisture_2();
        void lights_on_off(bool on_off);
        void heater_on_off(bool on_off);
        void operate_heat();
        void operate_light();
        void operate_water();
};

#endif

and the shortened .cpp file

#include "Arduino.h"
#include <DHT.h>
#include "greenhouse.h"

#define  DHT_TYPE                DHT11
#define  DHT_PIN                 8
// ... a bunch of other defines

Greenhouse::Greenhouse(int temp_low = 26, int temp_high = 33,
                       int humidity_low = 80, int light_thresh = 400) {
  _temp_low = temp_low;
  _temp_high = temp_high;
  _humidity_low = humidity_low;
  _light_thresh = light_thresh;
  _dht = DHT(DHT_PIN, DHT_TYPE);
}

void Greenhouse::begin() {

  _dht.begin();

  // Set pins as OUTPUT
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(LED_STRIP_PIN, OUTPUT);
  pinMode(WATER_PUMP_PIN, OUTPUT);
  pinMode(VALVE_01, OUTPUT);
  pinMode(VALVE_02, OUTPUT);
  pinMode(VALVE_03, OUTPUT);
  pinMode(HEATER_PIN, OUTPUT);

  // Set pins as INPUT
  pinMode(DHT_PIN, INPUT);
  pinMode(LIGHT_SENSOR_PIN, INPUT);
  pinMode(SOIL_MOISTURE01_PIN, INPUT);
  pinMode(SOIL_MOISTURE02_PIN, INPUT);

  _is_light_on = false;
  _is_heater_on = false;
}

The errors when I compile are:

/tmp/arduino-sketch-EEA7A73F83A344D98CD75E147D70995C/sketch/green_house.cpp: In constructor 'Greenhouse::Greenhouse(int, int, int, int)':
/tmp/arduino-sketch-EEA7A73F83A344D98CD75E147D70995C/sketch/green_house.cpp:18:69: error: no matching function for call to 'DHT::DHT()'
                        int humidity_low = 80, int light_thresh = 400) {
                                                                     ^
In file included from /tmp/arduino-sketch-EEA7A73F83A344D98CD75E147D70995C/sketch/green_house.cpp:2:0:
/home/pi/Arduino/libraries/DHT_sensor_library/DHT.h:65:3: note: candidate: DHT::DHT(uint8_t, uint8_t, uint8_t)
   DHT(uint8_t pin, uint8_t type, uint8_t count = 6);
   ^~~
/home/pi/Arduino/libraries/DHT_sensor_library/DHT.h:65:3: note:   candidate expects 3 arguments, 0 provided
/home/pi/Arduino/libraries/DHT_sensor_library/DHT.h:63:7: note: candidate: constexpr DHT::DHT(const DHT&)
 class DHT {
       ^~~
/home/pi/Arduino/libraries/DHT_sensor_library/DHT.h:63:7: note:   candidate expects 1 argument, 0 provided
/home/pi/Arduino/libraries/DHT_sensor_library/DHT.h:63:7: note: candidate: constexpr DHT::DHT(DHT&&)
/home/pi/Arduino/libraries/DHT_sensor_library/DHT.h:63:7: note:   candidate expects 1 argument, 0 provided

in .h file, you declared DHT dht;
but why in .ccp you called _dht.begin(); ??

My mistake, that was a hasitly added mistake as I was giving the code an edit for posting. I fixed it in op and verified the mistake wasn't in my actual code.

Because the DHT class's constructor takes arguments, you must use an initializer list. See Case #3 Here.

Awesome, that works a treat.

Thanks for the fantastic reference.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.