Merged unified and raw libraries

This commit is contained in:
microbuilder 2016-10-26 23:44:39 +02:00
parent 09344416d2
commit c978977718
5 changed files with 353 additions and 2 deletions

179
DHT_U.cpp Normal file
View File

@ -0,0 +1,179 @@
// DHT Temperature & Humidity Unified Sensor Library
// Copyright (c) 2014 Adafruit Industries
// Author: Tony DiCola
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "DHT_U.h"
DHT_Unified::DHT_Unified(uint8_t pin, uint8_t type, uint8_t count, int32_t tempSensorId, int32_t humiditySensorId):
_dht(pin, type, count),
_type(type),
_temp(this, tempSensorId),
_humidity(this, humiditySensorId)
{}
void DHT_Unified::begin() {
_dht.begin();
}
void DHT_Unified::setName(sensor_t* sensor) {
switch(_type) {
case DHT11:
strncpy(sensor->name, "DHT11", sizeof(sensor->name) - 1);
break;
case DHT21:
strncpy(sensor->name, "DHT21", sizeof(sensor->name) - 1);
break;
case DHT22:
strncpy(sensor->name, "DHT22", sizeof(sensor->name) - 1);
break;
default:
// TODO: Perhaps this should be an error? However main DHT library doesn't enforce
// restrictions on the sensor type value. Pick a generic name for now.
strncpy(sensor->name, "DHT?", sizeof(sensor->name) - 1);
break;
}
sensor->name[sizeof(sensor->name)- 1] = 0;
}
void DHT_Unified::setMinDelay(sensor_t* sensor) {
switch(_type) {
case DHT11:
sensor->min_delay = 1000000L; // 1 second (in microseconds)
break;
case DHT21:
sensor->min_delay = 2000000L; // 2 seconds (in microseconds)
break;
case DHT22:
sensor->min_delay = 2000000L; // 2 seconds (in microseconds)
break;
default:
// Default to slowest sample rate in case of unknown type.
sensor->min_delay = 2000000L; // 2 seconds (in microseconds)
break;
}
}
DHT_Unified::Temperature::Temperature(DHT_Unified* parent, int32_t id):
_parent(parent),
_id(id)
{}
bool DHT_Unified::Temperature::getEvent(sensors_event_t* event) {
// Clear event definition.
memset(event, 0, sizeof(sensors_event_t));
// Populate sensor reading values.
event->version = sizeof(sensors_event_t);
event->sensor_id = _id;
event->type = SENSOR_TYPE_AMBIENT_TEMPERATURE;
event->timestamp = millis();
event->temperature = _parent->_dht.readTemperature();
return true;
}
void DHT_Unified::Temperature::getSensor(sensor_t* sensor) {
// Clear sensor definition.
memset(sensor, 0, sizeof(sensor_t));
// Set sensor name.
_parent->setName(sensor);
// Set version and ID
sensor->version = DHT_SENSOR_VERSION;
sensor->sensor_id = _id;
// Set type and characteristics.
sensor->type = SENSOR_TYPE_AMBIENT_TEMPERATURE;
_parent->setMinDelay(sensor);
switch (_parent->_type) {
case DHT11:
sensor->max_value = 50.0F;
sensor->min_value = 0.0F;
sensor->resolution = 2.0F;
break;
case DHT21:
sensor->max_value = 80.0F;
sensor->min_value = -40.0F;
sensor->resolution = 0.1F;
break;
case DHT22:
sensor->max_value = 125.0F;
sensor->min_value = -40.0F;
sensor->resolution = 0.1F;
break;
default:
// Unknown type, default to 0.
sensor->max_value = 0.0F;
sensor->min_value = 0.0F;
sensor->resolution = 0.0F;
break;
}
}
DHT_Unified::Humidity::Humidity(DHT_Unified* parent, int32_t id):
_parent(parent),
_id(id)
{}
bool DHT_Unified::Humidity::getEvent(sensors_event_t* event) {
// Clear event definition.
memset(event, 0, sizeof(sensors_event_t));
// Populate sensor reading values.
event->version = sizeof(sensors_event_t);
event->sensor_id = _id;
event->type = SENSOR_TYPE_RELATIVE_HUMIDITY;
event->timestamp = millis();
event->relative_humidity = _parent->_dht.readHumidity();
return true;
}
void DHT_Unified::Humidity::getSensor(sensor_t* sensor) {
// Clear sensor definition.
memset(sensor, 0, sizeof(sensor_t));
// Set sensor name.
_parent->setName(sensor);
// Set version and ID
sensor->version = DHT_SENSOR_VERSION;
sensor->sensor_id = _id;
// Set type and characteristics.
sensor->type = SENSOR_TYPE_RELATIVE_HUMIDITY;
_parent->setMinDelay(sensor);
switch (_parent->_type) {
case DHT11:
sensor->max_value = 80.0F;
sensor->min_value = 20.0F;
sensor->resolution = 5.0F;
break;
case DHT21:
sensor->max_value = 100.0F;
sensor->min_value = 0.0F;
sensor->resolution = 0.1F;
break;
case DHT22:
sensor->max_value = 100.0F;
sensor->min_value = 0.0F;
sensor->resolution = 0.1F;
break;
default:
// Unknown type, default to 0.
sensor->max_value = 0.0F;
sensor->min_value = 0.0F;
sensor->resolution = 0.0F;
break;
}
}

78
DHT_U.h Normal file
View File

@ -0,0 +1,78 @@
// DHT Temperature & Humidity Unified Sensor Library
// Copyright (c) 2014 Adafruit Industries
// Author: Tony DiCola
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#ifndef DHT_U_H
#define DHT_U_H
#include <Adafruit_Sensor.h>
#include <DHT.h>
#define DHT_SENSOR_VERSION 1
class DHT_Unified {
public:
DHT_Unified(uint8_t pin, uint8_t type, uint8_t count=6, int32_t tempSensorId=-1, int32_t humiditySensorId=-1);
void begin();
class Temperature : public Adafruit_Sensor {
public:
Temperature(DHT_Unified* parent, int32_t id);
bool getEvent(sensors_event_t* event);
void getSensor(sensor_t* sensor);
private:
DHT_Unified* _parent;
int32_t _id;
};
class Humidity : public Adafruit_Sensor {
public:
Humidity(DHT_Unified* parent, int32_t id);
bool getEvent(sensors_event_t* event);
void getSensor(sensor_t* sensor);
private:
DHT_Unified* _parent;
int32_t _id;
};
Temperature temperature() {
return _temp;
}
Humidity humidity() {
return _humidity;
}
private:
DHT _dht;
uint8_t _type;
Temperature _temp;
Humidity _humidity;
void setName(sensor_t* sensor);
void setMinDelay(sensor_t* sensor);
};
#endif

View File

@ -3,3 +3,13 @@ This is an Arduino library for the DHT series of low cost temperature/humidity s
Tutorial: https://learn.adafruit.com/dht Tutorial: https://learn.adafruit.com/dht
To download. click the DOWNLOADS button in the top right corner, rename the uncompressed folder DHT. Check that the DHT folder contains DHT.cpp and DHT.h. Place the DHT library folder your <arduinosketchfolder>/libraries/ folder. You may need to create the libraries subfolder if its your first library. Restart the IDE. To download. click the DOWNLOADS button in the top right corner, rename the uncompressed folder DHT. Check that the DHT folder contains DHT.cpp and DHT.h. Place the DHT library folder your <arduinosketchfolder>/libraries/ folder. You may need to create the libraries subfolder if its your first library. Restart the IDE.
# Adafruit DHT Humidity & Temperature Unified Sensor Library
This library also includes an optional class for the
[DHT humidity and temperature sensor](https://learn.adafruit.com/dht/overview)
which is designed to work with the [Adafruit unified sensor library](https://learn.adafruit.com/using-the-adafruit-unified-sensor-driver/introduction).
You must have the following Arduino libraries installed to use this class:
- [Adafruit Unified Sensor Library](https://github.com/adafruit/Adafruit_Sensor)

View File

@ -0,0 +1,84 @@
// DHT Temperature & Humidity Sensor
// Unified Sensor Library Example
// Written by Tony DiCola for Adafruit Industries
// Released under an MIT license.
// Depends on the following Arduino libraries:
// - Adafruit Unified Sensor Library: https://github.com/adafruit/Adafruit_Sensor
// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#define DHTPIN 2 // Pin which is connected to the DHT sensor.
// Uncomment the type of sensor in use:
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// See guide for details on sensor wiring and usage:
// https://learn.adafruit.com/dht/overview
DHT_Unified dht(DHTPIN, DHTTYPE);
uint32_t delayMS;
void setup() {
Serial.begin(9600);
// Initialize device.
dht.begin();
Serial.println("DHTxx Unified Sensor Example");
// Print temperature sensor details.
sensor_t sensor;
dht.temperature().getSensor(&sensor);
Serial.println("------------------------------------");
Serial.println("Temperature");
Serial.print ("Sensor: "); Serial.println(sensor.name);
Serial.print ("Driver Ver: "); Serial.println(sensor.version);
Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" *C");
Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" *C");
Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" *C");
Serial.println("------------------------------------");
// Print humidity sensor details.
dht.humidity().getSensor(&sensor);
Serial.println("------------------------------------");
Serial.println("Humidity");
Serial.print ("Sensor: "); Serial.println(sensor.name);
Serial.print ("Driver Ver: "); Serial.println(sensor.version);
Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println("%");
Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println("%");
Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println("%");
Serial.println("------------------------------------");
// Set delay between sensor readings based on sensor details.
delayMS = sensor.min_delay / 1000;
}
void loop() {
// Delay between measurements.
delay(delayMS);
// Get temperature event and print its value.
sensors_event_t event;
dht.temperature().getEvent(&event);
if (isnan(event.temperature)) {
Serial.println("Error reading temperature!");
}
else {
Serial.print("Temperature: ");
Serial.print(event.temperature);
Serial.println(" *C");
}
// Get humidity event and print its value.
dht.humidity().getEvent(&event);
if (isnan(event.relative_humidity)) {
Serial.println("Error reading humidity!");
}
else {
Serial.print("Humidity: ");
Serial.print(event.relative_humidity);
Serial.println("%");
}
}

View File

@ -1,5 +1,5 @@
name=DHT sensor library name=DHT sensor library
version=1.2.3 version=1.3.0
author=Adafruit author=Adafruit
maintainer=Adafruit <info@adafruit.com> maintainer=Adafruit <info@adafruit.com>
sentence=Arduino library for DHT11, DHT22, etc Temp & Humidity Sensors sentence=Arduino library for DHT11, DHT22, etc Temp & Humidity Sensors