Reading several onewire sensors

I am using an Arduino that has several (3) sensors connected to it on digital pin 2 using normal mode (not parasite).

Two of the sensors is temperature sensors using the libraries "OneWire" (Arduino Playground - OneWire) and "DallasTemperature" (GitHub - milesburton/Arduino-Temperature-Control-Library: Arduino Temperature Library). With the DallasTemperature-library it is easy to access the sensor values with the command "getTempCByIndex(int)".

My third sensor is a combined temperature- and humiditysensor. The provided code for this sensor was a separate library "DHT11". That library is not that good and I have a hard time trying to read sensor values with both DHT11- and DallasTemperature-library.

I think that the OneWire-library should be universal for all OneWire-devices and that the DallasTemperature-library is a wrapper for that library providing a good interface for some sensors.

Is there someone who can help me understand how to include the DHT11-library in the DallasTemperature-library? A good function would be "getDHT11HumidityByIndex(int)".

Or is it easier to write a new wrapper using OneWire? In that case, how would that work?

Now I just try to use the provided libraries with the code below. The program often fails to read the humidity sensor, gets the status (-2) : "Read sensor: Time out error" and the index of the sensors change during runtime. Is there some small change I can do to fix this?

#include <OneWire.h>
#include <DallasTemperature.h>
#include <dht11.h>  //Library for the humidity sensor.

// Data wire is plugged into port 2 on the Arduino.
#define ONE_WIRE_BUS 2

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs).
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

// Declare object for Humidity sensor.
dht11 DHT11;



void setup(void)
{
  // Start serial port.
  Serial.begin(9600);

  // Start up the library.
  sensors.begin();
}

void loop(void)
{ 
  // Call sensors.requestTemperatures() to issue a global temperature 
  // request to all devices on the bus.
  sensors.requestTemperatures(); // Send the command to get temperatures.

  Serial.print("BEGIN-0#");
  Serial.print(sensors.getTempCByIndex(0));
  // You can have more than one IC on the same bus.
  // 0 refers to the first IC on the wire.
  Serial.println("#COMMIT");

  Serial.print("BEGIN-1#");
  Serial.print(sensors.getTempCByIndex(1));
  // You can have more than one IC on the same bus.
  // 0 refers to the first IC on the wire.
  Serial.println("#COMMIT");



  int chk = DHT11.read(ONE_WIRE_BUS);

  Serial.print("Read sensor: ");
  switch (chk)
  {
    case 0: Serial.println("OK"); break;
    case -1: Serial.println("Checksum error"); break;
    case -2: Serial.println("Time out error"); break;
    default: Serial.println("Unknown error"); break;
  }

  Serial.print("Humidity (%): ");
  Serial.println((float)DHT11.humidity, 2);

  Serial.print("Temperature (oC): ");
  Serial.println((float)DHT11.temperature, 2);
}

It might work better if you selected devices by their 64-bit address rather than their index. I don't know if either library is smart enough to only index devices that it understands.

Is there someone who can help me understand how to include the DHT11-library in the DallasTemperature-library? A good function would be "getDHT11HumidityByIndex(int)".

The Dallas and DHT sensors are two completely different types of sensors.
Dallas uses the OneWire protocol, has addresses so you can use multiple sensors on literally one wire.
The DHT has no addressing and needs one pin per sensor.
The problem is that some DHT documentation state it's protocol is similar to onewire, which is definitely not true.

For the DHT11 sensor check - Arduino Playground - HomePage - which is confirmed to work on several boards.

Extending the DallasLib to include support for DHT would be a design nightmare!
(why not include other temp sensors as well - analog sensors or PT100 alike etc)

robtillaart:
The DHT has no addressing and needs one pin per sensor.
The problem is that some DHT documentation state it's protocol is similar to onewire, which is definitely not true.

I stumbled over this information (DHTxx is "onewire") also on not only one distributor page so I thought first time mulitple sensors on one line it is not implemented yet in any lib. But as you wirte this informaition is wrong and you can not use multiple DHT on one wire. Good to know, I was confused with this supplier information also!