DHT11 Temperature & Humidity Sensor features a temperature & humidity sensor complex with a calibrated digital signal output.
DHT11.h@4:889926e2b6a5, 2019-08-09 (annotated)
- Committer:
- mcm
- Date:
- Fri Aug 09 07:39:17 2019 +0000
- Revision:
- 4:889926e2b6a5
- Parent:
- 3:082a970dca06
An example was added in order to show how to use this driver.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mcm | 1:ff93ee72c7a1 | 1 | /** |
mcm | 1:ff93ee72c7a1 | 2 | * @brief DHT11.h |
mcm | 1:ff93ee72c7a1 | 3 | * @details Temperature & humidity sensor complex with a calibrated digital signal output. |
mcm | 1:ff93ee72c7a1 | 4 | * Header file. |
mcm | 1:ff93ee72c7a1 | 5 | * |
mcm | 1:ff93ee72c7a1 | 6 | * |
mcm | 1:ff93ee72c7a1 | 7 | * @return N/A |
mcm | 1:ff93ee72c7a1 | 8 | * |
mcm | 1:ff93ee72c7a1 | 9 | * @author Manuel Caballero |
mcm | 1:ff93ee72c7a1 | 10 | * @date 08/August/2019 |
mcm | 1:ff93ee72c7a1 | 11 | * @version 08/August/2019 The ORIGIN |
mcm | 1:ff93ee72c7a1 | 12 | * @pre N/A |
mcm | 1:ff93ee72c7a1 | 13 | * @warning N/A |
mcm | 1:ff93ee72c7a1 | 14 | * @pre This code belongs to AqueronteBlog ( http://unbarquero.blogspot.com ). |
mcm | 1:ff93ee72c7a1 | 15 | */ |
mcm | 1:ff93ee72c7a1 | 16 | #ifndef DHT11_H_ |
mcm | 1:ff93ee72c7a1 | 17 | #define DHT11_H_ |
mcm | 1:ff93ee72c7a1 | 18 | |
mcm | 1:ff93ee72c7a1 | 19 | #include "mbed.h" |
mcm | 1:ff93ee72c7a1 | 20 | |
mcm | 1:ff93ee72c7a1 | 21 | |
mcm | 1:ff93ee72c7a1 | 22 | /** |
mcm | 1:ff93ee72c7a1 | 23 | Example: |
mcm | 1:ff93ee72c7a1 | 24 | |
mcm | 1:ff93ee72c7a1 | 25 | @code |
mcm | 4:889926e2b6a5 | 26 | #include "mbed.h" |
mcm | 4:889926e2b6a5 | 27 | #include "DHT11.h" |
mcm | 1:ff93ee72c7a1 | 28 | |
mcm | 4:889926e2b6a5 | 29 | DHT11 myDHT11 ( PB_8 ); // DHT11 --> PB_8 |
mcm | 4:889926e2b6a5 | 30 | Serial pc ( USBTX, USBRX ); // tx, rx |
mcm | 4:889926e2b6a5 | 31 | |
mcm | 4:889926e2b6a5 | 32 | DigitalOut myled ( LED1 ); |
mcm | 4:889926e2b6a5 | 33 | Ticker newAction; |
mcm | 4:889926e2b6a5 | 34 | |
mcm | 4:889926e2b6a5 | 35 | |
mcm | 4:889926e2b6a5 | 36 | //@brief Constants. |
mcm | 4:889926e2b6a5 | 37 | |
mcm | 4:889926e2b6a5 | 38 | |
mcm | 4:889926e2b6a5 | 39 | //@brief Variables. |
mcm | 4:889926e2b6a5 | 40 | volatile uint32_t myState; // State that indicates when to perform a new sample |
mcm | 4:889926e2b6a5 | 41 | |
mcm | 4:889926e2b6a5 | 42 | |
mcm | 4:889926e2b6a5 | 43 | //@brief FUNCTION PROTOTYPES |
mcm | 4:889926e2b6a5 | 44 | void changeDATA ( void ); |
mcm | 4:889926e2b6a5 | 45 | |
mcm | 4:889926e2b6a5 | 46 | |
mcm | 4:889926e2b6a5 | 47 | //@brief FUNCTION FOR APPLICATION MAIN ENTRY. |
mcm | 4:889926e2b6a5 | 48 | int main() |
mcm | 4:889926e2b6a5 | 49 | { |
mcm | 4:889926e2b6a5 | 50 | char myChecksum[] = "Ok"; |
mcm | 4:889926e2b6a5 | 51 | DHT11::DHT11_status_t aux; |
mcm | 4:889926e2b6a5 | 52 | DHT11::DHT11_data_t myDHT11_Data; |
mcm | 4:889926e2b6a5 | 53 | |
mcm | 4:889926e2b6a5 | 54 | pc.baud ( 115200 ); |
mcm | 4:889926e2b6a5 | 55 | |
mcm | 4:889926e2b6a5 | 56 | myled = 1; |
mcm | 4:889926e2b6a5 | 57 | wait(3); |
mcm | 4:889926e2b6a5 | 58 | myled = 0; |
mcm | 4:889926e2b6a5 | 59 | |
mcm | 4:889926e2b6a5 | 60 | // DHT11 starts: Release the bus |
mcm | 4:889926e2b6a5 | 61 | aux = myDHT11.DHT11_Init (); |
mcm | 4:889926e2b6a5 | 62 | |
mcm | 4:889926e2b6a5 | 63 | |
mcm | 4:889926e2b6a5 | 64 | myState = 0UL; // Reset the variable |
mcm | 4:889926e2b6a5 | 65 | newAction.attach( &changeDATA, 1U ); // the address of the function to be attached ( changeDATA ) and the interval ( 1s ) |
mcm | 4:889926e2b6a5 | 66 | |
mcm | 4:889926e2b6a5 | 67 | |
mcm | 4:889926e2b6a5 | 68 | // Let the callbacks take care of everything |
mcm | 4:889926e2b6a5 | 69 | while(1) { |
mcm | 4:889926e2b6a5 | 70 | sleep(); |
mcm | 4:889926e2b6a5 | 71 | |
mcm | 4:889926e2b6a5 | 72 | if ( myState == 1UL ) { |
mcm | 4:889926e2b6a5 | 73 | myled = 1U; |
mcm | 4:889926e2b6a5 | 74 | |
mcm | 4:889926e2b6a5 | 75 | // Get a new data |
mcm | 4:889926e2b6a5 | 76 | aux = myDHT11.DHT11_GetData ( &myDHT11_Data ); |
mcm | 4:889926e2b6a5 | 77 | |
mcm | 4:889926e2b6a5 | 78 | // Check checksum to validate data |
mcm | 4:889926e2b6a5 | 79 | if ( myDHT11_Data.checksumStatus == DHT11::DHT11_CHECKSUM_OK ) { |
mcm | 4:889926e2b6a5 | 80 | myChecksum[0] = 'O'; |
mcm | 4:889926e2b6a5 | 81 | myChecksum[1] = 'k'; |
mcm | 4:889926e2b6a5 | 82 | } else { |
mcm | 4:889926e2b6a5 | 83 | myChecksum[0] = 'E'; |
mcm | 4:889926e2b6a5 | 84 | myChecksum[1] = 'r'; |
mcm | 4:889926e2b6a5 | 85 | } |
mcm | 4:889926e2b6a5 | 86 | |
mcm | 4:889926e2b6a5 | 87 | // Send data through the UART |
mcm | 4:889926e2b6a5 | 88 | pc.printf ( "T: %d C | RH: %d %% | Checksum: %s\r\n", myDHT11_Data.temperature, myDHT11_Data.humidity, myChecksum ); |
mcm | 4:889926e2b6a5 | 89 | |
mcm | 4:889926e2b6a5 | 90 | |
mcm | 4:889926e2b6a5 | 91 | // Reset the variables |
mcm | 4:889926e2b6a5 | 92 | myState = 0UL; |
mcm | 4:889926e2b6a5 | 93 | myled = 0U; |
mcm | 4:889926e2b6a5 | 94 | } |
mcm | 4:889926e2b6a5 | 95 | } |
mcm | 4:889926e2b6a5 | 96 | } |
mcm | 4:889926e2b6a5 | 97 | |
mcm | 4:889926e2b6a5 | 98 | |
mcm | 4:889926e2b6a5 | 99 | // @brief changeDATA ( void ) |
mcm | 4:889926e2b6a5 | 100 | // |
mcm | 4:889926e2b6a5 | 101 | // @details It changes myState variable |
mcm | 4:889926e2b6a5 | 102 | // |
mcm | 4:889926e2b6a5 | 103 | // @param[in] N/A |
mcm | 4:889926e2b6a5 | 104 | // |
mcm | 4:889926e2b6a5 | 105 | // @param[out] N/A. |
mcm | 4:889926e2b6a5 | 106 | // |
mcm | 4:889926e2b6a5 | 107 | // @return N/A. |
mcm | 4:889926e2b6a5 | 108 | // |
mcm | 4:889926e2b6a5 | 109 | // @author Manuel Caballero |
mcm | 4:889926e2b6a5 | 110 | // @date 24/June/2019 |
mcm | 4:889926e2b6a5 | 111 | // @version 24/June/2019 The ORIGIN |
mcm | 4:889926e2b6a5 | 112 | // @pre N/A |
mcm | 4:889926e2b6a5 | 113 | // @warning N/A. |
mcm | 4:889926e2b6a5 | 114 | void changeDATA ( void ) |
mcm | 4:889926e2b6a5 | 115 | { |
mcm | 4:889926e2b6a5 | 116 | myState = 1UL; |
mcm | 4:889926e2b6a5 | 117 | } |
mcm | 1:ff93ee72c7a1 | 118 | @endcode |
mcm | 1:ff93ee72c7a1 | 119 | */ |
mcm | 1:ff93ee72c7a1 | 120 | |
mcm | 1:ff93ee72c7a1 | 121 | |
mcm | 1:ff93ee72c7a1 | 122 | /*! |
mcm | 3:082a970dca06 | 123 | Library for the DHT11 Temperature & humidity sensor complex with a calibrated digital signal output. |
mcm | 1:ff93ee72c7a1 | 124 | */ |
mcm | 1:ff93ee72c7a1 | 125 | class DHT11 |
mcm | 1:ff93ee72c7a1 | 126 | { |
mcm | 1:ff93ee72c7a1 | 127 | public: |
mcm | 1:ff93ee72c7a1 | 128 | /** |
mcm | 1:ff93ee72c7a1 | 129 | * @brief DEVICE DELAYS. |
mcm | 1:ff93ee72c7a1 | 130 | * NOTE: Values in microseconds. |
mcm | 1:ff93ee72c7a1 | 131 | */ |
mcm | 1:ff93ee72c7a1 | 132 | typedef enum { |
mcm | 1:ff93ee72c7a1 | 133 | DHT11_START_SIGNAL = 18000, /*!< Master: Start communication */ |
mcm | 1:ff93ee72c7a1 | 134 | DHT11_SAMPLE_DATA = 40, /*!< Sample data time */ |
mcm | 1:ff93ee72c7a1 | 135 | DHT11_WAIT_FOR_SENSOR_RESPONSE = 40 /*!< Master: Wait for sensor response */ |
mcm | 1:ff93ee72c7a1 | 136 | } DHT11_device_delays_t; |
mcm | 1:ff93ee72c7a1 | 137 | |
mcm | 1:ff93ee72c7a1 | 138 | |
mcm | 1:ff93ee72c7a1 | 139 | /** |
mcm | 1:ff93ee72c7a1 | 140 | * @brief DEVICE BUS STATUS. |
mcm | 1:ff93ee72c7a1 | 141 | * NOTE: N/A. |
mcm | 1:ff93ee72c7a1 | 142 | */ |
mcm | 1:ff93ee72c7a1 | 143 | typedef enum { |
mcm | 1:ff93ee72c7a1 | 144 | DHT11_PIN_HIGH = 1U, /*!< Pin high */ |
mcm | 1:ff93ee72c7a1 | 145 | DHT11_PIN_LOW = 0U, /*!< Pin low */ |
mcm | 1:ff93ee72c7a1 | 146 | DHT11_PIN_UNKNOWN = 2U /*!< Pin unknown */ |
mcm | 1:ff93ee72c7a1 | 147 | } DHT11_device_bus_status_t; |
mcm | 1:ff93ee72c7a1 | 148 | |
mcm | 1:ff93ee72c7a1 | 149 | |
mcm | 1:ff93ee72c7a1 | 150 | /** |
mcm | 1:ff93ee72c7a1 | 151 | * @brief CHECKSUM STATUS. |
mcm | 1:ff93ee72c7a1 | 152 | * NOTE: N/A. |
mcm | 1:ff93ee72c7a1 | 153 | */ |
mcm | 1:ff93ee72c7a1 | 154 | typedef enum { |
mcm | 1:ff93ee72c7a1 | 155 | DHT11_CHECKSUM_OK = 0U, /*!< Checksum correct */ |
mcm | 1:ff93ee72c7a1 | 156 | DHT11_CHECKSUM_ERROR = 1U /*!< Checksum error */ |
mcm | 1:ff93ee72c7a1 | 157 | } DHT11_checksum_status_t; |
mcm | 1:ff93ee72c7a1 | 158 | |
mcm | 1:ff93ee72c7a1 | 159 | |
mcm | 1:ff93ee72c7a1 | 160 | /** |
mcm | 1:ff93ee72c7a1 | 161 | * @brief TIMEOUT. |
mcm | 1:ff93ee72c7a1 | 162 | * NOTE: This value is just an estimation, the user must check it out. |
mcm | 1:ff93ee72c7a1 | 163 | */ |
mcm | 1:ff93ee72c7a1 | 164 | typedef enum { |
mcm | 1:ff93ee72c7a1 | 165 | DHT11_TIMEOUT = 0x23232 /*!< Timeout like a counter */ |
mcm | 1:ff93ee72c7a1 | 166 | } DHT11_timeout_t; |
mcm | 1:ff93ee72c7a1 | 167 | |
mcm | 1:ff93ee72c7a1 | 168 | |
mcm | 1:ff93ee72c7a1 | 169 | |
mcm | 1:ff93ee72c7a1 | 170 | |
mcm | 1:ff93ee72c7a1 | 171 | /** |
mcm | 1:ff93ee72c7a1 | 172 | * @brief ERROR STATUS. INTERNAL CONSTANTS |
mcm | 1:ff93ee72c7a1 | 173 | */ |
mcm | 1:ff93ee72c7a1 | 174 | typedef enum { |
mcm | 1:ff93ee72c7a1 | 175 | DHT11_SUCCESS = 0U, /*!< Communication success */ |
mcm | 1:ff93ee72c7a1 | 176 | DHT11_FAILURE = 1U, /*!< Communication failure */ |
mcm | 1:ff93ee72c7a1 | 177 | DHT11_DATA_CORRUPTED = 2U, /*!< Checksum error */ |
mcm | 1:ff93ee72c7a1 | 178 | DHT11_BUS_TIMEOUT = 3U /*!< Bus response timeout/error */ |
mcm | 1:ff93ee72c7a1 | 179 | } DHT11_status_t; |
mcm | 1:ff93ee72c7a1 | 180 | |
mcm | 1:ff93ee72c7a1 | 181 | |
mcm | 1:ff93ee72c7a1 | 182 | |
mcm | 1:ff93ee72c7a1 | 183 | |
mcm | 1:ff93ee72c7a1 | 184 | |
mcm | 1:ff93ee72c7a1 | 185 | #ifndef DHT11_VECTOR_STRUCT_H |
mcm | 1:ff93ee72c7a1 | 186 | #define DHT11_VECTOR_STRUCT_H |
mcm | 1:ff93ee72c7a1 | 187 | typedef struct { |
mcm | 1:ff93ee72c7a1 | 188 | /* Outputs */ |
mcm | 1:ff93ee72c7a1 | 189 | uint8_t temperature; /*!< Temperature value */ |
mcm | 1:ff93ee72c7a1 | 190 | uint8_t humidity; /*!< Relative humidity value */ |
mcm | 1:ff93ee72c7a1 | 191 | |
mcm | 1:ff93ee72c7a1 | 192 | uint16_t rawTemperature; /*!< Temperature raw value */ |
mcm | 1:ff93ee72c7a1 | 193 | uint16_t rawHumidity; /*!< Relative humidity raw value */ |
mcm | 1:ff93ee72c7a1 | 194 | |
mcm | 1:ff93ee72c7a1 | 195 | uint8_t checksum; /*!< Checksum */ |
mcm | 1:ff93ee72c7a1 | 196 | |
mcm | 1:ff93ee72c7a1 | 197 | /* Checksum status */ |
mcm | 1:ff93ee72c7a1 | 198 | DHT11_checksum_status_t checksumStatus; /*!< Checksum status */ |
mcm | 1:ff93ee72c7a1 | 199 | } DHT11_data_t; |
mcm | 1:ff93ee72c7a1 | 200 | #endif |
mcm | 1:ff93ee72c7a1 | 201 | |
mcm | 1:ff93ee72c7a1 | 202 | |
mcm | 1:ff93ee72c7a1 | 203 | |
mcm | 1:ff93ee72c7a1 | 204 | |
mcm | 1:ff93ee72c7a1 | 205 | |
mcm | 1:ff93ee72c7a1 | 206 | |
mcm | 1:ff93ee72c7a1 | 207 | /** |
mcm | 1:ff93ee72c7a1 | 208 | * @brief FUNCTION PROTOTYPES |
mcm | 1:ff93ee72c7a1 | 209 | */ |
mcm | 2:9fe166c478b7 | 210 | /** Create an DHT11 object connected to the specified DHT11 device pin. |
mcm | 2:9fe166c478b7 | 211 | * |
mcm | 2:9fe166c478b7 | 212 | * @param dht11 Data pin |
mcm | 2:9fe166c478b7 | 213 | */ |
mcm | 2:9fe166c478b7 | 214 | DHT11 ( PinName dht11 ); |
mcm | 2:9fe166c478b7 | 215 | |
mcm | 2:9fe166c478b7 | 216 | /** Delete DHT11 object. |
mcm | 2:9fe166c478b7 | 217 | */ |
mcm | 2:9fe166c478b7 | 218 | ~DHT11(); |
mcm | 4:889926e2b6a5 | 219 | |
mcm | 1:ff93ee72c7a1 | 220 | /** It configures the GPIO peripheral. |
mcm | 1:ff93ee72c7a1 | 221 | */ |
mcm | 3:082a970dca06 | 222 | DHT11_status_t DHT11_Init ( void ); |
mcm | 1:ff93ee72c7a1 | 223 | |
mcm | 1:ff93ee72c7a1 | 224 | /** It gets the raw data: Temperature, Humidity and Checksum. |
mcm | 1:ff93ee72c7a1 | 225 | */ |
mcm | 3:082a970dca06 | 226 | DHT11_status_t DHT11_GetRawData ( DHT11_data_t* myRawData ); |
mcm | 1:ff93ee72c7a1 | 227 | |
mcm | 1:ff93ee72c7a1 | 228 | /** It gets and calculates the current data: Temperature, Humidity and Checksum. |
mcm | 1:ff93ee72c7a1 | 229 | */ |
mcm | 3:082a970dca06 | 230 | DHT11_status_t DHT11_GetData ( DHT11_data_t* myData ); |
mcm | 4:889926e2b6a5 | 231 | |
mcm | 4:889926e2b6a5 | 232 | |
mcm | 2:9fe166c478b7 | 233 | private: |
mcm | 2:9fe166c478b7 | 234 | DigitalInOut _dht11Pin; |
mcm | 1:ff93ee72c7a1 | 235 | }; |
mcm | 1:ff93ee72c7a1 | 236 | |
mcm | 1:ff93ee72c7a1 | 237 | #endif /* DHT11_H */ |