-1

I have 8x AM2301 sensors which I can read individually using this DHT library and the example code modified to my requirement. This implies having to do something like this for each sensor (which results in a lot of repeating code).

DHT dht(DHTPIN, DHTTYPE);
dht.begin();
float t,h;
t = dht.readTemperature();
h = dht.readHumidity();

Now I am trying to refactor my code so that I have an array of DHT objects that I can iterate over to declare, initialize, and read the values from (into arrays of floats).

After checking out some threads on stackoverflow, etc. I came up with two versions of my code that compile.

Here is the relevant portion of my code. The entire source (PlatformIO based) is uploaded here.

    SERIAL.print("Reading sensors: ");
    uint32_t startTime = millis();

    uint8_t SENSORS[] =  { PIN_SENSOR0, PIN_SENSOR1, PIN_SENSOR2, PIN_SENSOR3,
                           PIN_SENSOR4, PIN_SENSOR5, PIN_SENSOR6, PIN_SENSOR7 };

    SERIAL.println("INIT SENSORS");

    /* Please disregard this snippet. 
    // I was trying various things before posting here and messed up.
    DHT am2301[8];
    uint8_t i;
    for (i=0; i<8; i++){
      *am2301[i] = DHT(SENSORS[i], DHT_TYPE);
      am2301[i]->begin();
    }
    */

    // This locks up
    DHT **am2301;
    am2301 = new DHT* [8];
    uint8_t i;
    for (i=0; i<8; i++){
      am2301[i] = new DHT(SENSORS[i], DHT_TYPE);
      am2301[i]->begin();
    }

However, both of these styles lock up the code execution just after printing "INIT SENSORS". The target platform is ATSAMD21.

I am not an expert C/C++ programmer, so there may be something I am missing or overlooking here. I need some help in trying to figure out what I am doing wrong, and if there is a better way to do this.

EDIT: Using vector as recommended in the accepted answer, I was able to have an iterable list of objects. However, the program still locks up. It seems this particular issue is related to the DHT library not liking multiple dynamic objects. Now off to github...

1
  • Please disregard the snippet with am2301[i]->begin();. I was trying out some different things just before posting this here, and messed up. At this point I just want an array of DHTs that I can init with (PIN_SENSORx, DHT_TYPE), and begin() and read from. Commented Apr 17, 2019 at 12:12

1 Answer 1

1

c++ supplies you with plenty of object storage types to help you with these sorts of things. Seeing DHT **am2301; can usually be replaced with something like a std::vector which is designed to be a vector of objects. Assuming as you have stated that this:

DHT dht(DHTPIN, DHTTYPE);
dht.begin();
float t = dht.readTemperature();

works correctly, then you can easily produce a vector of these reading types like this:

std::vector<DHT> am2301; // My vector
for (auto i = 0; i < 8; ++i) {
    am2301.emplace_back(SENSORS[i], DHT_TYPE); // Create the objects in place with the 
                                               // correct constructor arguments. std::vector
                                               // takes care of memory allocation.
    am2301[i].begin(); // Start the object
}

float t = am2301[5].readTemperature(); // Use the vector

This saves you having to remember to delete the objects later, and gives you much more flexibility in how to use that vector. Usage of types like this is one of the most powerful things in c++, RAII.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. I tried this, got an error about vector not being found, so included <vector>. Now I have a bunch of errors... Pastebin: pastebin.com/t8HBtjyL
@Pranav, You should open another question to solve those. From the error you post here and the code from your question, there is no way to know why that is happening.
Okay. Thanks a lot with this. I learnt something new, and I am running in the correct direction now.
I was able to get rid of the errors by undefining the min and max definitions used by Arduino core. #undef max #undef min

Your Answer

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.