DHT22Gen3_RK
Public Types | Public Member Functions | Static Public Attributes | Protected Member Functions | Protected Attributes
DHT22Gen3 Class Reference

Class for interfacing with one or more DHT22 sensors on a Gen3 Particle device. More...

#include <DHT22Gen3_RK.h>

Public Types

enum  State { State::IDLE_STATE, State::START_STATE, State::SEND_START_STATE, State::SAMPLING_STATE }
 Enumeration for possible states for the Finite State Machine. More...
 

Public Member Functions

 DHT22Gen3 (pin_t unusedPin1, pin_t unusedPin2)
 Initialize the DHT22Gen3 driver. More...
 
void setup ()
 Call from setup() to initialize the driver.
 
void loop ()
 Call from loop(), preferably every call to loop. More...
 
void getSample (pin_t dhtPin, std::function< void(DHTSample)> completion, DHTSensorType *sensorType=&sensorTypeDHT22)
 Get a sample on the specified pin. More...
 
bool canGetSample () const
 Returns true if you can call getSample(). Returns false if another call is still in progress.
 
DHTSample getLastResult () const
 Gets the last result if you want to poll instead of use the completion function.
 
DHT22Gen3withMaxTries (int tries)
 Maximum number of attempts to get a valid result (passes checksum). Default is 4. More...
 

Static Public Attributes

static DHTSensorTypeDHT11 sensorTypeDHT11
 Pass a pointer to sensorTypeDHT11 to getSamples() for DHT11 sensors.
 
static DHTSensorTypeDHT22 sensorTypeDHT22
 Pass a pointer to sensorTypeDHT22 to getSamples() for DHT22 sensors. This is the default.
 

Protected Member Functions

void callCompletion (DHTSample::SampleResult sampleResult)
 Used internally to call the completion handler. More...
 

Protected Attributes

pin_t unusedPin1
 Pin to output SCK (not used by DHT22, but unfortunately required by I2S)
 
pin_t unusedPin2
 Pin to output LRCK (not used by DHT22, but unfortunately required by I2S)
 
pin_t dhtPin = 0
 Pin to communicate with DHT22. Set by getSample()
 
DHTSensorTypesensorType = &sensorTypeDHT22
 Sensor type, optional parameter to getSample();.
 
unsigned long lastRequestTime = 0
 millis() value at last request. Used to prevent querying more often than minSamplePeriodMs (2 seconds)
 
unsigned long stateTime = 0
 millis() value used with state transitions
 
int maxTries = 4
 Maximum number of retries on checksum values. Default is 4. Each retry takes 2.5 seconds.
 
State state = State::IDLE_STATE
 State of the finite state machine.
 
DHTSample result
 Result that will be passed to the callback (by value)
 
std::function< void(DHTSample)> completion = 0
 Completion handler function or lambda. Set by getSample(). May be 0.
 

Detailed Description

Class for interfacing with one or more DHT22 sensors on a Gen3 Particle device.

Only allocate one of these, typically as a global variable, per device.

Be sure to call the setup() and loop() methods from the actual setup and loop.

Member Enumeration Documentation

◆ State

enum DHT22Gen3::State
strong

Enumeration for possible states for the Finite State Machine.

Enumerator
IDLE_STATE 

Idle, can call getSample()

START_STATE 

Getting ready to get a sample.

SEND_START_STATE 

Sending the start bit and starting the I2S peripheral.

SAMPLING_STATE 

Capturing samples.

Constructor & Destructor Documentation

◆ DHT22Gen3()

DHT22Gen3::DHT22Gen3 ( pin_t  unusedPin1,
pin_t  unusedPin2 
)

Initialize the DHT22Gen3 driver.

Parameters
unusedPin1An unused GPIO pin to use for SCK. It's unfortunate, but for the I2S peripheral to work, SCK must be mapped to a valid pin. It will be set to output and there will be 512 KHz signal output on this pin.
unusedPin2An unused GPIO pin to use for LRCK. It's unfortunate, but for the I2S peripheral to work, LRCK must be mapped to a valid pin. It will be set to output and there will be 32 KHz signal output on this pin. It must be a different pin than unusedPin1.
  • You will often allocate this object as a global variable.
  • There can only be one per device.
  • Make sure you call setup() and loop() methods from your actual setup() and loop()!

Member Function Documentation

◆ callCompletion()

void DHT22Gen3::callCompletion ( DHTSample::SampleResult  sampleResult)
protected

Used internally to call the completion handler.

Goes into IDLE state after calling the completion

◆ getSample()

void DHT22Gen3::getSample ( pin_t  dhtPin,
std::function< void(DHTSample)>  completion,
DHTSensorType sensorType = &sensorTypeDHT22 
)

Get a sample on the specified pin.

Parameters
dhtPinThe pin the sensor is connected to (A0, A1, ..., D0, D1, ...) can also use special pins when the port is not being use, for example, TX, RX, MISO, MOSI, SCK.
completionA function or C++ lambda to call when the operation completes.
sensorTypeOptional. Default to &sensorTypeDHT22. Can also be &sensorTypeDHT11.

This function is asynchronous and the completion function is called when done.

Normal operation takes 24 milliseconds. If a checksum failure occurs, each retry takes 2 seconds because the DHT22 cannot get new samples faster than that. So with 4 retries, it could take about 9 seconds.

◆ loop()

void DHT22Gen3::loop ( )

Call from loop(), preferably every call to loop.

This allows the completion function to be dispatched from normal loop execution time, which allows normal calls like Particle.publish to be called safely.

◆ withMaxTries()

DHT22Gen3& DHT22Gen3::withMaxTries ( int  tries)
inline

Maximum number of attempts to get a valid result (passes checksum). Default is 4.

Note that each retry takes 2.25 seconds, so you may not want to set the value too high.


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