DUDS
Distributed Update of Data from Something
duds::hardware::devices::instruments::AM2320 Class Reference

Support for the AM2320, a temperature and relative humidity sensor with poorly written English documentation. More...

#include <AM2320.hpp>

Inheritance diagram for duds::hardware::devices::instruments::AM2320:
Collaboration diagram for duds::hardware::devices::instruments::AM2320:

Public Member Functions

 AM2320 (std::unique_ptr< duds::hardware::interface::I2c > &c)
 Only address is 0x5C. More...
 
std::uint16_t rawRelHumid () const
 Ten times precentage. More...
 
std::int16_t rawTemp () const
 Ten times Celsius. More...
 
duds::data::Quantity relHumidity () const
 Returns the unitless relative humidity quantity. More...
 
void sample ()
 Reads in the last sample and causes the device to start another sample. More...
 
duds::data::Quantity temperature () const
 Returns the temperature in Kelvin. More...
 

Private Attributes

std::unique_ptr< duds::hardware::interface::I2ccom
 The I2C communication interface. More...
 
duds::hardware::interface::Conversation read
 Used to read in sampled data from the device. More...
 
std::uint16_t rh
 Relative humidity. More...
 
std::int16_t t
 Temperature. More...
 
duds::hardware::interface::Conversation wake
 Used to awaken the device; needed initially and after 3 or more seconds of not talking to the device. More...
 

Detailed Description

Support for the AM2320, a temperature and relative humidity sensor with poorly written English documentation.

High processor loads on single-core systems prevents this code from successfully reading data from a AM2320. This doesn't happen with other I2C connected instruments on the same system. Not sure how to fix this.

This part occasionally fails during reads. To mitigate the problem, sample() will make multiple read attempts after it sends the wake message.

Bug:
The CRC value is not checked because the calculated value never matches what the device sends. Even using the code from the datasheet produces the same non-matching result computed here.
Author
Jeff Jackowski
Examples:
am2320test.cpp.

Definition at line 44 of file AM2320.hpp.

Constructor & Destructor Documentation

◆ AM2320()

duds::hardware::devices::instruments::AM2320::AM2320 ( std::unique_ptr< duds::hardware::interface::I2c > &  c)

Only address is 0x5C.

Calls sample(); the received data will be either old or invalid.

Definition at line 22 of file AM2320.cpp.

Member Function Documentation

◆ rawRelHumid()

std::uint16_t duds::hardware::devices::instruments::AM2320::rawRelHumid ( ) const
inline

Ten times precentage.

Definition at line 116 of file AM2320.hpp.

◆ rawTemp()

std::int16_t duds::hardware::devices::instruments::AM2320::rawTemp ( ) const
inline

Ten times Celsius.

Definition at line 122 of file AM2320.hpp.

◆ relHumidity()

duds::data::Quantity duds::hardware::devices::instruments::AM2320::relHumidity ( ) const

Returns the unitless relative humidity quantity.

Examples:
am2320test.cpp.

Definition at line 119 of file AM2320.cpp.

◆ sample()

void duds::hardware::devices::instruments::AM2320::sample ( )

Reads in the last sample and causes the device to start another sample.

About two seconds after this function is done, the new sample will be available for reading by another call to sample(). This function's results are always a sample behind the most current data. Sampling takes two seconds to complete, so calling this function more often is not helpful.

These devices can fail on read for no clear reason. To mitigate the issue, up to three attempst are made at reading in the sample. Each attempt takes a little more than 10ms.

Note
Sampling is documented to cause some internal heating in the device that skews the sampled values. This does seem to occur at the maximum sampling rate (0.5Hz). It seems that the data is better when the sampling rate is no faster than once every four seconds. Longer times between sampling may require two samples in two seconds to deal with the fact that the first sample in the pair will be old.
Exceptions
duds::hardware::interface::I2cErrorThis occurs too often on parts that should be good. The first two such errors on read attempts are ignored; the next is thrown.
DeviceMisidentifiedThe device's response did not properly echo the command byte and requested data length. This likely means the device is not an AM2320.
AM2320CrcErrorThe data and the CRC received are not consistent with each other.
Todo:
Record time of sample to assign to data read by the next call to sample(). Skip using wake if the last call to sample() was less than 3 seconds (maybe 2.5 to avoid issues if system running slow).
Bug:
The CRC value is not checked because the calculated value never matches what the device sends. Even using the code from the datasheet produces the same non-matching result computed here.
Examples:
am2320test.cpp.

Definition at line 42 of file AM2320.cpp.

Referenced by AM2320().

◆ temperature()

duds::data::Quantity duds::hardware::devices::instruments::AM2320::temperature ( ) const

Returns the temperature in Kelvin.

Examples:
am2320test.cpp.

Definition at line 123 of file AM2320.cpp.

Member Data Documentation

◆ com

std::unique_ptr<duds::hardware::interface::I2c> duds::hardware::devices::instruments::AM2320::com
private

The I2C communication interface.

Definition at line 48 of file AM2320.hpp.

Referenced by AM2320(), and sample().

◆ read

duds::hardware::interface::Conversation duds::hardware::devices::instruments::AM2320::read
private

Used to read in sampled data from the device.

Definition at line 57 of file AM2320.hpp.

Referenced by AM2320(), and sample().

◆ rh

std::uint16_t duds::hardware::devices::instruments::AM2320::rh
private

Relative humidity.

Definition at line 61 of file AM2320.hpp.

Referenced by relHumidity(), and sample().

◆ t

std::int16_t duds::hardware::devices::instruments::AM2320::t
private

Temperature.

Definition at line 65 of file AM2320.hpp.

Referenced by sample(), and temperature().

◆ wake

duds::hardware::interface::Conversation duds::hardware::devices::instruments::AM2320::wake
private

Used to awaken the device; needed initially and after 3 or more seconds of not talking to the device.

Definition at line 53 of file AM2320.hpp.

Referenced by AM2320(), and sample().


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