Go Down

Topic: Multiple DHT22 sensor and multiple DS18B20 code amalgamation (Read 93 times) previous topic - next topic

Jonathan91

Hi all, Could use a little help integrating 2 codes, Ill show my attempt at integrating
them but I am not satisfied with how the code looks or my understanding of it.

Here is the code Id found and made fit based on what I was looking for on the DHT22 side.

4X DHT22 sensors, I know of arrays but using its a problem since it does not label the
sensors.

Code: [Select]
#include <DHT.h>;



//Constants
#define DHTPIN1 28        // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)
#define DHTPIN2 29
#define DHTTYPE DHT22   // DHT 22  (AM2302)
#define DHTPIN3 30
#define DHTTYPE DHT22   // DHT 22  (AM2302)
#define DHTPIN4 31
#define DHTTYPE DHT22   // DHT 22  (AM2302)
DHT dht1(DHTPIN1, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino
DHT dht2(DHTPIN2, DHTTYPE);
DHT dht3(DHTPIN3, DHTTYPE);
DHT dht4(DHTPIN4, DHTTYPE);

//Variables
int chk;
float hum1;  //Stores humidity value
float temp1; //Stores temperature value
float hum2;
float temp2;
float hum3;
float temp3;
float hum4;
float temp4;

void setup()
{
    Serial.begin(9600);
  dht1.begin();
  dht2.begin();
  dht3.begin();
  dht4.begin();


}

void loop()
{
    //Read data and store it to variables hum and temp
    hum1 = dht1.readHumidity();
    temp1= dht1.readTemperature();
    hum2 = dht2.readHumidity();
    temp2= dht2.readTemperature();
    hum3 = dht3.readHumidity();
    temp3= dht3.readTemperature();
    hum4 = dht4.readHumidity();
    temp4= dht4.readTemperature();
   
    //Print temp and humidity values to serial monitor
    Serial.print("Sensor 1: Humidity: ");
    Serial.print(hum1);
    Serial.print(" %, Temp: ");
    Serial.print(temp1);
    Serial.println(" Celsius");

    Serial.print("Sensor 2: Humidity: ");
    Serial.print(hum2);
    Serial.print(" %, Temp: ");
    Serial.print(temp2);
    Serial.println(" Celsius");

    Serial.print("Sensor 3: Humidity: ");
    Serial.print(hum3);
    Serial.print(" %, Temp: ");
    Serial.print(temp3);
    Serial.println(" Celsius");

    Serial.print("Sensor 4: Humidity: ");
    Serial.print(hum4);
    Serial.print(" %, Temp: ");
    Serial.print(temp4);
    Serial.println(" Celsius");
    delay(2000); //Delay 2 sec.
}



4X DS18B20 on one wire bus , This wasn't modified much at all from a code I found on
this forum. I know its not needed for Fahrenheit since I am only really using Celsius.

Code: [Select]
#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into digital pin 34 on the Arduino
#define ONE_WIRE_BUS 34

// Setup a oneWire instance to communicate with any OneWire device
OneWire oneWire(ONE_WIRE_BUS); 

// Pass oneWire reference to DallasTemperature library
DallasTemperature sensors(&oneWire);

int deviceCount = 0;
float tempC;

void setup(void)
{
  sensors.begin();  // Start up the library
  Serial.begin(9600);
 
  // locate devices on the bus
  Serial.print("Locating devices...");
  Serial.print("Found ");
  deviceCount = sensors.getDeviceCount();
  Serial.print(deviceCount, DEC);
  Serial.println(" devices.");
  Serial.println("");
}

void loop(void)
{
  // Send command to all the sensors for temperature conversion
  sensors.requestTemperatures();
 
  // Display temperature from each sensor
  for (int i = 0;  i < deviceCount;  i++)
  {
    Serial.print("Sensor ");
    Serial.print(i+1);
    Serial.print(" : ");
    tempC = sensors.getTempCByIndex(i);
    Serial.print(tempC);
    Serial.print((char)176);//shows degrees character
    Serial.print("C  |  ");
    Serial.print(DallasTemperature::toFahrenheit(tempC));
    Serial.print((char)176);//shows degrees character
    Serial.println("F");
  }
 
  Serial.println("");
  delay(1000);
}



This is as far as I had gotten on the amalgamation of both these codes.
But I am really not happy with it or where its going? If that makes sense.


//Libraries
Code: [Select]
#include <DHT.h>;

#include <OneWire.h>
#include <DallasTemperature.h>


//Constants
#define ONE_WIRE_BUS 2

#define DHTPIN1 28        // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)
#define DHTPIN2 29
#define DHTTYPE DHT22   // DHT 22  (AM2302)
#define DHTPIN3 30
#define DHTTYPE DHT22   // DHT 22  (AM2302)
#define DHTPIN4 31
#define DHTTYPE DHT22   // DHT 22  (AM2302)
DHT dht1(DHTPIN1, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino
DHT dht2(DHTPIN2, DHTTYPE);
DHT dht3(DHTPIN3, DHTTYPE);
DHT dht4(DHTPIN4, DHTTYPE);

OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);

//Variables
int deviceCount = 0;
float tempC;

int chk;
float hum1;  //Stores humidity value
float temp1; //Stores temperature value
float hum2;
float temp2;
float hum3;
float temp3;
float hum4;
float temp4;

void setup()
{
    sensors.begin();  // Start up the library
    Serial.begin(9600);
  dht1.begin();
  dht2.begin();
  dht3.begin();
  dht4.begin();

// locate devices on the bus
  Serial.print("Locating devices...");
  Serial.print("Found ");
  deviceCount = sensors.getDeviceCount();
  Serial.print(deviceCount, DEC);
  Serial.println(" devices.");
  Serial.println("");

}

void loop()
{
    // Send command to all the sensors for temperature conversion
  sensors.requestTemperatures();
 
  // Display temperature from each sensor
  for (int i = 0;  i < deviceCount;  i++)
  {
    Serial.print("Sensor ");
    Serial.print(i+1);
    Serial.print(" : ");
    tempC = sensors.getTempCByIndex(i);
    Serial.print(tempC);
    Serial.print((char)176);//shows degrees character
    Serial.print("C  |  ");
    Serial.print(DallasTemperature::toFahrenheit(tempC));
    Serial.print((char)176);//shows degrees character
    Serial.println("F");
   
    //Read data and store it to variables hum and temp
    hum1 = dht1.readHumidity();
    temp1= dht1.readTemperature();
    hum2 = dht2.readHumidity();
    temp2= dht2.readTemperature();
    hum3 = dht3.readHumidity();
    temp3= dht3.readTemperature();
    hum4 = dht4.readHumidity();
    temp4= dht4.readTemperature();
   
    //Print temp and humidity values to serial monitor
    Serial.print("Sensor 1: Humidity: ");
    Serial.print(hum1);
    Serial.print(" %, Temp: ");
    Serial.print(temp1);
    Serial.println(" Celsius");

    Serial.print("Sensor 2: Humidity: ");
    Serial.print(hum2);
    Serial.print(" %, Temp: ");
    Serial.print(temp2);
    Serial.println(" Celsius");

    Serial.print("Sensor 3: Humidity: ");
    Serial.print(hum3);
    Serial.print(" %, Temp: ");
    Serial.print(temp3);
    Serial.println(" Celsius");

    Serial.print("Sensor 4: Humidity: ");
    Serial.print(hum4);
    Serial.print(" %, Temp: ");
    Serial.print(temp4);
    Serial.println(" Celsius");
    delay(2000); //Delay 2 sec.
}



Could I please have some help regarding my code and integrating them together.
Any help or advice would be appreciated. I need to have all sensors labelled.
Is there any way to also identify my DS18B20 sensors? So for Eg Sensor 1 remains
as sensor 1 ? there was some mention of Identity on the sensors but i dont know
how to fetch or use them. you will see in the amalgamated code has mention of
3 DS18B20 sensors with ID's but I had to remove it since mine dont have this ID.


Thanks again,
Greetings from South Africa

pylon

Each OneWire has it's own serial number. Read them out once and assign each number with a label as you want to choose them. Then use getTempC() instead of getTempCByIndex().

Jonathan91

Thanks for the reply!

Ill give that a try and post my results or share my findings.

Go Up