Pages: [1]   Go Down
Author Topic: Problem with class pointers together with DHT sensor library  (Read 343 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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);
   
  }
}
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

What is the point of an array with one element?
Logged

UK
Offline Offline
Shannon Member
****
Karma: 183
Posts: 11138
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
// 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();
}
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 547
Posts: 45987
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
rooms[i].dhtPtr = new DHT(pin[i],DHTTYPE);
(assuming you give the structure member a decent name.)
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
  for (i=0;i<=nrRooms;i++) {

when it should have been

Code:
  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.
Logged

Pages: [1]   Go Up
Jump to: