arduinoprojects

git clone https://git.tarina.org/arduinoprojects
Log | Files | Refs

SimpleDHT.h (6822B)


      1 /*
      2  The MIT License (MIT)
      3 
      4  Copyright (c) 2016-2017 winlin
      5 
      6  Permission is hereby granted, free of charge, to any person obtaining a copy
      7  of this software and associated documentation files (the "Software"), to deal
      8  in the Software without restriction, including without limitation the rights
      9  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     10  copies of the Software, and to permit persons to whom the Software is
     11  furnished to do so, subject to the following conditions:
     12 
     13  The above copyright notice and this permission notice shall be included in all
     14  copies or substantial portions of the Software.
     15 
     16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     19  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     21  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     22  SOFTWARE.
     23  */
     24 
     25 #ifndef __SIMPLE_DHT_H
     26 #define __SIMPLE_DHT_H
     27 
     28 #include <Arduino.h>
     29 
     30 // High 8bits are time duration.
     31 // Low 8bits are error code.
     32 // For example, 0x0310 means t=0x03 and code=0x10,
     33 // which is start low signal(0x10) error.
     34 // @see https://github.com/winlinvip/SimpleDHT/issues/25
     35 #define simpleDHTCombileError(t, err) ((t << 8) & 0xff00) | (err & 0x00ff)
     36 
     37 // Get the time duration from error.
     38 #define SimpleDHTErrDuration(err) ((err&0xff00)>>8)
     39 // Get the error code defined bellow.
     40 #define SimpleDHTErrCode(err) (err&0x00ff)
     41 
     42 // Success.
     43 #define SimpleDHTErrSuccess 0
     44 // Error to wait for start low signal.
     45 #define SimpleDHTErrStartLow 16
     46 // Error to wait for start high signal.
     47 #define SimpleDHTErrStartHigh 17
     48 // Error to wait for data start low signal.
     49 #define SimpleDHTErrDataLow 18
     50 // Error to wait for data read signal.
     51 #define SimpleDHTErrDataRead 19
     52 // Error to wait for data EOF signal.
     53 #define SimpleDHTErrDataEOF 20
     54 // Error to validate the checksum.
     55 #define SimpleDHTErrDataChecksum 21
     56 // Error when temperature and humidity are zero, it shouldn't happen.
     57 #define SimpleDHTErrZeroSamples 22
     58 // Error when pin is not initialized.
     59 #define SimpleDHTErrNoPin 23
     60 // Error when pin mode is invalid.
     61 #define SimpleDHTErrPinMode 24
     62 
     63 class SimpleDHT {
     64 protected:
     65     long levelTimeout = 500000; // 500ms
     66     int pin = -1;
     67     uint8_t pinInputMode = INPUT;
     68 #ifdef __AVR
     69     // For direct GPIO access (8-bit AVRs only), store port and bitmask
     70     // of the digital pin connected to the DHT.
     71     // (other platforms use digitalRead(), do not need this)
     72     uint8_t bitmask = 0xFF;
     73     uint8_t port    = 0xFF;
     74 #endif
     75 public:
     76     SimpleDHT();
     77     SimpleDHT(int pin);
     78 public:
     79     // To (eventually) change the pin configuration for existing instance
     80     // @param pin The DHT11 or DHT22 pin.
     81     virtual void setPin(int pin);
     82     // Set the input mode of the pin from INPUT and INPUT_PULLUP
     83     // to permit the use of the internal pullup resistor for
     84     // for bare modules
     85     // @param mode the pin input mode.
     86     // @return SimpleDHTErrSuccess is success; otherwise, failed.
     87     virtual int setPinInputMode(uint8_t mode);
     88 public:
     89     // Read from dht11 or dht22.
     90     // @param pin The DHT11 pin.
     91     // @param ptemperature output, NULL to igore. In Celsius.
     92     // @param phumidity output, NULL to ignore.
     93     //      For DHT11, in H, such as 35H.
     94     //      For DHT22, in RH%, such as 53%RH.
     95     // @param pdata output 40bits sample, NULL to ignore.
     96     // @remark the min delay for this method is 1s(DHT11) or 2s(DHT22).
     97     // @return SimpleDHTErrSuccess is success; otherwise, failed.
     98     virtual int read(byte* ptemperature, byte* phumidity, byte pdata[5]);
     99     virtual int read(int pin, byte* ptemperature, byte* phumidity, byte pdata[5]);
    100     // To get a more accurate data.
    101     // @remark it's available for dht22. for dht11, it's the same of read().
    102     virtual int read2(float* ptemperature, float* phumidity, byte pdata[5]) = 0;
    103     virtual int read2(int pin, float* ptemperature, float* phumidity, byte pdata[5]) = 0;
    104 protected:
    105     // For only AVR - methods returning low level conf. of the pin
    106 #ifdef __AVR
    107     // @return Bitmask to access pin state from port input register
    108     virtual int getBitmask();
    109     // @return Bitmask to access pin state from port input register
    110     virtual int getPort();
    111 #endif
    112 protected:
    113     // Measure and return time (in microseconds)
    114     // with precision defined by interval between checking the state
    115     // while pin is in specified state (HIGH or LOW)
    116     // @param level    state which time is measured.
    117     // @param interval time interval between consecutive state checks.
    118     // @return measured time (microseconds). -1 if timeout.
    119     virtual long levelTime(byte level, int firstWait = 10, int interval = 6);
    120     // @data reverses a byte with reversed data
    121     // @remark please use simple_dht11_read().
    122     virtual byte reverse(byte data);
    123     // read temperature and humidity from dht11.
    124     // @param data a byte[5] to read bits to 5bytes.
    125     // @return 0 success; otherwise, error.
    126     // @remark please use simple_dht11_read().
    127     virtual int sample(byte data[5]) = 0;
    128     // parse the 40bits data to temperature and humidity.
    129     // @remark please use simple_dht11_read().
    130     virtual int parse(byte data[5], short* ptemperature, short* phumidity);
    131 };
    132 
    133 /*
    134     Simple DHT11
    135 
    136     Simple, Stable and Fast DHT11 library.
    137 
    138     The circuit:
    139     * VCC: 5V or 3V
    140     * GND: GND
    141     * DATA: Digital ping, for instance 2.
    142 
    143     23 Jan 2016 By winlin <winlin@vip.126.com>
    144 
    145     https://github.com/winlinvip/SimpleDHT#usage
    146     https://akizukidenshi.com/download/ds/aosong/DHT11.pdf
    147     https://cdn-shop.adafruit.com/datasheets/DHT11-chinese.pdf
    148 
    149 */
    150 class SimpleDHT11 : public SimpleDHT {
    151 public:
    152     SimpleDHT11();
    153     SimpleDHT11(int pin);
    154 public:
    155     virtual int read2(float* ptemperature, float* phumidity, byte pdata[5]);
    156     virtual int read2(int pin, float* ptemperature, float* phumidity, byte pdata[5]);
    157 protected:
    158     virtual int sample(byte data[5]);
    159 };
    160 
    161 /*
    162     Simple DHT22
    163 
    164     Simple, Stable and Fast DHT22 library.
    165 
    166     The circuit:
    167     * VCC: 5V or 3V
    168     * GND: GND
    169     * DATA: Digital ping, for instance 2.
    170 
    171     3 Jun 2017 By winlin <winlin@vip.126.com>
    172 
    173     https://github.com/winlinvip/SimpleDHT#usage
    174     http://akizukidenshi.com/download/ds/aosong/AM2302.pdf
    175     https://cdn-shop.adafruit.com/datasheets/DHT22.pdf
    176 
    177 */
    178 class SimpleDHT22 : public SimpleDHT {
    179 public:
    180     SimpleDHT22();
    181     SimpleDHT22(int pin);
    182 public:
    183     virtual int read2(float* ptemperature, float* phumidity, byte pdata[5]);
    184     virtual int read2(int pin, float* ptemperature, float* phumidity, byte pdata[5]);
    185 protected:
    186     virtual int sample(byte data[5]);
    187 };
    188 
    189 #endif