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. diff --git a/src/DHT11.cpp b/src/DHT11.cpp index cfcb3e2..2eb5da2 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" @@ -22,92 +23,90 @@ DHT11::DHT11(int pin) digitalWrite(_pin, HIGH); } +/** + * Optional begin method to initialise the sensor values + * + * @return 0 if OK or Error code + */ +int DHT11::begin() +{ + 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; + readSensor(); + 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(); + readSensor(); + 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; } /** diff --git a/src/DHT11.h b/src/DHT11.h index 7387d06..d545c15 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 @@ -28,6 +29,11 @@ public: */ DHT11(int pin); + /** + * Optional Initialiser + */ + int begin(); + /** * Reads and returns the humidity from the DHT11 sensor. * @@ -47,6 +53,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. /** @@ -59,6 +66,18 @@ 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 = 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 + */ + int readSensor(); /** * Reads a byte of data from the DHT11 sensor.