Giter Site home page Giter Site logo

dht's Introduction

DHT

Build Status

DHT temperature and humidity sensor library for the Arduino framework for ESP8266.
For ESP32, please look into this repo esp32DHT

This is yet another DHT library but it does come with a difference. It is completely non blocking so doesn't use delay(), anywhere. The inner working of the lib relies completely on pin interrupts and timers.

This library aims to be minimal. No bounds checks are in place, no extra calculation methods are available.

Installation

Usage

2 simple examples are included. I tested on a Wemos D1 mini (with Wemos DHT11 shield and a seperate DHT22 sensor). Keep in mind that this library is interrupt driven. Your callback function will also be called from within an interrupt.Therefore, the callback should be short, fast and not contain any interrupt driven tasks (like Serial). You can read the values and set a "flag" or bind your callback to "Schedule" and run your code at the next loop()-call.

Simple example:

#include <Arduino.h>
#include <Ticker.h>

#include <DHT.h>

Ticker ticker;
DHT22 sensor;  // change to DHT11 if needed!
volatile float humidity = 0;
volatile float temperature = 0;
volatile uint8_t error = 0;
volatile int8_t result = 0;

void readDHT() {
  sensor.read();
}

// this callback will be called from an interrupt
// it should be short and carry the ICACHE_RAM_ATTR attribute
void ICACHE_RAM_ATTR handleData(float h, float t) {
  humidity = h;
  temperature = t;
  result = 1;
}

// this callback will be called from an interrupt
// it should be short and carry the ICACHE_RAM_ATTR attribute
void ICACHE_RAM_ATTR handleError(uint8_t e) {
  error = e;
  result = -1;
}

void setup() {
  Serial.begin(74880);
  sensor.setup(D4);
  sensor.onData(handleData);
  sensor.onError(handleError);
  ticker.attach(30, readDHT);
}

void loop() {
  if (result > 0) {
    Serial.printf("Humid: %g%%\n", humidity);
    Serial.printf("Temp: %gdegC\n", temperature);
  } else if (result < 0) {
    Serial.printf("Error: %s\n", sensor.getError());
  }
  result = 0;
}

History

I was tired exploring all the DHT libraries only to find out they were blocking, using delay() or had some sort of other restrictions to read the sensor. So I ended up creating my own. Although I created this lib from scratch I did look into the other libraries to see how they were coded. Nevertheless I'm not going to give a full list of credits because I'll probably forget a few.

dht's People

Contributors

bertmelis avatar slowbro avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.