DHT11 used for Temperature & Humidity sensor.

Dependents:   LoRaWAN_mbed_lmic_agriculture_app

Fork of DHT by Components

Committer:
GTsapparellas
Date:
Wed Apr 11 19:42:32 2018 +0000
Revision:
5:e7e3db429c9e
Parent:
4:0c34c98a87ec
version 1.0 Commenting

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sam_grove 2:df22ddf10d75 1 /*
sam_grove 2:df22ddf10d75 2 * DHT Library for Digital-output Humidity and Temperature sensors
sam_grove 2:df22ddf10d75 3 *
Wimpie 0:9b5b3200688f 4 * Works with DHT11, DHT21, DHT22
Wimpie 0:9b5b3200688f 5 * SEN11301P, Grove - Temperature&Humidity Sensor (Seeed Studio)
Wimpie 0:9b5b3200688f 6 * SEN51035P, Grove - Temperature&Humidity Sensor Pro (Seeed Studio)
sam_grove 2:df22ddf10d75 7 * AM2302 , temperature-humidity sensor
GTsapparellas 4:0c34c98a87ec 8 * RHT01,RHT02, RHT03, Humidity and Temperature Sensor (Sparkfun)
Wimpie 0:9b5b3200688f 9 *
Wimpie 0:9b5b3200688f 10 * Copyright (C) Wim De Roeve
Wimpie 0:9b5b3200688f 11 * based on DHT22 sensor library by HO WING KIT
Wimpie 0:9b5b3200688f 12 * Arduino DHT11 library
Wimpie 0:9b5b3200688f 13 *
Wimpie 0:9b5b3200688f 14 * Permission is hereby granted, free of charge, to any person obtaining a copy
Wimpie 0:9b5b3200688f 15 * of this software and associated documnetation files (the "Software"), to deal
Wimpie 0:9b5b3200688f 16 * in the Software without restriction, including without limitation the rights
Wimpie 0:9b5b3200688f 17 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Wimpie 0:9b5b3200688f 18 * copies of the Software, and to permit persons to whom the Software is
Wimpie 0:9b5b3200688f 19 * furished to do so, subject to the following conditions:
Wimpie 0:9b5b3200688f 20 *
Wimpie 0:9b5b3200688f 21 * The above copyright notice and this permission notice shall be included in
Wimpie 0:9b5b3200688f 22 * all copies or substantial portions of the Software.
Wimpie 0:9b5b3200688f 23 *
Wimpie 0:9b5b3200688f 24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Wimpie 0:9b5b3200688f 25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Wimpie 0:9b5b3200688f 26 * FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Wimpie 0:9b5b3200688f 27 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Wimpie 0:9b5b3200688f 28 * LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Wimpie 0:9b5b3200688f 29 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Wimpie 0:9b5b3200688f 30 * THE SOFTWARE.
GTsapparellas 4:0c34c98a87ec 31 *
GTsapparellas 4:0c34c98a87ec 32 * /////////////////////////////////////////////////////////////////////////////
GTsapparellas 4:0c34c98a87ec 33 *
GTsapparellas 4:0c34c98a87ec 34 * Used by Giorgos Tsapparellas for Internet of Things (IoT) smart monitoring
GTsapparellas 4:0c34c98a87ec 35 * device for agriculture using LoRaWAN technology.
GTsapparellas 4:0c34c98a87ec 36 *
GTsapparellas 4:0c34c98a87ec 37 * Sensor Used: DHT11 - Digital-output Humidity and Temperature sensor.
GTsapparellas 4:0c34c98a87ec 38 *
GTsapparellas 4:0c34c98a87ec 39 * Date of issued copy: 10 January 2018
GTsapparellas 4:0c34c98a87ec 40 *
GTsapparellas 4:0c34c98a87ec 41 * Modifications:
GTsapparellas 4:0c34c98a87ec 42 * - No external modifications of the existing "AS IT IS" software.
GTsapparellas 4:0c34c98a87ec 43 * - Added some external comments for meeting good principles of
GTsapparellas 4:0c34c98a87ec 44 * source code re-usability.
Wimpie 0:9b5b3200688f 45 */
Wimpie 0:9b5b3200688f 46
Wimpie 0:9b5b3200688f 47 #ifndef MBED_DHT_H
Wimpie 0:9b5b3200688f 48 #define MBED_DHT_H
Wimpie 0:9b5b3200688f 49
Wimpie 0:9b5b3200688f 50 #include "mbed.h"
Wimpie 0:9b5b3200688f 51
GTsapparellas 4:0c34c98a87ec 52 // eType data structure regarding sensor selection (either 11 or 22 value).
sam_grove 2:df22ddf10d75 53 typedef enum eType eType;
sam_grove 2:df22ddf10d75 54 enum eType {
sam_grove 2:df22ddf10d75 55 DHT11 = 11,
sam_grove 2:df22ddf10d75 56 SEN11301P = 11,
sam_grove 2:df22ddf10d75 57 RHT01 = 11,
sam_grove 2:df22ddf10d75 58 DHT22 = 22,
sam_grove 2:df22ddf10d75 59 AM2302 = 22,
sam_grove 2:df22ddf10d75 60 SEN51035P = 22,
sam_grove 2:df22ddf10d75 61 RHT02 = 22,
sam_grove 2:df22ddf10d75 62 RHT03 = 22
sam_grove 2:df22ddf10d75 63 };
Wimpie 0:9b5b3200688f 64
GTsapparellas 4:0c34c98a87ec 65 // eError enumeration regarding possible sensor's errors (from 0 to 7).
sam_grove 2:df22ddf10d75 66 typedef enum eError eError;
Wimpie 0:9b5b3200688f 67 enum eError {
Wimpie 0:9b5b3200688f 68 ERROR_NONE = 0,
sam_grove 2:df22ddf10d75 69 BUS_BUSY,
sam_grove 2:df22ddf10d75 70 ERROR_NOT_PRESENT,
sam_grove 2:df22ddf10d75 71 ERROR_ACK_TOO_LONG,
sam_grove 2:df22ddf10d75 72 ERROR_SYNC_TIMEOUT,
sam_grove 2:df22ddf10d75 73 ERROR_DATA_TIMEOUT,
sam_grove 2:df22ddf10d75 74 ERROR_CHECKSUM,
sam_grove 2:df22ddf10d75 75 ERROR_NO_PATIENCE
sam_grove 2:df22ddf10d75 76 };
Wimpie 0:9b5b3200688f 77
GTsapparellas 4:0c34c98a87ec 78 // eScale enumeration for possible temperature's scales (from 0 to 3=2).
sam_grove 2:df22ddf10d75 79 typedef enum eScale eScale;
sam_grove 2:df22ddf10d75 80 enum eScale {
sam_grove 2:df22ddf10d75 81 CELCIUS = 0,
sam_grove 2:df22ddf10d75 82 FARENHEIT,
sam_grove 2:df22ddf10d75 83 KELVIN
sam_grove 2:df22ddf10d75 84 };
Wimpie 0:9b5b3200688f 85
GTsapparellas 4:0c34c98a87ec 86 /* Public and private declarations of DHT class. */
sam_grove 2:df22ddf10d75 87 class DHT
sam_grove 2:df22ddf10d75 88 {
GTsapparellas 4:0c34c98a87ec 89 /* Public function callbacks of DHT class. */
Wimpie 0:9b5b3200688f 90 public:
Wimpie 0:9b5b3200688f 91
sam_grove 2:df22ddf10d75 92 DHT(PinName pin, eType DHTtype);
Wimpie 0:9b5b3200688f 93 ~DHT();
sam_grove 2:df22ddf10d75 94 eError readData(void);
Wimpie 0:9b5b3200688f 95 float ReadHumidity(void);
sam_grove 2:df22ddf10d75 96 float ReadTemperature(eScale const Scale);
sam_grove 2:df22ddf10d75 97 float CalcdewPoint(float const celsius, float const humidity);
sam_grove 2:df22ddf10d75 98 float CalcdewPointFast(float const celsius, float const humidity);
Wimpie 0:9b5b3200688f 99
GTsapparellas 4:0c34c98a87ec 100 /* Private variables declarations and function callbacks of DHT class. */
Wimpie 0:9b5b3200688f 101 private:
Wimpie 0:9b5b3200688f 102 time_t _lastReadTime;
Wimpie 0:9b5b3200688f 103 float _lastTemperature;
Wimpie 0:9b5b3200688f 104 float _lastHumidity;
Wimpie 0:9b5b3200688f 105 PinName _pin;
Wimpie 0:9b5b3200688f 106 bool _firsttime;
sam_grove 2:df22ddf10d75 107 eType _DHTtype;
sam_grove 2:df22ddf10d75 108 uint8_t DHT_data[5];
Wimpie 0:9b5b3200688f 109 float CalcTemperature();
Wimpie 0:9b5b3200688f 110 float CalcHumidity();
sam_grove 2:df22ddf10d75 111 float ConvertCelciustoFarenheit(float const);
sam_grove 2:df22ddf10d75 112 float ConvertCelciustoKelvin(float const);
sam_grove 2:df22ddf10d75 113 eError stall(DigitalInOut &io, int const level, int const max_time);
Wimpie 0:9b5b3200688f 114
Wimpie 0:9b5b3200688f 115 };
Wimpie 0:9b5b3200688f 116
Wimpie 0:9b5b3200688f 117 #endif