Skip to content
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
11 changes: 10 additions & 1 deletion libs/sensors/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ 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
)

add_library(PollingSensors STATIC)
target_sources(PollingSensors PRIVATE src/PollingSensors.cpp)
target_include_directories(PollingSensors PUBLIC include)
Expand All @@ -30,4 +40,3 @@ target_link_libraries(PollingSensors
mbed-os
Logger
)

61 changes: 61 additions & 0 deletions libs/sensors/include/DHT22.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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
*/

#pragma once

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

namespace Sensor {
class DHT final : public Sensor {
public:
DHT(PinName pin);
~DHT();
[[nodiscard]] bool reset() override;
[[nodiscard]] bool update(void);
float read(void) override;
float alternateRead(void) override;
bool getStatus() const override;

private:
time_t _lastReadTime;
float _lastTemperature;
float _lastHumidity;
PinName _pin;
bool _firsttime;
int DHT_data[6];
float CalcTemperature();
float CalcHumidity();
};
} // namespace Sensor
195 changes: 195 additions & 0 deletions libs/sensors/src/DHT22.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
/*
* 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 "DHT22.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];

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) {
// printf("Too Fast!");
return false;
}
} else {
_firsttime = false;
_lastReadTime = currentTime;
}
retryCount = 0;

do {
if (retryCount > 125) {
// printf("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 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;
while (DHT_io == 1 && count < 100) {
wait_us(1);
count++;
}
}
}
DHT_io.output();
DHT_io = 1;

// Populate DHT_data byte array with bit 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 {
// printf("Checksum failed");
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;
}

// Returns the last read temperature in the wanted scale
float DHT::alternateRead() {
return _lastTemperature;
}

float DHT::CalcHumidity() {
int v;

v = DHT_data[0];
v *= 256;
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
10 changes: 10 additions & 0 deletions test-apps/test-temp-RH/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
add_executable(test-temp-RH)
target_sources(test-temp-RH PRIVATE src/main.cpp)
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)
23 changes: 23 additions & 0 deletions test-apps/test-temp-RH/src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "DHT22.h"
#include "Logger.h"
#include "PinNames.h"
#include "mbed.h"

int main() {
Sensor::DHT sensor(D7);
while (true) {
ThisThread::sleep_for(2000ms);

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");
}
}
}