From 1e7d50a405b58ee8d3d214f9a90cc90ca2c02788 Mon Sep 17 00:00:00 2001 From: jahnavithota2011 <34670246+jahnavithota2011@users.noreply.github.com> Date: Sun, 6 Feb 2022 23:23:39 -0500 Subject: [PATCH 1/8] Create PollingSensors.h --- libs/sensors/include/PollingSensors.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 libs/sensors/include/PollingSensors.h diff --git a/libs/sensors/include/PollingSensors.h b/libs/sensors/include/PollingSensors.h new file mode 100644 index 00000000..389e22ca --- /dev/null +++ b/libs/sensors/include/PollingSensors.h @@ -0,0 +1,16 @@ +#pragma once + +#include "Sensor.h" +#include "mbed.h" + +class PollingSensors final : Sensor { + public: + PollingSensors(PinName clk, PinName miso, PinName cs = NC); + float read() override; + float alternateRead() override; + bool getStatus() const override; + [[nodiscard]] bool reset() override; + [[nodiscard]] bool update() override; + + //Add functions specific to sensor modules +}; From 77ae7e2e4560206e9232a434c90b616bc72715cd Mon Sep 17 00:00:00 2001 From: jahnavithota2011 <34670246+jahnavithota2011@users.noreply.github.com> Date: Sun, 6 Feb 2022 23:39:58 -0500 Subject: [PATCH 2/8] Update PollingSensors.h --- libs/sensors/include/PollingSensors.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/libs/sensors/include/PollingSensors.h b/libs/sensors/include/PollingSensors.h index 389e22ca..9e489c3a 100644 --- a/libs/sensors/include/PollingSensors.h +++ b/libs/sensors/include/PollingSensors.h @@ -11,6 +11,5 @@ class PollingSensors final : Sensor { bool getStatus() const override; [[nodiscard]] bool reset() override; [[nodiscard]] bool update() override; - - //Add functions specific to sensor modules + // Add functions specific to sensor modules }; From 4667f7fdc6add18b0ce1a92058a75e8782bcff74 Mon Sep 17 00:00:00 2001 From: Stanley Hao Date: Thu, 17 Feb 2022 20:55:51 -0500 Subject: [PATCH 3/8] Created DHT22.h and DHT22.cpp driver files for Temp/RH sensor --- libs/sensors/include/DHT22.h | 32 ++++++++++++++++++++++++++++++++ libs/sensors/src/DHT22.cpp | 25 +++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 libs/sensors/include/DHT22.h create mode 100644 libs/sensors/src/DHT22.cpp diff --git a/libs/sensors/include/DHT22.h b/libs/sensors/include/DHT22.h new file mode 100644 index 00000000..861225dc --- /dev/null +++ b/libs/sensors/include/DHT22.h @@ -0,0 +1,32 @@ +#pragma once + +#include "Sensor.h" +#include "mbed.h" + +namespace Sensor { +class DHT22 final : public Sensor { + public: + DHT22(PinName data); + + // Returns internal humidity value (and temperature? Since all transmitted at the same time...) + float read() override; + + // Returns internal temperature reading *Probably irrelevant? + float alternateRead() override; + + // Returns whether sensor responds to request? + bool getStatus() const override; + + // irrelevant probably + [[nodiscard]] bool reset() override; + + // Requests relative humidity and temperature data from sensor + [[nodiscard]] bool update() override; + + private: + mutable DigitalInOut m_data; + bool updateData(); + + float m_humidity, m_temperature; +}; +} // namespace Sensor \ No newline at end of file diff --git a/libs/sensors/src/DHT22.cpp b/libs/sensors/src/DHT22.cpp new file mode 100644 index 00000000..42e556a6 --- /dev/null +++ b/libs/sensors/src/DHT22.cpp @@ -0,0 +1,25 @@ +#include "DHT22.h" + +using namespace Sensor; + +DHT22::DHT22(PinName data) : m_data(data) {} + +// Returns internal humidity value (and temperature? Since all transmitted at the same time...) +float DHT22::read() { + // Not sure what the mutex is used for, maybe ask later? + return m_humidity; +} + +float DHT22::alternateRead() { + return m_temperature; +} + +// bool DHT22::getStatus() {} + +// bool DHT22::reset() {} + +bool DHT22::update() { + // need to probably use the DigitalInOut type for getting the data from the sensor + // Setup a 2 second loop of sorts? or 80 microseconds? Refer to Arduino code + // Needs to check if 2 seconds have been passed already since last call +} \ No newline at end of file From b98fd7d7178b381c42bd752c2d2aefd6943307c4 Mon Sep 17 00:00:00 2001 From: Stanley Hao Date: Sun, 13 Mar 2022 15:33:16 -0400 Subject: [PATCH 4/8] Replaces previous code with premade DHT library code for mbed, changes to allow it to work with sensor module --- libs/sensors/include/DHT22.h | 114 +++++++++++----- libs/sensors/src/DHT22.cpp | 247 +++++++++++++++++++++++++++++++---- 2 files changed, 305 insertions(+), 56 deletions(-) diff --git a/libs/sensors/include/DHT22.h b/libs/sensors/include/DHT22.h index 861225dc..cb3a312a 100644 --- a/libs/sensors/include/DHT22.h +++ b/libs/sensors/include/DHT22.h @@ -1,32 +1,82 @@ -#pragma once - -#include "Sensor.h" -#include "mbed.h" - -namespace Sensor { -class DHT22 final : public Sensor { - public: - DHT22(PinName data); - - // Returns internal humidity value (and temperature? Since all transmitted at the same time...) - float read() override; - - // Returns internal temperature reading *Probably irrelevant? - float alternateRead() override; - - // Returns whether sensor responds to request? - bool getStatus() const override; - - // irrelevant probably - [[nodiscard]] bool reset() override; - - // Requests relative humidity and temperature data from sensor - [[nodiscard]] bool update() override; - - private: - mutable DigitalInOut m_data; - bool updateData(); - - float m_humidity, m_temperature; -}; -} // namespace Sensor \ No newline at end of file +/* + * DHT Library for Digital-output Humidity and Temperature sensors + * + * Works with DHT11, DHT21, DHT22 + * SEN11301P, Grove - Temperature&Humidity Sensor (Seeed Studio) + * SEN51035P, Grove - Temperature&Humidity Sensor Pro (Seeed Studio) + * AM2302 , temperature-humidity sensor + * RHT01,RHT02, RHT03 , Humidity and Temperature Sensor (Sparkfun) + * + * Copyright (C) Wim De Roeve + * based on DHT22 sensor library by HO WING KIT + * Arduino DHT11 library + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documnetation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + * This code has been modified to work with the sensor module + */ + +#ifndef MBED_DHT_H +#define MBED_DHT_H + +#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 update(void); + float read(void); + float alternateRead(void); + // float CalcdewPoint(float celsius, float humidity); + // float CalcdewPointFast(float celsius, float humidity); + + private: + time_t _lastReadTime; + float _lastTemperature; + float _lastHumidity; + PinName _pin; + bool _firsttime; + int _DHTtype; + int DHT_data[6]; + float CalcTemperature(); + float CalcHumidity(); + // float ConvertCelciustoFarenheit(float); + // float ConvertCelciustoKelvin(float); +}; +} // namespace Sensor + +#endif \ No newline at end of file diff --git a/libs/sensors/src/DHT22.cpp b/libs/sensors/src/DHT22.cpp index 42e556a6..91a9f15c 100644 --- a/libs/sensors/src/DHT22.cpp +++ b/libs/sensors/src/DHT22.cpp @@ -1,25 +1,224 @@ -#include "DHT22.h" - -using namespace Sensor; - -DHT22::DHT22(PinName data) : m_data(data) {} - -// Returns internal humidity value (and temperature? Since all transmitted at the same time...) -float DHT22::read() { - // Not sure what the mutex is used for, maybe ask later? - return m_humidity; -} - -float DHT22::alternateRead() { - return m_temperature; -} - -// bool DHT22::getStatus() {} - -// bool DHT22::reset() {} - -bool DHT22::update() { - // need to probably use the DigitalInOut type for getting the data from the sensor - // Setup a 2 second loop of sorts? or 80 microseconds? Refer to Arduino code - // Needs to check if 2 seconds have been passed already since last call +/* + * DHT Library for Digital-output Humidity and Temperature sensors + * + * Works with DHT11, DHT22 + * SEN11301P, Grove - Temperature&Humidity Sensor (Seeed Studio) + * SEN51035P, Grove - Temperature&Humidity Sensor Pro (Seeed Studio) + * AM2302 , temperature-humidity sensor + * HM2303 , Digital-output humidity and temperature sensor + * + * Copyright (C) Wim De Roeve + * based on DHT22 sensor library by HO WING KIT + * Arduino DHT11 library + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documnetation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS OR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + * This code has been modified to work with the sensor module + */ + +#include "DHT.h" + +#define DHT_DATA_BIT_COUNT 41 + +using namespace Sensor; + +DHT::DHT(PinName pin) { + _pin = pin; + _firsttime = true; +} + +DHT::~DHT() {} + +// Reads temparature and relative humidity data from sensor, saves it in corresponding variables +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); + + for (i = 0; i < DHT_DATA_BIT_COUNT; i++) { + bitTimes[i] = 0; + } + + // 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 { + _firsttime = false; + _lastReadTime = currentTime; + } + retryCount = 0; + + do { + if (retryCount > 125) { + // err = BUS_BUSY; + return false; + } + retryCount++; + wait_us(2); + } while ((DHT_io == 0)); + + // Send start signal + DHT_io.output(); + DHT_io = 0; + wait_us(18000); + DHT_io = 1; + wait_us(40); + DHT_io.input(); + + // Fails if no response from sensor + 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++; + wait_us(1); + } while (DHT_io == 0); + wait_us(40); + bitTimes[i * 8 + j] = DHT_io; + + int count = 0; + while (DHT_io == 1 && count < 100) { + wait_us(1); + count++; + } + } + } + DHT_io.output(); + DHT_io = 1; + + // Populate DHT_data with sensor data + for (i = 0; i < 5; i++) { + b = 0; + for (j = 0; j < 8; j++) { + if (bitTimes[i * 8 + j + 1] > 0) { + b |= (1 << (7 - j)); + } + } + DHT_data[i] = b; + } + + if (DHT_data[4] == ((DHT_data[0] + DHT_data[1] + DHT_data[2] + DHT_data[3]) & 0xFF)) { + _lastReadTime = currentTime; + _lastTemperature = CalcTemperature(); + _lastHumidity = CalcHumidity(); + + } else { + // err = ERROR_CHECKSUM; + return false; + } + + return true; +} + +// Map the temperature data from the sensor and return the mapped value +float DHT::CalcTemperature() { + int v; + + v = DHT_data[2] & 0x7F; + v *= 256; + v += DHT_data[3]; + v /= 10; + if (DHT_data[2] & 0x80) v *= -1; + return float(v); +} + +// Return last read humidity value +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; +} + +float DHT::CalcHumidity() { + int v; + + v = DHT_data[0]; + v *= 256; + v += DHT_data[1]; + v /= 10; + return float(v); } \ No newline at end of file From 0a24a27a0890cdb3a3ccb1d094fde33e9dd34076 Mon Sep 17 00:00:00 2001 From: Stanley Hao Date: Thu, 2 Jun 2022 21:25:19 -0400 Subject: [PATCH 5/8] Clean-up of DHT22 driver code, creates DHT22 test app --- CMakeLists.txt | 1 + libs/sensors/include/DHT22.h | 32 +++----------- libs/sensors/src/DHT22.cpp | 60 +++++---------------------- supported_build_configurations.yaml | 3 ++ test-apps/test-temp-RH/CMakeLists.txt | 4 ++ test-apps/test-temp-RH/src/main.cpp | 24 +++++++++++ 6 files changed, 49 insertions(+), 75 deletions(-) create mode 100644 test-apps/test-temp-RH/CMakeLists.txt create mode 100644 test-apps/test-temp-RH/src/main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 0a747dc2..345b3c0c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/libs/sensors/include/DHT22.h b/libs/sensors/include/DHT22.h index cb3a312a..4706abbf 100644 --- a/libs/sensors/include/DHT22.h +++ b/libs/sensors/include/DHT22.h @@ -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; @@ -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 \ No newline at end of file +} // namespace Sensor \ No newline at end of file diff --git a/libs/sensors/src/DHT22.cpp b/libs/sensors/src/DHT22.cpp index 91a9f15c..93cd0b76 100644 --- a/libs/sensors/src/DHT22.cpp +++ b/libs/sensors/src/DHT22.cpp @@ -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 @@ -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); @@ -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 { @@ -73,7 +71,6 @@ bool DHT::update() { do { if (retryCount > 125) { - // err = BUS_BUSY; return false; } retryCount++; @@ -92,17 +89,12 @@ 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++) { @@ -110,7 +102,6 @@ bool DHT::update() { retryCount = 0; do { if (retryCount > 75) { - // err = ERROR_DATA_TIMEOUT; return false; } retryCount++; @@ -146,7 +137,6 @@ bool DHT::update() { _lastHumidity = CalcHumidity(); } else { - // err = ERROR_CHECKSUM; return false; } @@ -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; } @@ -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; } \ No newline at end of file diff --git a/supported_build_configurations.yaml b/supported_build_configurations.yaml index 0a385884..fa011ecf 100644 --- a/supported_build_configurations.yaml +++ b/supported_build_configurations.yaml @@ -71,6 +71,9 @@ test-adafruitSTEMMA: - SCIENCE_REV2_2021 # - UWRT_NUCLEO +test-temp-RH: + - UWRT_NUCLEO + test-units: - UWRT_NUCLEO diff --git a/test-apps/test-temp-RH/CMakeLists.txt b/test-apps/test-temp-RH/CMakeLists.txt new file mode 100644 index 00000000..26f7dc4f --- /dev/null +++ b/test-apps/test-temp-RH/CMakeLists.txt @@ -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) diff --git a/test-apps/test-temp-RH/src/main.cpp b/test-apps/test-temp-RH/src/main.cpp new file mode 100644 index 00000000..3ebfa635 --- /dev/null +++ b/test-apps/test-temp-RH/src/main.cpp @@ -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()); + } +} \ No newline at end of file From e8b2495021775e3e30a8fc27f2fb7a70f766c65f Mon Sep 17 00:00:00 2001 From: Stanley Hao Date: Thu, 2 Jun 2022 22:08:45 -0400 Subject: [PATCH 6/8] Fix CMake configuration for DHT22 sensor driver --- libs/sensors/CMakeLists.txt | 9 +++++++++ test-apps/test-temp-RH/CMakeLists.txt | 8 +++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/libs/sensors/CMakeLists.txt b/libs/sensors/CMakeLists.txt index 835cb5c9..8cdec8b5 100644 --- a/libs/sensors/CMakeLists.txt +++ b/libs/sensors/CMakeLists.txt @@ -21,3 +21,12 @@ target_link_libraries(CurrentSensor Sensor mbed-os ) + +add_library(DHT22 STATIC) +target_sources(DHT22 PRIVATE src/DHT22.cpp) +target_include_directories(DHT22 PUBLIC include) +target_link_libraries(DHT22 + PRIVATE + Sensor + mbed-os + ) diff --git a/test-apps/test-temp-RH/CMakeLists.txt b/test-apps/test-temp-RH/CMakeLists.txt index 26f7dc4f..967cb90b 100644 --- a/test-apps/test-temp-RH/CMakeLists.txt +++ b/test-apps/test-temp-RH/CMakeLists.txt @@ -1,4 +1,10 @@ add_executable(test-temp-RH) target_sources(test-temp-RH PRIVATE src/main.cpp) -target_link_libraries(test-temp-RH PRIVATE DHT Logger) +target_include_directories(test-temp-RH PUBLIC include) +target_link_libraries(test-temp-RH + PRIVATE + DHT22 + Logger + uwrt-mars-rover-hw-bridge + ) mbed_set_post_build(test-temp-RH) From e76585492fceda75276c6630cf0669bb8f782c49 Mon Sep 17 00:00:00 2001 From: Stanley Hao Date: Thu, 23 Jun 2022 22:16:28 -0400 Subject: [PATCH 7/8] Fixes to testing app, streamlining DHT header and cpp file --- libs/sensors/include/DHT22.h | 1 - libs/sensors/src/DHT22.cpp | 13 +++++++++++-- test-apps/test-temp-RH/src/main.cpp | 13 +++++++------ 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/libs/sensors/include/DHT22.h b/libs/sensors/include/DHT22.h index 4706abbf..fd4697cc 100644 --- a/libs/sensors/include/DHT22.h +++ b/libs/sensors/include/DHT22.h @@ -54,7 +54,6 @@ class DHT final : public Sensor { float _lastHumidity; PinName _pin; bool _firsttime; - int _DHTtype; int DHT_data[6]; float CalcTemperature(); float CalcHumidity(); diff --git a/libs/sensors/src/DHT22.cpp b/libs/sensors/src/DHT22.cpp index 93cd0b76..f2d307a1 100644 --- a/libs/sensors/src/DHT22.cpp +++ b/libs/sensors/src/DHT22.cpp @@ -61,6 +61,7 @@ bool DHT::update() { // Only asks for data if more than 2 seconds has lapsed since last call if (!_firsttime) { if (int(currentTime - _lastReadTime) < 2) { + printf("Too Fast!"); return false; } } else { @@ -71,6 +72,7 @@ bool DHT::update() { do { if (retryCount > 125) { + printf("Bus Busy"); return false; } retryCount++; @@ -85,29 +87,35 @@ bool DHT::update() { wait_us(40); DHT_io.input(); - // Fails if no response from sensor + // Fails if sensor doesn't pull down data bus retryCount = 0; do { if (retryCount > 40) { + printf("No Response from sensor"); return false; } retryCount++; wait_us(1); } while ((DHT_io == 1)); + // Sensor pulls down for 80us wait_us(80); + // Gather data from sensor for (i = 0; i < 5; i++) { for (j = 0; j < 8; j++) { retryCount = 0; do { if (retryCount > 75) { + printf("Data Timeout"); return false; } retryCount++; wait_us(1); } while (DHT_io == 0); + wait_us(40); + bitTimes[i * 8 + j] = DHT_io; int count = 0; @@ -120,7 +128,7 @@ bool DHT::update() { DHT_io.output(); DHT_io = 1; - // Populate DHT_data with sensor data + // Populate DHT_data byte array with bit data for (i = 0; i < 5; i++) { b = 0; for (j = 0; j < 8; j++) { @@ -137,6 +145,7 @@ bool DHT::update() { _lastHumidity = CalcHumidity(); } else { + printf("Checksum failed"); return false; } diff --git a/test-apps/test-temp-RH/src/main.cpp b/test-apps/test-temp-RH/src/main.cpp index 3ebfa635..1fc35365 100644 --- a/test-apps/test-temp-RH/src/main.cpp +++ b/test-apps/test-temp-RH/src/main.cpp @@ -4,7 +4,7 @@ #include "mbed.h" int main() { - Sensor::DHT sensor(A0); + Sensor::DHT sensor(D7); while (true) { // MBED_ASSERT(sensor.update()); @@ -12,13 +12,14 @@ int main() { if (sensor.update()) { printf("\r\nSensor data received\r\n"); + + ThisThread::sleep_for(1000ms); + + printf("Humidity: %i\r\n", 1000 * (int)sensor.read()); + printf("Temperature: %i\r\n", 1000 * (int)sensor.alternateRead()); + } 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()); } } \ No newline at end of file From e0cacfaa331bb1b6c68f8e7e18571ed035809ccb Mon Sep 17 00:00:00 2001 From: Stanley Hao Date: Thu, 30 Jun 2022 22:55:23 -0400 Subject: [PATCH 8/8] Streamlines sensor driver code, comments out debug print statements --- libs/sensors/src/DHT22.cpp | 10 +++++----- test-apps/test-temp-RH/src/main.cpp | 2 -- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/libs/sensors/src/DHT22.cpp b/libs/sensors/src/DHT22.cpp index f2d307a1..8a8e6981 100644 --- a/libs/sensors/src/DHT22.cpp +++ b/libs/sensors/src/DHT22.cpp @@ -61,7 +61,7 @@ bool DHT::update() { // Only asks for data if more than 2 seconds has lapsed since last call if (!_firsttime) { if (int(currentTime - _lastReadTime) < 2) { - printf("Too Fast!"); + // printf("Too Fast!"); return false; } } else { @@ -72,7 +72,7 @@ bool DHT::update() { do { if (retryCount > 125) { - printf("Bus Busy"); + // printf("Bus Busy"); return false; } retryCount++; @@ -91,7 +91,7 @@ bool DHT::update() { retryCount = 0; do { if (retryCount > 40) { - printf("No Response from sensor"); + // printf("No Response from sensor"); return false; } retryCount++; @@ -107,7 +107,7 @@ bool DHT::update() { retryCount = 0; do { if (retryCount > 75) { - printf("Data Timeout"); + // printf("Data Timeout"); return false; } retryCount++; @@ -145,7 +145,7 @@ bool DHT::update() { _lastHumidity = CalcHumidity(); } else { - printf("Checksum failed"); + // printf("Checksum failed"); return false; } diff --git a/test-apps/test-temp-RH/src/main.cpp b/test-apps/test-temp-RH/src/main.cpp index 1fc35365..14091d7b 100644 --- a/test-apps/test-temp-RH/src/main.cpp +++ b/test-apps/test-temp-RH/src/main.cpp @@ -6,8 +6,6 @@ int main() { Sensor::DHT sensor(D7); while (true) { - // MBED_ASSERT(sensor.update()); - ThisThread::sleep_for(2000ms); if (sensor.update()) {