Fork of DHT component - https://os.mbed.com/teams/components/code/DHT/ and update for MbedOS6+

Committer:
JohnnyK
Date:
Mon May 31 07:47:49 2021 +0000
Revision:
4:655ecec987ca
Parent:
2:df22ddf10d75
Update for MbedOS6+

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
Wimpie 0:9b5b3200688f 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.
Wimpie 0:9b5b3200688f 31 */
JohnnyK 4:655ecec987ca 32
JohnnyK 4:655ecec987ca 33 /*
JohnnyK 4:655ecec987ca 34 * Example:
JohnnyK 4:655ecec987ca 35 * @code
JohnnyK 4:655ecec987ca 36 * #include "mbed.h"
JohnnyK 4:655ecec987ca 37 * #include "DHT.h"
JohnnyK 4:655ecec987ca 38 *
JohnnyK 4:655ecec987ca 39 * DHT sensor(D4, DHT11);
JohnnyK 4:655ecec987ca 40 *
JohnnyK 4:655ecec987ca 41 * int main()
JohnnyK 4:655ecec987ca 42 * {
JohnnyK 4:655ecec987ca 43 * int error = 0;
JohnnyK 4:655ecec987ca 44 * float h = 0.0f, c = 0.0f, f = 0.0f, k = 0.0f, dp = 0.0f, dpf = 0.0f;
JohnnyK 4:655ecec987ca 45 *
JohnnyK 4:655ecec987ca 46 * while(1) {
JohnnyK 4:655ecec987ca 47 * thread_sleep_for(2000);
JohnnyK 4:655ecec987ca 48 * error = sensor.readData();
JohnnyK 4:655ecec987ca 49 * if (0 == error) {
JohnnyK 4:655ecec987ca 50 * c = sensor.ReadTemperature(CELCIUS);
JohnnyK 4:655ecec987ca 51 * f = sensor.ReadTemperature(FARENHEIT);
JohnnyK 4:655ecec987ca 52 * k = sensor.ReadTemperature(KELVIN);
JohnnyK 4:655ecec987ca 53 * h = sensor.ReadHumidity();
JohnnyK 4:655ecec987ca 54 * dp = sensor.CalcdewPoint(c, h);
JohnnyK 4:655ecec987ca 55 * dpf = sensor.CalcdewPointFast(c, h);
JohnnyK 4:655ecec987ca 56 * printf("Temperature in Kelvin: %4.2f, Celcius: %4.2f, Farenheit %4.2f\n", k, c, f);
JohnnyK 4:655ecec987ca 57 * printf("Humidity is %4.2f, Dewpoint: %4.2f, Dewpoint fast: %4.2f\n", h, dp, dpf);
JohnnyK 4:655ecec987ca 58 * } else {
JohnnyK 4:655ecec987ca 59 * printf("Error: %d\n", error);
JohnnyK 4:655ecec987ca 60 * }
JohnnyK 4:655ecec987ca 61 * }
JohnnyK 4:655ecec987ca 62 * }
JohnnyK 4:655ecec987ca 63 * @endcode
JohnnyK 4:655ecec987ca 64 */
Wimpie 0:9b5b3200688f 65
Wimpie 0:9b5b3200688f 66 #ifndef MBED_DHT_H
Wimpie 0:9b5b3200688f 67 #define MBED_DHT_H
Wimpie 0:9b5b3200688f 68
Wimpie 0:9b5b3200688f 69 #include "mbed.h"
Wimpie 0:9b5b3200688f 70
JohnnyK 4:655ecec987ca 71
JohnnyK 4:655ecec987ca 72 typedef enum e_Type {
sam_grove 2:df22ddf10d75 73 DHT11 = 11,
sam_grove 2:df22ddf10d75 74 SEN11301P = 11,
sam_grove 2:df22ddf10d75 75 RHT01 = 11,
sam_grove 2:df22ddf10d75 76 DHT22 = 22,
sam_grove 2:df22ddf10d75 77 AM2302 = 22,
sam_grove 2:df22ddf10d75 78 SEN51035P = 22,
sam_grove 2:df22ddf10d75 79 RHT02 = 22,
sam_grove 2:df22ddf10d75 80 RHT03 = 22
JohnnyK 4:655ecec987ca 81 }eType;
Wimpie 0:9b5b3200688f 82
JohnnyK 4:655ecec987ca 83 typedef enum e_Error {
Wimpie 0:9b5b3200688f 84 ERROR_NONE = 0,
sam_grove 2:df22ddf10d75 85 BUS_BUSY,
sam_grove 2:df22ddf10d75 86 ERROR_NOT_PRESENT,
sam_grove 2:df22ddf10d75 87 ERROR_ACK_TOO_LONG,
sam_grove 2:df22ddf10d75 88 ERROR_SYNC_TIMEOUT,
sam_grove 2:df22ddf10d75 89 ERROR_DATA_TIMEOUT,
sam_grove 2:df22ddf10d75 90 ERROR_CHECKSUM,
sam_grove 2:df22ddf10d75 91 ERROR_NO_PATIENCE
JohnnyK 4:655ecec987ca 92 }eError;
Wimpie 0:9b5b3200688f 93
JohnnyK 4:655ecec987ca 94 typedef enum e_Scale {
sam_grove 2:df22ddf10d75 95 CELCIUS = 0,
sam_grove 2:df22ddf10d75 96 FARENHEIT,
sam_grove 2:df22ddf10d75 97 KELVIN
JohnnyK 4:655ecec987ca 98 }eScale;
Wimpie 0:9b5b3200688f 99
Wimpie 0:9b5b3200688f 100
sam_grove 2:df22ddf10d75 101 class DHT
sam_grove 2:df22ddf10d75 102 {
Wimpie 0:9b5b3200688f 103
Wimpie 0:9b5b3200688f 104 public:
Wimpie 0:9b5b3200688f 105
sam_grove 2:df22ddf10d75 106 DHT(PinName pin, eType DHTtype);
Wimpie 0:9b5b3200688f 107 ~DHT();
sam_grove 2:df22ddf10d75 108 eError readData(void);
Wimpie 0:9b5b3200688f 109 float ReadHumidity(void);
sam_grove 2:df22ddf10d75 110 float ReadTemperature(eScale const Scale);
sam_grove 2:df22ddf10d75 111 float CalcdewPoint(float const celsius, float const humidity);
sam_grove 2:df22ddf10d75 112 float CalcdewPointFast(float const celsius, float const humidity);
Wimpie 0:9b5b3200688f 113
Wimpie 0:9b5b3200688f 114 private:
Wimpie 0:9b5b3200688f 115 time_t _lastReadTime;
Wimpie 0:9b5b3200688f 116 float _lastTemperature;
Wimpie 0:9b5b3200688f 117 float _lastHumidity;
Wimpie 0:9b5b3200688f 118 PinName _pin;
Wimpie 0:9b5b3200688f 119 bool _firsttime;
sam_grove 2:df22ddf10d75 120 eType _DHTtype;
sam_grove 2:df22ddf10d75 121 uint8_t DHT_data[5];
Wimpie 0:9b5b3200688f 122 float CalcTemperature();
Wimpie 0:9b5b3200688f 123 float CalcHumidity();
sam_grove 2:df22ddf10d75 124 float ConvertCelciustoFarenheit(float const);
sam_grove 2:df22ddf10d75 125 float ConvertCelciustoKelvin(float const);
sam_grove 2:df22ddf10d75 126 eError stall(DigitalInOut &io, int const level, int const max_time);
Wimpie 0:9b5b3200688f 127
Wimpie 0:9b5b3200688f 128 };
Wimpie 0:9b5b3200688f 129
Wimpie 0:9b5b3200688f 130 #endif