You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Markus Pesch 0ebb817427
fix: migrate to gitea
10 months ago
LICENSE Go AM2302 / DHT22 / DHT11 2 years ago
README.md fix: migrate to gitea 10 months ago
dht.go fix: migrate to gitea 10 months ago
dhtNotWindows.go fix: migrate to gitea 10 months ago
dhtWindows.go Added windows and not windows 2 years ago
globals.go gofmt 2 years ago
go.mod fix: migrate to gitea 10 months ago
go.sum fix: migrate to gitea 10 months ago

README.md

Go DHT22 / AM2302 / DHT11 interface

Golang DHT22 / AM2302 / DHT11 interface using periph.io driver

Please note

Please make sure to setup your DHT22 / AM2302 / DHT11 correctly. Do a search on the internet to find guide. Here is an example of a guide:

https://learn.adafruit.com/dht/connecting-to-a-dhtxx-sensor

The examples below are from using a Raspberry Pi 3 with GPIO 19 for the pin. Your setup may be different, if so, your pin names would need to change in each example.

Side note, in my testing the sensor has a fairly high level of read errors, suggest using ReadRetry or ReadBackground rather then just Read.

Tested on Raspberry Pi 3 with AM2302. Please open an issue if there are any issues.

Get

go get git.cryptic.systems/volker.raschek/go-dht

ReadRetry example

package main

import (
	"fmt"

	"git.cryptic.systems/volker.raschek/go-dht"
)

func main() {
	err := dht.HostInit()
	if err != nil {
		fmt.Println("HostInit error:", err)
		return
	}

	dht, err := dht.NewDHT("GPIO19", dht.Fahrenheit, "")
	if err != nil {
		fmt.Println("NewDHT error:", err)
		return
	}

	humidity, temperature, err := dht.ReadRetry(11)
	if err != nil {
		fmt.Println("Read error:", err)
		return
	}

	fmt.Printf("humidity: %v\n", humidity)
	fmt.Printf("temperature: %v\n", temperature)
}

ReadBackground example

package main

import (
	"fmt"
	"time"

	"git.cryptic.systems/volker.raschek/go-dht"
)

func main() {
	err := dht.HostInit()
	if err != nil {
		fmt.Println("HostInit error:", err)
		return
	}

	dht, err := dht.NewDHT("GPIO19", dht.Fahrenheit, "")
	if err != nil {
		fmt.Println("NewDHT error:", err)
		return
	}

	stop := make(chan struct{})
	stopped := make(chan struct{})
	var humidity float64
	var temperature float64

	// get sensor reading every 20 seconds in background
	go dht.ReadBackground(&humidity, &temperature, 20*time.Second, stop, stopped)

	// should have at least read the sensor twice after 30 seconds
	time.Sleep(30 * time.Second)

	fmt.Printf("humidity: %v\n", humidity)
	fmt.Printf("temperature: %v\n", temperature)

	// to stop ReadBackground after done with reading, close the stop channel
	close(stop)

	// can check stopped channel to know when ReadBackground has stopped
	<-stopped
}

Read example

package main

import (
	"fmt"

	"git.cryptic.systems/volker.raschek/go-dht"
)

func main() {
	err := dht.HostInit()
	if err != nil {
		fmt.Println("HostInit error:", err)
		return
	}

	dht, err := dht.NewDHT("GPIO19", dht.Fahrenheit, "")
	if err != nil {
		fmt.Println("NewDHT error:", err)
		return
	}

	humidity, temperature, err := dht.Read()
	if err != nil {
		fmt.Println("Read error:", err)
		return
	}

	fmt.Printf("humidity: %v\n", humidity)
	fmt.Printf("temperature: %v\n", temperature)
}