Hello
I have an small problem with a part of my code:
Here is code which doesn't work as it should:
#include <Wire.h>
#include <DS3231.h>
#include <DHT22.h>
DS3231 RTC;
DHT22 DHT22_0( 2 );
DHT22 DHT22_1( 3 );
void DHT22_PrintData( DHT22 device );
unsigned long lastReadTime;
unsigned long currentTime;
void setup ()
{
Serial.begin(57600);
Wire.begin();
RTC.begin();
//DateTime dt(__DATE__, __TIME__);
//RTC.adjust(dt);
//Small delay needed for the DHT22 startup correctly
delay( 2000 );
lastReadTime = millis();
}
void loop ()
{
currentTime = millis();
if (currentTime - lastReadTime >= 1000)
{
DateTime now = RTC.now();
char str[120];
sprintf( str, "%s %02d %s %04d %02d:%02d:%02d - %.1f°C",
now.dayName(), now.date(), now.monthName(), now.year(), now.hour(), now.minute(), now.second(), RTC.getTemperature());
Serial.println(str);
Serial.print("DHT22_0: ");
DHT22_PrintData( DHT22_0 );
Serial.print("DHT22_1: ");
DHT22_PrintData( DHT22_1 );
Serial.println( );
lastReadTime = currentTime;
}
}
void DHT22_PrintData( DHT22 device )
{
switch( device.readData() )
{
case DHT_ERROR_NONE:
char buf[128];
sprintf(buf, "%.1f°C %.1f%% RH", device.getTemperatureC(), device.getHumidity() );
Serial.println(buf);
break;
case DHT_ERROR_CHECKSUM:
Serial.print("check sum error ");
Serial.print( device.getTemperatureC() );
Serial.print("C ");
Serial.print( device.getHumidity() );
Serial.println("%");
break;
case DHT_BUS_HUNG:
Serial.println("BUS Hung ");
break;
case DHT_ERROR_NOT_PRESENT:
Serial.println("Not Present ");
break;
case DHT_ERROR_ACK_TOO_LONG:
Serial.println("ACK time out ");
break;
case DHT_ERROR_SYNC_TIMEOUT:
Serial.println("Sync Timeout ");
break;
case DHT_ERROR_DATA_TIMEOUT:
Serial.println("Data Timeout ");
break;
case DHT_ERROR_TOOQUICK:
Serial.println("Polled to quick ");
break;
}
}
Here is the output of this program:
Monday 29 April 2013 01:37:47 - 21.3°C
DHT22_0: 21.8°C 48.9% RH
DHT22_1: 21.9°C 49.1% RH
Monday 29 April 2013 01:37:48 - 21.3°C
DHT22_0: 21.8°C 48.7% RH
DHT22_1: 21.9°C 49.1% RH
Monday 29 April 2013 01:37:49 - 21.3°C
DHT22_0: 21.8°C 48.9% RH
DHT22_1: 21.8°C 49.0% RH
Monday 29 April 2013 01:37:50 - 21.3°C
DHT22_0: 21.8°C 48.9% RH
DHT22_1: 21.8°C 49.0% RH
Monday 29 April 2013 01:37:51 - 21.3°C
DHT22_0: 21.8°C 48.9% RH
DHT22_1: 21.9°C 49.1% RH
It looks like it is correct, and while it works perfectly fine to me, in theory it should report an error "Polled too quick" once every 2 seconds, since there is this part in the library's readData function:
if(currentTime - _lastReadTime < 2000)
{
// Caller needs to wait 2 seconds between each call to readData
return DHT_ERROR_TOOQUICK;
}
(And it is said in the DHT22 datasheet, polling should be > 1.7 seconds )
Now if I try this code (the same thing but without my function "PrintData") then everything work as should:
#include <Wire.h>
#include <DS3231.h>
#include <DHT22.h>
DS3231 RTC;
DHT22 DHT22_0( 2 );
DHT22 DHT22_1( 3 );
unsigned long lastReadTime;
unsigned long currentTime;
void setup ()
{
Serial.begin(57600);
Wire.begin();
RTC.begin();
//DateTime dt(__DATE__, __TIME__);
//RTC.adjust(dt);
//Small delay needed for the DHT22 startup correctly
delay( 2000 );
lastReadTime = millis();
}
void loop ()
{
currentTime = millis();
if (currentTime - lastReadTime >= 1000)
{
DateTime now = RTC.now();
char str[120];
sprintf( str, "%s %02d %s %04d %02d:%02d:%02d - %.1f°C",
now.dayName(), now.date(), now.monthName(), now.year(), now.hour(), now.minute(), now.second(), RTC.getTemperature());
Serial.println(str);
switch( DHT22_0.readData() )
{
case DHT_ERROR_NONE:
char buf[128];
sprintf(buf, "DHT22_0: %.1f°C %.1f%% RH",
DHT22_0.getTemperatureC(),DHT22_0.getHumidity());
Serial.println(buf);
break;
case DHT_ERROR_CHECKSUM:
Serial.print("check sum error ");
Serial.print(DHT22_0.getTemperatureC());
Serial.print("C ");
Serial.print(DHT22_0.getHumidity());
Serial.println("%");
break;
case DHT_BUS_HUNG:
Serial.println("BUS Hung ");
break;
case DHT_ERROR_NOT_PRESENT:
Serial.println("Not Present ");
break;
case DHT_ERROR_ACK_TOO_LONG:
Serial.println("ACK time out ");
break;
case DHT_ERROR_SYNC_TIMEOUT:
Serial.println("Sync Timeout ");
break;
case DHT_ERROR_DATA_TIMEOUT:
Serial.println("Data Timeout ");
break;
case DHT_ERROR_TOOQUICK:
Serial.println("Polled to quick ");
break;
}
switch( DHT22_1.readData() )
{
case DHT_ERROR_NONE:
char buf[128];
sprintf(buf, "DHT22_1: %.1f°C %.1f%% RH",
DHT22_1.getTemperatureC(),DHT22_1.getHumidity());
Serial.println(buf);
break;
case DHT_ERROR_CHECKSUM:
Serial.print("check sum error ");
Serial.print(DHT22_1.getTemperatureC());
Serial.print("C ");
Serial.print(DHT22_1.getHumidity());
Serial.println("%");
break;
case DHT_BUS_HUNG:
Serial.println("BUS Hung ");
break;
case DHT_ERROR_NOT_PRESENT:
Serial.println("Not Present ");
break;
case DHT_ERROR_ACK_TOO_LONG:
Serial.println("ACK time out ");
break;
case DHT_ERROR_SYNC_TIMEOUT:
Serial.println("Sync Timeout ");
break;
case DHT_ERROR_DATA_TIMEOUT:
Serial.println("Data Timeout ");
break;
case DHT_ERROR_TOOQUICK:
Serial.println("Polled to quick ");
break;
}
Serial.println( );
lastReadTime = currentTime;
}
}
Monday 29 April 2013 01:44:36 - 21.0°C
DHT22_0: 21.7°C 48.1% RH
DHT22_1: 21.8°C 49.2% RH
Monday 29 April 2013 01:44:37 - 21.0°C
Polled to quick
Polled to quick
Monday 29 April 2013 01:44:38 - 21.0°C
DHT22_0: 21.7°C 48.1% RH
DHT22_1: 21.7°C 49.2% RH
Monday 29 April 2013 01:44:39 - 21.0°C
Polled to quick
Polled to quick
It's like if in the first code, my device.readData() always return 0 (DHT_ERROR_NONE)... but the device's temp and humidity were still updated!
Can you find what is the problem?