diff --git a/SimpleDHT.cpp b/SimpleDHT.cpp index 6fa990a..6078fac 100644 --- a/SimpleDHT.cpp +++ b/SimpleDHT.cpp @@ -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) { @@ -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); } @@ -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; @@ -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) { @@ -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); @@ -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: @@ -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: @@ -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; } @@ -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; @@ -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: @@ -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: diff --git a/SimpleDHT.h b/SimpleDHT.h index f931d87..bbafdf6 100644 --- a/SimpleDHT.h +++ b/SimpleDHT.h @@ -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 @@ -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); }; /* @@ -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]); }; /* @@ -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 diff --git a/examples/DHT11WithRawBits/DHT11WithRawBits.ino b/examples/DHT11WithRawBits/DHT11WithRawBits.ino index a2408d1..e1563eb 100644 --- a/examples/DHT11WithRawBits/DHT11WithRawBits.ino +++ b/examples/DHT11WithRawBits/DHT11WithRawBits.ino @@ -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)); @@ -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(' '); } diff --git a/examples/DHT22WithRawBits/DHT22WithRawBits.ino b/examples/DHT22WithRawBits/DHT22WithRawBits.ino index d1d5b24..6a1016a 100644 --- a/examples/DHT22WithRawBits/DHT22WithRawBits.ino +++ b/examples/DHT22WithRawBits/DHT22WithRawBits.ino @@ -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)); @@ -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(' '); }