0.2.0 DHT2pin

This commit is contained in:
rob tillaart 2023-01-29 20:16:17 +01:00
parent 4c65b33e04
commit a4812a874f
12 changed files with 230 additions and 172 deletions

View File

@ -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

View File

@ -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

View File

@ -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:

View File

@ -6,6 +6,18 @@ 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.2.0] - 2023-01-29
- add temperature() and humidity() as access function.
- move code to .cpp
- removed inline wrappers (minimize concept)
- update GitHub actions
- update license 2023
- update metadata
- add constants to unit test
- minor edits
----
## [0.1.3] - 2022-11-01 ## [0.1.3] - 2022-11-01
- add changelog.md - add changelog.md
- add rp2040 to build-CI - add rp2040 to build-CI
@ -20,6 +32,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- own repository - own repository
- #pragma once - #pragma once
----
## [0.0.3] ## [0.0.3]
- Fix issue #33 - Fix issue #33

View File

@ -1,6 +1,6 @@
MIT License MIT License
Copyright (c) 2016-2022 Rob Tillaart Copyright (c) 2016-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

View File

@ -1,12 +1,10 @@
// //
// FILE: DHT2pin.cpp // FILE: DHT2pin.cpp
// AUTHOR: Rob Tillaart // AUTHOR: Rob Tillaart
// VERSION: 0.1.3 // VERSION: 0.2.0
// PURPOSE: Experimental DHT Temperature & Humidity Sensor library for Arduino // PURPOSE: Experimental DHT _temperature & _humidiy Sensor library for Arduino
// URL: https://github.com/RobTillaart/DHT2pin // URL: https://github.com/RobTillaart/DHT2pin
// http://arduino.cc/playground/Main/DHTLib // http://arduino.cc/playground/Main/DHTLib
//
// HISTORY: see changelog.md
#include "DHT2pin.h" #include "DHT2pin.h"
@ -16,6 +14,21 @@
// //
// PUBLIC // PUBLIC
// //
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);
}
// return values: // return values:
// DHTLIB_OK // DHTLIB_OK
@ -23,25 +36,25 @@
// DHTLIB_ERROR_TIMEOUT // DHTLIB_ERROR_TIMEOUT
int DHT2pin::read11() int DHT2pin::read11()
{ {
// READ VALUES // READ VALUES
int rv = _readSensor(DHTLIB_DHT11_WAKEUP); int rv = _readSensor(DHTLIB_DHT11_WAKEUP);
if (rv != DHTLIB_OK) if (rv != DHTLIB_OK)
{ {
humidity = DHTLIB_INVALID_VALUE; // invalid value, or is NaN prefered? _humidity = DHTLIB_INVALID_VALUE;
temperature = DHTLIB_INVALID_VALUE; // invalid value _temperature = DHTLIB_INVALID_VALUE;
return rv; return rv;
} }
// CONVERT AND STORE // CONVERT AND STORE
humidity = bits[0]; // bits[1] == 0; _humidity = _bits[0]; // _bits[1] == 0;
temperature = bits[2]; // bits[3] == 0; _temperature = _bits[2]; // _bits[3] == 0;
// TEST CHECKSUM // TEST CHECKSUM
// bits[1] && bits[3] both 0 // _bits[1] && _bits[3] both 0
uint8_t sum = bits[0] + bits[2]; uint8_t sum = _bits[0] + _bits[2];
if (bits[4] != sum) return DHTLIB_ERROR_CHECKSUM; if (_bits[4] != sum) return DHTLIB_ERROR_CHECKSUM;
return DHTLIB_OK; return DHTLIB_OK;
} }
@ -51,32 +64,45 @@ int DHT2pin::read11()
// DHTLIB_ERROR_TIMEOUT // DHTLIB_ERROR_TIMEOUT
int DHT2pin::read() int DHT2pin::read()
{ {
// READ VALUES // READ VALUES
int rv = _readSensor(DHTLIB_DHT_WAKEUP); int rv = _readSensor(DHTLIB_DHT_WAKEUP);
if (rv != DHTLIB_OK) if (rv != DHTLIB_OK)
{ {
humidity = DHTLIB_INVALID_VALUE; // invalid value, or is NaN prefered? _humidity = DHTLIB_INVALID_VALUE;
temperature = DHTLIB_INVALID_VALUE; // invalid value _temperature = DHTLIB_INVALID_VALUE;
return rv; // propagate error value return rv; // propagate error value
} }
// CONVERT AND STORE // CONVERT AND STORE
humidity = word(bits[0], bits[1]) * 0.1; _humidity = word(_bits[0], _bits[1]) * 0.1;
temperature = word(bits[2] & 0x7F, bits[3]) * 0.1; _temperature = word(_bits[2] & 0x7F, _bits[3]) * 0.1;
if (bits[2] & 0x80) // negative temperature if (_bits[2] & 0x80) // negative _temperature
{ {
temperature = -temperature; _temperature = -_temperature;
} }
// TEST CHECKSUM // TEST CHECKSUM
uint8_t sum = bits[0] + bits[1] + bits[2] + bits[3]; uint8_t sum = _bits[0] + _bits[1] + _bits[2] + _bits[3];
if (bits[4] != sum) if (_bits[4] != sum)
{ {
return DHTLIB_ERROR_CHECKSUM; return DHTLIB_ERROR_CHECKSUM;
} }
return DHTLIB_OK; return DHTLIB_OK;
} }
float DHT2pin::temperature()
{
return _temperature;
}
float DHT2pin::humidity()
{
return _humidity;
}
///////////////////////////////////////////////////// /////////////////////////////////////////////////////
// //
// PRIVATE // PRIVATE
@ -87,63 +113,63 @@ int DHT2pin::read()
// DHTLIB_ERROR_TIMEOUT // DHTLIB_ERROR_TIMEOUT
int DHT2pin::_readSensor(uint8_t wakeupDelay) int DHT2pin::_readSensor(uint8_t wakeupDelay)
{ {
// INIT BUFFERVAR TO RECEIVE DATA // INIT BUFFERVAR TO RECEIVE DATA
uint8_t mask = 128; uint8_t mask = 128;
uint8_t idx = 0; uint8_t idx = 0;
// EMPTY BUFFER // EMPTY BUFFER
for (uint8_t i = 0; i < 5; i++) bits[i] = 0; for (uint8_t i = 0; i < 5; i++) _bits[i] = 0;
// REQUEST SAMPLE // REQUEST SAMPLE
digitalWrite(_wpin, LOW); digitalWrite(_wpin, LOW);
delay(wakeupDelay); delay(wakeupDelay);
digitalWrite(_wpin, HIGH); digitalWrite(_wpin, HIGH);
delayMicroseconds(40); delayMicroseconds(40);
// GET ACKNOWLEDGE or TIMEOUT // GET ACKNOWLEDGE or TIMEOUT
uint16_t loopCnt = DHTLIB_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;
while(digitalRead(_rpin) == LOW) while(digitalRead(_rpin) == LOW)
{ {
if (--loopCnt == 0) return DHTLIB_ERROR_TIMEOUT; if (--loopCnt == 0) return DHTLIB_ERROR_TIMEOUT;
} }
uint32_t t = micros();
loopCnt = DHTLIB_TIMEOUT; loopCnt = DHTLIB_TIMEOUT;
while(digitalRead(_rpin) == HIGH) while(digitalRead(_rpin) == HIGH)
{ {
if (--loopCnt == 0) return DHTLIB_ERROR_TIMEOUT; if (--loopCnt == 0) return DHTLIB_ERROR_TIMEOUT;
} }
// READ THE OUTPUT - 40 BITS => 5 BYTES if ((micros() - t) > 40)
for (uint8_t i = 40; i != 0; i--)
{ {
loopCnt = DHTLIB_TIMEOUT; _bits[idx] |= mask;
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); mask >>= 1;
if (mask == 0) // next byte?
{
mask = 128;
idx++;
}
}
digitalWrite(_wpin, HIGH);
return DHTLIB_OK; return DHTLIB_OK;
} }

View File

@ -2,7 +2,7 @@
// //
// FILE: dht2pin.h // FILE: dht2pin.h
// AUTHOR: Rob Tillaart // AUTHOR: Rob Tillaart
// VERSION: 0.1.3 // VERSION: 0.2.0
// PURPOSE: DHT Temperature & Humidity Sensor library for Arduino // PURPOSE: DHT Temperature & Humidity Sensor library for Arduino
// URL: https://github.com/RobTillaart/DHT2pin // URL: https://github.com/RobTillaart/DHT2pin
// http://arduino.cc/playground/Main/DHTLib // http://arduino.cc/playground/Main/DHTLib
@ -11,7 +11,8 @@
#include <Arduino.h> #include <Arduino.h>
#define DHT2PIN_LIB_VERSION (F("0.1.3")) #define DHT2PIN_LIB_VERSION (F("0.2.0"))
#define DHTLIB_OK 0 #define DHTLIB_OK 0
#define DHTLIB_ERROR_CHECKSUM -1 #define DHTLIB_ERROR_CHECKSUM -1
@ -25,8 +26,8 @@
#define DHTLIB_DHT_WAKEUP 1 #define DHTLIB_DHT_WAKEUP 1
// max timeout is 100usec. // max timeout is 100usec.
// For a 16Mhz proc that is max 1600 clock cycles // For a 16Mhz processor that is max 1600 clock cycles
// loops using TIMEOUT use at least 4 clock cycli // loops using TIMEOUT use at least 4 clock cycles
// so 100 us takes max 400 loops // so 100 us takes max 400 loops
// so by dividing F_CPU by 40000 we "fail" as fast as possible // so by dividing F_CPU by 40000 we "fail" as fast as possible
#ifdef F_CPU #ifdef F_CPU
@ -35,43 +36,31 @@
#define DHTLIB_TIMEOUT (75000000/40000) #define DHTLIB_TIMEOUT (75000000/40000)
#endif #endif
class DHT2pin class DHT2pin
{ {
public: public:
// return values: DHT2pin(uint8_t rpin, uint8_t wpin);
// DHTLIB_OK
// DHTLIB_ERROR_CHECKSUM
// DHTLIB_ERROR_TIMEOUT
DHT2pin(uint8_t rpin, uint8_t wpin)
{
_rpin = rpin;
_wpin = wpin;
temperature = 0;
humidity = 0;
};
void begin() void begin();
{
pinMode(_rpin, INPUT);
pinMode(_wpin, OUTPUT);
}
int read11(); // return values:
int read(); // DHTLIB_OK
// DHTLIB_ERROR_CHECKSUM
// DHTLIB_ERROR_TIMEOUT
int read11();
int read();
inline int read21() { return read(); }; float humidity();
inline int read22() { return read(); }; float temperature();
inline int read33() { return read(); };
inline int read44() { return read(); };
float humidity;
float temperature;
private: private:
uint8_t _rpin; uint8_t _rpin;
uint8_t _wpin; uint8_t _wpin;
uint8_t bits[5]; // buffer to receive data uint8_t _bits[5]; // buffer to receive data
int _readSensor(uint8_t wakeupDelay); float _humidity;
float _temperature;
int _readSensor(uint8_t wakeupDelay);
}; };

View File

@ -1,19 +1,12 @@
// //
// FILE: DHT2pin.ino // FILE: DHT2pin.ino
// AUTHOR: Rob Tillaart // AUTHOR: Rob Tillaart
// VERSION: 0.1.2 // VERSION: 0.2.0
// PURPOSE: demo reading an DHT with 2 pins iso 1 // PURPOSE: demo reading an DHT with 2 pins instead of 1
// DATE: 2016 sep 5 // DATE: 2016 sep 5
// URL: https://github.com/RobTillaart/DHT2pin // URL: https://github.com/RobTillaart/DHT2pin
// http://arduino.cc/playground/Main/DHTLib // http://arduino.cc/playground/Main/DHTLib
//
// HISTORY:
// 0.1.2 nade it arduino-CI compatible
// 0.1.01 changed name to DHT2pin
// 0.1.00 initial version
//
// Released to the public domain
//
#include <DHT2pin.h> #include <DHT2pin.h>
@ -53,7 +46,7 @@ void loop()
Serial.print("DHT22, \t"); Serial.print("DHT22, \t");
uint32_t start = micros(); uint32_t start = micros();
int chk = DHT.read22(); int chk = DHT.read();
uint32_t stop = micros(); uint32_t stop = micros();
counter.total++; counter.total++;
@ -89,9 +82,9 @@ void loop()
break; break;
} }
// DISPLAY DATA // DISPLAY DATA
Serial.print(DHT.humidity, 1); Serial.print(DHT.humidity(), 1);
Serial.print(",\t"); Serial.print(",\t");
Serial.print(DHT.temperature, 1); Serial.print(DHT.temperature(), 1);
Serial.print(",\t"); Serial.print(",\t");
Serial.print(stop - start); Serial.print(stop - start);
Serial.println(); Serial.println();
@ -118,6 +111,7 @@ void loop()
} }
delay(2000); delay(2000);
} }
//
// END OF FILE
// // -- END OF FILE --

View File

@ -1,13 +1,16 @@
{ {
"name": "DHT2pin", "name": "DHT2pin",
"keywords": "DHT11,DHT22,DHT33,DHT44,AM2301,AM2302,AM2303", "keywords": "DHT11,DHT22,DHT33,DHT44,AM2301,AM2302,AM2303",
"description": "Experimental library of the DHT library that uses 2 pins instead of 3.", "description": "Experimental version of the DHT library, using 2 data pins instead of 1.",
"authors": "authors":
[ [
{ {
"name": "Rob Tillaart", "name": "Rob Tillaart",
"email": "Rob.Tillaart@gmail.com", "email": "Rob.Tillaart@gmail.com",
"maintainer": true "maintainer": true
},
{
"name": "Maria Emanuella Moura Silva"
} }
], ],
"repository": "repository":
@ -15,7 +18,7 @@
"type": "git", "type": "git",
"url": "https://github.com/RobTillaart/DHT2pin" "url": "https://github.com/RobTillaart/DHT2pin"
}, },
"version": "0.1.3", "version": "0.2.0",
"license": "MIT", "license": "MIT",
"frameworks": "arduino", "frameworks": "arduino",
"platforms": "*", "platforms": "*",

View File

@ -1,8 +1,8 @@
name=DHT2pin name=DHT2pin
version=0.1.3 version=0.2.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=Experimental library of the DHT library that uses 2 pins. sentence=Experimental version of the DHT library, using 2 data pins instead of 1.
paragraph= paragraph=
category=Sensors category=Sensors
url=https://github.com/RobTillaart/DHT2pin url=https://github.com/RobTillaart/DHT2pin

View File

@ -11,42 +11,54 @@
Arduino library for experimental 2 pin DHT library. Arduino library for experimental 2 pin DHT library.
## Description
**NOTE: THIS LIB IS NOT TESTED EXTENSIVELY YET SO USE WITH CARE**
This library is an experimental version of the DHT library that uses 2 pins.
One pin for all read actions and one pin for write actions. This way one does
not need to switch a pin between **INPUT** and **OUTPUT**.
Note: It needs a diode in the hardware between the input pin of the MCU
and the data pin of the DHT sensor.
The output pin of the MCU is directly connected to the data pin of the DHT sensor.
It was made after a request which also referred to the links below.
https://communities.intel.com/thread/53869
(this link looks dead)
http://bigdinotech.com/tutorials/galileo-tutorials/using-1-wire-device-with-intel-galileo/
**NOTE: THIS LIB IS NOT TESTED EXTENSIVELY YET SO USE WITH CARE**
## Credits & testing ## Credits & testing
Maria Emanuella Moura Silva for testing and verifying this experimental Maria Emanuella Moura Silva for testing and verifying this experimental
code on a Galileo. code on a Galileo.
## Description
**NOTE: THIS LIB IS NOT TESTED EXTENSIVELY YET SO USE WITH CARE**
This library is an **experimental** version of the DHT library that uses **two** data pins.
One pin for all read actions and one pin for all write actions.
This way one does not need to switch a pin between **INPUT** and **OUTPUT**.
Note:
It needs a diode between the input pin of the MCU and the data pin of the DHT sensor.
The output pin of the MCU is directly connected to the data pin of the DHT sensor.
This library was made after a request which also referred to the links below.
https://web.archive.org/web/20150912065850/https://communities.intel.com/thread/53869
https://web.archive.org/web/20180510215445/http://bigdinotech.com/tutorials/galileo-tutorials/using-1-wire-device-with-intel-galileo
(there might be later versions)
**NOTE: THIS LIB IS NOT TESTED EXTENSIVELY YET SO USE WITH CARE**
## interface
```cpp
#include "DHT2pin.h"
```
- **DHT2pin(uint8_t rpin, uint8_t wpin)** constructor.
- **void begin()** set pinModes correctly, must be called before read().
- **int read11()** read a DHT11.
- **int read()** read a DHT22 and equivalents.
- **float humidity()** get last humidity read.
- **float temperature()** get last temperature read.
## Operation ## Operation
See examples See example
## Future ## Future
- no active development planned - no active development planned
- improve unit tests (constants)
-

View File

@ -3,7 +3,7 @@
// AUTHOR: Rob Tillaart // AUTHOR: Rob Tillaart
// DATE: 2020-12-03 // DATE: 2020-12-03
// PURPOSE: unit tests for the DHT2pin library // PURPOSE: unit tests for the DHT2pin library
// https://github.com/RobTillaart/ // https://github.com/RobTillaart/dht2pin
// https://github.com/Arduino-CI/arduino_ci/blob/master/REFERENCE.md // https://github.com/Arduino-CI/arduino_ci/blob/master/REFERENCE.md
// //
@ -28,34 +28,54 @@
#include "DHT2pin.h" #include "DHT2pin.h"
unittest_setup() unittest_setup()
{ {
fprintf(stderr, "DHT2PIN_LIB_VERSION: %s\n", (char *) DHT2PIN_LIB_VERSION); fprintf(stderr, "DHT2PIN_LIB_VERSION: %s\n", (char *) DHT2PIN_LIB_VERSION);
} }
unittest_teardown() unittest_teardown()
{ {
} }
unittest(test_constants)
{
assertEqual(00, DHTLIB_OK);
assertEqual(-1, DHTLIB_ERROR_CHECKSUM);
assertEqual(-2, DHTLIB_ERROR_TIMEOUT);
assertEqual(-3, DHTLIB_ERROR_CONNECT);
assertEqual(-4, DHTLIB_ERROR_ACK_L);
assertEqual(-5, DHTLIB_ERROR_ACK_H);
assertEqual(-999, DHTLIB_INVALID_VALUE);
assertEqual(18, DHTLIB_DHT11_WAKEUP);
assertEqual(01, DHTLIB_DHT_WAKEUP);
// just printing.
fprintf(stderr, "DHTLIB_TIMEOUT: %d\n", DHTLIB_TIMEOUT);
}
unittest(test_constructor) unittest(test_constructor)
{ {
DHT2pin DHT(2, 3); DHT2pin DHT(2, 3);
assertEqualFloat(0, DHT.temperature, 0.001); assertEqualFloat(0, DHT.temperature(), 0.001);
assertEqualFloat(0, DHT.humidity, 0.001); assertEqualFloat(0, DHT.humidity(), 0.001);
int chk = DHT.read22(); int chk = DHT.read();
// Nothing connected so timeout expected // Nothing connected so timeout expected
assertEqual(DHTLIB_ERROR_TIMEOUT, chk); assertEqual(DHTLIB_ERROR_TIMEOUT, chk);
// This will set the temperature and humidity to -999 // This will set the temperature and humidity to -999
assertEqualFloat(-999, DHT.temperature, 0.001); assertEqualFloat(-999, DHT.temperature(), 0.001);
assertEqualFloat(-999, DHT.humidity, 0.001); assertEqualFloat(-999, DHT.humidity(), 0.001);
} }
unittest_main() unittest_main()
// --------
// -- END OF FILE --