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

152 lines
3.3 KiB
C++
Raw Normal View History

2016-12-17 15:03:32 -05:00
//
// FILE: DHT2pin.cpp
// AUTHOR: Rob Tillaart
2022-11-01 15:26:33 -04:00
// VERSION: 0.1.3
2017-07-27 09:51:10 -04:00
// PURPOSE: Experimental DHT Temperature & Humidity Sensor library for Arduino
2020-11-27 05:10:47 -05:00
// URL: https://github.com/RobTillaart/DHT2pin
// http://arduino.cc/playground/Main/DHTLib
2016-12-17 15:03:32 -05:00
//
2022-11-01 15:26:33 -04:00
// HISTORY: see changelog.md
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
//
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()
{
2022-11-01 15:26:33 -04:00
// READ VALUES
2016-12-17 15:03:32 -05:00
int rv = _readSensor(DHTLIB_DHT11_WAKEUP);
if (rv != DHTLIB_OK)
{
humidity = DHTLIB_INVALID_VALUE; // invalid value, or is NaN prefered?
temperature = DHTLIB_INVALID_VALUE; // invalid value
return rv;
}
2022-11-01 15:26:33 -04:00
// CONVERT AND STORE
2016-12-17 15:03:32 -05:00
humidity = bits[0]; // bits[1] == 0;
temperature = bits[2]; // bits[3] == 0;
2022-11-01 15:26:33 -04:00
// TEST CHECKSUM
// bits[1] && bits[3] both 0
2016-12-17 15:03:32 -05:00
uint8_t sum = bits[0] + bits[2];
if (bits[4] != sum) return DHTLIB_ERROR_CHECKSUM;
return DHTLIB_OK;
}
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()
{
2022-11-01 15:26:33 -04:00
// READ VALUES
2016-12-17 15:03:32 -05:00
int rv = _readSensor(DHTLIB_DHT_WAKEUP);
if (rv != DHTLIB_OK)
{
2022-11-01 15:26:33 -04:00
humidity = DHTLIB_INVALID_VALUE; // invalid value, or is NaN prefered?
temperature = DHTLIB_INVALID_VALUE; // invalid value
return rv; // propagate error value
2016-12-17 15:03:32 -05:00
}
2022-11-01 15:26:33 -04:00
// CONVERT AND STORE
2016-12-17 15:03:32 -05:00
humidity = word(bits[0], bits[1]) * 0.1;
temperature = word(bits[2] & 0x7F, bits[3]) * 0.1;
2022-11-01 15:26:33 -04:00
if (bits[2] & 0x80) // negative temperature
2016-12-17 15:03:32 -05:00
{
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;
}
/////////////////////////////////////////////////////
//
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)
{
2022-11-01 15:26:33 -04:00
// INIT BUFFERVAR TO RECEIVE DATA
2016-12-17 15:03:32 -05:00
uint8_t mask = 128;
uint8_t idx = 0;
2022-11-01 15:26:33 -04:00
// EMPTY BUFFER
2016-12-17 15:03:32 -05:00
for (uint8_t i = 0; i < 5; i++) bits[i] = 0;
2022-11-01 15:26:33 -04:00
// REQUEST SAMPLE
2016-12-17 15:03:32 -05:00
digitalWrite(_wpin, LOW);
delay(wakeupDelay);
digitalWrite(_wpin, HIGH);
delayMicroseconds(40);
2022-11-01 15:26:33 -04:00
// GET ACKNOWLEDGE or TIMEOUT
2016-12-17 15:03:32 -05:00
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;
while(digitalRead(_rpin) == LOW)
{
if (--loopCnt == 0) return DHTLIB_ERROR_TIMEOUT;
}
uint32_t t = micros();
loopCnt = DHTLIB_TIMEOUT;
while(digitalRead(_rpin) == HIGH)
{
if (--loopCnt == 0) return DHTLIB_ERROR_TIMEOUT;
}
if ((micros() - t) > 40)
{
bits[idx] |= mask;
}
mask >>= 1;
if (mask == 0) // next byte?
{
mask = 128;
idx++;
}
}
digitalWrite(_wpin, HIGH);
return DHTLIB_OK;
}
2022-11-01 15:26:33 -04:00
// -- END OF FILE --