【发布时间】:2014-08-13 21:06:00
【问题描述】:
我使用的 Arduino 使用正常模式(非寄生模式)在数字引脚 2 上连接了数 (3) 个传感器。
其中两个传感器是使用库“OneWire”(Library Page)和“DallasTemperature”(Library on GitHub)的温度传感器。使用 DallasTemperature 库,可以轻松地使用命令“getTempCByIndex(int)”访问传感器值。
我的第三个传感器是温度和湿度组合传感器。为这个传感器提供的代码是一个单独的库“DHT11”。那个库不是很好,我很难用 DHT11- 和 DallasTemperature-library 读取传感器值。
我认为 OneWire 库应该对所有 OneWire 设备都是通用的,并且 DallasTemperature 库是该库的包装器,为某些传感器提供了良好的接口。
是否有人可以帮助我了解如何将 DHT11 库包含在 DallasTemperature 库中?一个好的函数是“getDHT11HumidityByIndex(int)”。
或者使用 OneWire 编写新的包装器更容易?在那种情况下,这将如何运作?
现在我只是尝试将提供的库与下面的代码一起使用。程序经常无法读取湿度传感器,得到状态(-2):“读取传感器:超时错误”,并且传感器的索引在运行期间发生变化。 我可以做一些小的改变来解决这个问题吗?
#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);
}
【问题讨论】:
标签: c arduino shared-libraries