2017-12-12 16:04:06 -05:00
|
|
|
//
|
|
|
|
// FILE: DHT12.cpp
|
|
|
|
// AUTHOR: Rob Tillaart
|
2020-11-27 05:10:47 -05:00
|
|
|
// VERSION: 0.2.0
|
2017-12-12 16:04:06 -05:00
|
|
|
// PURPOSE: I2C library for DHT12 for Arduino.
|
|
|
|
//
|
|
|
|
// HISTORY:
|
2017-12-21 07:05:45 -05:00
|
|
|
// 0.1.0: 2017-12-11 initial version
|
|
|
|
// 0.1.1: 2017-12-19 added ESP8266 - issue #86
|
|
|
|
// Verified by Viktor Balint
|
2018-09-02 10:26:19 -04:00
|
|
|
// 0.1.2: 2018-09-02 fix negative temperature DHT12 - issue #111
|
2020-11-27 05:10:47 -05:00
|
|
|
// 0.2.0 2020-04-11 explicit constructors, select other Wire interface, #pragma once
|
|
|
|
// 0.2.1 2020-06-07 fix library.json
|
2017-12-12 16:04:06 -05:00
|
|
|
//
|
|
|
|
|
|
|
|
#include <DHT12.h>
|
|
|
|
|
2017-12-21 07:05:45 -05:00
|
|
|
#define DHT12_ADDRESS ((uint8_t)0x5C)
|
|
|
|
|
|
|
|
|
2020-11-27 05:10:47 -05:00
|
|
|
DHT12::DHT12()
|
2017-12-21 07:05:45 -05:00
|
|
|
{
|
2020-11-27 05:10:47 -05:00
|
|
|
DHT12(Wire);
|
2017-12-21 07:05:45 -05:00
|
|
|
}
|
2020-11-27 05:10:47 -05:00
|
|
|
|
|
|
|
|
|
|
|
DHT12::DHT12(TwoWire *wire)
|
|
|
|
{
|
|
|
|
_wire = wire;
|
|
|
|
}
|
|
|
|
|
2017-12-21 07:05:45 -05:00
|
|
|
|
|
|
|
void DHT12::begin()
|
2017-12-12 16:04:06 -05:00
|
|
|
{
|
2020-11-27 05:10:47 -05:00
|
|
|
_wire->begin();
|
2017-12-12 16:04:06 -05:00
|
|
|
}
|
|
|
|
|
2020-11-27 05:10:47 -05:00
|
|
|
|
|
|
|
#if defined(ESP8266) || defined(ESP32)
|
|
|
|
void DHT12::begin(const uint8_t dataPin, const uint8_t clockPin)
|
|
|
|
{
|
|
|
|
if ((dataPin < 255) && (clockPin < 255))
|
|
|
|
{
|
|
|
|
_wire->begin(dataPin, clockPin);
|
|
|
|
} else {
|
|
|
|
_wire->begin();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2017-12-12 16:04:06 -05:00
|
|
|
int8_t DHT12::read()
|
|
|
|
{
|
|
|
|
// READ SENSOR
|
|
|
|
int status = _readSensor();
|
|
|
|
if (status < 0) return status;
|
|
|
|
|
|
|
|
// CONVERT AND STORE
|
|
|
|
humidity = bits[0] + bits[1] * 0.1;
|
2018-09-02 10:26:19 -04:00
|
|
|
temperature = bits[2] + (bits[3] & 0x7F) * 0.1;
|
|
|
|
if (bits[3] & 0x80)
|
2017-12-12 16:04:06 -05:00
|
|
|
{
|
|
|
|
temperature = -temperature;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TEST CHECKSUM
|
|
|
|
uint8_t checksum = bits[0] + bits[1] + bits[2] + bits[3];
|
|
|
|
if (bits[4] != checksum)
|
|
|
|
{
|
|
|
|
return DHT12_ERROR_CHECKSUM;
|
|
|
|
}
|
|
|
|
|
|
|
|
return DHT12_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int DHT12::_readSensor()
|
|
|
|
{
|
|
|
|
// GET CONNECTION
|
2020-11-27 05:10:47 -05:00
|
|
|
_wire->beginTransmission(DHT12_ADDRESS);
|
|
|
|
_wire->write(0);
|
|
|
|
int rv = _wire->endTransmission();
|
2017-12-12 16:04:06 -05:00
|
|
|
if (rv < 0) return rv;
|
|
|
|
|
|
|
|
// GET DATA
|
|
|
|
const uint8_t length = 5;
|
2020-11-27 05:10:47 -05:00
|
|
|
int bytes = _wire->requestFrom(DHT12_ADDRESS, length);
|
2017-12-12 16:04:06 -05:00
|
|
|
if (bytes == 0) return DHT12_ERROR_CONNECT;
|
|
|
|
if (bytes < length) return DHT12_MISSING_BYTES;
|
|
|
|
|
|
|
|
for (int i = 0; i < bytes; i++)
|
|
|
|
{
|
2020-11-27 05:10:47 -05:00
|
|
|
bits[i] = _wire->read();
|
2017-12-12 16:04:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return bytes;
|
|
|
|
}
|
2020-11-27 05:10:47 -05:00
|
|
|
|
|
|
|
// -- END OF FILE --
|