COSA
An Object-Oriented Platform for Arduino Programming
DHT11 Class Reference

#include <DHT.hh>

Inheritance diagram for DHT11:
Inheritance graph
Collaboration diagram for DHT11:
Collaboration graph

Public Types

enum  InterruptMode { ON_LOW_LEVEL_MODE = 0, ON_CHANGE_MODE = _BV(ISC00), ON_FALLING_MODE = _BV(ISC01), ON_RISING_MODE = (_BV(ISC01) | _BV(ISC00)) }
 
enum  Mode { OUTPUT_MODE = 0, INPUT_MODE = 1 }
 
enum  Direction { MSB_FIRST = 0, LSB_FIRST = 1 }
 

Public Member Functions

 DHT11 (Board::ExternalInterruptPin pin=Board::EXT0)
 
int16_t humidity () const
 
int16_t temperature () const
 
bool sample_request ()
 
bool sample_await ()
 
bool sample ()
 
bool sample (int16_t &humidity, int16_t &temperature)
 
OutputPinoperator<< (int value)
 
virtual void enable ()
 
virtual void disable ()
 
virtual void clear ()
 
void clear () const
 
void mode (Mode mode)
 
Mode mode () const
 
void _set () const
 
void _set (int value) const
 
void set () const
 
void set (int value) const
 
void high () const
 
void on () const
 
void _clear () const
 
void low () const
 
void off () const
 
void _toggle () const
 
void toggle () const
 
void _write (int value) const
 
void write (int value) const
 
void write (uint8_t value, OutputPin &clk, Direction order=MSB_FIRST) const
 
void write (uint16_t value, uint8_t bits, uint16_t us) const
 
void pulse (uint16_t us) const
 
uint8_t pin () const
 
bool is_set () const
 
bool is_high () const
 
bool is_on () const
 
bool is_clear () const
 
bool is_low () const
 
bool is_off () const
 
bool read () const
 
uint8_t read (OutputPin &clk, Direction order=MSB_FIRST) const
 
 operator bool () const
 
Pinoperator>> (uint8_t &var)
 

Static Public Member Functions

static void mode (Board::DigitalPin pin, Mode mode)
 
static Mode mode (Board::DigitalPin pin)
 
static void mode (Board::DigitalPin pin, uint8_t initial=0)
 
static void toggle (Board::DigitalPin pin)
 
static void _write (Board::DigitalPin pin, uint8_t value)
 
static void write (Board::DigitalPin pin, int value)
 
static uint8_t MASK (uint8_t pin)
 
static volatile uint8_t * PIN (uint8_t pin)
 
static volatile uint8_t * DDR (uint8_t pin)
 
static volatile uint8_t * PORT (uint8_t pin)
 
static bool read (Board::DigitalPin pin)
 

Static Public Attributes

static const int16_t INIT_HUMIDITY_SAMPLE = 1000
 
static const int16_t INIT_TEMPERATURE_SAMPLE = 850
 

Protected Types

enum  {
  INIT, IDLE, REQUEST, RESPONSE,
  SAMPLING, COMPLETED
}
 

Protected Member Functions

virtual void adjust_data ()
 
virtual void on_interrupt (uint16_t arg=0)
 
virtual void on_sample_completed (bool valid)
 
bool is_valid ()
 
volatile uint8_t * PIN () const
 
volatile uint8_t * DDR () const
 
volatile uint8_t * PORT () const
 
volatile uint8_t * PCIMR () const
 

Protected Attributes

volatile uint8_t m_state
 
uint16_t m_start
 
uint8_t m_value
 
uint8_t m_bits
 
uint8_t m_ix
 
data_t m_data
 
int16_t m_humidity
 
int16_t m_temperature
 
volatile uint8_t *const m_sfr
 
const uint8_t m_mask
 
const uint8_t m_pin
 

Static Protected Attributes

static const uint16_t MIN_PERIOD = 2048
 
static const uint16_t LOW_THRESHOLD = 50
 
static const uint16_t BIT_THRESHOLD = 100
 
static const uint16_t HIGH_THRESHOLD = 200
 
static const uint8_t DATA_MAX = 5
 
static const uint8_t DATA_LAST = DATA_MAX - 1
 

Detailed Description

DHT11 Humidity & Temperature Sensor device driver.

Circuit

+------------+
(VCC)---------------1-|VCC ====== |
(D2/EXT0)-----------2-|DATA ====== |
3-| ====== |
(GND)---------------4-|GND ====== |
+------------+

Connect DHT11 to pin, VCC and ground. A pullup resistor from the pin to VCC should be used. Most DHT11 modules have a built-in pullup resistor.

References

  1. DHT11 Humidity & Temperature Sensor, Robotics UK, www.droboticsonline.com, http://www.micro4you.com/files/sensor/DHT11.pdf
  2. DHTxx Sensor Tutorial, http://learn.adafruit.com/dht

Definition at line 244 of file DHT.hh.

Member Enumeration Documentation

anonymous enum
protectedinherited
Enumerator
INIT 

Initial state.

IDLE 

Periodic wait.

REQUEST 

Issued a request.

RESPONSE 

Waiting for response.

SAMPLING 

Collecting samples.

COMPLETED 

Data transfer completed.

Definition at line 152 of file DHT.hh.

enum Pin::Direction
inherited

Serialization directions; most or least significant bit first.

Enumerator
MSB_FIRST 
LSB_FIRST 

Definition at line 84 of file Pin.hh.

Enumerator
ON_LOW_LEVEL_MODE 
ON_CHANGE_MODE 
ON_FALLING_MODE 
ON_RISING_MODE 

Definition at line 35 of file ExternalInterrupt.hh.

enum IOPin::Mode
inherited
Enumerator
OUTPUT_MODE 
INPUT_MODE 

Definition at line 31 of file IOPin.hh.

Constructor & Destructor Documentation

DHT11::DHT11 ( Board::ExternalInterruptPin  pin = Board::EXT0)
inline

Construct connection to a DHT11 device on given in/output-pin.

Parameters
[in]pindata (Default EXT0).

Definition at line 250 of file DHT.hh.

Member Function Documentation

void OutputPin::_clear ( ) const
inlineinherited

Clear the output pin. Unprotected version.

Definition at line 114 of file OutputPin.hh.

void OutputPin::_set ( ) const
inlineinherited

Set the output pin. Unprotected version.

Definition at line 75 of file OutputPin.hh.

void OutputPin::_set ( int  value) const
inlineinherited

Set the output pin with the given value. Zero(0) to clear and non-zero to set. Unprotected version.

Parameters
[in]valueto set.

Definition at line 184 of file OutputPin.hh.

void OutputPin::_toggle ( ) const
inlineinherited

Toggle the output pin. Unprotected version.

Definition at line 153 of file OutputPin.hh.

void OutputPin::_write ( int  value) const
inlineinherited

Set the output pin with the given value. Zero(0) to clear and non-zero to set. Unprotected version.

Parameters
[in]valueto set.

Definition at line 219 of file OutputPin.hh.

static void OutputPin::_write ( Board::DigitalPin  pin,
uint8_t  value 
)
inlinestaticinherited

Set the given output pin with the given value. Zero(0) to clear and non-zero to set. Unprotected version when pin and value are constants.

Parameters
[in]pinnumber (must be constant).
[in]valueto write (must be constant).

Definition at line 345 of file OutputPin.hh.

void DHT11::adjust_data ( )
protectedvirtual

Adjust data from the DHT11 device; scale by 10 for uniform number range as DHT22.

Implements DHT.

Definition at line 126 of file DHT.cpp.

void ExternalInterrupt::clear ( )
virtualinherited

Clear external interrupt flag.

Note
atomic

Reimplemented from Interrupt::Handler.

Definition at line 205 of file ExternalInterrupt.cpp.

void OutputPin::clear ( ) const
inlineinherited

Clear the output pin.

Note
atomic

Definition at line 124 of file OutputPin.hh.

static volatile uint8_t* Pin::DDR ( uint8_t  pin)
inlinestaticinherited

Return pointer to Data Direction Register.

Parameters
[in]pinnumber.
Returns
DDR register pointer.

Definition at line 64 of file Pin.hh.

volatile uint8_t* Pin::DDR ( ) const
inlineprotectedinherited

Return pointer to Data Direction Register.

Returns
DDR register pointer.

Definition at line 245 of file Pin.hh.

void ExternalInterrupt::disable ( )
virtualinherited

Disable external interrupt handler.

Note
atomic

Reimplemented from Interrupt::Handler.

Definition at line 199 of file ExternalInterrupt.cpp.

void ExternalInterrupt::enable ( )
virtualinherited

Enable external interrupt handler.

Note
atomic

Reimplemented from Interrupt::Handler.

Definition at line 190 of file ExternalInterrupt.cpp.

void OutputPin::high ( ) const
inlineinherited

Set the output pin.

Note
atomic

Definition at line 95 of file OutputPin.hh.

int16_t DHT::humidity ( ) const
inlineinherited

Return humidity from latest read.

Returns
humidity.

Definition at line 61 of file DHT.hh.

bool Pin::is_clear ( ) const
inlineinherited

Return true(1) if the pin is clear otherwise false(0).

Returns
boolean.

Definition at line 142 of file Pin.hh.

bool Pin::is_high ( ) const
inlineinherited

Return true(1) if the pin is set otherwise false(0).

Returns
boolean.

Definition at line 122 of file Pin.hh.

bool Pin::is_low ( ) const
inlineinherited

Return true(1) if the pin is clear otherwise false(0).

Returns
boolean.

Definition at line 152 of file Pin.hh.

bool Pin::is_off ( ) const
inlineinherited

Return true(1) if the pin is clear otherwise false(0).

Returns
boolean.

Definition at line 162 of file Pin.hh.

bool Pin::is_on ( ) const
inlineinherited

Return true(1) if the pin is set otherwise false(0).

Returns
boolean.

Definition at line 132 of file Pin.hh.

bool Pin::is_set ( ) const
inlineinherited

Return true(1) if the pin is set otherwise false(0).

Returns
boolean.

Definition at line 112 of file Pin.hh.

bool DHT::is_valid ( )
protectedinherited

Validate received data block. Return true if valid data with correct check sum otherwise false.

Returns
bool.

Definition at line 116 of file DHT.cpp.

void OutputPin::low ( ) const
inlineinherited

Clear the output pin.

Note
atomic

Definition at line 134 of file OutputPin.hh.

static uint8_t Pin::MASK ( uint8_t  pin)
inlinestaticinherited

Return bit mask for given Arduino pin number.

Parameters
[in]pinnumber.
Returns
pin bit mask.

Definition at line 42 of file Pin.hh.

void IOPin::mode ( Mode  mode)
inlineinherited

Change IO-pin to given mode.

Parameters
[in]modenew operation mode.
Note
atomic

Definition at line 56 of file IOPin.hh.

static void OutputPin::mode ( Board::DigitalPin  pin,
uint8_t  initial = 0 
)
inlinestaticinherited

Set output pin to mode.

Parameters
[in]pinnumber.
[in]initialvalue.
Note
atomic

Definition at line 56 of file OutputPin.hh.

Mode IOPin::mode ( ) const
inlineinherited

Get current IO-pin mode.

Returns
mode.

Definition at line 71 of file IOPin.hh.

static void IOPin::mode ( Board::DigitalPin  pin,
Mode  mode 
)
inlinestaticinherited

Change IO-pin to given mode.

Parameters
[in]pinnumber.
[in]modenew operation mode.
Note
atomic

Definition at line 83 of file IOPin.hh.

static Mode IOPin::mode ( Board::DigitalPin  pin)
inlinestaticinherited

Get IO-pin mode.

Parameters
[in]pinnumber.
Returns
mode.

Definition at line 101 of file IOPin.hh.

void OutputPin::off ( ) const
inlineinherited

Clear the output pin.

Note
atomic

Definition at line 144 of file OutputPin.hh.

void OutputPin::on ( ) const
inlineinherited

Set the output pin.

Note
atomic

Definition at line 105 of file OutputPin.hh.

void DHT::on_interrupt ( uint16_t  arg = 0)
protectedvirtualinherited

The device driver interrupt level state machine.

Parameters
[in]argargument from interrupt service routine.

Reimplemented from ExternalInterrupt.

Definition at line 35 of file DHT.cpp.

virtual void DHT::on_sample_completed ( bool  valid)
inlineprotectedvirtualinherited

Callback when data sample is completed. Called from interrupt service routine. Typically used to push an event for further processing. Default implementation is an empty function.

Parameters
[in]validdata received and adjusted.

Definition at line 131 of file DHT.hh.

Pin::operator bool ( ) const
inlineinherited

Read input pin and assign variable.

Parameters
[out]varto assign.
Returns
pin.

Definition at line 204 of file Pin.hh.

OutputPin& OutputPin::operator<< ( int  value)
inlineinherited

Set the output pin with the given value. Zero(0) to clear and non-zero to set. Output operator syntax.

Parameters
[in]valueto write.
Returns
output pin.

Definition at line 364 of file OutputPin.hh.

Pin& Pin::operator>> ( uint8_t &  var)
inlineinherited

Read input pin and assign variable.

Parameters
[out]varto assign.
Returns
pin.

Definition at line 215 of file Pin.hh.

volatile uint8_t* Pin::PCIMR ( ) const
inlineprotectedinherited

Return pin change interrupt mask register.

Returns
pin change mask register pointer.

Definition at line 263 of file Pin.hh.

static volatile uint8_t* Pin::PIN ( uint8_t  pin)
inlinestaticinherited

Return pointer to PIN register.

Parameters
[in]pinnumber.
Returns
PIN register pointer.

Definition at line 53 of file Pin.hh.

uint8_t Pin::pin ( ) const
inlineinherited

Return Arduino pin number of abstract pin.

Returns
pin number.

Definition at line 103 of file Pin.hh.

volatile uint8_t* Pin::PIN ( ) const
inlineprotectedinherited

Return pointer to PIN register.

Returns
PIN register pointer.

Definition at line 236 of file Pin.hh.

static volatile uint8_t* Pin::PORT ( uint8_t  pin)
inlinestaticinherited

Return pointer to data PORT register.

Parameters
[in]pinnumber.
Returns
PORT register pointer.

Definition at line 75 of file Pin.hh.

volatile uint8_t* Pin::PORT ( ) const
inlineprotectedinherited

Return pointer to data PORT register.

Returns
PORT register pointer.

Definition at line 254 of file Pin.hh.

void OutputPin::pulse ( uint16_t  us) const
inlineinherited

Toggle the output pin to form a pulse with given width in micro-seconds.

Parameters
[in]uspulse width in micro seconds
Note
atomic
pulse width is limited to 1000 us

Definition at line 378 of file OutputPin.hh.

bool Pin::read ( ) const
inlineinherited

Return true(1) if the pin is set otherwise false(0).

Returns
boolean.

Definition at line 172 of file Pin.hh.

uint8_t Pin::read ( OutputPin clk,
Direction  order = MSB_FIRST 
) const
inherited

Shift in a byte from the input pin using the given clock output pin. Shift in according to given direction.

Parameters
[in]clkoutput pin.
[in]orderbit first.
Returns
value.

Definition at line 25 of file Pin.cpp.

static bool Pin::read ( Board::DigitalPin  pin)
inlinestaticinherited

Use pin number directly to read value. Does not require an instance. Return true(1) if the pin is set otherwise false(0).

Parameters
[in]pinnumber.
Returns
boolean.

Definition at line 193 of file Pin.hh.

bool DHT::sample ( )
inlineinherited

Read temperature and humidity from the device. Return true(1) and values if successful otherwise false(0).

Returns
bool.

Definition at line 94 of file DHT.hh.

bool DHT::sample ( int16_t &  humidity,
int16_t &  temperature 
)
inlineinherited

Read temperature and humidity from the device. Return true(1) and values if successful otherwise false(0).

Parameters
[out]humidityreading.
[out]temperaturereading.
Returns
bool.

Definition at line 107 of file DHT.hh.

bool DHT::sample_await ( )
inherited

Wait for a sample request to complete. Return true(1) and values if successful otherwise false(0).

Returns
bool.

Definition at line 102 of file DHT.cpp.

bool DHT::sample_request ( )
inherited

Initiate a sample request from the device. Return true(1) and values if successful otherwise false(0).

Returns
bool.

Definition at line 83 of file DHT.cpp.

void OutputPin::set ( ) const
inlineinherited

Set the output pin.

Note
atomic

Definition at line 85 of file OutputPin.hh.

void OutputPin::set ( int  value) const
inlineinherited

Set the output pin with the given value. Zero(0) to clear and non-zero to set.

Parameters
[in]valueto set.
Note
atomic

Definition at line 201 of file OutputPin.hh.

int16_t DHT::temperature ( ) const
inlineinherited

Return temperature from latest read.

Returns
temperature.

Definition at line 70 of file DHT.hh.

void OutputPin::toggle ( ) const
inlineinherited

Toggle the output pin.

Note
atomic

Definition at line 163 of file OutputPin.hh.

static void OutputPin::toggle ( Board::DigitalPin  pin)
inlinestaticinherited

Toggle the output pin. Atomic per definition

Parameters
[in]pinnumber.

Definition at line 173 of file OutputPin.hh.

void OutputPin::write ( int  value) const
inlineinherited

Set the output pin with the given value. Zero(0) to clear and non-zero to set.

Parameters
[in]valueto write.
Note
atomic

Definition at line 236 of file OutputPin.hh.

void OutputPin::write ( uint8_t  value,
OutputPin clk,
Direction  order = MSB_FIRST 
) const
inherited

Shift out given byte to the output pin using the given clock output pin. Shift out according to given direction. Data (bits) are transfered on clock transition. Interrupts are allowed during the shift out.

Parameters
[in]valueto write.
[in]clkoutput pin.
[in]orderbit first.

Definition at line 24 of file OutputPin.cpp.

void OutputPin::write ( uint16_t  value,
uint8_t  bits,
uint16_t  us 
) const
inherited

Shift out given value and number of bits to the output pin using the given pulse length in micro-seconds. Shift out from LSB(0) to MSB(bits-1). This operation is synchronized and will turn off interrupt handling during the transmission period.

Parameters
[in]valueto write.
[in]bitsto write.
[in]usmicro-second bit period.
Note
atomic
us should not exceed 1000

Definition at line 46 of file OutputPin.cpp.

static void OutputPin::write ( Board::DigitalPin  pin,
int  value 
)
inlinestaticinherited

Set the given output pin with the given value. Zero(0) to clear and non-zero to set.

Parameters
[in]pinnumber.
[in]valueto write.
Note
atomic

Definition at line 306 of file OutputPin.hh.

Member Data Documentation

const uint16_t DHT::BIT_THRESHOLD = 100
staticprotectedinherited

Definition at line 166 of file DHT.hh.

const uint8_t DHT::DATA_LAST = DATA_MAX - 1
staticprotectedinherited

Last data element index

Definition at line 173 of file DHT.hh.

const uint8_t DHT::DATA_MAX = 5
staticprotectedinherited

Size of data buffer

Definition at line 170 of file DHT.hh.

const uint16_t DHT::HIGH_THRESHOLD = 200
staticprotectedinherited

Definition at line 167 of file DHT.hh.

const int16_t DHT::INIT_HUMIDITY_SAMPLE = 1000
staticinherited

Initial humidity; 100.0 % RH.

Definition at line 37 of file DHT.hh.

const int16_t DHT::INIT_TEMPERATURE_SAMPLE = 850
staticinherited

Initial temperature; 85 C.

Definition at line 40 of file DHT.hh.

const uint16_t DHT::LOW_THRESHOLD = 50
staticprotectedinherited

Sample thresholds.

Definition at line 165 of file DHT.hh.

uint8_t DHT::m_bits
protectedinherited

Current number of bits read.

Definition at line 198 of file DHT.hh.

data_t DHT::m_data
protectedinherited

Current data being transfered.

Definition at line 204 of file DHT.hh.

int16_t DHT::m_humidity
protectedinherited

Definition at line 207 of file DHT.hh.

uint8_t DHT::m_ix
protectedinherited

Current data byte index stream.

Definition at line 201 of file DHT.hh.

const uint8_t Pin::m_mask
protectedinherited

Pin mask in port.

Definition at line 227 of file Pin.hh.

const uint8_t Pin::m_pin
protectedinherited

Pin identity.

Definition at line 230 of file Pin.hh.

volatile uint8_t* const Pin::m_sfr
protectedinherited

Special function register pointer.

Definition at line 224 of file Pin.hh.

uint16_t DHT::m_start
protectedinherited

Micro-seconds since latest rising of data signal; pulse start.

Definition at line 192 of file DHT.hh.

volatile uint8_t DHT::m_state
protectedinherited

State of device driver.

Definition at line 189 of file DHT.hh.

int16_t DHT::m_temperature
protectedinherited

Definition at line 210 of file DHT.hh.

uint8_t DHT::m_value
protectedinherited

Current byte being read from device.

Definition at line 195 of file DHT.hh.

const uint16_t DHT::MIN_PERIOD = 2048
staticprotectedinherited

Minimum periodic wait (approx. 2 seconds).

Definition at line 162 of file DHT.hh.


The documentation for this class was generated from the following files: