|
@@ -26,6 +26,7 @@
|
|
|
//
|
|
|
// Released to the public domain
|
|
|
//
|
|
|
+// #define NODE_DEBUG
|
|
|
|
|
|
#include "user_interface.h"
|
|
|
#include "platform.h"
|
|
@@ -40,21 +41,14 @@
|
|
|
#define HIGH 1
|
|
|
#endif /* ifndef HIGH */
|
|
|
|
|
|
+#define COMBINE_HIGH_AND_LOW_BYTE(byte_high, byte_low) ((uint16_t)((byte_high) << 8) | (byte_low))
|
|
|
+
|
|
|
static double dht_humidity;
|
|
|
static double dht_temperature;
|
|
|
|
|
|
static uint8_t dht_bytes[5]; // buffer to receive data
|
|
|
|
|
|
-typedef enum {
|
|
|
- Humidity = 0,
|
|
|
- Temperature,
|
|
|
- Humidity8,
|
|
|
- Temperature8
|
|
|
-} dht_Signal;
|
|
|
-
|
|
|
static int dht_readSensor(uint8_t pin, uint8_t wakeupDelay);
|
|
|
-static double getValue(dht_Signal s);
|
|
|
-static bool verifyChecksum();
|
|
|
|
|
|
/////////////////////////////////////////////////////
|
|
|
//
|
|
@@ -79,10 +73,15 @@ double dht_getTemperature(void)
|
|
|
// DHTLIB_OK
|
|
|
// DHTLIB_ERROR_CHECKSUM
|
|
|
// DHTLIB_ERROR_TIMEOUT
|
|
|
-int dht_read_universal(uint8_t pin)
|
|
|
+int dht_read(uint8_t pin, dht_type type)
|
|
|
{
|
|
|
// READ VALUES
|
|
|
- int rv = dht_readSensor(pin, DHTLIB_DHT_UNI_WAKEUP);
|
|
|
+ int rv = dht_readSensor(pin,
|
|
|
+ type == DHT22 ? DHTLIB_DHT_WAKEUP :
|
|
|
+ type == DHT11 ? DHTLIB_DHT11_WAKEUP :
|
|
|
+ DHTLIB_DHT_UNI_WAKEUP
|
|
|
+ );
|
|
|
+
|
|
|
if (rv != DHTLIB_OK)
|
|
|
{
|
|
|
dht_humidity = DHTLIB_INVALID_VALUE; // invalid value, or is NaN prefered?
|
|
@@ -90,129 +89,73 @@ int dht_read_universal(uint8_t pin)
|
|
|
return rv; // propagate error value
|
|
|
}
|
|
|
|
|
|
-#if defined(DHT_DEBUG_BYTES)
|
|
|
- int i;
|
|
|
- for (i = 0; i < 5; i++)
|
|
|
- {
|
|
|
- DHT_DEBUG("%02X\n", dht_bytes[i]);
|
|
|
- }
|
|
|
-#endif // defined(DHT_DEBUG_BYTES)
|
|
|
+ NODE_DBG("DHT registers: %x\t%x\t%x\t%x\t%x == %x\n", dht_bytes[0], dht_bytes[1], dht_bytes[2], dht_bytes[3], dht_bytes[4], (uint8_t)(dht_bytes[0] + dht_bytes[1] + dht_bytes[2] + dht_bytes[3]));
|
|
|
|
|
|
- // Assume it is DHT11
|
|
|
+ // Assume it is special case of DHT11,
|
|
|
+ // i.e. positive temperature and dht_bytes[3] == 0 ((dht_bytes[3] & 0x0f) * 0.1 to be added to temperature readout)
|
|
|
// If it is DHT11, both temp and humidity's decimal
|
|
|
+ dht_humidity = dht_bytes[0];
|
|
|
+ dht_temperature = dht_bytes[2];
|
|
|
if ((dht_bytes[1] == 0) && (dht_bytes[3] == 0))
|
|
|
{
|
|
|
// It may DHT11
|
|
|
// CONVERT AND STORE
|
|
|
- DHT_DEBUG("DHT11 method\n");
|
|
|
- dht_humidity = getValue(Humidity8);
|
|
|
- dht_temperature = getValue(Temperature8);
|
|
|
+ NODE_DBG("DHT11 method\n");
|
|
|
|
|
|
// TEST CHECKSUM
|
|
|
- if (!verifyChecksum())
|
|
|
- {
|
|
|
- // It may not DHT11
|
|
|
- dht_humidity = DHTLIB_INVALID_VALUE; // invalid value, or is NaN prefered?
|
|
|
- dht_temperature = DHTLIB_INVALID_VALUE; // invalid value
|
|
|
- // Do nothing
|
|
|
- }
|
|
|
- else
|
|
|
+ uint8_t sum = dht_bytes[0] + dht_bytes[2];
|
|
|
+ if (dht_bytes[4] == sum)
|
|
|
{
|
|
|
return DHTLIB_OK;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // Assume it is not DHT11
|
|
|
+ // Assume it is not DHT11 special case
|
|
|
// CONVERT AND STORE
|
|
|
- DHT_DEBUG("DHTxx method\n");
|
|
|
- dht_humidity = getValue(Humidity);
|
|
|
- dht_temperature = getValue(Temperature);
|
|
|
-
|
|
|
- // TEST CHECKSUM
|
|
|
- if (!verifyChecksum())
|
|
|
- {
|
|
|
- return DHTLIB_ERROR_CHECKSUM;
|
|
|
+ NODE_DBG("DHTxx method\n");
|
|
|
+
|
|
|
+ switch (type) {
|
|
|
+ case DHT11:
|
|
|
+ case DHT12:
|
|
|
+ dht_humidity += dht_bytes[1] * 0.1;
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ dht_humidity = COMBINE_HIGH_AND_LOW_BYTE(dht_bytes[0], dht_bytes[1]) * 0.1;
|
|
|
+ break;
|
|
|
}
|
|
|
- return DHTLIB_OK;
|
|
|
-}
|
|
|
|
|
|
-// return values:
|
|
|
-// DHTLIB_OK
|
|
|
-// DHTLIB_ERROR_CHECKSUM
|
|
|
-// DHTLIB_ERROR_TIMEOUT
|
|
|
-int dht_read11(uint8_t pin)
|
|
|
-{
|
|
|
- // READ VALUES
|
|
|
- int rv = dht_readSensor(pin, DHTLIB_DHT11_WAKEUP);
|
|
|
- if (rv != DHTLIB_OK)
|
|
|
- {
|
|
|
- dht_humidity = DHTLIB_INVALID_VALUE; // invalid value, or is NaN prefered?
|
|
|
- dht_temperature = DHTLIB_INVALID_VALUE; // invalid value
|
|
|
- return rv;
|
|
|
- }
|
|
|
-
|
|
|
- // CONVERT AND STORE
|
|
|
- dht_humidity = getValue(Humidity8);
|
|
|
- dht_temperature = getValue(Temperature8);
|
|
|
-
|
|
|
- // TEST CHECKSUM
|
|
|
- if (!verifyChecksum()) return DHTLIB_ERROR_CHECKSUM;
|
|
|
-
|
|
|
- return DHTLIB_OK;
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-// return values:
|
|
|
-// DHTLIB_OK
|
|
|
-// DHTLIB_ERROR_CHECKSUM
|
|
|
-// DHTLIB_ERROR_TIMEOUT
|
|
|
-int dht_read(uint8_t pin)
|
|
|
-{
|
|
|
- // READ VALUES
|
|
|
- int rv = dht_readSensor(pin, DHTLIB_DHT_WAKEUP);
|
|
|
- if (rv != DHTLIB_OK)
|
|
|
- {
|
|
|
- dht_humidity = DHTLIB_INVALID_VALUE; // invalid value, or is NaN prefered?
|
|
|
- dht_temperature = DHTLIB_INVALID_VALUE; // invalid value
|
|
|
- return rv; // propagate error value
|
|
|
+ switch (type) {
|
|
|
+ case DHT11:
|
|
|
+ if (dht_bytes[3] & 0x80) {
|
|
|
+ dht_temperature = -1 - dht_temperature;
|
|
|
+ }
|
|
|
+ dht_temperature += (dht_bytes[3] & 0x0f) * 0.1;
|
|
|
+ break;
|
|
|
+ case DHT12:
|
|
|
+ dht_temperature += (dht_bytes[3] & 0x0f) * 0.1;
|
|
|
+ if (dht_bytes[2] & 0x80) // negative dht_temperature
|
|
|
+ {
|
|
|
+ dht_temperature *= -1;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ default: // DHT22, DHT_NON11
|
|
|
+ dht_temperature = COMBINE_HIGH_AND_LOW_BYTE(dht_bytes[2] & 0x7F, dht_bytes[3]) * 0.1;
|
|
|
+ if (dht_bytes[2] & 0x80) // negative dht_temperature
|
|
|
+ {
|
|
|
+ dht_temperature *= -1;
|
|
|
+ }
|
|
|
+ break;
|
|
|
}
|
|
|
|
|
|
- // CONVERT AND STORE
|
|
|
- dht_humidity = getValue(Humidity);
|
|
|
- dht_temperature = getValue(Temperature);
|
|
|
-
|
|
|
// TEST CHECKSUM
|
|
|
- if (!verifyChecksum())
|
|
|
+ uint8_t sum = dht_bytes[0] + dht_bytes[1] + dht_bytes[2] + dht_bytes[3];
|
|
|
+ if (dht_bytes[4] != sum)
|
|
|
{
|
|
|
return DHTLIB_ERROR_CHECKSUM;
|
|
|
}
|
|
|
return DHTLIB_OK;
|
|
|
}
|
|
|
|
|
|
-// return values:
|
|
|
-// DHTLIB_OK
|
|
|
-// DHTLIB_ERROR_CHECKSUM
|
|
|
-// DHTLIB_ERROR_TIMEOUT
|
|
|
-int dht_read21(uint8_t pin) __attribute__((alias("dht_read")));
|
|
|
-
|
|
|
-// return values:
|
|
|
-// DHTLIB_OK
|
|
|
-// DHTLIB_ERROR_CHECKSUM
|
|
|
-// DHTLIB_ERROR_TIMEOUT
|
|
|
-int dht_read22(uint8_t pin) __attribute__((alias("dht_read")));
|
|
|
-
|
|
|
-// return values:
|
|
|
-// DHTLIB_OK
|
|
|
-// DHTLIB_ERROR_CHECKSUM
|
|
|
-// DHTLIB_ERROR_TIMEOUT
|
|
|
-int dht_read33(uint8_t pin) __attribute__((alias("dht_read")));
|
|
|
-
|
|
|
-// return values:
|
|
|
-// DHTLIB_OK
|
|
|
-// DHTLIB_ERROR_CHECKSUM
|
|
|
-// DHTLIB_ERROR_TIMEOUT
|
|
|
-int dht_read44(uint8_t pin) __attribute__((alias("dht_read")));
|
|
|
-
|
|
|
/////////////////////////////////////////////////////
|
|
|
//
|
|
|
// PRIVATE
|
|
@@ -236,7 +179,7 @@ int dht_readSensor(uint8_t pin, uint8_t wakeupDelay)
|
|
|
// volatile uint8_t *PIR = portInputRegister(port);
|
|
|
|
|
|
// EMPTY BUFFER
|
|
|
- memset(dht_bytes, sizeof(uint8_t)*5, 0);
|
|
|
+ memset(dht_bytes, 0, sizeof(uint8_t)*5);
|
|
|
|
|
|
// REQUEST SAMPLE
|
|
|
// pinMode(pin, OUTPUT);
|
|
@@ -309,32 +252,6 @@ int dht_readSensor(uint8_t pin, uint8_t wakeupDelay)
|
|
|
return DHTLIB_OK;
|
|
|
}
|
|
|
|
|
|
-// Assembles the high and low byte in a signed 16bit value
|
|
|
-static double getValue(dht_Signal s)
|
|
|
-{
|
|
|
- uint8_t high=0, low=0;
|
|
|
-
|
|
|
- // the '8' variants leave the low byte set to 0
|
|
|
- switch(s){
|
|
|
- case Humidity:
|
|
|
- low = dht_bytes[1];
|
|
|
- case Humidity8:
|
|
|
- high = dht_bytes[0];
|
|
|
- break;
|
|
|
- case Temperature:
|
|
|
- low = dht_bytes[3];
|
|
|
- case Temperature8:
|
|
|
- high = dht_bytes[2];
|
|
|
- break;
|
|
|
- }
|
|
|
- return ((high << 8) | low) * 0.1;
|
|
|
-}
|
|
|
-
|
|
|
-static bool verifyChecksum(){
|
|
|
- uint8_t sum = dht_bytes[0] + dht_bytes[1] + dht_bytes[2] + dht_bytes[3];
|
|
|
- return (dht_bytes[4] == sum);
|
|
|
-}
|
|
|
-
|
|
|
//
|
|
|
// END OF FILE
|
|
|
//
|