GY-63_MS5611/libraries/DHT2pin/dht2pin.cpp

178 lines
3.3 KiB
C++
Raw Normal View History

2016-12-17 15:03:32 -05:00
//
// FILE: DHT2pin.cpp
// AUTHOR: Rob Tillaart
2023-01-29 14:16:17 -05:00
// VERSION: 0.2.0
// PURPOSE: Experimental DHT _temperature & _humidiy Sensor library for Arduino
2020-11-27 05:10:47 -05:00
// URL: https://github.com/RobTillaart/DHT2pin
// http://arduino.cc/playground/Main/DHTLib
2022-11-01 15:26:33 -04:00
2016-12-17 15:03:32 -05:00
#include "DHT2pin.h"
2022-11-01 15:26:33 -04:00
2016-12-17 15:03:32 -05:00
/////////////////////////////////////////////////////
//
2022-11-01 15:26:33 -04:00
// PUBLIC
2016-12-17 15:03:32 -05:00
//
2023-01-29 14:16:17 -05:00
DHT2pin::DHT2pin(uint8_t rpin, uint8_t wpin)
{
_rpin = rpin;
_wpin = wpin;
_temperature = 0;
_humidity = 0;
};
void DHT2pin::begin()
{
pinMode(_rpin, INPUT);
pinMode(_wpin, OUTPUT);
}
2016-12-17 15:03:32 -05:00
2022-11-01 15:26:33 -04:00
// return values:
// DHTLIB_OK
// DHTLIB_ERROR_CHECKSUM
// DHTLIB_ERROR_TIMEOUT
2016-12-17 15:03:32 -05:00
int DHT2pin::read11()
{
2023-01-29 14:16:17 -05:00
// READ VALUES
int rv = _readSensor(DHTLIB_DHT11_WAKEUP);
if (rv != DHTLIB_OK)
{
_humidity = DHTLIB_INVALID_VALUE;
_temperature = DHTLIB_INVALID_VALUE;
return rv;
}
// CONVERT AND STORE
_humidity = _bits[0]; // _bits[1] == 0;
_temperature = _bits[2]; // _bits[3] == 0;
// TEST CHECKSUM
// _bits[1] && _bits[3] both 0
uint8_t sum = _bits[0] + _bits[2];
if (_bits[4] != sum) return DHTLIB_ERROR_CHECKSUM;
return DHTLIB_OK;
2016-12-17 15:03:32 -05:00
}
2022-11-01 15:26:33 -04:00
// return values:
// DHTLIB_OK
// DHTLIB_ERROR_CHECKSUM
// DHTLIB_ERROR_TIMEOUT
2016-12-17 15:03:32 -05:00
int DHT2pin::read()
{
2023-01-29 14:16:17 -05:00
// READ VALUES
int rv = _readSensor(DHTLIB_DHT_WAKEUP);
if (rv != DHTLIB_OK)
{
_humidity = DHTLIB_INVALID_VALUE;
_temperature = DHTLIB_INVALID_VALUE;
return rv; // propagate error value
}
// CONVERT AND STORE
_humidity = word(_bits[0], _bits[1]) * 0.1;
_temperature = word(_bits[2] & 0x7F, _bits[3]) * 0.1;
if (_bits[2] & 0x80) // negative _temperature
{
_temperature = -_temperature;
}
// TEST CHECKSUM
uint8_t sum = _bits[0] + _bits[1] + _bits[2] + _bits[3];
if (_bits[4] != sum)
{
return DHTLIB_ERROR_CHECKSUM;
}
return DHTLIB_OK;
}
2016-12-17 15:03:32 -05:00
2023-01-29 14:16:17 -05:00
float DHT2pin::temperature()
{
return _temperature;
}
float DHT2pin::humidity()
{
return _humidity;
2016-12-17 15:03:32 -05:00
}
2023-01-29 14:16:17 -05:00
2016-12-17 15:03:32 -05:00
/////////////////////////////////////////////////////
//
2022-11-01 15:26:33 -04:00
// PRIVATE
2016-12-17 15:03:32 -05:00
//
2022-11-01 15:26:33 -04:00
// return values:
// DHTLIB_OK
// DHTLIB_ERROR_TIMEOUT
2016-12-17 15:03:32 -05:00
int DHT2pin::_readSensor(uint8_t wakeupDelay)
{
2023-01-29 14:16:17 -05:00
// INIT BUFFERVAR TO RECEIVE DATA
uint8_t mask = 128;
uint8_t idx = 0;
// EMPTY BUFFER
for (uint8_t i = 0; i < 5; i++) _bits[i] = 0;
// REQUEST SAMPLE
digitalWrite(_wpin, LOW);
delay(wakeupDelay);
digitalWrite(_wpin, HIGH);
delayMicroseconds(40);
// GET ACKNOWLEDGE or TIMEOUT
uint16_t loopCnt = DHTLIB_TIMEOUT;
while(digitalRead(_rpin) == LOW)
{
if (--loopCnt == 0) return DHTLIB_ERROR_TIMEOUT;
}
loopCnt = DHTLIB_TIMEOUT;
while(digitalRead(_rpin) == HIGH)
{
if (--loopCnt == 0) return DHTLIB_ERROR_TIMEOUT;
}
// READ THE OUTPUT - 40 _bits => 5 BYTES
for (uint8_t i = 40; i != 0; i--)
{
loopCnt = DHTLIB_TIMEOUT;
2016-12-17 15:03:32 -05:00
while(digitalRead(_rpin) == LOW)
{
2023-01-29 14:16:17 -05:00
if (--loopCnt == 0) return DHTLIB_ERROR_TIMEOUT;
2016-12-17 15:03:32 -05:00
}
2023-01-29 14:16:17 -05:00
uint32_t t = micros();
2016-12-17 15:03:32 -05:00
loopCnt = DHTLIB_TIMEOUT;
while(digitalRead(_rpin) == HIGH)
{
2023-01-29 14:16:17 -05:00
if (--loopCnt == 0) return DHTLIB_ERROR_TIMEOUT;
2016-12-17 15:03:32 -05:00
}
2023-01-29 14:16:17 -05:00
if ((micros() - t) > 40)
{
_bits[idx] |= mask;
}
mask >>= 1;
if (mask == 0) // next byte?
2016-12-17 15:03:32 -05:00
{
2023-01-29 14:16:17 -05:00
mask = 128;
idx++;
2016-12-17 15:03:32 -05:00
}
2023-01-29 14:16:17 -05:00
}
digitalWrite(_wpin, HIGH);
2016-12-17 15:03:32 -05:00
2023-01-29 14:16:17 -05:00
return DHTLIB_OK;
2016-12-17 15:03:32 -05:00
}
2022-11-01 15:26:33 -04:00
// -- END OF FILE --