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:
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,6 +6,18 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
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
- add changelog.md
- add rp2040 to build-CI
@ -20,6 +32,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- own repository
- #pragma once
----
## [0.0.3]
- Fix issue #33

View File

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

View File

@ -1,12 +1,10 @@
//
// FILE: DHT2pin.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.3
// PURPOSE: Experimental DHT Temperature & Humidity Sensor library for Arduino
// VERSION: 0.2.0
// PURPOSE: Experimental DHT _temperature & _humidiy Sensor library for Arduino
// URL: https://github.com/RobTillaart/DHT2pin
// http://arduino.cc/playground/Main/DHTLib
//
// HISTORY: see changelog.md
#include "DHT2pin.h"
@ -16,6 +14,21 @@
//
// 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:
// DHTLIB_OK
@ -23,25 +36,25 @@
// DHTLIB_ERROR_TIMEOUT
int DHT2pin::read11()
{
// READ VALUES
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;
}
// READ VALUES
int rv = _readSensor(DHTLIB_DHT11_WAKEUP);
if (rv != DHTLIB_OK)
{
_humidity = DHTLIB_INVALID_VALUE;
_temperature = DHTLIB_INVALID_VALUE;
return rv;
}
// CONVERT AND STORE
humidity = bits[0]; // bits[1] == 0;
temperature = bits[2]; // bits[3] == 0;
// CONVERT AND STORE
_humidity = _bits[0]; // _bits[1] == 0;
_temperature = _bits[2]; // _bits[3] == 0;
// TEST CHECKSUM
// bits[1] && bits[3] both 0
uint8_t sum = bits[0] + bits[2];
if (bits[4] != sum) return DHTLIB_ERROR_CHECKSUM;
// TEST CHECKSUM
// _bits[1] && _bits[3] both 0
uint8_t sum = _bits[0] + _bits[2];
if (_bits[4] != sum) return DHTLIB_ERROR_CHECKSUM;
return DHTLIB_OK;
return DHTLIB_OK;
}
@ -51,32 +64,45 @@ int DHT2pin::read11()
// DHTLIB_ERROR_TIMEOUT
int DHT2pin::read()
{
// READ VALUES
int rv = _readSensor(DHTLIB_DHT_WAKEUP);
if (rv != DHTLIB_OK)
{
humidity = DHTLIB_INVALID_VALUE; // invalid value, or is NaN prefered?
temperature = DHTLIB_INVALID_VALUE; // invalid value
return rv; // propagate error value
}
// READ VALUES
int rv = _readSensor(DHTLIB_DHT_WAKEUP);
if (rv != DHTLIB_OK)
{
_humidity = DHTLIB_INVALID_VALUE;
_temperature = DHTLIB_INVALID_VALUE;
return rv; // propagate error value
}
// CONVERT AND STORE
humidity = word(bits[0], bits[1]) * 0.1;
temperature = word(bits[2] & 0x7F, bits[3]) * 0.1;
if (bits[2] & 0x80) // negative temperature
{
temperature = -temperature;
}
// CONVERT AND STORE
_humidity = word(_bits[0], _bits[1]) * 0.1;
_temperature = word(_bits[2] & 0x7F, _bits[3]) * 0.1;
if (_bits[2] & 0x80) // negative _temperature
{
_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;
// TEST CHECKSUM
uint8_t sum = _bits[0] + _bits[1] + _bits[2] + _bits[3];
if (_bits[4] != sum)
{
return DHTLIB_ERROR_CHECKSUM;
}
return DHTLIB_OK;
}
float DHT2pin::temperature()
{
return _temperature;
}
float DHT2pin::humidity()
{
return _humidity;
}
/////////////////////////////////////////////////////
//
// PRIVATE
@ -87,63 +113,63 @@ int DHT2pin::read()
// DHTLIB_ERROR_TIMEOUT
int DHT2pin::_readSensor(uint8_t wakeupDelay)
{
// INIT BUFFERVAR TO RECEIVE DATA
uint8_t mask = 128;
uint8_t idx = 0;
// INIT BUFFERVAR TO RECEIVE DATA
uint8_t mask = 128;
uint8_t idx = 0;
// EMPTY BUFFER
for (uint8_t i = 0; i < 5; i++) bits[i] = 0;
// EMPTY BUFFER
for (uint8_t i = 0; i < 5; i++) _bits[i] = 0;
// REQUEST SAMPLE
digitalWrite(_wpin, LOW);
delay(wakeupDelay);
digitalWrite(_wpin, HIGH);
delayMicroseconds(40);
// REQUEST SAMPLE
digitalWrite(_wpin, LOW);
delay(wakeupDelay);
digitalWrite(_wpin, HIGH);
delayMicroseconds(40);
// GET ACKNOWLEDGE or TIMEOUT
uint16_t loopCnt = DHTLIB_TIMEOUT;
// GET ACKNOWLEDGE or 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)
{
if (--loopCnt == 0) return DHTLIB_ERROR_TIMEOUT;
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 (--loopCnt == 0) return DHTLIB_ERROR_TIMEOUT;
}
// READ THE OUTPUT - 40 BITS => 5 BYTES
for (uint8_t i = 40; i != 0; i--)
if ((micros() - t) > 40)
{
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++;
}
_bits[idx] |= mask;
}
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
// AUTHOR: Rob Tillaart
// VERSION: 0.1.3
// VERSION: 0.2.0
// PURPOSE: DHT Temperature & Humidity Sensor library for Arduino
// URL: https://github.com/RobTillaart/DHT2pin
// http://arduino.cc/playground/Main/DHTLib
@ -11,7 +11,8 @@
#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_ERROR_CHECKSUM -1
@ -25,8 +26,8 @@
#define DHTLIB_DHT_WAKEUP 1
// max timeout is 100usec.
// For a 16Mhz proc that is max 1600 clock cycles
// loops using TIMEOUT use at least 4 clock cycli
// For a 16Mhz processor that is max 1600 clock cycles
// loops using TIMEOUT use at least 4 clock cycles
// so 100 us takes max 400 loops
// so by dividing F_CPU by 40000 we "fail" as fast as possible
#ifdef F_CPU
@ -35,43 +36,31 @@
#define DHTLIB_TIMEOUT (75000000/40000)
#endif
class DHT2pin
{
public:
// return values:
// DHTLIB_OK
// DHTLIB_ERROR_CHECKSUM
// DHTLIB_ERROR_TIMEOUT
DHT2pin(uint8_t rpin, uint8_t wpin)
{
_rpin = rpin;
_wpin = wpin;
temperature = 0;
humidity = 0;
};
DHT2pin(uint8_t rpin, uint8_t wpin);
void begin()
{
pinMode(_rpin, INPUT);
pinMode(_wpin, OUTPUT);
}
void begin();
int read11();
int read();
// return values:
// DHTLIB_OK
// DHTLIB_ERROR_CHECKSUM
// DHTLIB_ERROR_TIMEOUT
int read11();
int read();
inline int read21() { return read(); };
inline int read22() { return read(); };
inline int read33() { return read(); };
inline int read44() { return read(); };
float humidity;
float temperature;
float humidity();
float temperature();
private:
uint8_t _rpin;
uint8_t _wpin;
uint8_t bits[5]; // buffer to receive data
int _readSensor(uint8_t wakeupDelay);
uint8_t _rpin;
uint8_t _wpin;
uint8_t _bits[5]; // buffer to receive data
float _humidity;
float _temperature;
int _readSensor(uint8_t wakeupDelay);
};

View File

@ -1,19 +1,12 @@
//
// FILE: DHT2pin.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.2
// PURPOSE: demo reading an DHT with 2 pins iso 1
// VERSION: 0.2.0
// PURPOSE: demo reading an DHT with 2 pins instead of 1
// DATE: 2016 sep 5
// URL: https://github.com/RobTillaart/DHT2pin
// 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>
@ -53,7 +46,7 @@ void loop()
Serial.print("DHT22, \t");
uint32_t start = micros();
int chk = DHT.read22();
int chk = DHT.read();
uint32_t stop = micros();
counter.total++;
@ -89,9 +82,9 @@ void loop()
break;
}
// DISPLAY DATA
Serial.print(DHT.humidity, 1);
Serial.print(DHT.humidity(), 1);
Serial.print(",\t");
Serial.print(DHT.temperature, 1);
Serial.print(DHT.temperature(), 1);
Serial.print(",\t");
Serial.print(stop - start);
Serial.println();
@ -118,6 +111,7 @@ void loop()
}
delay(2000);
}
//
// END OF FILE
//
// -- END OF FILE --

View File

@ -1,13 +1,16 @@
{
"name": "DHT2pin",
"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":
[
{
"name": "Rob Tillaart",
"email": "Rob.Tillaart@gmail.com",
"maintainer": true
},
{
"name": "Maria Emanuella Moura Silva"
}
],
"repository":
@ -15,7 +18,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/DHT2pin"
},
"version": "0.1.3",
"version": "0.2.0",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*",

View File

@ -1,8 +1,8 @@
name=DHT2pin
version=0.1.3
version=0.2.0
author=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=
category=Sensors
url=https://github.com/RobTillaart/DHT2pin

View File

@ -11,42 +11,54 @@
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
Maria Emanuella Moura Silva for testing and verifying this experimental
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
See examples
See example
## Future
- no active development planned
- improve unit tests (constants)
-

View File

@ -3,7 +3,7 @@
// AUTHOR: Rob Tillaart
// DATE: 2020-12-03
// 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
//
@ -28,34 +28,54 @@
#include "DHT2pin.h"
unittest_setup()
{
fprintf(stderr, "DHT2PIN_LIB_VERSION: %s\n", (char *) DHT2PIN_LIB_VERSION);
}
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)
{
DHT2pin DHT(2, 3);
assertEqualFloat(0, DHT.temperature, 0.001);
assertEqualFloat(0, DHT.humidity, 0.001);
assertEqualFloat(0, DHT.temperature(), 0.001);
assertEqualFloat(0, DHT.humidity(), 0.001);
int chk = DHT.read22();
// Nothing connected so timeout expected
int chk = DHT.read();
// Nothing connected so timeout expected
assertEqual(DHTLIB_ERROR_TIMEOUT, chk);
// This will set the temperature and humidity to -999
assertEqualFloat(-999, DHT.temperature, 0.001);
assertEqualFloat(-999, DHT.humidity, 0.001);
// This will set the temperature and humidity to -999
assertEqualFloat(-999, DHT.temperature(), 0.001);
assertEqualFloat(-999, DHT.humidity(), 0.001);
}
unittest_main()
// --------
// -- END OF FILE --