Giter Site home page Giter Site logo

hugovdm / go-dht Goto Github PK

View Code? Open in Web Editor NEW

This project forked from d2r2/go-dht

0.0 1.0 0.0 3.23 MB

Golang library to interact with DHT11/DHT22 temperature and humidity sensors from Raspberry PI or counterparts.

License: MIT License

Go 48.19% C 51.81%

go-dht's Introduction

DHTxx temperature and humidity sensors

DHT11 ([pdf reference](https://raw.github.com/d2r2/go-dht/master/docs/DHT11 (1).pdf)) and DHT22 (pdf reference) sensors are quite popular among Arduino, Raspberry PI and their counterparts developers (here you will find comparision DHT11 vs DHT22): dht11 and dht22

They are cheap enough and affordable. So, here is a code written in Go programming language for Raspberry PI and counterparts, which gives you at the output temperature and humidity values (making all necessary signal processing via their own 1-wire bus protocol behind the scenes).

Compatibility

Tested on Raspberry PI 1 (model B) and Banana PI (model M1).

Golang usage

func main() {
	// Read DHT11 sensor data from pin 4, retrying 10 times in case of failure.
	// You may enable "boost GPIO performance" parameter, if your device is old
	// as Raspberry PI 1 (this will require root privileges). You can switch off
	// "boost GPIO performance" parameter for old devices, but it may increase
	// retry attempts. Play with this parameter.
	temperature, humidity, retried, err :=
		dht.ReadDHTxxWithRetry(dht.DHT11, 4, true, 10)
	if err != nil {
		log.Fatal(err)
	}
	// Print temperature and humidity
	fmt.Printf("Temperature = %v*C, Humidity = %v%% (retried %d times)\n",
		temperature, humidity, retried)
}

Getting help

GoDoc documentation.

For detailed explanation read great article "Golang with Raspberry Pi : Read RH and Temperature from DHT22 or AM2302" written by Joseph Mathew. Thanks Joseph!

Installation

$ go get -u github.com/d2r2/go-dht

Quick tutorial

There are two functions you could use: ReadDHTxx(...) and ReadDHTxxWithRetry(...). They both do exactly same thing - activate sensor then read and decode temperature and humidity values. The only thing which distinguish one from another - "retry count" parameter as additinal argument in ReadDHTxxWithRetry(...). So, it's highly recomended to utilize ReadDHTxxWithRetry(...) with "retry count" not less than 7, since sensor asynchronouse protocol is not very stable causing errors time to time. Each additinal retry attempt takes 1.5-2 seconds (according to specification before repeated attempt you should wait 1-2 seconds).

This functionality works not only with Raspberry PI, but with counterparts as well (tested with Raspberry PI and Banana PI).

Note: If you enable "boost GPIO performance" parameter, application should run with root privileges, since C code inside requires this. In most cases it is sufficient to add "sudo -E" before "go run ...".

Note: This package does not require any external C code or library.

License

Go-dht is licensed under MIT License.

go-dht's People

Contributors

d2r2 avatar hugovdm 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.