2 dht11's into 1 microcontroller

I am struggling to find the route code for reading two DHT11 temperature sensors in to 1 microcontroller board so as to provide for the user the temperature underground of a ‘Heat Pump’ and the temperature of the area to be heated in this case a ‘Green House’.

I have tried to modify Adafruits program that uses a number of #Includes but most importantly DHT.h, and all as the following code. Although the code is suggested as altered the code provided herewith is the bare bones Adafruit working code:

// atempt to produce a sketch for DHT humidity/temperature sensors
// & LCD temp & humidity readouts
// Originally aurhored by Adafruit Industries.
#include “DHT.h”
#include <LiquidCrystal.h>
#include <Wire.h> // Wire header file for I2C and 2 wire
#define DHTPIN 2 // what pin we’re connected to
#define DHTTYPE DHT11 //#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
// Connect pin 1 (on the left) of the sensor to +5V. Connect pin 2 of the sensor to whatever your DHTPIN is. Connect pin 4 (on the right) of the sensor to GROUND. Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
void setup() {
Serial.begin(9600);
Serial.println(“DHTxx test!”);
// SerialDegC += char(176); // Setup a Degrees C Serial symbol
// SerialDegC += "C ";

dht.begin();
}

void loop() {
dht.readHumidity() = digital.read(DHTPIN 2);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds ‘old’ (its a very slow sensor)
float h = dht.readHumidity();
float t = dht.readTemperature();

// check if returns are valid, if they are NaN (not a number) then something went wrong!
if (isnan(t) || isnan(h)) {
Serial.println(“Failed to read from DHT”);
} else {
Serial.print(“Humidity: “);
Serial.print(h);
Serial.print(” %\t”);
Serial.print(“Temperature: “);
Serial.print(t);
// Serial.println(SerialDegC);
// Serial.println(” *C”);
}
}

The above code is supported by way of the #include DHT.h all as follows:

#ifndef DHT_H
#define DHT_H
#if ARDUINO >= 100
#include “Arduino.h”
#else
#include “WProgram.h”
#endif

/* DHT library

MIT license
written by Adafruit Industries
*/

// how many timing transitions we need to keep track of. 2 * number bits + extra
#define MAXTIMINGS 85

#define DHT11 11
#define DHT22 22
#define DHT21 21
#define AM2301 21

class DHT {
private:
uint8_t data[6];
uint8_t _pin, _type, _count;
boolean read(void);
unsigned long _lastreadtime;
boolean firstreading;

public:
DHT(uint8_t pin, uint8_t type, uint8_t count=6);
void begin(void);
float readTemperature(bool S=false);
float convertCtoF(float);
float readHumidity(void);

};
#endif

Any help or lead would be greatly appreciated.
Cheers,
Chris S.

Going by the way one adds servos say, I'd guess that you dupe this line:

DHT dht(DHTPIN, DHTTYPE);

and have

#define DHTPIN2 3    // added later, see rogerClark's reply#2

DHT dht1(DHTPIN, DHTTYPE);
DHT dht2(DHTPIN2, DHTTYPE);  //changed to DHTPIN2, see rogerClark's reply#2

Then have

  dht1.begin();
  dht2.begin();

.... and later on:

float h2 = dht2.readHumidity();

Total flier, though.

@JimboZA,

I think you mean

#define DHTPIN2 3 

DHT dht1(DHTPIN, DHTTYPE);
DHT dht2(DHTPIN2, DHTTYPE);

rogerClark: @JimboZA, I think you mean....

Probably 8)

That makes sense yes......

Hi,

Many thanks for your help, I have rewritten it all as follows with some starting #includes not so far in use. One thing I can not find in any example of this sensor is the retrieval of the insignificants or fractions but reading the somewhat scant descriptions by the manufacturers they show I think that this information is available. Would you guys know how to achieve their discovery? Cheers, Chris:

// atempt to produce a sketch for DHT humidity/temperature sensors
// & LCD temp & humidity Green House readouts
// Author H C Spreckley.

#include “DHT.h”
#include <LiquidCrystal.h>
#include <Wire.h>
#define DHTPIN1 2
#define DHTPIN2 3
#define DHTTYPE DHT11
DHT dht1(DHTPIN1, DHTTYPE);
DHT dht2(DHTPIN2, DHTTYPE);

void setup() {
Serial.begin(9600);
dht1.begin();
dht2.begin(); // This is one of the prime parts I failed to write in my first “dream”.
}

void loop() {
float h1 = dht1.readHumidity();
float t1 = dht1.readTemperature(); // Ditto the above
float h2 = dht2.readHumidity();
float t2 = dht2.readTemperature();

if (isnan(t1) || isnan(h1)) {
Serial.println(“Failed to read from DHT”);
}
else {
Serial.print(“Temp “);
Serial.print(t1,0);
Serial.println(” degC”);
}
}

I can't advise- never used one.

You didn't actually say so, but assuming my stab in the dark worked Chris?

Jimbo,

Yes I thought I had I even put a "Quote" into the heading but some how or other It went missing. Yep It did work exactly as I h ad said it was not too far in front of my own derisory coding though it didn't parse. So I am very grateful for your input, thanks!

Do you know by any chance how to read the fractions of degrees t hat I am unable to.

Cheers, Chris.

One thing I can not find in any example of this sensor is the retrieval of the insignificants or fraction

The DHT11 has no fraction part, it only return whole numbers. It also does no negative temperature.

If you want that you need to check the DHT22.

JimboZA: I can't advise- never used one.

You didn't actually say so, but assuming my stab in the dark worked Chris?

Jimbo,

Is it possible you could explain to me just how you new how to index the variables in the way you did; the pin numbers were obvious but how to use them elsewhere, was not. You see I guessed at something similar although as I said it wouldn't parse and I didn't know just what I was doing. Due to them being in a long statement as they were I din't know what each of the syllables was or was doing as this form of programming is still hard fr me to follow. Simple statements I understand but when they are compound my brain shrinks to an even smaller size than it really is and drops out.

I would be very much obliged if you could spare me the a few minutes of your time in explanation.

Cheers, and many thanks. Chris S., Manchester, UK.

robtillaart:

One thing I can not find in any example of this sensor is the retrieval of the insignificants or fraction

The DHT11 has no fraction part, it only return whole numbers. It also does no negative temperature.

If you want that you need to check the DHT22.

Dear Rob,

Many thanks for the help. I looked at your DHT11 program and ran it. It is very, very good but all the ducking and diving that the program takes (I say with admiration) and the amount of information it portrays are well beyond my brief from the neighbour for whom I am building this package. I also like to build these programs my self and even the disseminating useful information from your program was just not on as I couldn't understand very much of it and I would hate to be thought of as a parasite.

As a young man I could write fluently in MS., Basic and later used quite a lot of ASP., but much time has passed and so I am now attempting to learn, C# and the Arduino syntax differs.

So very many thanks for your advise; I will look into DHT22 as soon as I can.

Cheers and very many thanks, Chris Spreckley

Chris I kinda went off how the servo code works- standard example in the IDE. I knew from having more than one servo in there, to double upon every thing that set a servo up, obviously giving them unique names. Then when you want to use each servo, you can them individually by their names.

I think the key is understanding that you need 2 of these.....

DHT dht1(DHTPIN1, DHTTYPE);
DHT dht2(DHTPIN2, DHTTYPE);

Each of these fashions a new instance of the DHT class, here named dht1 and dht2. They could have course been named bill and ben, and Roger spotted that I hadn't put the 2nd one on its own pin.

Once you have dht1 and dht2 setup like that, you merely treat them each the same as the original dht you already had.

I can't say much more than that!

Thanks for your help. I'm sure you can't explain it any more. I think its my ignorance in not really understanding properly or fully just what a class is and exactly how an *.h file works as when I write my own stuff I try not not produce a class *,h file unless I really can't seem to do without it.

I'm not too sure of what I am about to say so I hope it's understandable, however: The DHT.h file used here presumably carries the necessary code to extract the Temperature and Humidity from the code issued by the DHT11 however I can't see this in the DHT.h file. I have also come across a lot of dht/DHT11.h files by way of Google that do not work with this program; now that I do not understand, as presumably all users wish to extract the very same information from the DHT11. It is this sort of coding with which I get very confused.

Anyway, cheers and thanks, Chris

The DHT.h file used here presumably carries the necessary code to extract the Temperature and Humidity from the code issued by the DHT11

Normally the header files (.h) don’t contain the code. In basic terms they contain a list of the feature available in the library, i.e the functions within the library as well as any variables that are part of the library

However in some cases, people put the actual functions inside the .h file

Depending on which DHT library you are using, the .h may be this one

class DHT {
private:
uint8_t data[6];
uint8_t _pin, _type, _count;
unsigned long _lastreadtime;
boolean firstreading;
public:
DHT(uint8_t pin, uint8_t type, uint8_t count=6);
void begin(void);
float readTemperature(bool S=false);
float convertCtoF(float);
float convertFtoC(float);
float computeHeatIndex(float tempFahrenheit, float percentHumidity);
float readHumidity(void);
boolean read(void);
};

which lists the functions at the bottom. e.g. readTemperature

The cpp file contains all the actual programming code for the library

You don’t need to know the internals of how this (or any) library works, you only need to know what functions are in the library and how to use them

rogerClark:

The DHT.h file used here presumably carries the necessary code to extract the Temperature and Humidity from the code issued by the DHT11

Normally the header files (.h) don't contain the code. In basic terms they contain a list of the feature available in the library, i.e the functions within the library as well as any variables that are part of the library

However in some cases, people put the actual functions inside the .h file

Depending on which DHT library you are using, the .h may be this one

Roger many thanks; may I try to tell you what I think your lines of code mean and may I dare to ask you whether you could comment on my misunderstandings:

( ?) = Don't know what it means

class DHT {
private:
uint8_t data[6];                                                                    [i]8 bit integer array of 6 pockets[/i]
uint8_t _pin, _type, _count;                                            [i]8 bit integer _t, (?)   _type, (?)  _count; (?)[/i]
unsigned long _lastreadtime;                                          [i] +or- variable number          _lastreadtime (does underscore mean its a 
                                                                                                                                                                                         pointer)[/i]
boolean firstreading;                                                           [i]boolean variable of firstreading of sensor[/i]
public:                                                                                        [i]public (?)  Guess as the following: can be read in any part of the document
                                                                                                                                       rather like 'Global'[/i]
DHT(uint8_t pin, uint8_t type, uint8_t count=6);   [i]Not sure of anything here excepting the 8bit . Can guess of course as t o 
                                                                                                         what the pin is etc but what of the _t of which I cannot even guess.[/i]
void begin(void);                                                                  [i] Obviously the actuating code starts here and does not provide a value.[/i]
float readTemperature(bool S=false);
float convertCtoF(float);
float convertFtoC(float);
float computeHeatIndex(float tempFahrenheit, float percentHumidity);
float readHumidity(void);
boolean read(void);
};

which lists the functions at the bottom. e.g. readTemperature

The cpp file contains all the actual programming code for the library

You don't need to know the internals of how this (or any) library works, you only need to know what functions are in the library and how to use them. OK but I would like to understand as I think that this is the only true way you can write the content of the cpp or ino file. Hope that I am not asking too much. (?)