Skip to content
Prev Previous commit
Next Next commit
Clean-up of DHT22 driver code, creates DHT22 test app
  • Loading branch information
Stanlist committed Jun 3, 2022
commit 0a24a27a0890cdb3a3ccb1d094fde33e9dd34076
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ add_app_subdirectory(${TEST_APPS_DIR}/test-pwm)
add_app_subdirectory(${TEST_APPS_DIR}/test-pwmin)
add_app_subdirectory(${TEST_APPS_DIR}/test-quadrature64cpr)
add_app_subdirectory(${TEST_APPS_DIR}/test-stress-can)
add_app_subdirectory(${TEST_APPS_DIR}/test-temp-RH)
add_app_subdirectory(${TEST_APPS_DIR}/test-units)
add_app_subdirectory(${TEST_APPS_DIR}/test-watchdog)

Expand Down
32 changes: 6 additions & 26 deletions libs/sensors/include/DHT22.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,37 +32,21 @@
* This code has been modified to work with the sensor module
*/

#ifndef MBED_DHT_H
#define MBED_DHT_H
#pragma once

#include "Sensor.h"
#include "mbed.h"

// enum eType { DHT11 = 11, SEN11301P = 11, RHT01 = 11, DHT22 = 22, AM2302 = 22, SEN51035P = 22, RHT02 = 22, RHT03 = 22
// };

// enum eError {
// ERROR_NONE = 0,
// BUS_BUSY = 1,
// ERROR_NOT_PRESENT = 2,
// ERROR_ACK_TOO_LONG = 3,
// ERROR_SYNC_TIMEOUT = 4,
// ERROR_DATA_TIMEOUT = 5,
// ERROR_CHECKSUM = 6,
// ERROR_NO_PATIENCE = 7
// };

// typedef enum { CELCIUS = 0, FARENHEIT = 1, KELVIN = 2 } eScale;
namespace Sensor {
class DHT final : public Sensor {
public:
DHT(PinName pin);
~DHT();
[[nodiscard]] bool reset() override;
[[nodiscard]] bool update(void);
float read(void);
float alternateRead(void);
// float CalcdewPoint(float celsius, float humidity);
// float CalcdewPointFast(float celsius, float humidity);
float read(void) override;
float alternateRead(void) override;
bool getStatus() const override;

private:
time_t _lastReadTime;
Expand All @@ -74,9 +58,5 @@ class DHT final : public Sensor {
int DHT_data[6];
float CalcTemperature();
float CalcHumidity();
// float ConvertCelciustoFarenheit(float);
// float ConvertCelciustoKelvin(float);
};
} // namespace Sensor

#endif
} // namespace Sensor
60 changes: 11 additions & 49 deletions libs/sensors/src/DHT22.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* This code has been modified to work with the sensor module
*/

#include "DHT.h"
#include "DHT22.h"

#define DHT_DATA_BIT_COUNT 41

Expand All @@ -50,7 +50,6 @@ bool DHT::update() {
int i, j, retryCount, b;
unsigned int bitTimes[DHT_DATA_BIT_COUNT];

// eError err = ERROR_NONE;
time_t currentTime = time(NULL);

DigitalInOut DHT_io(_pin);
Expand All @@ -62,7 +61,6 @@ bool DHT::update() {
// Only asks for data if more than 2 seconds has lapsed since last call
if (!_firsttime) {
if (int(currentTime - _lastReadTime) < 2) {
// err = ERROR_NO_PATIENCE;
return false;
}
} else {
Expand All @@ -73,7 +71,6 @@ bool DHT::update() {

do {
if (retryCount > 125) {
// err = BUS_BUSY;
return false;
}
retryCount++;
Expand All @@ -92,25 +89,19 @@ bool DHT::update() {
retryCount = 0;
do {
if (retryCount > 40) {
// err = ERROR_NOT_PRESENT;
return false;
}
retryCount++;
wait_us(1);
} while ((DHT_io == 1));

// if (err != ERROR_NONE) {
// return false;
// }

wait_us(80);

for (i = 0; i < 5; i++) {
for (j = 0; j < 8; j++) {
retryCount = 0;
do {
if (retryCount > 75) {
// err = ERROR_DATA_TIMEOUT;
return false;
}
retryCount++;
Expand Down Expand Up @@ -146,7 +137,6 @@ bool DHT::update() {
_lastHumidity = CalcHumidity();

} else {
// err = ERROR_CHECKSUM;
return false;
}

Expand All @@ -170,46 +160,8 @@ float DHT::read() {
return _lastHumidity;
}

// float DHT::ConvertCelciustoFarenheit(float celsius) {
// return celsius * 9 / 5 + 32;
// }

// float DHT::ConvertCelciustoKelvin(float celsius) {
// return celsius + 273.15;
// }

// dewPoint function NOAA
// reference: http://wahiduddin.net/calc/density_algorithms.htm
// float DHT::CalcdewPoint(float celsius, float humidity) {
// float A0 = 373.15 / (273.15 + celsius);
// float SUM = -7.90298 * (A0 - 1);
// SUM += 5.02808 * log10(A0);
// SUM += -1.3816e-7 * (pow(10, (11.344 * (1 - 1 / A0))) - 1);
// SUM += 8.1328e-3 * (pow(10, (-3.49149 * (A0 - 1))) - 1);
// SUM += log10(1013.246);
// float VP = pow(10, SUM - 3) * humidity;
// float T = log(VP / 0.61078); // temp var
// return (241.88 * T) / (17.558 - T);
// }

// // delta max = 0.6544 wrt dewPoint()
// // 5x faster than dewPoint()
// // reference: http://en.wikipedia.org/wiki/Dew_point
// float DHT::CalcdewPointFast(float celsius, float humidity) {
// float a = 17.271;
// float b = 237.7;
// float temp = (a * celsius) / (b + celsius) + log(humidity / 100);
// float Td = (b * temp) / (a - temp);
// return Td;
// }

// Returns the last read temperature in the wanted scale
float DHT::alternateRead() {
// if (Scale == FARENHEIT)
// return ConvertCelciustoFarenheit(_lastTemperature);
// else if (Scale == KELVIN)
// return ConvertCelciustoKelvin(_lastTemperature);
// else
return _lastTemperature;
}

Expand All @@ -221,4 +173,14 @@ float DHT::CalcHumidity() {
v += DHT_data[1];
v /= 10;
return float(v);
}

// No current purpose
bool DHT::reset() {
return true;
}

// No current purpose
bool DHT::getStatus() const {
return true;
}
3 changes: 3 additions & 0 deletions supported_build_configurations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ test-adafruitSTEMMA:
- SCIENCE_REV2_2021
# - UWRT_NUCLEO

test-temp-RH:
- UWRT_NUCLEO

test-units:
- UWRT_NUCLEO

Expand Down
4 changes: 4 additions & 0 deletions test-apps/test-temp-RH/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
add_executable(test-temp-RH)
target_sources(test-temp-RH PRIVATE src/main.cpp)
target_link_libraries(test-temp-RH PRIVATE DHT Logger)
mbed_set_post_build(test-temp-RH)
24 changes: 24 additions & 0 deletions test-apps/test-temp-RH/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "DHT22.h"
#include "Logger.h"
#include "PinNames.h"
#include "mbed.h"

int main() {
Sensor::DHT sensor(A0);
while (true) {
// MBED_ASSERT(sensor.update());

ThisThread::sleep_for(2000ms);

if (sensor.update()) {
printf("\r\nSensor data received\r\n");
} else {
printf("\r\nSensor reading failed\r\n");
}

ThisThread::sleep_for(1000ms);

printf("Temperature: %f \r\n", sensor.read());
printf("Temperature: %f \r\n", sensor.alternateRead());
}
}