Go Down

Topic: Problem with class pointers together with DHT sensor library (Read 464 times) previous topic - next topic

corrax

Hello,

I'm trying to read multiple DHT sensors. I'd like to loop through a class array that contains the data for the DHT sensors (as well as other sensors, which I have removed from my code to increase readibility here). I'm using LadyAda's DHT library at https://github.com/adafruit/DHT-sensor-library (tried also DHT22, but that didn't help). The problem seems to be that the pointer does not point to the DHT. Usually it fails to read, but sometimes there can be wrong data. I included a simple uint_8 pointer as well, to help in debugging. It gets written over sometimes by the temperature reading. Here's the code:

Code: [Select]

#include <DHT.h>

#define DHTTYPE DHT22   // DHT 22  (AM2302)

struct roomData {
    DHT* dhtPin;
    uint8_t* testPtr;
};


byte i;
float celsius;

const int noRooms=1;
roomData* roomPtrs[noRooms];
roomData roomVH;

DHT VH_dht(32,DHTTYPE);
uint8_t foo;

void setup() {
  foo=5;
  roomPtrs[0]=&roomVH;
  roomVH.dhtPin=&VH_dht;
  roomVH.testPtr=&foo;

  Serial.begin(9600);
 
  roomPtrs[0]->dhtPin->begin();
}


void loop(void) {

  for (i=0;i<=noRooms;i++) {
    delay(1000);     // maybe 750ms is enough, maybe not

    celsius = roomVH.dhtPin->readTemperature();
    Serial.print("DHT Obj - Temp: ");
    Serial.println(celsius);

    celsius = roomPtrs[i]->dhtPin->readTemperature();
    Serial.print("DHT Ptr - Temp: ");
    Serial.println(celsius);

    Serial.print("foo: ");
    Serial.println(*roomPtrs[i]->testPtr);
   
  }
}

Nick Gammon

Code: [Select]

const int noRooms=1;
roomData* roomPtrs[noRooms];


What is the point of an array with one element?
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

PeterH

I'm surprised that compiles. You need to declare an array of roomData. Instead, you declare an array of pointers to roomData, and then initialise them all to point to the same variable roomVH.

I haven't looked for other problems, but to address that problem you would need something like this:

Code: [Select]

// byte i; // <<< using a global variable as a loop variable is a very bad idea
const int noRooms=1;
roomData rooms[noRooms];
//roomData roomVH; // <<< serves no purpose

DHT VH_dht(32,DHTTYPE);
uint8_t foo;

void setup()
{
foo=5;
        for (int i = 0; i < noRooms; i++)
        {
            rooms[i].dhtPin=&VH_dht;
            rooms[i].testPtr=&foo;
        }

Serial.begin(9600);

VH_dht.begin();
}
I only provide help via the forum - please do not contact me for private consultancy.

PaulS

If noRooms (lousy name; sounds like a hotel with no vacancies) is ever changed from 1 to some other value, aren't all instances of the struct going to be pointing to the same DHT instance? Shouldn't the DHT instances be created in setup, during the initialization loop?

dhtPin is another lousy name, since its type is pointer to DHT, not a pin number.
Code: [Select]
rooms[i].dhtPtr = new DHT(pin[i],DHTTYPE);
(assuming you give the structure member a decent name.)

corrax

Thanks for your input and sorry for the poor choice of variable names.

I corrected the mistakes you pointed out, but the problem remained. However, I managed to track it down. My loop was going one too far (a sloppy mistake I know):

Code: [Select]

  for (i=0;i<=nrRooms;i++) {


when it should have been

Code: [Select]

  for (i=0;i<noRooms;i++) {


This caused the memory problems I was experiencing.

Ps. I was using roomVH (class object) and roomData (class array) to demonstrate that the first one was working while the second was not. Sorry for not being clear about that.

Go Up