I made a simple temperature controller on DHT11. I want it to work with DHT22 as well having the same code.
However, I can’t find a way how to get known what type of sensor is installed. Is there a way?
I made a simple temperature controller on DHT11. I want it to work with DHT22 as well having the same code.
However, I can’t find a way how to get known what type of sensor is installed. Is there a way?
You can use another library for DHT: https://github.com/markruys/arduino-DHT Although, the library is quite old, it works fine and has auto-detection feature.
I don't think that is possible.
What you should do is pass the type (DHT11 or DHT12) to the code, store it and change calculations (or functions) accordingly.
If you use a class, you can pass it as argument of the constructor, so the user of the library only need to set it up once (in the constructor where it belongs). Make it an enumeration, copy it into a class variable and all methods inside your class can use the type.
Something like (not checked):
MyDht.hpp:
class MyDht
{
public:
enum EType { DHT11, DHT12 };
public MyDht(EType type);
private:
EType _type;
}
Dht.cpp:
MyDht::MyDht(EType type)
{
_type = type;
}
And you create the class by:
MyDht sensor1(MyDht::DHT11);
MyDht sensor2(Mydht::DHT12);