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);
}