Skip to content

Commit

Permalink
Merge pull request #14 from dhrubasaha08/hotfix-v2.1.0
Browse files Browse the repository at this point in the history
Hotfix v2.1.0
  • Loading branch information
dhrubasaha08 authored Feb 8, 2024
2 parents 9feccb9 + e4aaf41 commit cc33e77
Show file tree
Hide file tree
Showing 10 changed files with 229 additions and 176 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2023 Dhruba Saha
Copyright (c) 2024 Dhruba Saha

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
75 changes: 46 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,42 +1,59 @@
# DHT11 Arduino Library
![Static Badge](https://img.shields.io/badge/Author-Dhruba%20Saha-red?link=https%3A%2F%2Fgithub.com%2Fdhrubasaha08)
![GitHub Release](https://img.shields.io/github/v/release/dhrubasaha08/DHT11)
[![GitHub](https://img.shields.io/github/license/dhrubasaha08/DHT11)](LICENSE)
[![GitHub contributors](https://img.shields.io/github/contributors/dhrubasaha08/DHT11)](https://github.com/dhrubasaha08/DHT11/graphs/contributors)
![GitHub Downloads (all assets, all releases)](https://img.shields.io/github/downloads/dhrubasaha08/DHT11/total)

## Table of Contents

- [Introduction](#introduction)
- [v2.0.0 Updates](#v200-updates)
- [How It Works](#how-it-works)
- [Internal Protocol Handling](#internal-protocol-handling)
- [Features](#features)
- [Installation](#installation)
- [DHT11 Arduino Library](#dht11-arduino-library)
- [Table of Contents](#table-of-contents)
- [Introduction](#introduction)
- [Updates](#updates)
- [v2.1.0 Hotfix `Latest`](#v210-hotfix-latest)
- [v2.0.0 Update](#v200-update)
- [How It Works](#how-it-works)
- [Internal Protocol Handling](#internal-protocol-handling)
- [Features](#features)
- [Installation](#installation)
- [Arduino IDE Library Manager](#arduino-ide-library-manager)
- [Installing Manually from GitHub](#installing-manually-from-github)
- [Usage](#usage)
- [Basic Usage](#basic-usage)
- [New Methods in v2.0.0](#new-methods-in-v200)
- [Wiring Details](#wiring-details)
- [Examples](#examples)
- [Error Handling](#error-handling)
- [Troubleshooting](#troubleshooting)
- [FAQ](#faq)
- [Compatibility](#compatibility)
- [Contribute by Testing](#contribute-by-testing)
- [Contributing](#contributing)
- [Code of Conduct](#code-of-conduct)
- [License](#license)
- [External References](#external-references)
- [Usage](#usage)
- [Basic Usage](#basic-usage)
- [New Methods](#new-methods)
- [Wiring Details](#wiring-details)
- [Examples](#examples)
- [Error Handling](#error-handling)
- [Troubleshooting](#troubleshooting)
- [FAQ](#faq)
- [Compatibility](#compatibility)
- [Contribute by Testing](#contribute-by-testing)
- [Contributing](#contributing)
- [Code of Conduct](#code-of-conduct)
- [License](#license)
- [External References](#external-references)

## Introduction

This Arduino library is designed for the DHT11 temperature and humidity sensor. It simplifies the process of reading temperature and humidity data, making it easy to integrate the DHT11 sensor into your Arduino projects.

**Author:** [Dhruba Saha](https://github.com/dhrubasaha08)

**Version:** 2.0.0
**Version:** 2.1.0

**License:** [MIT](/LICENSE)

## v2.0.0 Updates
## Updates

### v2.1.0 Hotfix `Latest`
- Introduced the `readTemperatureHumidity(int &temperature, int &humidity)` method, allowing for efficient simultaneous reading of temperature and humidity, streamlining the data acquisition process.
- Refactored the internal data reading process, reducing code repetition and improving maintainability.
- Enhanced error handling: Now, specific error codes are returned from the `readTemperatureHumidity` method, making it consistent with the `readTemperature` and `readHumidity` methods.
- Documentation updated to reflect the new changes and provide clear guidance on using the new library version.

*Note: The updates in version 2.1.0 are backward compatible with the previous versions(v2.x.x), ensuring a smooth transition for existing projects.*

### v2.0.0 Update
- Changed the return type of `readTemperature()` and `readHumidity()` methods from `float` to `int`. This aligns with the DHT11 sensor's 1-degree resolution.
- Enhanced code documentation for easier maintenance and better readability.
- Added the `getErrorString` method to return human-readable error messages based on error codes.
Expand Down Expand Up @@ -117,10 +134,12 @@ This library abstracts these complexities, allowing users to easily read tempera
- Include the `DHT11.h` header file.
- Create an instance of the DHT11 class, specifying the digital pin connected to the sensor's data pin.
- Use `readTemperature()` and `readHumidity()` methods to read the data.
- `getErrorString(int errorCode)`: Returns a human-readable error message based on the provided error code.

### New Methods in v2.0.0
### New Methods

- `readTemperatureHumidity(int &temperature, int &humidity)`: This new method allows for simultaneous reading of temperature and humidity, reducing the complexity and time required for separate readings. It returns an `int` indicating the success or specific error encountered during the operation.

- `getErrorString(int errorCode)`: Returns a human-readable error message based on the provided error code.

### Wiring Details

Expand All @@ -131,7 +150,7 @@ The DHT11 sensor has three or four pins, depending on the variant:
- **Ground (GND)**: Connect to the ground of your MCU.
- **NC (No Connect)**: Some variants have this pin. It is not used and can be left unconnected.

Remember to use a pull-up resistor (typically 10kΩ) between the VCC and Data pins for reliable communication.
Remember to use a pull-up resistor (typically 10kΩ) between the VCC and Data pins for reliable communication.(Optional)

## Examples

Expand All @@ -153,7 +172,7 @@ All examples provided use Arduino UNO's digital pin 2 as the default connection

The library provides clear error handling mechanisms. When reading data:

- If there's a timeout while waiting for a response from the sensor, the methods `readTemperature()` and `readHumidity()` return the value `DHT11::ERROR_TIMEOUT`.
- If there's a timeout while waiting for a response from the sensor, the methods `readTemperature()` and `readHumidity()` & return the value `DHT11::ERROR_TIMEOUT`. <!-- TODO update for int readTemperatureHumidity(int &temperature, int &humidity); -->
- If there's a checksum mismatch indicating data corruption, the methods return the value `DHT11::ERROR_CHECKSUM`.

For translating these error codes to human-readable strings, the library offers the `DHT11::getErrorString(int errorCode)` method.
Expand Down Expand Up @@ -202,8 +221,6 @@ The library has been tested and confirmed to work on the following boards:
- NodeMCU ESP32S v1.1 **`ESP-WROOM-32`** (Tensilica Xtensa LX6 - xtensa architecture) `*`
- NodeMCU ESP8266 v1.0 **`ESP8266MOD`** (Tensilica Xtensa LX106 - xtensa architecture) `*`

`*` For xtensa-based boards (ESP32 and ESP8266), a delay is required between consecutive method calls for optimal performance. Check [Examples](/examples/) for the implementation details.

Given the vast number of boards and architectures available, it's a challenge for a solo developer to test on all. Community contributions in terms of compatibility testing are highly encouraged.

### Contribute by Testing
Expand Down
2 changes: 1 addition & 1 deletion SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Supported Versions

This section lists the versions of the DHT11 library that are currently receiving security updates.
This section lists the versions of the DHT11 library that are currently receiving updates.

| Version | Supported |
| ------- | ------------------ |
Expand Down
24 changes: 10 additions & 14 deletions examples/ReadHumidity/ReadHumidity.ino
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/**
* DHT11 Humidity Reader for Arduino
* DHT11 Humidity Reader
* This sketch reads humidity data from the DHT11 sensor and prints the value to the serial port.
* It also handles potential error states that might occur during reading.
*
* Author: Dhruba Saha
* Version: 2.0.0
* Version: 2.1.0
* License: MIT
*/

Expand All @@ -17,32 +17,28 @@
// - For ESP8266: Connect the sensor to GPIO2 or D4.
DHT11 dht11(2);

void setup()
{
void setup() {
// Initialize serial communication to allow debugging and data readout.
// Using a baud rate of 9600 bps.
Serial.begin(9600);

// Uncomment the line below to set a custom delay between sensor readings (in milliseconds).
// dht11.setDelay(500); // Set this to the desired delay. Default is 500ms.
}

void loop()
{
void loop() {
// Attempt to read the humidity value from the DHT11 sensor.
int humidity = dht11.readHumidity();

// Check the result of the reading.
// If there's no error, print the humidity value.
// If there's an error, print the appropriate error message.
if (humidity != DHT11::ERROR_CHECKSUM && humidity != DHT11::ERROR_TIMEOUT)
{
if (humidity != DHT11::ERROR_CHECKSUM && humidity != DHT11::ERROR_TIMEOUT) {
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");
}
else
{
} else {
// Print error message based on the error code.
Serial.println(DHT11::getErrorString(humidity));
}

// Wait for 1 seconds before the next reading.
delay(1000);
}
38 changes: 17 additions & 21 deletions examples/ReadPlot/ReadPlot.ino
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* DHT11 Sensor Reader for Arduino
* This sketch reads temperature and humidity data from the DHT11 sensor and prints the values to the serial plotter.
* DHT11 Sensor Serial Plotter Reader
* This sketch reads temperature and humidity data from the DHT11 sensor and prints the values to the Arduino IDE serial plotter.
*
* Author: Dhruba Saha
* Version: 2.0.0
* Version: 2.1.0
* License: MIT
*/

Expand All @@ -16,34 +16,30 @@
// - For ESP8266: Connect the sensor to GPIO2 or D4.
DHT11 dht11(2);

void setup()
{
void setup() {
// Initialize serial communication to allow debugging and data readout.
// Using a baud rate of 9600 bps.
Serial.begin(9600);
}

void loop()
{
// Attempt to read the temperature and humidity values from the DHT11 sensor.
int temperature = dht11.readTemperature();
// Uncomment the line below to set a custom delay between sensor readings (in milliseconds).
// dht11.setDelay(500); // Set this to the desired delay. Default is 500ms.
}

// If using ESP32 or ESP8266 (xtensa architecture), uncomment the delay below.
// This ensures stable readings when calling methods consecutively.
// delay(50);
void loop() {
int temperature, humidity;

int humidity = dht11.readHumidity();
// Attempt to read the temperature and humidity values from the DHT11 sensor.
int result = dht11.readTemperatureHumidity(temperature, humidity);

// Check the results of the readings.
// If there are no errors, print the temperature and humidity values in CSV format.
if (temperature != DHT11::ERROR_CHECKSUM && temperature != DHT11::ERROR_TIMEOUT && humidity != DHT11::ERROR_CHECKSUM && humidity != DHT11::ERROR_TIMEOUT)
{
// If the reading is successful, print the temperature and humidity values in CSV format.
if (result == 0) {
Serial.print("Temperature:");
Serial.print(temperature);
Serial.print(",Humidity:");
Serial.println(humidity);
} else {
// Print error message based on the error code.
Serial.println(DHT11::getErrorString(result));
}

// Wait for 1 seconds before the next reading.
delay(1000);
}
}
53 changes: 17 additions & 36 deletions examples/ReadTempAndHumidity/ReadTempAndHumidity.ino
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/**
* DHT11 Sensor Reader for Arduino
* DHT11 Sensor Reader
* This sketch reads temperature and humidity data from the DHT11 sensor and prints the values to the serial port.
* It also handles potential error states that might occur during reading.
*
* Author: Dhruba Saha
* Version: 2.0.0
* Version: 2.1.0
* License: MIT
*/

Expand All @@ -17,52 +17,33 @@
// - For ESP8266: Connect the sensor to GPIO2 or D4.
DHT11 dht11(2);

void setup()
{
void setup() {
// Initialize serial communication to allow debugging and data readout.
// Using a baud rate of 9600 bps.
Serial.begin(9600);

// Uncomment the line below to set a custom delay between sensor readings (in milliseconds).
// dht11.setDelay(500); // Set this to the desired delay. Default is 500ms.
}

void loop()
{
// Attempt to read the temperature and humidity values from the DHT11 sensor.
int temperature = dht11.readTemperature();

// If using ESP32 or ESP8266 (xtensa architecture), uncomment the delay below.
// This ensures stable readings when calling methods consecutively.
// delay(50);
void loop() {
int temperature = 0;
int humidity = 0;

int humidity = dht11.readHumidity();
// Attempt to read the temperature and humidity values from the DHT11 sensor.
int result = dht11.readTemperatureHumidity(temperature, humidity);

// Check the results of the readings.
// If there are no errors, print the temperature and humidity values.
// If the reading is successful, print the temperature and humidity values.
// If there are errors, print the appropriate error messages.
if (temperature != DHT11::ERROR_CHECKSUM && temperature != DHT11::ERROR_TIMEOUT &&
humidity != DHT11::ERROR_CHECKSUM && humidity != DHT11::ERROR_TIMEOUT)
{
if (result == 0) {
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");

Serial.print("Humidity: ");
Serial.print(" °C\tHumidity: ");
Serial.print(humidity);
Serial.println(" %");
} else {
// Print error message based on the error code.
Serial.println(DHT11::getErrorString(result));
}
else
{
if (temperature == DHT11::ERROR_TIMEOUT || temperature == DHT11::ERROR_CHECKSUM)
{
Serial.print("Temperature Reading Error: ");
Serial.println(DHT11::getErrorString(temperature));
}
if (humidity == DHT11::ERROR_TIMEOUT || humidity == DHT11::ERROR_CHECKSUM)
{
Serial.print("Humidity Reading Error: ");
Serial.println(DHT11::getErrorString(humidity));
}
}

// Wait for 1 seconds before the next reading.
delay(1000);
}
24 changes: 10 additions & 14 deletions examples/ReadTemperature/ReadTemperature.ino
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/**
* DHT11 Temperature Reader for Arduino
* DHT11 Temperature Reader
* This sketch reads temperature data from the DHT11 sensor and prints the value to the serial port.
* It also handles potential error states that might occur during reading.
*
* Author: Dhruba Saha
* Version: 2.0.0
* Version: 2.1.0
* License: MIT
*/

Expand All @@ -17,32 +17,28 @@
// - For ESP8266: Connect the sensor to GPIO2 or D4.
DHT11 dht11(2);

void setup()
{
void setup() {
// Initialize serial communication to allow debugging and data readout.
// Using a baud rate of 9600 bps.
Serial.begin(9600);

// Uncomment the line below to set a custom delay between sensor readings (in milliseconds).
// dht11.setDelay(500); // Set this to the desired delay. Default is 500ms.
}

void loop()
{
void loop() {
// Attempt to read the temperature value from the DHT11 sensor.
int temperature = dht11.readTemperature();

// Check the result of the reading.
// If there's no error, print the temperature value.
// If there's an error, print the appropriate error message.
if (temperature != DHT11::ERROR_CHECKSUM && temperature != DHT11::ERROR_TIMEOUT)
{
if (temperature != DHT11::ERROR_CHECKSUM && temperature != DHT11::ERROR_TIMEOUT) {
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
}
else
{
} else {
// Print error message based on the error code.
Serial.println(DHT11::getErrorString(temperature));
}

// Wait for 1 seconds before the next reading.
delay(1000);
}
Loading

0 comments on commit cc33e77

Please sign in to comment.