2013-08-28 07:57:45 -04:00
|
|
|
//
|
|
|
|
// FILE: dht22_test.ino
|
|
|
|
// AUTHOR: Rob Tillaart
|
|
|
|
// PURPOSE: DHT library test sketch for DHT22 && Arduino
|
2021-01-29 06:31:58 -05:00
|
|
|
// URL: https://github.com/RobTillaart/DHTlib
|
2021-12-16 10:44:08 -05:00
|
|
|
|
2013-08-28 07:57:45 -04:00
|
|
|
|
|
|
|
#include <dht.h>
|
|
|
|
|
|
|
|
dht DHT;
|
|
|
|
|
2021-12-16 10:44:08 -05:00
|
|
|
#define DHT22_PIN 5
|
|
|
|
|
2013-08-28 07:57:45 -04:00
|
|
|
|
2014-10-08 10:37:30 -04:00
|
|
|
struct
|
|
|
|
{
|
2014-10-23 10:15:26 -04:00
|
|
|
uint32_t total;
|
|
|
|
uint32_t ok;
|
|
|
|
uint32_t crc_error;
|
|
|
|
uint32_t time_out;
|
|
|
|
uint32_t connect;
|
|
|
|
uint32_t ack_l;
|
|
|
|
uint32_t ack_h;
|
|
|
|
uint32_t unknown;
|
|
|
|
} stat = { 0,0,0,0,0,0,0,0};
|
2014-10-08 10:37:30 -04:00
|
|
|
|
2021-12-16 10:44:08 -05:00
|
|
|
|
2013-08-28 07:57:45 -04:00
|
|
|
void setup()
|
|
|
|
{
|
2014-02-08 07:53:52 -05:00
|
|
|
Serial.begin(115200);
|
2014-10-08 10:37:30 -04:00
|
|
|
Serial.println("dht22_test.ino");
|
2014-02-08 07:53:52 -05:00
|
|
|
Serial.print("LIBRARY VERSION: ");
|
|
|
|
Serial.println(DHT_LIB_VERSION);
|
|
|
|
Serial.println();
|
|
|
|
Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)\tTime (us)");
|
2013-08-28 07:57:45 -04:00
|
|
|
}
|
|
|
|
|
2021-12-16 10:44:08 -05:00
|
|
|
|
2013-08-28 07:57:45 -04:00
|
|
|
void loop()
|
|
|
|
{
|
2014-02-08 07:53:52 -05:00
|
|
|
// READ DATA
|
|
|
|
Serial.print("DHT22, \t");
|
|
|
|
|
|
|
|
uint32_t start = micros();
|
|
|
|
int chk = DHT.read22(DHT22_PIN);
|
|
|
|
uint32_t stop = micros();
|
|
|
|
|
2014-10-08 10:37:30 -04:00
|
|
|
stat.total++;
|
2014-02-08 07:53:52 -05:00
|
|
|
switch (chk)
|
|
|
|
{
|
|
|
|
case DHTLIB_OK:
|
2014-10-08 10:37:30 -04:00
|
|
|
stat.ok++;
|
2014-02-08 07:53:52 -05:00
|
|
|
Serial.print("OK,\t");
|
|
|
|
break;
|
|
|
|
case DHTLIB_ERROR_CHECKSUM:
|
2014-10-08 10:37:30 -04:00
|
|
|
stat.crc_error++;
|
2014-02-08 07:53:52 -05:00
|
|
|
Serial.print("Checksum error,\t");
|
|
|
|
break;
|
|
|
|
case DHTLIB_ERROR_TIMEOUT:
|
2014-10-08 10:37:30 -04:00
|
|
|
stat.time_out++;
|
2014-02-08 07:53:52 -05:00
|
|
|
Serial.print("Time out error,\t");
|
|
|
|
break;
|
2014-10-23 10:15:26 -04:00
|
|
|
case DHTLIB_ERROR_CONNECT:
|
|
|
|
stat.connect++;
|
|
|
|
Serial.print("Connect error,\t");
|
|
|
|
break;
|
|
|
|
case DHTLIB_ERROR_ACK_L:
|
|
|
|
stat.ack_l++;
|
|
|
|
Serial.print("Ack Low error,\t");
|
|
|
|
break;
|
|
|
|
case DHTLIB_ERROR_ACK_H:
|
|
|
|
stat.ack_h++;
|
|
|
|
Serial.print("Ack High error,\t");
|
|
|
|
break;
|
2014-02-08 07:53:52 -05:00
|
|
|
default:
|
2014-10-08 10:37:30 -04:00
|
|
|
stat.unknown++;
|
2014-02-08 07:53:52 -05:00
|
|
|
Serial.print("Unknown error,\t");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// DISPLAY DATA
|
|
|
|
Serial.print(DHT.humidity, 1);
|
|
|
|
Serial.print(",\t");
|
|
|
|
Serial.print(DHT.temperature, 1);
|
|
|
|
Serial.print(",\t");
|
|
|
|
Serial.print(stop - start);
|
|
|
|
Serial.println();
|
|
|
|
|
2014-10-08 10:37:30 -04:00
|
|
|
if (stat.total % 20 == 0)
|
|
|
|
{
|
2017-05-08 23:09:22 -04:00
|
|
|
Serial.println("\nTOT\tOK\tCRC\tTO\tCON\tACK_L\tACK_H\tUNK");
|
2014-10-23 10:15:26 -04:00
|
|
|
Serial.print(stat.total);
|
|
|
|
Serial.print("\t");
|
|
|
|
Serial.print(stat.ok);
|
|
|
|
Serial.print("\t");
|
|
|
|
Serial.print(stat.crc_error);
|
|
|
|
Serial.print("\t");
|
|
|
|
Serial.print(stat.time_out);
|
|
|
|
Serial.print("\t");
|
|
|
|
Serial.print(stat.connect);
|
|
|
|
Serial.print("\t");
|
|
|
|
Serial.print(stat.ack_l);
|
|
|
|
Serial.print("\t");
|
|
|
|
Serial.print(stat.ack_h);
|
|
|
|
Serial.print("\t");
|
|
|
|
Serial.print(stat.unknown);
|
|
|
|
Serial.println("\n");
|
2014-10-08 10:37:30 -04:00
|
|
|
}
|
2014-02-08 07:53:52 -05:00
|
|
|
delay(2000);
|
2013-08-28 07:57:45 -04:00
|
|
|
}
|
2021-12-16 10:44:08 -05:00
|
|
|
|
|
|
|
|
|
|
|
// -- END OF FILE --
|
|
|
|
|
|
|
|
|