From 5bba4adae10d9663a90876af687a325be2c74910 Mon Sep 17 00:00:00 2001 From: Dhruba Saha Date: Thu, 21 Sep 2023 17:21:29 +0530 Subject: [PATCH 1/6] Create SECURITY.md Signed-off-by: Dhruba Saha --- SECURITY.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 SECURITY.md diff --git a/SECURITY.md b/SECURITY.md new file mode 100644 index 0000000..12bfd0d --- /dev/null +++ b/SECURITY.md @@ -0,0 +1,26 @@ +# Security Policy + +## Supported Versions + +This section lists the versions of the DHT11 library that are currently receiving security updates. + +| Version | Supported | +| ------- | ------------------ | +| 2.0.x | :white_check_mark: | +| < 2.0 | :x: | + +## Reporting a Vulnerability + +The DHT11 Arduino library primarily facilitates interaction with the DHT11 temperature and humidity sensor. While the risk associated with this is minimal, maintaining a secure and reliable codebase remains a priority. + +If you believe you've found a security vulnerability in the DHT11 library, please follow the steps below: + +1. **Do Not Open a Public Issue:** To ensure the vulnerability doesn't become public knowledge and put users at risk, refrain from opening an issue on the public GitHub repository. + +2. **Contact the Maintainer:** Send a detailed description of the vulnerability directly to [dhrubasaha@outlook.com](mailto:dhrubasaha@outlook.com). Please provide as much information as possible to help understand the scope and severity of the potential issue. + +3. **Response Time:** I aim to acknowledge and respond to your report within 7 days. In the response, you can expect an evaluation of the issue and an estimated timeline for a fix if deemed necessary. + +4. **Disclosure:** Once the vulnerability has been addressed, I'll work with you to publicly disclose the issue in a responsible manner, ensuring the community is informed and can take appropriate actions. + +Your efforts to responsibly disclose your findings are sincerely appreciated and will be acknowledged. From f9ff3f4ec313086ced1d67e0cad21ef25594386a Mon Sep 17 00:00:00 2001 From: DrJPK <91477569+DrJPK@users.noreply.github.com> Date: Wed, 27 Sep 2023 13:15:28 +1000 Subject: [PATCH 2/6] Update DHT11.h Adds private class variables for temperature and humidity so that both humidity and temperature can be read simultaneously. Adds a lastPollTime variable to prevent polling the sensor too frequently Signed-off-by: DrJPK <91477569+DrJPK@users.noreply.github.com> --- src/DHT11.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/DHT11.h b/src/DHT11.h index 7387d06..c5f10c9 100644 --- a/src/DHT11.h +++ b/src/DHT11.h @@ -6,6 +6,7 @@ * Author: Dhruba Saha * Version: 2.0.0 * License: MIT + * Modified: John Kennedy */ #ifndef DHT11_h @@ -59,6 +60,16 @@ public: private: int _pin; // Pin number used for communication with the DHT11 sensor. + int _temperature; // Holds the last known value of the Temperature. + int _humidity; // Holds the last known value of the Humidity. + long _lastPollTime; //Value of millis() when sensor last polled. DHT11 cannot be polled faster than 1Hz + static const int _pollTime = 1000; + + /** + * Reads the Temperature and Humidity data from the DHT11 and saves them to the private variables + * _temperature and _humidity + */ + void readSensor(); /** * Reads a byte of data from the DHT11 sensor. From 324998ee8abd8e347554a57c601b40ffece610bd Mon Sep 17 00:00:00 2001 From: DrJPK <91477569+DrJPK@users.noreply.github.com> Date: Wed, 27 Sep 2023 13:31:57 +1000 Subject: [PATCH 3/6] Update DHT11.cpp Simplified readHumidity and readTemperature methods to return value of private variables. Added readSensor() method to store values of humidity and temperature in private variables Signed-off-by: DrJPK <91477569+DrJPK@users.noreply.github.com> --- src/DHT11.cpp | 108 ++++++++++++++++++++++---------------------------- 1 file changed, 48 insertions(+), 60 deletions(-) diff --git a/src/DHT11.cpp b/src/DHT11.cpp index cfcb3e2..367dbbd 100644 --- a/src/DHT11.cpp +++ b/src/DHT11.cpp @@ -5,6 +5,7 @@ * Author: Dhruba Saha * Version: 2.0.0 * License: MIT + * Modified: John Kennedy */ #include "DHT11.h" @@ -20,94 +21,81 @@ DHT11::DHT11(int pin) _pin = pin; pinMode(_pin, OUTPUT); digitalWrite(_pin, HIGH); + DHT11::readSensor(); } /** * Reads and returns the temperature from the DHT11 sensor. * - * @return: Temperature value in Celsius. Returns DHT11::ERROR_TIMEOUT if reading times out. - * Returns DHT11::ERROR_CHECKSUM if checksum validation fails. + * @return: Temperature value in Celsius. */ int DHT11::readTemperature() { - delay(150); - byte data[5] = {0, 0, 0, 0, 0}; - startSignal(); - unsigned long timeout_start = millis(); - - while (digitalRead(_pin) == HIGH) - { - if (millis() - timeout_start > DHT11::TIMEOUT_DURATION) - { - return DHT11::ERROR_TIMEOUT; - } - } - - if (digitalRead(_pin) == LOW) - { - delayMicroseconds(80); - if (digitalRead(_pin) == HIGH) - { - delayMicroseconds(80); - for (int i = 0; i < 5; i++) - { - data[i] = readByte(); - if (data[i] == DHT11::ERROR_TIMEOUT) - { - return DHT11::ERROR_TIMEOUT; - } - } - if (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) - { - return data[2]; - } - } - } - return DHT11::ERROR_CHECKSUM; + return DHT11::_temperature; } /** * Reads and returns the humidity from the DHT11 sensor. * - * @return: Humidity value in percentage. Returns DHT11::ERROR_TIMEOUT if reading times out. - * Returns DHT11::ERROR_CHECKSUM if checksum validation fails. + * @return: Humidity value in percentage. */ int DHT11::readHumidity() { - delay(150); - byte data[5] = {0, 0, 0, 0, 0}; - startSignal(); - unsigned long timeout_start = millis(); + return DHT11::_humidity; +} - while (digitalRead(_pin) == HIGH) +/** + * Reads and saves the humidity and temperature from the DHT11 sensor. + * + * @return: Returns 0 if all OK. + * Returns DHT11::ERROR_TOOFREQUENT if poll frequency is too high + * Returns DHT11::ERROR_TIMEOUT if reading times out. + * Returns DHT11::ERROR_CHECKSUM if checksum validation fails. + */ +int DHT11::readSensor() +{ + if (millis() - DHT11::_pollTime > DHT11::_lastPollTime) { - if (millis() - timeout_start > DHT11::TIMEOUT_DURATION) + DHT11::_lastPollTime = millis(); + delay(150); + byte data[5] = {0, 0, 0, 0, 0}; + startSignal(); + unsigned long timeout_start = millis(); + + while (digitalRead(_pin) == HIGH) { - return DHT11::ERROR_TIMEOUT; + if (millis() - timeout_start > DHT11::TIMEOUT_DURATION) + { + return DHT11::ERROR_TIMEOUT; + } } - } - - if (digitalRead(_pin) == LOW) - { - delayMicroseconds(80); - if (digitalRead(_pin) == HIGH) + + if (digitalRead(_pin) == LOW) { delayMicroseconds(80); - for (int i = 0; i < 5; i++) + if (digitalRead(_pin) == HIGH) { - data[i] = readByte(); - if (data[i] == DHT11::ERROR_TIMEOUT) + delayMicroseconds(80); + for (int i = 0; i < 5; i++) { - return DHT11::ERROR_TIMEOUT; + data[i] = readByte(); + if (data[i] == DHT11::ERROR_TIMEOUT) + { + return DHT11::ERROR_TIMEOUT; + } + } + if (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) + { + DHT11::_temperature = data[2]; + DHT11::_humidity = data[0]; + return 0; } } - if (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) - { - return data[0]; - } } + return DHT11::ERROR_CHECKSUM; + } else { + return DHT11::ERROR_TOOFREQUENT; } - return DHT11::ERROR_CHECKSUM; } /** From 3ebf6d29096f2af11731e73f16972e4342de9eef Mon Sep 17 00:00:00 2001 From: DrJPK <91477569+DrJPK@users.noreply.github.com> Date: Wed, 27 Sep 2023 13:33:56 +1000 Subject: [PATCH 4/6] Update DHT11.h Signed-off-by: DrJPK <91477569+DrJPK@users.noreply.github.com> --- src/DHT11.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/DHT11.h b/src/DHT11.h index c5f10c9..be7e094 100644 --- a/src/DHT11.h +++ b/src/DHT11.h @@ -48,6 +48,7 @@ public: // Constants to represent error codes. static const int ERROR_CHECKSUM = 254; // Error code indicating checksum mismatch. static const int ERROR_TIMEOUT = 253; // Error code indicating a timeout occurred during reading. + static const int ERROR_TOOFREQUENT = 252; // Error code indicating that polling frequency is too high static const int TIMEOUT_DURATION = 300; // Duration (in milliseconds) to wait before timing out. /** @@ -62,14 +63,16 @@ private: int _pin; // Pin number used for communication with the DHT11 sensor. int _temperature; // Holds the last known value of the Temperature. int _humidity; // Holds the last known value of the Humidity. - long _lastPollTime; //Value of millis() when sensor last polled. DHT11 cannot be polled faster than 1Hz + long _lastPollTime = 0; //Value of millis() when sensor last polled. DHT11 cannot be polled faster than 1Hz static const int _pollTime = 1000; /** * Reads the Temperature and Humidity data from the DHT11 and saves them to the private variables * _temperature and _humidity + * + * @return: Returns 0 or an error code */ - void readSensor(); + int readSensor(); /** * Reads a byte of data from the DHT11 sensor. From 373afdc4720228cec1b95614a590e31c5fc1123a Mon Sep 17 00:00:00 2001 From: DrJPK <91477569+DrJPK@users.noreply.github.com> Date: Wed, 27 Sep 2023 13:48:18 +1000 Subject: [PATCH 5/6] Update DHT11.cpp add optional begin() method Signed-off-by: DrJPK <91477569+DrJPK@users.noreply.github.com> --- src/DHT11.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/DHT11.cpp b/src/DHT11.cpp index 367dbbd..2eb5da2 100644 --- a/src/DHT11.cpp +++ b/src/DHT11.cpp @@ -21,7 +21,16 @@ DHT11::DHT11(int pin) _pin = pin; pinMode(_pin, OUTPUT); digitalWrite(_pin, HIGH); - DHT11::readSensor(); +} + +/** + * Optional begin method to initialise the sensor values + * + * @return 0 if OK or Error code + */ +int DHT11::begin() +{ + readSensor(); } /** @@ -31,6 +40,7 @@ DHT11::DHT11(int pin) */ int DHT11::readTemperature() { + readSensor(); return DHT11::_temperature; } @@ -41,6 +51,7 @@ int DHT11::readTemperature() */ int DHT11::readHumidity() { + readSensor(); return DHT11::_humidity; } From 37c3d84bcdba60154bb80f93a336d6564dec75c5 Mon Sep 17 00:00:00 2001 From: DrJPK <91477569+DrJPK@users.noreply.github.com> Date: Wed, 27 Sep 2023 13:49:15 +1000 Subject: [PATCH 6/6] Update DHT11.h Signed-off-by: DrJPK <91477569+DrJPK@users.noreply.github.com> --- src/DHT11.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/DHT11.h b/src/DHT11.h index be7e094..d545c15 100644 --- a/src/DHT11.h +++ b/src/DHT11.h @@ -29,6 +29,11 @@ public: */ DHT11(int pin); + /** + * Optional Initialiser + */ + int begin(); + /** * Reads and returns the humidity from the DHT11 sensor. *