trying the SimpleDHT

This commit is contained in:
Roald Batts 2022-12-28 22:36:14 -05:00
parent 569cd4e795
commit 7d09bff498
15 changed files with 1145 additions and 0 deletions

View file

@ -0,0 +1,35 @@
#include <SimpleDHT.h>
// for DHT11,
// VCC: 5V or 3V
// GND: GND
// DATA: 2
int pinDHT11 = 2;
SimpleDHT11 dht11(pinDHT11);
void setup() {
Serial.begin(115200);
}
void loop() {
// start working...
Serial.println("=================================");
Serial.println("Sample DHT11...");
// read without samples.
byte temperature = 0;
byte humidity = 0;
int err = SimpleDHTErrSuccess;
if ((err = dht11.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
Serial.print("Read DHT11 failed, err="); Serial.print(SimpleDHTErrCode(err));
Serial.print(","); Serial.println(SimpleDHTErrDuration(err)); delay(1000);
return;
}
Serial.print("Sample OK: ");
Serial.print((int)temperature); Serial.print(" *C, ");
Serial.print((int)humidity); Serial.println(" H");
// DHT11 sampling rate is 1HZ.
delay(1500);
}

View file

@ -0,0 +1,42 @@
#include <SimpleDHT.h>
// for DHT11,
// VCC: 5V or 3V
// GND: GND
// DATA: 2
int pinDHT11 = 2;
SimpleDHT11 dht11(pinDHT11);
void setup() {
Serial.begin(115200);
}
void loop() {
// start working...
Serial.println("=================================");
Serial.println("Sample DHT11 with error count");
int cnt = 0;
int err_cnt = 0;
for (;;) {
cnt++;
byte temperature = 0;
byte humidity = 0;
int err = SimpleDHTErrSuccess;
if ((err = dht11.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
Serial.print("Read DHT11 failed, err="); Serial.print(SimpleDHTErrCode(err));
Serial.print(","); Serial.print(SimpleDHTErrDuration(err));
err_cnt++;
} else {
Serial.print("DHT11, ");
Serial.print((int)temperature); Serial.print(" *C, ");
Serial.print((int)humidity); Serial.print(" H");
}
Serial.print(", total: "); Serial.print(cnt);
Serial.print(", err: "); Serial.print(err_cnt);
Serial.print(", success rate: "); Serial.print((cnt - err_cnt) * 100.0 / (float)cnt); Serial.println("%");
delay(1500);
}
}

View file

@ -0,0 +1,45 @@
#include <SimpleDHT.h>
// for DHT11,
// VCC: 5V or 3V
// GND: GND
// DATA: 2
int pinDHT11 = 2;
SimpleDHT11 dht11(pinDHT11);
void setup() {
Serial.begin(115200);
}
void loop() {
// start working...
Serial.println("=================================");
Serial.println("Sample DHT11 with RAW bits...");
// read with raw sample data.
byte temperature = 0;
byte humidity = 0;
byte data[40] = {0};
int err = SimpleDHTErrSuccess;
if ((err = dht11.read(&temperature, &humidity, data)) != SimpleDHTErrSuccess) {
Serial.print("Read DHT11 failed, err="); Serial.print(SimpleDHTErrCode(err));
Serial.print(","); Serial.println(SimpleDHTErrDuration(err)); delay(1000);
return;
}
Serial.print("Sample RAW Bits: ");
for (int i = 0; i < 40; i++) {
Serial.print((int)data[i]);
if (i > 0 && ((i + 1) % 4) == 0) {
Serial.print(' ');
}
}
Serial.println("");
Serial.print("Sample OK: ");
Serial.print((int)temperature); Serial.print(" *C, ");
Serial.print((int)humidity); Serial.println(" H");
// DHT11 sampling rate is 1HZ.
delay(1500);
}

View file

@ -0,0 +1,37 @@
#include <SimpleDHT.h>
// for DHT22,
// VCC: 5V or 3V
// GND: GND
// DATA: 2
int pinDHT22 = 2;
SimpleDHT22 dht22(pinDHT22);
void setup() {
Serial.begin(115200);
}
void loop() {
// start working...
Serial.println("=================================");
Serial.println("Sample DHT22...");
// read without samples.
// @remark We use read2 to get a float data, such as 10.1*C
// 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;
int err = SimpleDHTErrSuccess;
if ((err = dht22.read2(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
Serial.print("Read DHT22 failed, err="); Serial.print(SimpleDHTErrCode(err));
Serial.print(","); Serial.println(SimpleDHTErrDuration(err)); delay(2000);
return;
}
Serial.print("Sample OK: ");
Serial.print((float)temperature); Serial.print(" *C, ");
Serial.print((float)humidity); Serial.println(" RH%");
// DHT22 sampling rate is 0.5HZ.
delay(2500);
}

View file

@ -0,0 +1,42 @@
#include <SimpleDHT.h>
// for DHT22,
// VCC: 5V or 3V
// GND: GND
// DATA: 2
int pinDHT22 = 2;
SimpleDHT22 dht22(pinDHT22);
void setup() {
Serial.begin(115200);
}
void loop() {
// start working...
Serial.println("=================================");
Serial.println("Sample DHT22 with error count");
int cnt = 0;
int err_cnt = 0;
for (;;) {
cnt++;
float temperature = 0;
float humidity = 0;
int err = SimpleDHTErrSuccess;
if ((err = dht22.read2(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
Serial.print("Read DHT22 failed, err="); Serial.print(SimpleDHTErrCode(err));
Serial.print(","); Serial.print(SimpleDHTErrDuration(err));
err_cnt++;
} else {
Serial.print("DHT22, ");
Serial.print((float)temperature); Serial.print(" *C, ");
Serial.print((float)humidity); Serial.print(" RH%");
}
Serial.print(", total: "); Serial.print(cnt);
Serial.print(", err: "); Serial.print(err_cnt);
Serial.print(", success rate: "); Serial.print((cnt - err_cnt) * 100.0 / (float)cnt); Serial.println("%");
delay(2500);
}
}

View file

@ -0,0 +1,35 @@
#include <SimpleDHT.h>
// for DHT22,
// VCC: 5V or 3V
// GND: GND
// DATA: 2
int pinDHT22 = 2;
SimpleDHT22 dht22(pinDHT22);
void setup() {
Serial.begin(115200);
}
void loop() {
// start working...
Serial.println("=================================");
Serial.println("Sample DHT22...");
// read without samples.
byte temperature = 0;
byte humidity = 0;
int err = SimpleDHTErrSuccess;
if ((err = dht22.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
Serial.print("Read DHT22 failed, err="); Serial.print(SimpleDHTErrCode(err));
Serial.print(","); Serial.println(SimpleDHTErrDuration(err)); delay(2000);
return;
}
Serial.print("Sample OK: ");
Serial.print((int)temperature); Serial.print(" *C, ");
Serial.print((int)humidity); Serial.println(" RH%");
// DHT22 sampling rate is 0.5HZ.
delay(2500);
}

View file

@ -0,0 +1,47 @@
#include <SimpleDHT.h>
// for DHT22,
// VCC: 5V or 3V
// GND: GND
// DATA: 2
int pinDHT22 = 2;
SimpleDHT22 dht22(pinDHT22);
void setup() {
Serial.begin(115200);
}
void loop() {
// start working...
Serial.println("=================================");
Serial.println("Sample DHT22 with RAW bits...");
// read with raw sample data.
// @remark We use read2 to get a float data, such as 10.1*C
// 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};
int err = SimpleDHTErrSuccess;
if ((err = dht22.read2(&temperature, &humidity, data)) != SimpleDHTErrSuccess) {
Serial.print("Read DHT22 failed, err="); Serial.print(SimpleDHTErrCode(err));
Serial.print(","); Serial.println(SimpleDHTErrDuration(err)); delay(2000);
return;
}
Serial.print("Sample RAW Bits: ");
for (int i = 0; i < 40; i++) {
Serial.print((int)data[i]);
if (i > 0 && ((i + 1) % 4) == 0) {
Serial.print(' ');
}
}
Serial.println("");
Serial.print("Sample OK: ");
Serial.print((float)temperature); Serial.print(" *C, ");
Serial.print((float)humidity); Serial.println(" RH%");
// DHT22 sampling rate is 0.5HZ.
delay(2500);
}

View file

@ -0,0 +1,66 @@
#include <SimpleDHT.h>
// Created by santosomar Ωr using SimpleDHT library to read data from two DHT11 sensors
// for DHT11,
// VCC: 5V or 3V
// GND: GND
// SENSOR 1 is in Digital Data pin: 2
// SENSOR 2 is in Digital Data pin: 4
int dataPinSensor1 = 2;
int dataPinSensor2 = 4;
SimpleDHT11 dht1(dataPinSensor1);
SimpleDHT11 dht2(dataPinSensor2);
void setup() {
Serial.begin(115200);
}
void loop() {
// Reading data from sensor 1...
Serial.println("=================================");
// Reading data from sensor 1...
Serial.println("Getting data from sensor 1...");
// read without samples.
byte temperature = 0;
byte humidity = 0;
int err = SimpleDHTErrSuccess;
if ((err = dht1.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
Serial.print("Read Sensor 1 failed, err="); Serial.print(SimpleDHTErrCode(err));
Serial.print(","); Serial.println(SimpleDHTErrDuration(err)); delay(1000);
return;
}
// converting Celsius to Fahrenheit
byte f = temperature * 1.8 + 32;
Serial.print("Sample OK: ");
Serial.print((int)temperature); Serial.print(" *C, ");
Serial.print((int)f); Serial.print(" *F, ");
Serial.print((int)humidity); Serial.println(" H humidity");
// Reading data from sensor 2...
// ============================
Serial.println("Getting data from sensor 2...");
byte temperature2 = 0;
byte humidity2 = 0;
if ((err = dht2.read(&temperature2, &humidity2, NULL)) != SimpleDHTErrSuccess) {
Serial.print("Read Sensor 2 failed, err="); Serial.print(SimpleDHTErrCode(err));
Serial.print(","); Serial.println(SimpleDHTErrDuration(err)); delay(1000);
return;
}
// converting Celsius to Fahrenheit
byte fb = temperature2 * 1.8 + 32;
Serial.print("Sample OK: ");
Serial.print((int)temperature2); Serial.print(" *C, ");
Serial.print((int)fb); Serial.print(" *F, ");
Serial.print((int)humidity2); Serial.println(" H humidity");
// DHT11 sampling rate is 1HZ.
delay(1500);
}