Jan Kamidra / DHT11
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DHT11.h Source File

DHT11.h

00001 /* Copyright (c) 2014 Shigenori Inoue, MIT License
00002  *
00003  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
00004  * and associated documentation files (the "Software"), to deal in the Software without restriction, 
00005  * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
00006  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
00007  * furnished to do so, subject to the following conditions:
00008  *
00009  * The above copyright notice and this permission notice shall be included in all copies or 
00010  * substantial portions of the Software.
00011  *
00012  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
00013  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
00014  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
00015  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
00016  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017  */
00018  
00019 #ifndef __DHT11__
00020 #define __DHT11__
00021 #include "mbed.h"
00022 
00023 /**  Example:
00024  * @code
00025  * #include "mbed.h"
00026  * #include "DHT11.h"
00027  *
00028  * DHT11 d(D8); // Here fill your PIN mask/name
00029  *
00030  * main()
00031  * {
00032  *     int s;
00033  *     s = d.readData();
00034  *     if (s != DHT11::OK) {
00035  *         printf("Error!\r\n");
00036  *     }
00037  *     else {
00038  *         printf("T:%d, H:%d\r\n", d.readTemperature(), d.readHumidity());
00039  *     }
00040  * }
00041  * @endcode
00042  */
00043 
00044 class DHT11
00045 {   
00046 public:
00047     /** Create a DHT11 interface
00048      * @param pin 1-wire-like serial I/O port of DHT11
00049      */
00050     DHT11(PinName pin);
00051     ~DHT11();
00052 
00053     /** Reading the data from the DHT11
00054      * @return Error code
00055      *     0: OK.
00056      *     1: Reading the data too often.
00057      *     2: 1-wire bus is busy.
00058      *     3: DHT11 does not respond.
00059      *     4: DHT11 is not ready.
00060      *     5: Checksum is incorrect.
00061      *     6: Timeout.
00062      */
00063     int readData(void);
00064 
00065     /** Reading the humidity from the data
00066      * @return Humidity in %,
00067      * regardless of the error from readData()
00068      */
00069     int readHumidity(void);
00070 
00071     /** Reading the temperature from the data
00072      * @return Temperature in Celcius,
00073      * regardless of the error from readData()
00074      */
00075     int readTemperature(void);
00076 
00077     enum ErrorDHT11 {
00078         OK = 0,
00079         READ_TOO_OFTEN = 1,
00080         BUS_BUSY = 2,
00081         NOT_PRESENT = 3,
00082         NOT_READY = 4,
00083         CHKSUM_ERR = 5,
00084         WATCHDOG_ERR = 6,
00085     };
00086 
00087 private:
00088     DigitalInOut io;
00089     InterruptIn io_irq;
00090     Timer t;
00091     uint32_t t_pulse_us;
00092     const static int t_tol_start;    
00093     const static int t_tol_pulse;
00094     bool first_time;
00095     uint64_t data;
00096     uint32_t chksum;
00097     uint32_t cnt;
00098     uint32_t wdt;
00099     bool eod;
00100     void init(void);
00101     void pos_edge(void);
00102     void neg_edge(void);
00103 };
00104 
00105 #endif