diff --git a/SimpleDHT.cpp b/SimpleDHT.cpp index 3d99cf8..fd3a6e6 100644 --- a/SimpleDHT.cpp +++ b/SimpleDHT.cpp @@ -24,14 +24,6 @@ SOFTWARE. #include "SimpleDHT.h" -int SimpleDHT::ErrSuccess = 0; -int SimpleDHT::ErrStartLow = 100; -int SimpleDHT::ErrStartHigh = 101; -int SimpleDHT::ErrDataLow = 102; -int SimpleDHT::ErrDataRead = 103; -int SimpleDHT::ErrDataEOF = 104; -int SimpleDHT::ErrDataChecksum = 105; - int SimpleDHT::confirm(int pin, int us, byte level) { // wait one more count to ensure. int cnt = us / 10 + 1; @@ -48,7 +40,7 @@ int SimpleDHT::confirm(int pin, int us, byte level) { if (!ok) { return -1; } - return ErrSuccess; + return SimpleDHTErrSuccess; } byte SimpleDHT::bits2byte(byte data[8]) { @@ -67,24 +59,24 @@ int SimpleDHT::parse(byte data[40], byte* ptemperature, byte* phumidity) { byte check = bits2byte(data + 32); byte expect = humidity + humidity2 + temperature + temperature2; if (check != expect) { - return ErrDataChecksum; + return SimpleDHTErrDataChecksum; } *ptemperature = temperature; *phumidity = humidity; - return ErrSuccess; + return SimpleDHTErrSuccess; } int SimpleDHT::read(int pin, byte* ptemperature, byte* phumidity, byte pdata[40]) { - int ret = ErrSuccess; + int ret = SimpleDHTErrSuccess; byte data[40] = {0}; - if ((ret = sample(pin, data)) != ErrSuccess) { + if ((ret = sample(pin, data)) != SimpleDHTErrSuccess) { return ret; } byte temperature = 0; byte humidity = 0; - if ((ret = parse(data, &temperature, &humidity)) != ErrSuccess) { + if ((ret = parse(data, &temperature, &humidity)) != SimpleDHTErrSuccess) { return ret; } @@ -120,10 +112,10 @@ int SimpleDHT11::sample(int pin, byte data[40]) { // 1. PULL LOW 80us // 2. PULL HIGH 80us if (confirm(pin, 80, LOW)) { - return ErrStartLow; + return SimpleDHTErrStartLow; } if (confirm(pin, 80, HIGH)) { - return ErrStartHigh; + return SimpleDHTErrStartHigh; } // DHT11 data transmite: @@ -132,7 +124,7 @@ int SimpleDHT11::sample(int pin, byte data[40]) { // 3. PULL HIGH 70us, bit(1) for (int j = 0; j < 40; j++) { if (confirm(pin, 50, LOW)) { - return ErrDataLow; + return SimpleDHTErrDataLow; } // read a bit, should never call method, @@ -148,7 +140,7 @@ int SimpleDHT11::sample(int pin, byte data[40]) { delayMicroseconds(10); } if (!ok) { - return ErrDataRead; + return SimpleDHTErrDataRead; } data[j] = (tick > 3? 1:0); } @@ -156,10 +148,10 @@ int SimpleDHT11::sample(int pin, byte data[40]) { // DHT11 EOF: // 1. PULL LOW 50us. if (confirm(pin, 50, LOW)) { - return ErrDataEOF; + return SimpleDHTErrDataEOF; } - return ErrSuccess; + return SimpleDHTErrSuccess; } int SimpleDHT22::sample(int pin, byte data[40]) { @@ -181,10 +173,10 @@ int SimpleDHT22::sample(int pin, byte data[40]) { // 1. PULL LOW 80us // 2. PULL HIGH 80us if (confirm(pin, 80, LOW)) { - return ErrStartLow; + return SimpleDHTErrStartLow; } if (confirm(pin, 80, HIGH)) { - return ErrStartHigh; + return SimpleDHTErrStartHigh; } // DHT11 data transmite: @@ -193,7 +185,7 @@ int SimpleDHT22::sample(int pin, byte data[40]) { // 3. PULL HIGH 70us, bit(1) for (int j = 0; j < 40; j++) { if (confirm(pin, 50, LOW)) { - return ErrDataLow; + return SimpleDHTErrDataLow; } // read a bit, should never call method, @@ -209,7 +201,7 @@ int SimpleDHT22::sample(int pin, byte data[40]) { delayMicroseconds(10); } if (!ok) { - return ErrDataRead; + return SimpleDHTErrDataRead; } data[j] = (tick > 3? 1:0); } @@ -217,9 +209,9 @@ int SimpleDHT22::sample(int pin, byte data[40]) { // DHT11 EOF: // 1. PULL LOW 50us. if (confirm(pin, 50, LOW)) { - return ErrDataEOF; + return SimpleDHTErrDataEOF; } - return ErrSuccess; + return SimpleDHTErrSuccess; } diff --git a/SimpleDHT.h b/SimpleDHT.h index 63eec49..6b67f76 100644 --- a/SimpleDHT.h +++ b/SimpleDHT.h @@ -27,22 +27,22 @@ #include +// Success. +#define SimpleDHTErrSuccess 0 +// Error to wait for start low signal. +#define SimpleDHTErrStartLow 100 +// Error to wait for start high signal. +#define SimpleDHTErrStartHigh 101 +// Error to wait for data start low signal. +#define SimpleDHTErrDataLow 102 +// Error to wait for data read signal. +#define SimpleDHTErrDataRead 103 +// Error to wait for data EOF signal. +#define SimpleDHTErrDataEOF 104 +// Error to validate the checksum. +#define SimpleDHTErrDataChecksum 105 + class SimpleDHT { -public: - // Success. - static int ErrSuccess; - // Error to wait for start low signal. - static int ErrStartLow; - // Error to wait for start high signal. - static int ErrStartHigh; - // Error to wait for data start low signal. - static int ErrDataLow; - // Error to wait for data read signal. - static int ErrDataRead; - // Error to wait for data EOF signal. - static int ErrDataEOF; - // Error to validate the checksum. - static int ErrDataChecksum; public: // to read from dht11. // @param pin the DHT11 pin. @@ -50,7 +50,7 @@ class SimpleDHT { // @param phumidity output, NULL to ignore. // @param pdata output 40bits sample, NULL to ignore. // @remark the min delay for this method is 1s. - // @return SimpleDHT11::ErrSuccess is success; otherwise, failed. + // @return SimpleDHTErrSuccess is success; otherwise, failed. int read(int pin, byte* ptemperature, byte* phumidity, byte pdata[40]); protected: // confirm the OUTPUT is level in us, @@ -76,7 +76,6 @@ class SimpleDHT { int parse(byte data[40], byte* ptemperature, byte* phumidity); }; - /* Simple DHT11 diff --git a/examples/DHT11Default/DHT11Default.ino b/examples/DHT11Default/DHT11Default.ino index 7b44047..e7e5e7a 100644 --- a/examples/DHT11Default/DHT11Default.ino +++ b/examples/DHT11Default/DHT11Default.ino @@ -19,8 +19,8 @@ void loop() { // read without samples. byte temperature = 0; byte humidity = 0; - int err = SimpleDHT::ErrSuccess; - if ((err = dht11.read(pinDHT11, &temperature, &humidity, NULL)) != SimpleDHT::ErrSuccess) { + int err = SimpleDHTErrSuccess; + if ((err = dht11.read(pinDHT11, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) { Serial.print("Read DHT11 failed, err="); Serial.print(err); return; } diff --git a/examples/DHT11WithRawBits/DHT11WithRawBits.ino b/examples/DHT11WithRawBits/DHT11WithRawBits.ino index 3110f13..9fa3c71 100644 --- a/examples/DHT11WithRawBits/DHT11WithRawBits.ino +++ b/examples/DHT11WithRawBits/DHT11WithRawBits.ino @@ -20,8 +20,8 @@ void loop() { byte temperature = 0; byte humidity = 0; byte data[40] = {0}; - int err = SimpleDHT::ErrSuccess; - if ((err = dht11.read(pinDHT11, &temperature, &humidity, data)) != SimpleDHT::ErrSuccess) { + int err = SimpleDHTErrSuccess; + if ((err = dht11.read(pinDHT11, &temperature, &humidity, data)) != SimpleDHTErrSuccess) { Serial.print("Read DHT11 failed, err="); Serial.print(err); return; } diff --git a/examples/DHT22Default/DHT22Default.ino b/examples/DHT22Default/DHT22Default.ino index 3c240c4..f5fbd05 100644 --- a/examples/DHT22Default/DHT22Default.ino +++ b/examples/DHT22Default/DHT22Default.ino @@ -19,8 +19,8 @@ void loop() { // read without samples. byte temperature = 0; byte humidity = 0; - int err = SimpleDHT::ErrSuccess; - if ((err = dht22.read(pinDHT22, &temperature, &humidity, NULL)) != SimpleDHT::ErrSuccess) { + int err = SimpleDHTErrSuccess; + if ((err = dht22.read(pinDHT22, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) { Serial.print("Read DHT22 failed, err="); Serial.print(err); return; } diff --git a/examples/TwoSensorsDefault/TwoSensorsDefault.ino b/examples/TwoSensorsDefault/TwoSensorsDefault.ino index d157104..ec7216c 100644 --- a/examples/TwoSensorsDefault/TwoSensorsDefault.ino +++ b/examples/TwoSensorsDefault/TwoSensorsDefault.ino @@ -25,8 +25,8 @@ void loop() { // read without samples. byte temperature = 0; byte humidity = 0; - int err = SimpleDHT::ErrSuccess; - if ((err = dht11.read(dataPinSensor1, &temperature, &humidity, NULL)) != SimpleDHT::ErrSuccess) { + int err = SimpleDHTErrSuccess; + if ((err = dht11.read(dataPinSensor1, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) { Serial.print("Communication error with Sensor 1, err="); Serial.print(err); return; } @@ -47,7 +47,7 @@ void loop() { byte temperature2 = 0; byte humidity2 = 0; - if ((err = dht11.read(dataPinSensor2, &temperature, &humidity, NULL)) != SimpleDHT::ErrSuccess) { + if ((err = dht11.read(dataPinSensor2, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) { Serial.print("Communication error with Sensor 2, err="); Serial.print(err); return; } diff --git a/library.properties b/library.properties index 505e116..4c6bd74 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=SimpleDHT -version=1.0.2 +version=1.0.4 author=Winlin maintainer=Winlin sentence=Arduino Temp & Humidity Sensors for DHT11 and DHT22.