v2.0.0: Enhanced docs, error handling & examples

This commit is contained in:
Dhruba Saha
2023-09-19 11:02:49 +05:30
parent 323e8c192b
commit 542161d1f0
11 changed files with 570 additions and 78 deletions

View File

@@ -1,6 +1,20 @@
/**
* DHT11.cpp
* Library for reading temperature and humidity from the DHT11 sensor.
*
* Author: Dhruba Saha
* Version: 2.0.0
* License: MIT
*/
#include "DHT11.h"
// Initializes the pin and sets it to output mode.
/**
* Constructor for the DHT11 class.
* Initializes the pin to be used for communication and sets it to output mode.
*
* @param pin: Digital pin number on the Arduino board to which the DHT11 sensor is connected.
*/
DHT11::DHT11(int pin)
{
_pin = pin;
@@ -8,12 +22,26 @@ DHT11::DHT11(int pin)
digitalWrite(_pin, HIGH);
}
// Reads and returns the temperature from the sensor. Returns -1 if the checksum is incorrect.
float DHT11::readTemperature()
/**
* 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.
*/
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)
{
@@ -21,27 +49,43 @@ float DHT11::readTemperature()
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 -1;
return DHT11::ERROR_CHECKSUM;
}
// Reads and returns the humidity from the sensor. Returns -1 if the checksum is incorrect.
float DHT11::readHumidity()
/**
* 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.
*/
int DHT11::readHumidity()
{
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)
{
@@ -49,22 +93,28 @@ float DHT11::readHumidity()
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[0];
}
}
}
return -1;
return DHT11::ERROR_CHECKSUM;
}
// Reads a byte of data from the sensor.
/**
* Reads a byte of data from the DHT11 sensor during the communication process.
*
* @return: A byte of data read from the sensor.
*/
byte DHT11::readByte()
{
byte value = 0;
@@ -84,7 +134,11 @@ byte DHT11::readByte()
return value;
}
// Sends the start signal to the sensor.
/**
* Sends a start signal to the DHT11 sensor to initiate a data read.
* This involves setting the data pin low for a specific duration, then high,
* and finally setting it to input mode to read the data.
*/
void DHT11::startSignal()
{
pinMode(_pin, OUTPUT);
@@ -94,3 +148,24 @@ void DHT11::startSignal()
delayMicroseconds(40);
pinMode(_pin, INPUT);
}
/**
* Returns a human-readable error message based on the provided error code.
* This method facilitates easier debugging and user feedback by translating
* numeric error codes into descriptive strings.
*
* @param errorCode The error code for which the description is required.
* @return A descriptive string explaining the error.
*/
String DHT11::getErrorString(int errorCode)
{
switch (errorCode)
{
case DHT11::ERROR_TIMEOUT:
return "Error: Reading from DHT11 timed out.";
case DHT11::ERROR_CHECKSUM:
return "Error: Checksum mismatch while reading from DHT11.";
default:
return "Error: Unknown error code.";
}
}

View File

@@ -1,29 +1,78 @@
/**
* DHT11.h
* Header file for the DHT11 library, providing functionalities to interface with
* the DHT11 temperature and humidity sensor.
*
* Author: Dhruba Saha
* Version: 2.0.0
* License: MIT
*/
#ifndef DHT11_h
#define DHT11_h
#include "Arduino.h"
// DHT11 class for reading temperature and humidity data from a DHT11 sensor.
/**
* DHT11 Class
* Provides methods to read temperature and humidity data from the DHT11 sensor.
*/
class DHT11
{
public:
// Takes the pin number as an argument.
/**
* Constructor
* Initializes the data pin to be used for communication with the DHT11 sensor.
*
* @param pin: Digital pin number on the Arduino board to which the DHT11 sensor is connected.
*/
DHT11(int pin);
// Reads and returns the humidity from the sensor. Returns -1 if the checksum is incorrect.
float readHumidity();
/**
* 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.
*/
int readHumidity();
// Reads and returns the temperature from the sensor. Returns -1 if the checksum is incorrect.
float readTemperature();
/**
* 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.
*/
int readTemperature();
// 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 TIMEOUT_DURATION = 300; // Duration (in milliseconds) to wait before timing out.
/**
* Returns a human-readable error message based on the provided error code.
*
* @param errorCode: The error code for which the message is required.
* @return: A string describing the error.
*/
static String getErrorString(int errorCode);
private:
int _pin;
int _pin; // Pin number used for communication with the DHT11 sensor.
// Reads a byte of data from the sensor.
/**
* Reads a byte of data from the DHT11 sensor.
*
* @return: A byte of data read from the sensor.
*/
byte readByte();
// Sends the start signal to the sensor.
/**
* Sends a start signal to the DHT11 sensor to initiate a data read.
* This involves setting the data pin low for a specific duration, then high,
* and finally setting it to input mode to read the data.
*/
void startSignal();
};
#endif
#endif