All Projects → d2r2 → Go Dht

d2r2 / Go Dht

Licence: mit
Golang library to interact with DHT11/DHT22/DHT12 temperature and humidity sensors from Raspberry PI.

Programming Languages

c
50402 projects - #5 most used programming language
golang
3204 projects

Labels

Projects that are alternatives of or similar to Go Dht

Rust Sysfs Gpio
A Rust Interface to the Linux sysfs GPIO interface (https://www.kernel.org/doc/Documentation/gpio/sysfs.txt)
Stars: ✭ 320 (+210.68%)
Mutual labels:  gpio
Ruby I2c Devices
i2c-devices is a library for using I2C devices by using /dev/i2c-* or /sys/class/gpio with bit-banging.
Stars: ✭ 23 (-77.67%)
Mutual labels:  gpio
Mraa
Linux Library for low speed IO Communication in C with bindings for C++, Python, Node.js & Java. Supports generic io platforms, as well as Intel Edison, Intel Joule, Raspberry Pi and many more.
Stars: ✭ 1,220 (+1084.47%)
Mutual labels:  gpio
Cylon
JavaScript framework for robotics, drones, and the Internet of Things (IoT)
Stars: ✭ 3,862 (+3649.51%)
Mutual labels:  gpio
Imanager
Advantech iManager Linux driver set for Advantech Embedded Boards
Stars: ✭ 16 (-84.47%)
Mutual labels:  gpio
Raspberrysharp
A .NET/Mono IO Library for Raspberry Pi This library is a complete refactoring of Raspberry-Sharp libraries, merged into one library and updated to RB3, CM3 and RB3+
Stars: ✭ 41 (-60.19%)
Mutual labels:  gpio
Octoprint Enclosure
OctoPrint Enclosure Plugin
Stars: ✭ 267 (+159.22%)
Mutual labels:  gpio
Cutehmi
CuteHMI is an open-source HMI (Human Machine Interface) software written in C++ and QML, using Qt libraries as a framework. GitHub repository is a mirror!
Stars: ✭ 90 (-12.62%)
Mutual labels:  gpio
Upboard ros
ROS nodes for upboard usage
Stars: ✭ 22 (-78.64%)
Mutual labels:  gpio
Swiftygpio
A Swift library for hardware projects on Linux/ARM boards with support for GPIOs/SPI/I2C/PWM/UART/1Wire.
Stars: ✭ 1,188 (+1053.4%)
Mutual labels:  gpio
W1thermsensor
A Python package and CLI tool to work with w1 temperature sensors like DS1822, DS18S20 & DS18B20 on the Raspberry Pi, Beagle Bone and other devices.
Stars: ✭ 446 (+333.01%)
Mutual labels:  gpio
Pigpio
Fast GPIO, PWM, servo control, state change notification and interrupt handling with Node.js on the Raspberry Pi
Stars: ✭ 747 (+625.24%)
Mutual labels:  gpio
Tinygo
Go compiler for small places. Microcontrollers, WebAssembly (WASM/WASI), and command-line tools. Based on LLVM.
Stars: ✭ 9,068 (+8703.88%)
Mutual labels:  gpio
Elixir ale
Interact with hardware in Elixir - GPIOs, I2C and SPI
Stars: ✭ 336 (+226.21%)
Mutual labels:  gpio
Gpio Utils
Userspace Utilities for managing GPIOs in Linux
Stars: ✭ 82 (-20.39%)
Mutual labels:  gpio
Homegenie
HomeGenie, the open source, programmable, home automation server for smart connected devices and applications
Stars: ✭ 313 (+203.88%)
Mutual labels:  gpio
Cimonitor
Displays CI statuses on a dashboard and triggers fun modules representing the status!
Stars: ✭ 34 (-66.99%)
Mutual labels:  gpio
Gpiozero
A simple interface to GPIO devices with Raspberry Pi
Stars: ✭ 1,302 (+1164.08%)
Mutual labels:  gpio
Gopi
Raspberry Pi Go Language Interface
Stars: ✭ 82 (-20.39%)
Mutual labels:  gpio
Onoff
GPIO access and interrupt detection with Node.js
Stars: ✭ 1,050 (+919.42%)
Mutual labels:  gpio

DHTxx temperature and humidity sensors

Build Status Go Report Card GoDoc MIT License

Preamble

All sensors supported by this library communicate with Raspberry PI via "single-wire digital interface". This approach is require very accurate synchronous timing. Raspberry PI hardware and its clones equipped with Linux - most massive and useful operating system (Linux is my favorite OS) - but not a real time system strictly speaking. It cannot guaranty that code (particularly user, not kernel layer code) might provide exact microsecond switching necessary to meet "single-wire digital interface" specification. Especially when you run this code from language whose runtime is based on background garbage collector processes, which might additionally raise "stop the world" phenomenon, when running code might unexpectedly freeze for a moment. Starting from Go 1.5 GC STW significantly improved, but still can affect millisecond/microsecond world.

So here is my recommendation - if you want to control sensors and peripheral devices from Golang environment running on Linux OS, go and buy sensors with I2C interface. I personally support such sensors in my hobby projects via I2C library (you can go and find list of supported devices in the middle).

But if you want to learn how signal processing algorithm attempt to work with "sinlge-wire digital interface" from not real time user code layer, go and find it!

About

DHT11 (pdf reference), AM2302/DHT22 (pdf reference) and DHT12 (pdf reference) sensors, which quite popular among Arduino, Raspberry PI developers (here you will find comparison 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 clones, which gives you at the output temperature and humidity values (making all necessary signal processing via their own 1-wire bus protocol behind the scene).

Technology overview

There are 2 methods how we can drive such devices which require special pins switch from low to high level and back (employing specific "1-wire protocol" described in pdf documentation):

  1. First approach implies to work on the most lower layer to handle pins via GPIO chip registers using Linux "memory mapped" device (/dev/mem). This approach is most reliable (until you move to other RPI clone) and fastest with regard to the transmission speed. Disadvantage of this method is explained by the fact that each RPI-clone have their own GPIO registers set to drive device GPIO pins.
  2. Second option implies to access GPIO pins via special layer based on Linux "device tree" approach (/sys/class/gpio/... virtual file system), which translate such operations to direct register writes and reads described in 1st approach. In some sense it is more compatible when you move from original Raspberry PI to RPI-clones, but may have some issues in stability of specific implementations. As it was found some clones don't implement this layer at all from the box (Beaglebone for instance).

So, here I'm using second approach.

Compatibility

Tested on Raspberry PI 1/2 (model B), Banana PI (model M1), Orange PI One.

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.
	// Note: "boost GPIO performance" parameter is not work anymore from some
	// specific Go release. Never put true value here.
	temperature, humidity, retried, err :=
		dht.ReadDHTxxWithRetry(dht.DHT11, 4, false, 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)
}

Installation

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

Quick start

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 additional argument in ReadDHTxxWithRetry(...). So, it's highly recommended to utilize ReadDHTxxWithRetry(...) with "retry count" not less than 7, since sensor asynchronous protocol is not very stable causing errors time to time. Each additional 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: This package does not have dependency on any sensor-specific 3-rd party C-code or library.

Tutorial

The library consists of 2 parts: low level C-code to send queries and read raw data from sensor and front-end Golang functions with raw data decoding.

Originally attempt was made to write whole library in Golang, but during debugging it was found that Garbage Collector (GC) "stop the world" characteristic in early version of Golang noticeably freeze library in the middle of sensor reading process, which lead to unpredictable mistakes when some signals from sensor just have been lost. Starting from Go 1.5 version GC behavior was improved significantly, but original design left as is, since it been tested and works reliably in most cases.

To install library on your Raspberry PI device you should execute console command go get -u github.com/d2r2/go-dht to download and install/update package to you device $GOPATH/src path.

You may start from simple test with DHTxx sensor using ./examples/example1/example1.go application which will interact with the sensor connected to some specific physical hardware pin (you may google pinout of any Raspberry PI version either its clones).

Also you can use cross compile technique, to build ARM application from x86/64bit system. For this your should install GCC tool-chain for ARM target platform. So, your x86/64bit Linux system should have specific gcc compiler installed: in case of Debian or Ubuntu arm-linux-gnueabi-gcc (in case of Arch Linux arm-linux-gnueabihf-gcc). After all, for instance, for cross compiling test application "./examples/example1/example1.go" to ARM target platform in Ubuntu/Debian you should run CC=arm-linux-gnueabi-gcc CGO_ENABLED=1 GOOS=linux GOARCH=arm GOARM=6 go build ./examples/example1/example1.go.

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!

Contribute authors

Contact

Please use Github issue tracker for filing bugs or feature requests.

License

Go-dht is licensed under MIT License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].