Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stored as byte, bits conversion is no longer required #55

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 21 additions & 29 deletions SimpleDHT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ int SimpleDHT::setPinInputMode(uint8_t mode) {
return SimpleDHTErrSuccess;
}

int SimpleDHT::read(byte* ptemperature, byte* phumidity, byte pdata[40]) {
int SimpleDHT::read(byte* ptemperature, byte* phumidity, byte pdata[5]) {
int ret = SimpleDHTErrSuccess;

if (pin == -1) {
Expand All @@ -72,7 +72,7 @@ int SimpleDHT::read(byte* ptemperature, byte* phumidity, byte pdata[40]) {
return ret;
}

int SimpleDHT::read(int pin, byte* ptemperature, byte* phumidity, byte pdata[40]) {
int SimpleDHT::read(int pin, byte* ptemperature, byte* phumidity, byte pdata[5]) {
setPin(pin);
return read(ptemperature, phumidity, pdata);
}
Expand Down Expand Up @@ -124,20 +124,12 @@ long SimpleDHT::levelTime(byte level, int firstWait, int interval) {
return time;
}

byte SimpleDHT::bits2byte(byte data[8]) {
byte v = 0;
for (int i = 0; i < 8; i++) {
v += data[i] << (7 - i);
}
return v;
}

int SimpleDHT::parse(byte data[40], short* ptemperature, short* phumidity) {
short humidity = bits2byte(data);
short humidity2 = bits2byte(data + 8);
short temperature = bits2byte(data + 16);
short temperature2 = bits2byte(data + 24);
byte check = bits2byte(data + 32);
int SimpleDHT::parse(byte data[5], short* ptemperature, short* phumidity) {
short humidity = data[0];
short humidity2 = data[1];
short temperature = data[2];
short temperature2 = data[3];
byte check = data[4];
byte expect = (byte)humidity + (byte)humidity2 + (byte)temperature + (byte)temperature2;
if (check != expect) {
return SimpleDHTErrDataChecksum;
Expand All @@ -155,7 +147,7 @@ SimpleDHT11::SimpleDHT11() {
SimpleDHT11::SimpleDHT11(int pin) : SimpleDHT (pin) {
}

int SimpleDHT11::read2(float* ptemperature, float* phumidity, byte pdata[40]) {
int SimpleDHT11::read2(float* ptemperature, float* phumidity, byte pdata[5]) {
int ret = SimpleDHTErrSuccess;

if (pin == -1) {
Expand All @@ -174,7 +166,7 @@ int SimpleDHT11::read2(float* ptemperature, float* phumidity, byte pdata[40]) {
}

if (pdata) {
memcpy(pdata, data, 40);
memcpy(pdata, data, 5);
}
if (ptemperature) {
*ptemperature = (int)(temperature>>8);
Expand All @@ -191,14 +183,14 @@ int SimpleDHT11::read2(float* ptemperature, float* phumidity, byte pdata[40]) {
return ret;
}

int SimpleDHT11::read2(int pin, float* ptemperature, float* phumidity, byte pdata[40]) {
int SimpleDHT11::read2(int pin, float* ptemperature, float* phumidity, byte pdata[5]) {
setPin(pin);
return read2(ptemperature, phumidity, pdata);
}

int SimpleDHT11::sample(byte data[40]) {
int SimpleDHT11::sample(byte data[5]) {
// empty output data.
memset(data, 0, 40);
memset(data, 0, 5);

// According to protocol: [1] https://akizukidenshi.com/download/ds/aosong/DHT11.pdf
// notify DHT11 to start:
Expand Down Expand Up @@ -250,7 +242,7 @@ int SimpleDHT11::sample(byte data[40]) {
if (t < 11) { // specs say: 20us
return simpleDHTCombileError(t, SimpleDHTErrDataRead);
}
data[ j ] = (t > 40 ? 1 : 0); // specs: 26-28us -> 0, 70us -> 1
data[j / 8] =(data[j / 8] << 1) | (t > 40 ? 1 : 0); // specs: 26-28us -> 0, 70us -> 1
}

// DHT11 EOF:
Expand All @@ -269,14 +261,14 @@ SimpleDHT22::SimpleDHT22() {
SimpleDHT22::SimpleDHT22(int pin) : SimpleDHT (pin) {
}

int SimpleDHT22::read2(float* ptemperature, float* phumidity, byte pdata[40]) {
int SimpleDHT22::read2(float* ptemperature, float* phumidity, byte pdata[5]) {
int ret = SimpleDHTErrSuccess;

if (pin == -1) {
return SimpleDHTErrNoPin;
}

byte data[40] = {0};
byte data[5] = {0};
if ((ret = sample(data)) != SimpleDHTErrSuccess) {
return ret;
}
Expand All @@ -288,7 +280,7 @@ int SimpleDHT22::read2(float* ptemperature, float* phumidity, byte pdata[40]) {
}

if (pdata) {
memcpy(pdata, data, 40);
memcpy(pdata, data, 5);
}
if (ptemperature) {
*ptemperature = (float)((temperature & 0x8000 ? -1 : 1) * (temperature & 0x7FFF)) / 10.0;
Expand All @@ -300,14 +292,14 @@ int SimpleDHT22::read2(float* ptemperature, float* phumidity, byte pdata[40]) {
return ret;
}

int SimpleDHT22::read2(int pin, float* ptemperature, float* phumidity, byte pdata[40]) {
int SimpleDHT22::read2(int pin, float* ptemperature, float* phumidity, byte pdata[5]) {
setPin(pin);
return read2(ptemperature, phumidity, pdata);
}

int SimpleDHT22::sample(byte data[40]) {
int SimpleDHT22::sample(byte data[5]) {
// empty output data.
memset(data, 0, 40);
memset(data, 0, 5);

// According to protocol: http://akizukidenshi.com/download/ds/aosong/AM2302.pdf
// notify DHT22 to start:
Expand Down Expand Up @@ -350,7 +342,7 @@ int SimpleDHT22::sample(byte data[40]) {
if (t < 11) { // specs say: 26us
return simpleDHTCombileError(t, SimpleDHTErrDataRead);
}
data[ j ] = (t > 40 ? 1 : 0); // specs: 22-30us -> 0, 70us -> 1
data[j / 8] = (data[j / 8] << 1) | (t > 40 ? 1 : 0); // specs: 22-30us -> 0, 70us -> 1
}

// DHT22 EOF:
Expand Down
30 changes: 13 additions & 17 deletions SimpleDHT.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ class SimpleDHT {
// @param pdata output 40bits sample, NULL to ignore.
// @remark the min delay for this method is 1s(DHT11) or 2s(DHT22).
// @return SimpleDHTErrSuccess is success; otherwise, failed.
virtual int read(byte* ptemperature, byte* phumidity, byte pdata[40]);
virtual int read(int pin, byte* ptemperature, byte* phumidity, byte pdata[40]);
virtual int read(byte* ptemperature, byte* phumidity, byte pdata[5]);
virtual int read(int pin, byte* ptemperature, byte* phumidity, byte pdata[5]);
// To get a more accurate data.
// @remark it's available for dht22. for dht11, it's the same of read().
virtual int read2(float* ptemperature, float* phumidity, byte pdata[40]) = 0;
virtual int read2(int pin, float* ptemperature, float* phumidity, byte pdata[40]) = 0;
virtual int read2(float* ptemperature, float* phumidity, byte pdata[5]) = 0;
virtual int read2(int pin, float* ptemperature, float* phumidity, byte pdata[5]) = 0;
protected:
// For only AVR - methods returning low level conf. of the pin
#ifdef __AVR
Expand All @@ -117,17 +117,13 @@ class SimpleDHT {
// @param interval time interval between consecutive state checks.
// @return measured time (microseconds). -1 if timeout.
virtual long levelTime(byte level, int firstWait = 10, int interval = 6);
// @data the bits of a byte.
// @remark please use simple_dht11_read().
virtual byte bits2byte(byte data[8]);
// read temperature and humidity from dht11.
// @param data a byte[40] to read bits to 5bytes.
// @return 0 success; otherwise, error.
// @remark please use simple_dht11_read().
virtual int sample(byte data[40]) = 0;
// parse the 40bits data to temperature and humidity.
virtual int sample(byte data[5]) = 0;
// parse data to temperature and humidity.
// @remark please use simple_dht11_read().
virtual int parse(byte data[40], short* ptemperature, short* phumidity);
virtual int parse(byte data[5], short* ptemperature, short* phumidity);
};

/*
Expand All @@ -152,10 +148,10 @@ class SimpleDHT11 : public SimpleDHT {
SimpleDHT11();
SimpleDHT11(int pin);
public:
virtual int read2(float* ptemperature, float* phumidity, byte pdata[40]);
virtual int read2(int pin, float* ptemperature, float* phumidity, byte pdata[40]);
virtual int read2(float* ptemperature, float* phumidity, byte pdata[5]);
virtual int read2(int pin, float* ptemperature, float* phumidity, byte pdata[5]);
protected:
virtual int sample(byte data[40]);
virtual int sample(byte data[5]);
};

/*
Expand All @@ -180,10 +176,10 @@ class SimpleDHT22 : public SimpleDHT {
SimpleDHT22();
SimpleDHT22(int pin);
public:
virtual int read2(float* ptemperature, float* phumidity, byte pdata[40]);
virtual int read2(int pin, float* ptemperature, float* phumidity, byte pdata[40]);
virtual int read2(float* ptemperature, float* phumidity, byte pdata[5]);
virtual int read2(int pin, float* ptemperature, float* phumidity, byte pdata[5]);
protected:
virtual int sample(byte data[40]);
virtual int sample(byte data[5]);
};

#endif
4 changes: 2 additions & 2 deletions examples/DHT11WithRawBits/DHT11WithRawBits.ino
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void loop() {
// read with raw sample data.
byte temperature = 0;
byte humidity = 0;
byte data[40] = {0};
byte data[5] = {0};
int err = SimpleDHTErrSuccess;
if ((err = dht11.read(&temperature, &humidity, data)) != SimpleDHTErrSuccess) {
Serial.print("Read DHT11 failed, err="); Serial.print(SimpleDHTErrCode(err));
Expand All @@ -29,7 +29,7 @@ void loop() {

Serial.print("Sample RAW Bits: ");
for (int i = 0; i < 40; i++) {
Serial.print((int)data[i]);
Serial.print((int)(data[i / 8] >> (i % 8)) & 1);
if (i > 0 && ((i + 1) % 4) == 0) {
Serial.print(' ');
}
Expand Down
4 changes: 2 additions & 2 deletions examples/DHT22WithRawBits/DHT22WithRawBits.ino
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void loop() {
// if user doesn't care about the accurate data, use read to get a byte data, such as 10*C.
float temperature = 0;
float humidity = 0;
byte data[40] = {0};
byte data[5] = {0};
int err = SimpleDHTErrSuccess;
if ((err = dht22.read2(&temperature, &humidity, data)) != SimpleDHTErrSuccess) {
Serial.print("Read DHT22 failed, err="); Serial.print(SimpleDHTErrCode(err));
Expand All @@ -31,7 +31,7 @@ void loop() {

Serial.print("Sample RAW Bits: ");
for (int i = 0; i < 40; i++) {
Serial.print((int)data[i]);
Serial.print((int)(data[i / 8] >> (i % 8)) & 1);
if (i > 0 && ((i + 1) % 4) == 0) {
Serial.print(' ');
}
Expand Down