0.4.0 DHT12

This commit is contained in:
rob tillaart 2023-02-09 18:45:30 +01:00
parent dfac27089e
commit 1316821ef3
12 changed files with 157 additions and 68 deletions

View File

@ -6,7 +6,7 @@ jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: arduino/arduino-lint-action@v1
with:
library-manager: update

View File

@ -8,7 +8,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: ruby/setup-ruby@v1
with:
ruby-version: 2.6

View File

@ -10,7 +10,7 @@ jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: json-syntax-check
uses: limitusus/json-syntax-check@v1
with:

View File

@ -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/).
## [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
- add changelog.md
- add rp2040 to build-CI
## [0.3.2] - 2021-12-16
- update library.json
- update license

View File

@ -1,10 +1,9 @@
//
// FILE: DHT12.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.3.3
// PURPOSE: I2C library for DHT12 for Arduino.
//
// HISTORY: see changelog.md
// VERSION: 0.4.0
// PURPOSE: Arduino library for I2C DHT12 temperature and humidity sensor.
// URL: https://github.com/RobTillaart/DHT12
#include "DHT12.h"
@ -19,6 +18,7 @@ DHT12::DHT12(TwoWire *wire)
_humidity = 0;
_humOffset = 0;
_tempOffset = 0;
_lastRead = 0;
}
@ -53,11 +53,11 @@ bool DHT12::isConnected()
int8_t DHT12::read()
{
// READ SENSOR
// READ SENSOR
int status = _readSensor();
if (status < 0) return status;
// CONVERT AND STORE
// CONVERT AND STORE
_humidity = _bits[0] + _bits[1] * 0.1;
_temperature = _bits[2] + (_bits[3] & 0x7F) * 0.1;
if (_bits[3] & 0x80)
@ -65,9 +65,12 @@ int8_t DHT12::read()
_temperature = -_temperature;
}
// TEST CHECKSUM
// TEST CHECKSUM
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();
@ -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()
{
// GET CONNECTION
// GET CONNECTION
_wire->beginTransmission(DHT12_ADDRESS);
_wire->write(0);
int rv = _wire->endTransmission();
if (rv < 0) return rv;
if (rv < 0)
{
return rv;
}
// GET DATA
// GET DATA
const uint8_t length = 5;
int bytes = _wire->requestFrom(DHT12_ADDRESS, length);
if (bytes == 0) return DHT12_ERROR_CONNECT;
if (bytes < length) return DHT12_MISSING_BYTES;
if (bytes == 0)
{
return DHT12_ERROR_CONNECT;
}
if (bytes < length)
{
return DHT12_MISSING_BYTES;
}
for (int i = 0; i < bytes; i++)
{
@ -99,4 +153,5 @@ int DHT12::_readSensor()
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -2,17 +2,15 @@
//
// FILE: DHT12.h
// AUTHOR: Rob Tillaart
// PURPOSE: DHT_I2C library for Arduino .
// VERSION: 0.3.3
// HISTORY: See DHT12.cpp
// VERSION: 0.4.0
// PURPOSE: Arduino library for I2C DHT12 temperature and humidity sensor.
// URL: https://github.com/RobTillaart/DHT12
//
#include "Arduino.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_ERROR_CHECKSUM -10
@ -23,7 +21,7 @@
class DHT12
{
public:
DHT12(TwoWire *wire); // to be tested explicitly
DHT12(TwoWire *wire = &Wire); // to be tested explicitly
#if defined(ESP8266) || defined(ESP32)
bool begin(const uint8_t dataPin, const uint8_t clockPin);
@ -33,15 +31,17 @@ public:
bool isConnected();
int8_t read();
float getHumidity() { return _humidity + _humOffset; };
float getTemperature() { return _temperature + _tempOffset; };
float getHumidity();
float getTemperature();
// allows 1st order calibration
void setHumOffset(float offset) { _humOffset = offset; };
void setTempOffset(float offset) { _tempOffset = offset; };
float getHumOffset() { return _humOffset; };
float getTempOffset() { return _tempOffset; };
uint32_t lastRead() { return _lastRead; };
void setHumOffset(float offset = 0);
void setTempOffset(float offset = 0);
float getHumOffset();
float getTempOffset();
uint32_t lastRead();
private:
float _humidity;
@ -50,10 +50,11 @@ private:
float _tempOffset;
uint8_t _bits[5];
uint32_t _lastRead;
int _readSensor();
TwoWire* _wire;
};
// -- END OF FILE --
// -- END OF FILE --

View File

@ -1,6 +1,6 @@
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
of this software and associated documentation files (the "Software"), to deal

View File

@ -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.
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
```cpp
#include "DHT12.h"
```
### Constructor
- **DHT12(TwoWire \*wire)** constructor, using a specific Wire (I2C bus).
- **bool begin(uint8_t dataPin, uint8_t clockPin)** begin for ESP32 et al, to set I2C bus pins
#### Constructor
- **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 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.
- **float getHumidity()** returns last Humidity 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.
- **float getHumOffset()** return current offset, default 0.
- **void setTempOffset(float offset)** set an offset to calibrate (1st order) the sensor
- **float getTempOffset()** return current 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
- **void setHumOffset(float offset = 0)** set an offset to calibrate (1st order) the sensor.
Default offset is 0.
- **float getHumOffset()** return current humidity 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.
## Future
- check for optimizations. although I2C overhead is much more...
- test at different I2C speeds 400 KHz should be possible.
#### Must
#### Should
- test at different I2C speeds
- 400 KHz should be possible.
- 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

View File

@ -1,13 +1,13 @@
# Syntax Coloring Map For DHT_I2C
# Syntax Colouring Map For DHT12
# Datatypes (KEYWORD1)
# Data types (KEYWORD1)
DHT12 KEYWORD1
# Methods and Functions (KEYWORD2)
begin KEYWORD2
isConnected KEYWORD2
read KEYWORD2
read KEYWORD2
getHumidity KEYWORD2
getTemperature KEYWORD2
@ -15,6 +15,7 @@ setHumOffset KEYWORD2
setTempOffset KEYWORD2
getHumOffset KEYWORD2
getTempOffset KEYWORD2
lastRead KEYWORD2

View File

@ -1,7 +1,7 @@
{
"name": "DHT12",
"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":
[
{
@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/DHT12.git"
},
"version": "0.3.3",
"version": "0.4.0",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*",

View File

@ -1,9 +1,9 @@
name=DHT12
version=0.3.3
version=0.4.0
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library for I2C DHT12 temperature and humidity sensor.
paragraph=DHT12
paragraph=Temperature and humidity can be calibrated with an offset.
category=Sensors
url=https://github.com/RobTillaart/DHT12
architectures=*

View File

@ -34,6 +34,7 @@ unittest_setup()
fprintf(stderr, "DHT12_LIB_VERSION: %s \n", (char *) DHT12_LIB_VERSION);
}
unittest_teardown()
{
}
@ -91,4 +92,6 @@ unittest(test_offset)
unittest_main()
// --------
// -- END OF FILE --