fixed DHT2pin lib

This commit is contained in:
RobTillaart 2016-12-17 21:03:32 +01:00
parent c97f801ba4
commit 619b84df62
5 changed files with 383 additions and 0 deletions

View File

@ -0,0 +1,155 @@
//
// FILE: DHT2pin.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.0.2
// PURPOSE: DHT Temperature & Humidity Sensor library for Arduino
// URL: http://arduino.cc/playground/Main/DHTLib
// https://github.com/RobTillaart/Arduino/tree/master/libraries
//
// HISTORY:
// 0.0.2 support for non "F_CPU" boards especially Galileo
// Tested and verified by Maria Emanuella Moura Silva (thanks)
// changed name to DHT2pin
// 0.0.1 initial version - 2016 SEP 05 (based upon 0.1.13 DHT)
//
// Released to the public domain
//
#include "DHT2pin.h"
/////////////////////////////////////////////////////
//
// PUBLIC
//
// return values:
// DHTLIB_OK
// DHTLIB_ERROR_CHECKSUM
// 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;
}
// 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;
return DHTLIB_OK;
}
// return values:
// DHTLIB_OK
// DHTLIB_ERROR_CHECKSUM
// 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
}
// 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;
}
/////////////////////////////////////////////////////
//
// PRIVATE
//
// return values:
// DHTLIB_OK
// DHTLIB_ERROR_TIMEOUT
int DHT2pin::_readSensor(uint8_t wakeupDelay)
{
// 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;
// REQUEST SAMPLE
digitalWrite(_wpin, LOW);
delay(wakeupDelay);
digitalWrite(_wpin, HIGH);
delayMicroseconds(40);
// 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;
}
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);
return DHTLIB_OK;
}
//
// END OF FILE
//

View File

@ -0,0 +1,80 @@
//
// FILE: dht2pin.h
// AUTHOR: Rob Tillaart
// VERSION: 0.0.2
// PURPOSE: DHT Temperature & Humidity Sensor library for Arduino
// URL: http://arduino.cc/playground/Main/DHTLib
//
// HISTORY:
// see dht.cpp file
//
#ifndef dht_h
#define dht_h
#include <Arduino.h>
#define DHT2PIN_LIB_VERSION "0.0.1"
#define DHTLIB_OK 0
#define DHTLIB_ERROR_CHECKSUM -1
#define DHTLIB_ERROR_TIMEOUT -2
#define DHTLIB_ERROR_CONNECT -3
#define DHTLIB_ERROR_ACK_L -4
#define DHTLIB_ERROR_ACK_H -5
#define DHTLIB_INVALID_VALUE -999
#define DHTLIB_DHT11_WAKEUP 18
#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
// so 100 us takes max 400 loops
// so by dividing F_CPU by 40000 we "fail" as fast as possible
#ifdef F_CPU
#define DHTLIB_TIMEOUT (F_CPU/40000)
#else
#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;
};
void begin()
{
pinMode(_rpin, INPUT);
pinMode(_wpin, OUTPUT);
}
int read11();
int read();
inline int read21() { return read(); };
inline int read22() { return read(); };
inline int read33() { return read(); };
inline int read44() { return read(); };
double humidity;
double temperature;
private:
uint8_t _rpin;
uint8_t _wpin;
uint8_t bits[5]; // buffer to receive data
int _readSensor(uint8_t wakeupDelay);
};
#endif
//
// END OF FILE
//

View File

@ -0,0 +1,120 @@
//
// FILE: DHT2pin.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.01
// PURPOSE: demo reading an DHT with 2 pins iso 1
// DATE: 2016 sep 5
// URL: -
// HISTORY:
// 0.1.01 changed name to DHT2pin
// 0.1.00 initial version
//
// Released to the public domain
//
#include <DHT2pin.h>
DHT2pin DHT(2, 3);
struct
{
uint32_t total;
uint32_t ok;
uint32_t crc_error;
uint32_t time_out;
uint32_t connect;
uint32_t ack_l;
uint32_t ack_h;
uint32_t unknown;
} stat = { 0, 0, 0, 0, 0, 0, 0, 0};
uint32_t start;
uint32_t stop;
void setup()
{
DHT.begin();
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("LIBRARY VERSION: ");
Serial.println(DHT2PIN_LIB_VERSION);
Serial.println();
Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)\tTime (us)");
}
void loop()
{
// READ DATA
Serial.print("DHT22, \t");
uint32_t start = micros();
int chk = DHT.read22();
uint32_t stop = micros();
stat.total++;
switch (chk)
{
case DHTLIB_OK:
stat.ok++;
Serial.print("OK,\t");
break;
case DHTLIB_ERROR_CHECKSUM:
stat.crc_error++;
Serial.print("Checksum error,\t");
break;
case DHTLIB_ERROR_TIMEOUT:
stat.time_out++;
Serial.print("Time out error,\t");
break;
case DHTLIB_ERROR_CONNECT:
stat.connect++;
Serial.print("Connect error,\t");
break;
case DHTLIB_ERROR_ACK_L:
stat.ack_l++;
Serial.print("Ack Low error,\t");
break;
case DHTLIB_ERROR_ACK_H:
stat.ack_h++;
Serial.print("Ack High error,\t");
break;
default:
stat.unknown++;
Serial.print("Unknown error,\t");
break;
}
// DISPLAY DATA
Serial.print(DHT.humidity, 1);
Serial.print(",\t");
Serial.print(DHT.temperature, 1);
Serial.print(",\t");
Serial.print(stop - start);
Serial.println();
if (stat.total % 20 == 0)
{
Serial.println("\nTOT\tOK\tCRC\tTO\tUNK");
Serial.print(stat.total);
Serial.print("\t");
Serial.print(stat.ok);
Serial.print("\t");
Serial.print(stat.crc_error);
Serial.print("\t");
Serial.print(stat.time_out);
Serial.print("\t");
Serial.print(stat.connect);
Serial.print("\t");
Serial.print(stat.ack_l);
Serial.print("\t");
Serial.print(stat.ack_h);
Serial.print("\t");
Serial.print(stat.unknown);
Serial.println("\n");
}
delay(2000);
}
//
// END OF FILE
//

View File

@ -0,0 +1,15 @@
{
"name": "DHT2pin",
"keywords": "DHT11 DHT22 DHT33 DHT44 AM2301 AM2302 AM2303",
"description": "Experimental library of the DHT library that uses 2 pins instead of 3.",
"repository":
{
"type": "git",
"url": "https://github.com/RobTillaart/Arduino.git"
},
"frameworks": "arduino",
"platforms": "*",
"export": {
"include": "libraries/DHT2pin"
}
}

View File

@ -0,0 +1,13 @@
NOTE: THIS LIB IS NOT TESTED EXTENSIVELY YET SO ALL DISCLAIMERS APPLY
This library is an experimental version of the DHT library that uses 2 pins instead of 3.
One pin for all read actions and one pin for write actions.
It was made after a request which also refered to the links below.
https://communities.intel.com/thread/53869
http://bigdinotech.com/tutorials/galileo-tutorials/using-1-wire-device-with-intel-galileo/
NOTE: THIS LIB IS NOT TESTED EXTENSIVELY YET SO ALL DISCLAIMERS APPLY