mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
0.4.0 DHT12
This commit is contained in:
parent
dfac27089e
commit
1316821ef3
@ -6,7 +6,7 @@ jobs:
|
|||||||
lint:
|
lint:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v3
|
||||||
- uses: arduino/arduino-lint-action@v1
|
- uses: arduino/arduino-lint-action@v1
|
||||||
with:
|
with:
|
||||||
library-manager: update
|
library-manager: update
|
||||||
|
@ -8,7 +8,7 @@ jobs:
|
|||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v3
|
||||||
- uses: ruby/setup-ruby@v1
|
- uses: ruby/setup-ruby@v1
|
||||||
with:
|
with:
|
||||||
ruby-version: 2.6
|
ruby-version: 2.6
|
||||||
|
@ -10,7 +10,7 @@ jobs:
|
|||||||
test:
|
test:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v3
|
||||||
- name: json-syntax-check
|
- name: json-syntax-check
|
||||||
uses: limitusus/json-syntax-check@v1
|
uses: limitusus/json-syntax-check@v1
|
||||||
with:
|
with:
|
||||||
|
@ -6,11 +6,20 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
|
|||||||
and this project adheres to [Semantic Versioning](http://semver.org/).
|
and this project adheres to [Semantic Versioning](http://semver.org/).
|
||||||
|
|
||||||
|
|
||||||
|
## [0.4.0] - 2023-02-09
|
||||||
|
- fix **lastRead()** initial value.
|
||||||
|
- add defaults to functions.
|
||||||
|
- update readme.md
|
||||||
|
- move code to .cpp
|
||||||
|
- update GitHub actions
|
||||||
|
- update license 2023
|
||||||
|
|
||||||
|
----
|
||||||
|
|
||||||
## [0.3.3] - 2022-11-01
|
## [0.3.3] - 2022-11-01
|
||||||
- add changelog.md
|
- add changelog.md
|
||||||
- add rp2040 to build-CI
|
- add rp2040 to build-CI
|
||||||
|
|
||||||
|
|
||||||
## [0.3.2] - 2021-12-16
|
## [0.3.2] - 2021-12-16
|
||||||
- update library.json
|
- update library.json
|
||||||
- update license
|
- update license
|
||||||
|
@ -1,10 +1,9 @@
|
|||||||
//
|
//
|
||||||
// FILE: DHT12.cpp
|
// FILE: DHT12.cpp
|
||||||
// AUTHOR: Rob Tillaart
|
// AUTHOR: Rob Tillaart
|
||||||
// VERSION: 0.3.3
|
// VERSION: 0.4.0
|
||||||
// PURPOSE: I2C library for DHT12 for Arduino.
|
// PURPOSE: Arduino library for I2C DHT12 temperature and humidity sensor.
|
||||||
//
|
// URL: https://github.com/RobTillaart/DHT12
|
||||||
// HISTORY: see changelog.md
|
|
||||||
|
|
||||||
|
|
||||||
#include "DHT12.h"
|
#include "DHT12.h"
|
||||||
@ -19,6 +18,7 @@ DHT12::DHT12(TwoWire *wire)
|
|||||||
_humidity = 0;
|
_humidity = 0;
|
||||||
_humOffset = 0;
|
_humOffset = 0;
|
||||||
_tempOffset = 0;
|
_tempOffset = 0;
|
||||||
|
_lastRead = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -53,11 +53,11 @@ bool DHT12::isConnected()
|
|||||||
|
|
||||||
int8_t DHT12::read()
|
int8_t DHT12::read()
|
||||||
{
|
{
|
||||||
// READ SENSOR
|
// READ SENSOR
|
||||||
int status = _readSensor();
|
int status = _readSensor();
|
||||||
if (status < 0) return status;
|
if (status < 0) return status;
|
||||||
|
|
||||||
// CONVERT AND STORE
|
// CONVERT AND STORE
|
||||||
_humidity = _bits[0] + _bits[1] * 0.1;
|
_humidity = _bits[0] + _bits[1] * 0.1;
|
||||||
_temperature = _bits[2] + (_bits[3] & 0x7F) * 0.1;
|
_temperature = _bits[2] + (_bits[3] & 0x7F) * 0.1;
|
||||||
if (_bits[3] & 0x80)
|
if (_bits[3] & 0x80)
|
||||||
@ -65,9 +65,12 @@ int8_t DHT12::read()
|
|||||||
_temperature = -_temperature;
|
_temperature = -_temperature;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TEST CHECKSUM
|
// TEST CHECKSUM
|
||||||
uint8_t checksum = _bits[0] + _bits[1] + _bits[2] + _bits[3];
|
uint8_t checksum = _bits[0] + _bits[1] + _bits[2] + _bits[3];
|
||||||
if (_bits[4] != checksum) return DHT12_ERROR_CHECKSUM;
|
if (_bits[4] != checksum)
|
||||||
|
{
|
||||||
|
return DHT12_ERROR_CHECKSUM;
|
||||||
|
}
|
||||||
|
|
||||||
_lastRead = millis();
|
_lastRead = millis();
|
||||||
|
|
||||||
@ -75,20 +78,71 @@ int8_t DHT12::read()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
float DHT12::getHumidity()
|
||||||
|
{
|
||||||
|
return _humidity + _humOffset;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
float DHT12::getTemperature()
|
||||||
|
{
|
||||||
|
return _temperature + _tempOffset;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void DHT12::setHumOffset(float offset)
|
||||||
|
{
|
||||||
|
_humOffset = offset;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void DHT12::setTempOffset(float offset)
|
||||||
|
{
|
||||||
|
_tempOffset = offset;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
float DHT12::getHumOffset()
|
||||||
|
{
|
||||||
|
return _humOffset;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
float DHT12::getTempOffset()
|
||||||
|
{
|
||||||
|
return _tempOffset;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
uint32_t DHT12::lastRead()
|
||||||
|
{
|
||||||
|
return _lastRead;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int DHT12::_readSensor()
|
int DHT12::_readSensor()
|
||||||
{
|
{
|
||||||
// GET CONNECTION
|
// GET CONNECTION
|
||||||
_wire->beginTransmission(DHT12_ADDRESS);
|
_wire->beginTransmission(DHT12_ADDRESS);
|
||||||
_wire->write(0);
|
_wire->write(0);
|
||||||
int rv = _wire->endTransmission();
|
int rv = _wire->endTransmission();
|
||||||
if (rv < 0) return rv;
|
if (rv < 0)
|
||||||
|
{
|
||||||
|
return rv;
|
||||||
|
}
|
||||||
|
|
||||||
// GET DATA
|
// GET DATA
|
||||||
const uint8_t length = 5;
|
const uint8_t length = 5;
|
||||||
int bytes = _wire->requestFrom(DHT12_ADDRESS, length);
|
int bytes = _wire->requestFrom(DHT12_ADDRESS, length);
|
||||||
|
|
||||||
if (bytes == 0) return DHT12_ERROR_CONNECT;
|
if (bytes == 0)
|
||||||
if (bytes < length) return DHT12_MISSING_BYTES;
|
{
|
||||||
|
return DHT12_ERROR_CONNECT;
|
||||||
|
}
|
||||||
|
if (bytes < length)
|
||||||
|
{
|
||||||
|
return DHT12_MISSING_BYTES;
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = 0; i < bytes; i++)
|
for (int i = 0; i < bytes; i++)
|
||||||
{
|
{
|
||||||
@ -99,4 +153,5 @@ int DHT12::_readSensor()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// -- END OF FILE --
|
// -- END OF FILE --
|
||||||
|
|
||||||
|
@ -2,17 +2,15 @@
|
|||||||
//
|
//
|
||||||
// FILE: DHT12.h
|
// FILE: DHT12.h
|
||||||
// AUTHOR: Rob Tillaart
|
// AUTHOR: Rob Tillaart
|
||||||
// PURPOSE: DHT_I2C library for Arduino .
|
// VERSION: 0.4.0
|
||||||
// VERSION: 0.3.3
|
// PURPOSE: Arduino library for I2C DHT12 temperature and humidity sensor.
|
||||||
// HISTORY: See DHT12.cpp
|
|
||||||
// URL: https://github.com/RobTillaart/DHT12
|
// URL: https://github.com/RobTillaart/DHT12
|
||||||
//
|
|
||||||
|
|
||||||
|
|
||||||
#include "Arduino.h"
|
#include "Arduino.h"
|
||||||
#include "Wire.h"
|
#include "Wire.h"
|
||||||
|
|
||||||
#define DHT12_LIB_VERSION (F("0.3.3"))
|
#define DHT12_LIB_VERSION (F("0.4.0"))
|
||||||
|
|
||||||
#define DHT12_OK 0
|
#define DHT12_OK 0
|
||||||
#define DHT12_ERROR_CHECKSUM -10
|
#define DHT12_ERROR_CHECKSUM -10
|
||||||
@ -23,7 +21,7 @@
|
|||||||
class DHT12
|
class DHT12
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
DHT12(TwoWire *wire); // to be tested explicitly
|
DHT12(TwoWire *wire = &Wire); // to be tested explicitly
|
||||||
|
|
||||||
#if defined(ESP8266) || defined(ESP32)
|
#if defined(ESP8266) || defined(ESP32)
|
||||||
bool begin(const uint8_t dataPin, const uint8_t clockPin);
|
bool begin(const uint8_t dataPin, const uint8_t clockPin);
|
||||||
@ -33,15 +31,17 @@ public:
|
|||||||
bool isConnected();
|
bool isConnected();
|
||||||
|
|
||||||
int8_t read();
|
int8_t read();
|
||||||
float getHumidity() { return _humidity + _humOffset; };
|
float getHumidity();
|
||||||
float getTemperature() { return _temperature + _tempOffset; };
|
float getTemperature();
|
||||||
|
|
||||||
// allows 1st order calibration
|
// allows 1st order calibration
|
||||||
void setHumOffset(float offset) { _humOffset = offset; };
|
void setHumOffset(float offset = 0);
|
||||||
void setTempOffset(float offset) { _tempOffset = offset; };
|
void setTempOffset(float offset = 0);
|
||||||
float getHumOffset() { return _humOffset; };
|
float getHumOffset();
|
||||||
float getTempOffset() { return _tempOffset; };
|
float getTempOffset();
|
||||||
uint32_t lastRead() { return _lastRead; };
|
|
||||||
|
uint32_t lastRead();
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
float _humidity;
|
float _humidity;
|
||||||
@ -50,10 +50,11 @@ private:
|
|||||||
float _tempOffset;
|
float _tempOffset;
|
||||||
uint8_t _bits[5];
|
uint8_t _bits[5];
|
||||||
uint32_t _lastRead;
|
uint32_t _lastRead;
|
||||||
|
|
||||||
int _readSensor();
|
int _readSensor();
|
||||||
TwoWire* _wire;
|
TwoWire* _wire;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// -- END OF FILE --
|
// -- END OF FILE --
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
MIT License
|
MIT License
|
||||||
|
|
||||||
Copyright (c) 2017-2022 Rob Tillaart
|
Copyright (c) 2017-2023 Rob Tillaart
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
@ -20,52 +20,72 @@ Thereafter one has to call the **read()** function to do the actual reading,
|
|||||||
and with **getTemperature()** and **getHumidity()** to get the read values.
|
and with **getTemperature()** and **getHumidity()** to get the read values.
|
||||||
Calling these latter again will return the same values until a new **read()** is called.
|
Calling these latter again will return the same values until a new **read()** is called.
|
||||||
|
|
||||||
|
#### I2C
|
||||||
|
|
||||||
|
The DHT12 has a fixed I2C address of 0x5C. To use multiple DHT12's one need an
|
||||||
|
I2C multiplexer like PCA9548 or TCA9548.
|
||||||
|
|
||||||
|
The DHT12 should work up to 400 KHz however this is not tested (yet).
|
||||||
|
|
||||||
|
|
||||||
## Interface
|
## Interface
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
#include "DHT12.h"
|
||||||
|
```
|
||||||
|
|
||||||
### Constructor
|
|
||||||
|
|
||||||
- **DHT12(TwoWire \*wire)** constructor, using a specific Wire (I2C bus).
|
#### Constructor
|
||||||
- **bool begin(uint8_t dataPin, uint8_t clockPin)** begin for ESP32 et al, to set I2C bus pins
|
|
||||||
|
- **DHT12(TwoWire \*wire = &Wire)** constructor, using a specific Wire (I2C bus).
|
||||||
|
Default is set to Wire.
|
||||||
|
- **bool begin(uint8_t dataPin, uint8_t clockPin)** begin for ESP32 et al, to set I2C bus pins.
|
||||||
- **bool begin()** initializer for non ESP32. Returns true if connected.
|
- **bool begin()** initializer for non ESP32. Returns true if connected.
|
||||||
- **bool isConnected()** returns true if the address of the DHT12 can be seen on the I2C bus. (since 0.3.2)
|
- **bool isConnected()** returns true if the address of the DHT12 can be seen on the I2C bus. (since 0.3.2)
|
||||||
|
|
||||||
|
|
||||||
### Core
|
#### Core
|
||||||
|
|
||||||
- **int8_t read()** read the sensor and store the values internally. It returns the status of the read which should be 0.
|
- **int8_t read()** read the sensor and store the values internally. It returns the status of the read which should be 0.
|
||||||
- **float getHumidity()** returns last Humidity read, or -999 in case of error.
|
- **float getHumidity()** returns last Humidity read, or -999 in case of error.
|
||||||
- **float getTemperature()** returns last Temperature read, or -999 in case of error.
|
- **float getTemperature()** returns last Temperature read, or -999 in case of error.
|
||||||
- **uint32_t lastRead()** returns the timestamp in milliseconds since startup of the last successful read. (added in 0.3.1)
|
- **uint32_t lastRead()** returns the timestamp of the last successful read in milliseconds since startup.
|
||||||
|
If zero there has been no **read()** called yet.
|
||||||
|
|
||||||
|
|
||||||
### Offset
|
#### Offset
|
||||||
|
|
||||||
- **void setHumOffset(float offset)** set an offset to calibrate (1st order) the sensor.
|
- **void setHumOffset(float offset = 0)** set an offset to calibrate (1st order) the sensor.
|
||||||
- **float getHumOffset()** return current offset, default 0.
|
Default offset is 0.
|
||||||
- **void setTempOffset(float offset)** set an offset to calibrate (1st order) the sensor
|
- **float getHumOffset()** return current humidity offset, default 0.
|
||||||
- **float getTempOffset()** return current offset, default 0.
|
- **void setTempOffset(float offset = 0)** set an offset to calibrate (1st order) the sensor.
|
||||||
|
Default offset is 0.
|
||||||
|
- **float getTempOffset()** return current temperature offset, default 0.
|
||||||
### 0.3.0
|
|
||||||
|
|
||||||
- The members **temperature** and **humidity** became private in 0.3.0.
|
|
||||||
- added offset for temperature and humidity.
|
|
||||||
|
|
||||||
|
|
||||||
## Operation
|
|
||||||
|
|
||||||
See examples
|
|
||||||
|
|
||||||
|
|
||||||
## Future
|
## Future
|
||||||
|
|
||||||
- check for optimizations. although I2C overhead is much more...
|
#### Must
|
||||||
- test at different I2C speeds 400 KHz should be possible.
|
|
||||||
|
|
||||||
|
#### Should
|
||||||
|
|
||||||
|
- test at different I2C speeds
|
||||||
|
- 400 KHz should be possible.
|
||||||
- add examples.
|
- add examples.
|
||||||
- **void setIgnoreChecksum(bool = false)** ignore checksum flag speeds up communication a bit
|
|
||||||
- **bool getIgnoreChecksum()** get status. for completeness.
|
|
||||||
-
|
#### Could
|
||||||
|
|
||||||
|
- check for optimizations. although I2C overhead is much more.
|
||||||
|
- add **void setIgnoreChecksum(bool = false)** ignore checksum flag speeds up communication a bit
|
||||||
|
- add **bool getIgnoreChecksum()** get status. for completeness.
|
||||||
|
- investigate if it is possible to extract temp and hum separately
|
||||||
|
- faster?
|
||||||
|
- add **void suppressErrorReads(bool)** prevents the -999, returns previous value
|
||||||
|
- add **bool getSuppressError()**
|
||||||
|
|
||||||
|
|
||||||
|
#### Wont
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,13 +1,13 @@
|
|||||||
# Syntax Coloring Map For DHT_I2C
|
# Syntax Colouring Map For DHT12
|
||||||
|
|
||||||
# Datatypes (KEYWORD1)
|
# Data types (KEYWORD1)
|
||||||
DHT12 KEYWORD1
|
DHT12 KEYWORD1
|
||||||
|
|
||||||
# Methods and Functions (KEYWORD2)
|
# Methods and Functions (KEYWORD2)
|
||||||
begin KEYWORD2
|
begin KEYWORD2
|
||||||
isConnected KEYWORD2
|
isConnected KEYWORD2
|
||||||
read KEYWORD2
|
|
||||||
|
|
||||||
|
read KEYWORD2
|
||||||
getHumidity KEYWORD2
|
getHumidity KEYWORD2
|
||||||
getTemperature KEYWORD2
|
getTemperature KEYWORD2
|
||||||
|
|
||||||
@ -15,6 +15,7 @@ setHumOffset KEYWORD2
|
|||||||
setTempOffset KEYWORD2
|
setTempOffset KEYWORD2
|
||||||
getHumOffset KEYWORD2
|
getHumOffset KEYWORD2
|
||||||
getTempOffset KEYWORD2
|
getTempOffset KEYWORD2
|
||||||
|
|
||||||
lastRead KEYWORD2
|
lastRead KEYWORD2
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "DHT12",
|
"name": "DHT12",
|
||||||
"keywords": "Temperature, Humidity, DHT12, I2C",
|
"keywords": "Temperature, Humidity, DHT12, I2C",
|
||||||
"description": "Arduino library for I2C DHT12 temperature and humidity sensor. ",
|
"description": "Arduino library for I2C DHT12 temperature and humidity sensor.\nTemperature and humidity can be calibrated with an offset. ",
|
||||||
"authors":
|
"authors":
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
@ -15,7 +15,7 @@
|
|||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/RobTillaart/DHT12.git"
|
"url": "https://github.com/RobTillaart/DHT12.git"
|
||||||
},
|
},
|
||||||
"version": "0.3.3",
|
"version": "0.4.0",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"frameworks": "arduino",
|
"frameworks": "arduino",
|
||||||
"platforms": "*",
|
"platforms": "*",
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
name=DHT12
|
name=DHT12
|
||||||
version=0.3.3
|
version=0.4.0
|
||||||
author=Rob Tillaart <rob.tillaart@gmail.com>
|
author=Rob Tillaart <rob.tillaart@gmail.com>
|
||||||
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
||||||
sentence=Arduino library for I2C DHT12 temperature and humidity sensor.
|
sentence=Arduino library for I2C DHT12 temperature and humidity sensor.
|
||||||
paragraph=DHT12
|
paragraph=Temperature and humidity can be calibrated with an offset.
|
||||||
category=Sensors
|
category=Sensors
|
||||||
url=https://github.com/RobTillaart/DHT12
|
url=https://github.com/RobTillaart/DHT12
|
||||||
architectures=*
|
architectures=*
|
||||||
|
@ -34,6 +34,7 @@ unittest_setup()
|
|||||||
fprintf(stderr, "DHT12_LIB_VERSION: %s \n", (char *) DHT12_LIB_VERSION);
|
fprintf(stderr, "DHT12_LIB_VERSION: %s \n", (char *) DHT12_LIB_VERSION);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
unittest_teardown()
|
unittest_teardown()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
@ -91,4 +92,6 @@ unittest(test_offset)
|
|||||||
|
|
||||||
unittest_main()
|
unittest_main()
|
||||||
|
|
||||||
// --------
|
|
||||||
|
// -- END OF FILE --
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user