Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
TilenS6 authored Apr 10, 2023
1 parent ef52202 commit 28ba318
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 0 deletions.
73 changes: 73 additions & 0 deletions dht.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
Simple_DHT.cpp - Library for comunication and data interpretation with DHT sensors.
Created by Tilen Stermecki, Apr 2023.
Released into the public domain.
*/

#ifndef DHT_CPP
#define DHT_CPP

#include "dht.h"
DHT::DHT(byte DHT_PIN) {
pin = DHT_PIN;
pinMode(pin, OUTPUT);
digitalWrite(pin, 1);
fail = false;
}
void DHT::readSensor() {
byte data[5];
digitalWrite(pin, 0);
delayMicroseconds(1000);
digitalWrite(pin, 1);
delayMicroseconds(30);

pinMode(pin, INPUT);
delayMicroseconds(10);
// while (digitalRead(pin));
uint8_t timeout = 1;
while (!digitalRead(pin) && timeout != 0) {
++timeout;
}
if (timeout == 0) { // most of the time comes to ~19 (Arduino UNO R3, this is very bad method, I know)
fail = 2;
pinMode(pin, OUTPUT);
digitalWrite(pin, 1);
return;
}

for (byte dat = 0; dat < 5; dat++) {
data[dat] = 0;
for (byte i = 0; i < 8; i++) {
unsigned long t = pulseIn(pin, HIGH, 10000UL);
if (t == 0) {
fail = 2;
pinMode(pin, OUTPUT);
digitalWrite(pin, 1);
return;
}
data[dat] |= (t > 50) << (7 - i);
}
}
pinMode(pin, OUTPUT);
digitalWrite(pin, 1);

uint8_t tmp = 0;
for (byte i = 0; i < 4; i++)
tmp += data[i];

if (tmp == data[4]) {
fail = 0;
hum = (float)(((uint16_t)(data[0] & 0b01111111) << 8) | data[1]) / 10.0;
if (data[0] & 0b10000000) hum *= -1;
temp = (float)(((uint16_t)(data[2] & 0b01111111) << 8) | data[3]) / 10.0;
if (data[2] & 0b10000000) temp *= -1;
} else {
fail = 1;
}
}

String DHT::errorVerbose() {
return errorLookup[fail];
}

#endif
23 changes: 23 additions & 0 deletions dht.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
Simple_DHT.cpp - Library for comunication and data interpretation with DHT sensors.
Created by Tilen Stermecki, Apr 2023.
Released into the public domain.
*/

#ifndef DHT_H
#define DHT_H
#include <Arduino.h>

class DHT {
byte pin;
String errorLookup[3]= {"", "Error while checking for data validation. Low quality data recieved.", "Device didn't respond. Check wiring."};
public:
float temp, hum;
uint8_t fail;

DHT(byte);
void readSensor();
String errorVerbose();
};

#endif
22 changes: 22 additions & 0 deletions examples/dht_read_temp_hum/dht_read_temp_hum.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
Simple_DHT - Library for comunication and data interpretation with DHT sensors.
Created by Tilen Stermecki, Apr 2023.
Released into the public domain.
*/

#include <dht.h>
DHT dht(A0); // data pin of DHT on A0
void setup() {
Serial.begin(9600);
}

void loop() {
dht.readSensor(); // read values from sensor
if (dht.fail) { // if reading fails...
Serial.println("Detected errors in transmition:\n - " + dht.errorVerbose()); // error occured
} else {
Serial.println("temp: " + (String)dht.temp + "°C");
Serial.println("hum: " + (String)dht.hum + "%\n");
}
delay(2000);
}
5 changes: 5 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
DHT22 KEYWORD1
readSensor KEYWORD2
temperature KEYWORD2
humidity KEYWORD2
failed KEYWORD2

0 comments on commit 28ba318

Please sign in to comment.