3

I came across the problem to handle two DHT22 devices.

I use the Adafruit DHT Sensor library.

Since my Arduino sketch works as a "relay" for Serial commands on a host, I tried to wire up and dynamically instantiate DHT22. With no luck.

I checked the source and found, that the DHT object only gets constructed before setup() as a constant in the examples, because the library is written that way.

I did a quick hack, which works, but seems ugly to me:

DHT.cpp

DHT::DHT(uint8_t pin, uint8_t type, uint8_t count) {
- _pin = pin;
- _type = type;
- #ifdef __AVR
- _bit = digitalPinToBitMask(pin);
- _port = digitalPinToPort(pin);
- #endif
+ //_pin = pin;
+ //_type = type;
+ //#ifdef __AVR
+ // _bit = digitalPinToBitMask(pin);
+ // _port = digitalPinToPort(pin);
+ //#endif
...
-void DHT::begin(void) {
+void DHT::begin(uint8_t pin, uint8_t type, uint8_t count) {
+ _pin = pin;
+ _type = type;
+ #ifdef __AVR
+ _bit = digitalPinToBitMask(pin);
+ _port = digitalPinToPort(pin);
+ #endif

DHT.h*

- void begin(void);
+ void begin(uint8_t pin, uint8_t type, uint8_t count=6);

Basically, I moved a few lines from the constructor to the begin() method. The whole thing works. I can even rewire & change the data-pin dynamically while the Arduino runs.

Here are my questions:

  • Is the hacked away behavior a bug or feature?
  • Would my solution be good enough for a pull request to Adafruit? I would clean it up beforehand.
Nick Gammon
38.9k13 gold badges69 silver badges125 bronze badges
asked Mar 26, 2016 at 7:42
2
  • 2
    I would contact the owners of the Github repository and point them to your "hack" and get the thumbs up first. At least, you would be notifying them about the issue. Either they don't know about it, or are working on a fix Commented Mar 26, 2016 at 9:00
  • Upvote for editing my post. Nice diff feature btw. Commented Mar 26, 2016 at 9:30

2 Answers 2

3

There is nothing wrong with the library. It does not allow just one object instance.

What is wrong is your way of using it.

It, and many other libraries, specify the pin numbers and other (under normal usage) static data in the constructor. You want to have two objects then create two objects.

But it seems you want to change that static data. So you need to construct differently. Instead of statically declaring your objects you need to be dynamic. Use new and delete.

DHT *myDHT = NULL;
myDHT = new DHT(4,5);
// do something
// change pins
delete myDHT;
myDHT = new DHT(6,7);
answered Mar 26, 2016 at 9:18
4
  • Cool, I missed that, since I'm not familar with the underlying concepts of C, but your answer is what I was looking for. This is why I wrote, that my solution seems ugly to me. I keep the repo on github, to keep this post understandable. Commented Mar 26, 2016 at 9:28
  • 1
    The onky caveat with it is that excessive use on boards with very low memory could lead to heap fragmentation and stability issues. Shouldn't happen as long as you new immediately after deleteing. Commented Mar 26, 2016 at 9:30
  • On second thought. Wouldn't it save mem on the arduino, if only one Object exists? I think this was the origin of the hack. Changing an objects attribute value should be much cheaper, than instanciating an object and destroying it again. Commented Mar 26, 2016 at 12:42
  • No memory difference. Just where in memory it resides. Maybe a minor difference in processing at the time of instantiation, but minimal. Commented Mar 26, 2016 at 12:44
1

Changing the pins sounds like a reasonable requirement, and you shouldn't have to delete/new to achieve it.

I would leave the library alone (for backwards compatibility) except to add a changePins function which you may choose whether or not to call, to assign new pins to the class structure. (Effectively it would be your rewritten begin function with a different name).

answered Mar 26, 2016 at 20:52

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.