DHT22Gen3_RK
|
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. | |
DHT22Gen3 & | withMaxTries (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() | |
DHTSensorType * | sensorType = &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. | |
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.
|
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. |
DHT22Gen3::DHT22Gen3 | ( | pin_t | unusedPin1, |
pin_t | unusedPin2 | ||
) |
Initialize the DHT22Gen3 driver.
unusedPin1 | An 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. |
unusedPin2 | An 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. |
|
protected |
Used internally to call the completion handler.
Goes into IDLE state after calling the completion
void DHT22Gen3::getSample | ( | pin_t | dhtPin, |
std::function< void(DHTSample)> | completion, | ||
DHTSensorType * | sensorType = &sensorTypeDHT22 |
||
) |
Get a sample on the specified pin.
dhtPin | The 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. |
completion | A function or C++ lambda to call when the operation completes. |
sensorType | Optional. 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.
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.
|
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.