arduinoprojects

git clone https://git.tarina.org/arduinoprojects
Log | Files | Refs

DHT_U.cpp (6438B)


      1 /*!
      2  *  @file DHT_U.cpp
      3  *
      4  *  Temperature & Humidity Unified Sensor Library
      5  *
      6  *  This is a library for DHT series of low cost temperature/humidity sensors.
      7  *
      8  *  You must have Adafruit Unified Sensor Library library installed to use this
      9  * class.
     10  *
     11  *  Adafruit invests time and resources providing this open source code,
     12  *  please support Adafruit andopen-source hardware by purchasing products
     13  *  from Adafruit!
     14  */
     15 #include "DHT_U.h"
     16 
     17 /*!
     18  *  @brief  Instantiates a new DHT_Unified class
     19  *  @param  pin
     20  *          pin number that sensor is connected
     21  *  @param  type
     22  *          type of sensor
     23  *  @param  count
     24  *          number of sensors
     25  *  @param  tempSensorId
     26  *          temperature sensor id
     27  *  @param  humiditySensorId
     28  *          humidity sensor id
     29  */
     30 DHT_Unified::DHT_Unified(uint8_t pin, uint8_t type, uint8_t count,
     31                          int32_t tempSensorId, int32_t humiditySensorId)
     32     : _dht(pin, type, count), _type(type), _temp(this, tempSensorId),
     33       _humidity(this, humiditySensorId) {}
     34 
     35 /*!
     36  *  @brief  Setup sensor (calls begin on It)
     37  */
     38 void DHT_Unified::begin() { _dht.begin(); }
     39 
     40 /*!
     41  *  @brief  Sets sensor name
     42  *  @param  sensor
     43  *          Sensor that will be set
     44  */
     45 void DHT_Unified::setName(sensor_t *sensor) {
     46   switch (_type) {
     47   case DHT11:
     48     strncpy(sensor->name, "DHT11", sizeof(sensor->name) - 1);
     49     break;
     50   case DHT12:
     51     strncpy(sensor->name, "DHT12", sizeof(sensor->name) - 1);
     52     break;
     53   case DHT21:
     54     strncpy(sensor->name, "DHT21", sizeof(sensor->name) - 1);
     55     break;
     56   case DHT22:
     57     strncpy(sensor->name, "DHT22", sizeof(sensor->name) - 1);
     58     break;
     59   default:
     60     // TODO: Perhaps this should be an error?  However main DHT library doesn't
     61     // enforce restrictions on the sensor type value.  Pick a generic name for
     62     // now.
     63     strncpy(sensor->name, "DHT?", sizeof(sensor->name) - 1);
     64     break;
     65   }
     66   sensor->name[sizeof(sensor->name) - 1] = 0;
     67 }
     68 
     69 /*!
     70  *  @brief  Sets Minimum Delay Value
     71  *  @param  sensor
     72  *          Sensor that will be set
     73  */
     74 void DHT_Unified::setMinDelay(sensor_t *sensor) {
     75   switch (_type) {
     76   case DHT11:
     77     sensor->min_delay = 1000000L; // 1 second (in microseconds)
     78     break;
     79   case DHT12:
     80     sensor->min_delay = 2000000L; // 2 second (in microseconds)
     81     break;
     82   case DHT21:
     83     sensor->min_delay = 2000000L; // 2 seconds (in microseconds)
     84     break;
     85   case DHT22:
     86     sensor->min_delay = 2000000L; // 2 seconds (in microseconds)
     87     break;
     88   default:
     89     // Default to slowest sample rate in case of unknown type.
     90     sensor->min_delay = 2000000L; // 2 seconds (in microseconds)
     91     break;
     92   }
     93 }
     94 
     95 /*!
     96  *  @brief  Instantiates a new DHT_Unified Temperature Class
     97  *  @param  parent
     98  *          Parent Sensor
     99  *  @param  id
    100  *          Sensor id
    101  */
    102 DHT_Unified::Temperature::Temperature(DHT_Unified *parent, int32_t id)
    103     : _parent(parent), _id(id) {}
    104 
    105 /*!
    106  *  @brief  Reads the sensor and returns the data as a sensors_event_t
    107  *  @param  event
    108  *  @return always returns true
    109  */
    110 bool DHT_Unified::Temperature::getEvent(sensors_event_t *event) {
    111   // Clear event definition.
    112   memset(event, 0, sizeof(sensors_event_t));
    113   // Populate sensor reading values.
    114   event->version = sizeof(sensors_event_t);
    115   event->sensor_id = _id;
    116   event->type = SENSOR_TYPE_AMBIENT_TEMPERATURE;
    117   event->timestamp = millis();
    118   event->temperature = _parent->_dht.readTemperature();
    119 
    120   return true;
    121 }
    122 
    123 /*!
    124  *  @brief  Provides the sensor_t data for this sensor
    125  *  @param  sensor
    126  */
    127 void DHT_Unified::Temperature::getSensor(sensor_t *sensor) {
    128   // Clear sensor definition.
    129   memset(sensor, 0, sizeof(sensor_t));
    130   // Set sensor name.
    131   _parent->setName(sensor);
    132   // Set version and ID
    133   sensor->version = DHT_SENSOR_VERSION;
    134   sensor->sensor_id = _id;
    135   // Set type and characteristics.
    136   sensor->type = SENSOR_TYPE_AMBIENT_TEMPERATURE;
    137   _parent->setMinDelay(sensor);
    138   switch (_parent->_type) {
    139   case DHT11:
    140     sensor->max_value = 50.0F;
    141     sensor->min_value = 0.0F;
    142     sensor->resolution = 2.0F;
    143     break;
    144   case DHT12:
    145     sensor->max_value = 60.0F;
    146     sensor->min_value = -20.0F;
    147     sensor->resolution = 0.5F;
    148     break;
    149   case DHT21:
    150     sensor->max_value = 80.0F;
    151     sensor->min_value = -40.0F;
    152     sensor->resolution = 0.1F;
    153     break;
    154   case DHT22:
    155     sensor->max_value = 125.0F;
    156     sensor->min_value = -40.0F;
    157     sensor->resolution = 0.1F;
    158     break;
    159   default:
    160     // Unknown type, default to 0.
    161     sensor->max_value = 0.0F;
    162     sensor->min_value = 0.0F;
    163     sensor->resolution = 0.0F;
    164     break;
    165   }
    166 }
    167 
    168 /*!
    169  *  @brief  Instantiates a new DHT_Unified Humidity Class
    170  *  @param  parent
    171  *          Parent Sensor
    172  *  @param  id
    173  *          Sensor id
    174  */
    175 DHT_Unified::Humidity::Humidity(DHT_Unified *parent, int32_t id)
    176     : _parent(parent), _id(id) {}
    177 
    178 /*!
    179  *  @brief  Reads the sensor and returns the data as a sensors_event_t
    180  *  @param  event
    181  *  @return always returns true
    182  */
    183 bool DHT_Unified::Humidity::getEvent(sensors_event_t *event) {
    184   // Clear event definition.
    185   memset(event, 0, sizeof(sensors_event_t));
    186   // Populate sensor reading values.
    187   event->version = sizeof(sensors_event_t);
    188   event->sensor_id = _id;
    189   event->type = SENSOR_TYPE_RELATIVE_HUMIDITY;
    190   event->timestamp = millis();
    191   event->relative_humidity = _parent->_dht.readHumidity();
    192 
    193   return true;
    194 }
    195 
    196 /*!
    197  *  @brief  Provides the sensor_t data for this sensor
    198  *  @param  sensor
    199  */
    200 void DHT_Unified::Humidity::getSensor(sensor_t *sensor) {
    201   // Clear sensor definition.
    202   memset(sensor, 0, sizeof(sensor_t));
    203   // Set sensor name.
    204   _parent->setName(sensor);
    205   // Set version and ID
    206   sensor->version = DHT_SENSOR_VERSION;
    207   sensor->sensor_id = _id;
    208   // Set type and characteristics.
    209   sensor->type = SENSOR_TYPE_RELATIVE_HUMIDITY;
    210   _parent->setMinDelay(sensor);
    211   switch (_parent->_type) {
    212   case DHT11:
    213     sensor->max_value = 80.0F;
    214     sensor->min_value = 20.0F;
    215     sensor->resolution = 5.0F;
    216     break;
    217   case DHT12:
    218     sensor->max_value = 95.0F;
    219     sensor->min_value = 20.0F;
    220     sensor->resolution = 5.0F;
    221     break;
    222   case DHT21:
    223     sensor->max_value = 100.0F;
    224     sensor->min_value = 0.0F;
    225     sensor->resolution = 0.1F;
    226     break;
    227   case DHT22:
    228     sensor->max_value = 100.0F;
    229     sensor->min_value = 0.0F;
    230     sensor->resolution = 0.1F;
    231     break;
    232   default:
    233     // Unknown type, default to 0.
    234     sensor->max_value = 0.0F;
    235     sensor->min_value = 0.0F;
    236     sensor->resolution = 0.0F;
    237     break;
    238   }
    239 }