Skip to content
Prev Previous commit
Next Next commit
Replaces previous code with premade DHT library code for mbed, change…
…s to allow it to work with sensor module
  • Loading branch information
Stanlist committed Mar 13, 2022
commit b98fd7d7178b381c42bd752c2d2aefd6943307c4
114 changes: 82 additions & 32 deletions libs/sensors/include/DHT22.h
Original file line number Diff line number Diff line change
@@ -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
/*
* 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
247 changes: 223 additions & 24 deletions libs/sensors/src/DHT22.cpp
Original file line number Diff line number Diff line change
@@ -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);
}