dhtnew.h (3488B)
1 #pragma once 2 // 3 // FILE: dhtnew.h 4 // AUTHOR: Rob Tillaart 5 // VERSION: 0.4.5 6 // PURPOSE: DHT Temperature & Humidity Sensor library for Arduino 7 // URL: https://github.com/RobTillaart/DHTNEW 8 // 9 // HISTORY: 10 // see dhtnew.cpp file 11 12 13 // DHT PIN layout from left to right 14 // ================================= 15 // FRONT : DESCRIPTION 16 // pin 1 : VCC 17 // pin 2 : DATA 18 // pin 3 : Not Connected 19 // pin 4 : GND 20 21 22 #include "Arduino.h" 23 24 25 #define DHTNEW_LIB_VERSION (F("0.4.5")) 26 27 28 #define DHTLIB_OK 0 29 #define DHTLIB_ERROR_CHECKSUM -1 30 #define DHTLIB_ERROR_TIMEOUT_A -2 31 #define DHTLIB_ERROR_BIT_SHIFT -3 32 #define DHTLIB_ERROR_SENSOR_NOT_READY -4 33 #define DHTLIB_ERROR_TIMEOUT_C -5 34 #define DHTLIB_ERROR_TIMEOUT_D -6 35 #define DHTLIB_ERROR_TIMEOUT_B -7 36 #define DHTLIB_WAITING_FOR_READ -8 37 38 #ifndef DHTLIB_INVALID_VALUE 39 #define DHTLIB_INVALID_VALUE -999 40 #endif 41 42 43 // bits are timing based (datasheet) 44 // 26-28us ==> 0 45 // 70 us ==> 1 46 // See https://github.com/RobTillaart/DHTNew/issues/11 47 #ifndef DHTLIB_BIT_THRESHOLD 48 #define DHTLIB_BIT_THRESHOLD 50 49 #endif 50 51 52 class DHTNEW 53 { 54 public: 55 56 DHTNEW(uint8_t pin); 57 58 // resets all internals to construction time 59 // might help to reset a sensor behaving badly.. 60 void reset(); 61 62 // 0 = unknown, 11 or 22 63 uint8_t getType(); 64 void setType(uint8_t type = 0); 65 int read(); 66 67 // lastRead is in MilliSeconds since start sketch 68 uint32_t lastRead() { return _lastRead; }; 69 70 // preferred interface 71 float getHumidity() { return _humidity; }; 72 float getTemperature() { return _temperature; }; 73 74 // adding offsets works well in normal range 75 // might introduce under- or overflow at the ends of the sensor range 76 void setHumOffset(float offset) { _humOffset = offset; }; 77 void setTempOffset(float offset) { _tempOffset = offset; }; 78 float getHumOffset() { return _humOffset; }; 79 float getTempOffset() { return _tempOffset; }; 80 81 bool getDisableIRQ() { return _disableIRQ; }; 82 void setDisableIRQ(bool b ) { _disableIRQ = b; }; 83 84 bool getWaitForReading() { return _waitForRead; }; 85 void setWaitForReading(bool b ) { _waitForRead = b; }; 86 87 // set readDelay to 0 will reset to datasheet values 88 uint16_t getReadDelay() { return _readDelay; }; 89 void setReadDelay(uint16_t rd = 0) { _readDelay = rd; }; 90 91 // minimal support for low power applications. 92 // after powerUp one must wait up to two seconds. 93 void powerUp(); 94 void powerDown(); 95 96 // suppress error values of -999 => check return value of read() instead 97 bool getSuppressError() { return _suppressError; }; 98 void setSuppressError(bool b) { _suppressError = b; }; 99 100 101 private: 102 uint8_t _dataPin = 0; 103 uint8_t _wakeupDelay = 0; 104 uint8_t _type = 0; 105 float _humOffset = 0.0; 106 float _tempOffset = 0.0; 107 float _humidity = 0.0; 108 float _temperature = 0.0; 109 uint32_t _lastRead = 0; 110 bool _disableIRQ = true; 111 bool _waitForRead = false; 112 bool _suppressError = false; 113 uint16_t _readDelay = 0; 114 115 uint8_t _bits[5]; // buffer to receive data 116 int _read(); 117 int _readSensor(); 118 bool _waitFor(uint8_t state, uint32_t timeout); 119 }; 120 121 // -- END OF FILE --