DHT11 library

Dependents:   mDot_TTN_DHT11

Fork of DHT11 by Shigenori Inoue

Committer:
merckeng
Date:
Wed Aug 17 18:20:40 2016 +0000
Revision:
12:64f27df8f2a9
Parent:
10:f0d789f49df7
TTN DHT11 Demo Working

Who changed what in which revision?

UserRevisionLine numberNew contents of line
s_inoue_mbed 10:f0d789f49df7 1 /* Copyright (c) 2014 Shigenori Inoue, MIT License
s_inoue_mbed 10:f0d789f49df7 2 *
s_inoue_mbed 10:f0d789f49df7 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
s_inoue_mbed 10:f0d789f49df7 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
s_inoue_mbed 10:f0d789f49df7 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
s_inoue_mbed 10:f0d789f49df7 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
s_inoue_mbed 10:f0d789f49df7 7 * furnished to do so, subject to the following conditions:
s_inoue_mbed 10:f0d789f49df7 8 *
s_inoue_mbed 10:f0d789f49df7 9 * The above copyright notice and this permission notice shall be included in all copies or
s_inoue_mbed 10:f0d789f49df7 10 * substantial portions of the Software.
s_inoue_mbed 10:f0d789f49df7 11 *
s_inoue_mbed 10:f0d789f49df7 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
merckeng 12:64f27df8f2a9 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHAN TABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
s_inoue_mbed 10:f0d789f49df7 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
s_inoue_mbed 10:f0d789f49df7 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
s_inoue_mbed 10:f0d789f49df7 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
s_inoue_mbed 0:4d4c5ea17d86 17 */
s_inoue_mbed 10:f0d789f49df7 18
s_inoue_mbed 0:4d4c5ea17d86 19 #ifndef __DHT11__
s_inoue_mbed 0:4d4c5ea17d86 20 #define __DHT11__
s_inoue_mbed 0:4d4c5ea17d86 21 #include "mbed.h"
s_inoue_mbed 0:4d4c5ea17d86 22
s_inoue_mbed 0:4d4c5ea17d86 23 /** Example:
s_inoue_mbed 0:4d4c5ea17d86 24 * @code
s_inoue_mbed 0:4d4c5ea17d86 25 * #include "mbed.h"
s_inoue_mbed 0:4d4c5ea17d86 26 * #include "DHT11.h"
s_inoue_mbed 0:4d4c5ea17d86 27 *
s_inoue_mbed 0:4d4c5ea17d86 28 * DHT11 d;
s_inoue_mbed 0:4d4c5ea17d86 29 *
s_inoue_mbed 0:4d4c5ea17d86 30 * main()
s_inoue_mbed 0:4d4c5ea17d86 31 * {
s_inoue_mbed 0:4d4c5ea17d86 32 * int s;
s_inoue_mbed 0:4d4c5ea17d86 33 * s = d.readData();
s_inoue_mbed 2:1e997b7a1f9a 34 * if (s != DHT11::OK) {
s_inoue_mbed 0:4d4c5ea17d86 35 * printf("Error!\r\n");
s_inoue_mbed 0:4d4c5ea17d86 36 * }
s_inoue_mbed 0:4d4c5ea17d86 37 * else {
s_inoue_mbed 0:4d4c5ea17d86 38 * printf("T:%d, H:%d\r\n", d.readTemperature(), d.readHumidity());
s_inoue_mbed 0:4d4c5ea17d86 39 * }
s_inoue_mbed 0:4d4c5ea17d86 40 * }
s_inoue_mbed 0:4d4c5ea17d86 41 * @endcode
s_inoue_mbed 0:4d4c5ea17d86 42 */
s_inoue_mbed 0:4d4c5ea17d86 43
s_inoue_mbed 0:4d4c5ea17d86 44 class DHT11
s_inoue_mbed 9:056d1e9b428c 45 {
s_inoue_mbed 0:4d4c5ea17d86 46 public:
s_inoue_mbed 0:4d4c5ea17d86 47 /** Create a DHT11 interface
s_inoue_mbed 0:4d4c5ea17d86 48 * @param pin 1-wire-like serial I/O port of DHT11
s_inoue_mbed 0:4d4c5ea17d86 49 */
s_inoue_mbed 0:4d4c5ea17d86 50 DHT11(PinName pin);
s_inoue_mbed 0:4d4c5ea17d86 51 ~DHT11();
s_inoue_mbed 0:4d4c5ea17d86 52
s_inoue_mbed 0:4d4c5ea17d86 53 /** Reading the data from the DHT11
s_inoue_mbed 0:4d4c5ea17d86 54 * @return Error code
s_inoue_mbed 0:4d4c5ea17d86 55 * 0: OK.
s_inoue_mbed 0:4d4c5ea17d86 56 * 1: Reading the data too often.
s_inoue_mbed 0:4d4c5ea17d86 57 * 2: 1-wire bus is busy.
s_inoue_mbed 0:4d4c5ea17d86 58 * 3: DHT11 does not respond.
s_inoue_mbed 0:4d4c5ea17d86 59 * 4: DHT11 is not ready.
s_inoue_mbed 0:4d4c5ea17d86 60 * 5: Checksum is incorrect.
s_inoue_mbed 5:da586c935e88 61 * 6: Timeout.
s_inoue_mbed 0:4d4c5ea17d86 62 */
s_inoue_mbed 3:8cd064147bde 63 int readData(void);
s_inoue_mbed 3:8cd064147bde 64
s_inoue_mbed 0:4d4c5ea17d86 65 /** Reading the humidity from the data
s_inoue_mbed 9:056d1e9b428c 66 * @return Humidity in %,
s_inoue_mbed 9:056d1e9b428c 67 * regardless of the error from readData()
s_inoue_mbed 0:4d4c5ea17d86 68 */
s_inoue_mbed 0:4d4c5ea17d86 69 int readHumidity(void);
s_inoue_mbed 3:8cd064147bde 70
s_inoue_mbed 3:8cd064147bde 71 /** Reading the temperature from the data
s_inoue_mbed 9:056d1e9b428c 72 * @return Temperature in Celcius,
s_inoue_mbed 9:056d1e9b428c 73 * regardless of the error from readData()
s_inoue_mbed 0:4d4c5ea17d86 74 */
s_inoue_mbed 0:4d4c5ea17d86 75 int readTemperature(void);
s_inoue_mbed 3:8cd064147bde 76
s_inoue_mbed 0:4d4c5ea17d86 77 enum ErrorDHT11 {
s_inoue_mbed 0:4d4c5ea17d86 78 OK = 0,
s_inoue_mbed 9:056d1e9b428c 79 READ_TOO_OFTEN = 1,
s_inoue_mbed 0:4d4c5ea17d86 80 BUS_BUSY = 2,
s_inoue_mbed 0:4d4c5ea17d86 81 NOT_PRESENT = 3,
s_inoue_mbed 0:4d4c5ea17d86 82 NOT_READY = 4,
s_inoue_mbed 0:4d4c5ea17d86 83 CHKSUM_ERR = 5,
s_inoue_mbed 0:4d4c5ea17d86 84 WATCHDOG_ERR = 6,
s_inoue_mbed 0:4d4c5ea17d86 85 };
s_inoue_mbed 0:4d4c5ea17d86 86
s_inoue_mbed 0:4d4c5ea17d86 87 private:
s_inoue_mbed 0:4d4c5ea17d86 88 DigitalInOut io;
s_inoue_mbed 0:4d4c5ea17d86 89 InterruptIn io_irq;
s_inoue_mbed 0:4d4c5ea17d86 90 Timer t;
s_inoue_mbed 0:4d4c5ea17d86 91 uint32_t t_pulse_us;
s_inoue_mbed 9:056d1e9b428c 92 const static int t_tol_start;
s_inoue_mbed 9:056d1e9b428c 93 const static int t_tol_pulse;
s_inoue_mbed 0:4d4c5ea17d86 94 bool first_time;
s_inoue_mbed 0:4d4c5ea17d86 95 uint64_t data;
s_inoue_mbed 0:4d4c5ea17d86 96 uint32_t chksum;
s_inoue_mbed 0:4d4c5ea17d86 97 uint32_t cnt;
s_inoue_mbed 0:4d4c5ea17d86 98 uint32_t wdt;
s_inoue_mbed 0:4d4c5ea17d86 99 bool eod;
s_inoue_mbed 0:4d4c5ea17d86 100 void init(void);
s_inoue_mbed 0:4d4c5ea17d86 101 void pos_edge(void);
s_inoue_mbed 0:4d4c5ea17d86 102 void neg_edge(void);
s_inoue_mbed 0:4d4c5ea17d86 103 };
s_inoue_mbed 0:4d4c5ea17d86 104
s_inoue_mbed 0:4d4c5ea17d86 105 #endif