dht

package module
v0.0.0-...-e8a8b0e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 17, 2022 License: MIT Imports: 7 Imported by: 0

README

Go DHT22 / AM2302 / DHT11 interface

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

GoDoc Reference Go Report Card

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 github.com/MichaelS11/go-dht

ReadRetry example

package main

import (
	"fmt"

	"github.com/MichaelS11/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"

	"github.com/MichaelS11/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"

	"github.com/MichaelS11/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)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HostInit

func HostInit() error

HostInit calls periph.io host.Init(). This needs to be done before DHT can be used.

Types

type DHT

type DHT struct {
	// contains filtered or unexported fields
}

DHT struct to interface with the sensor. Call NewDHT to create a new one.

func NewDHT

func NewDHT(pinName string, temperatureUnit TemperatureUnit, sensorType string) (*DHT, error)

NewDHT to create a new DHT struct. sensorType is dht11 for DHT11, anything else for AM2302 / DHT22.

func (*DHT) Read

func (dht *DHT) Read() (humidity float64, temperature float64, err error)

Read reads the sensor once, returing humidity and temperature, or an error. Note that Read will sleep for at least 2 seconds between last call. Each reads error adds a half second to sleep time to max of 30 seconds.

func (*DHT) ReadBackground

func (dht *DHT) ReadBackground(humidity *float64, temperature *float64, sleepDuration time.Duration, stop chan struct{}, stopped chan struct{})

ReadBackground it meant to be run in the background, run as a Goroutine. sleepDuration is how long it will try to sleep between reads. If there is ongoing read errors there will be no notice except that the values will not be updated. Will continue to read sensor until stop is closed. After it has been stopped, the stopped chan will be closed. Will panic if humidity, temperature, or stop are nil.

func (*DHT) ReadRetry

func (dht *DHT) ReadRetry(maxRetries int) (humidity float64, temperature float64, err error)

ReadRetry will call Read until there is no errors or the maxRetries is hit. Suggest maxRetries to be set around 11.

type TemperatureUnit

type TemperatureUnit int

TemperatureUnit is the temperature unit wanted, either Celsius or Fahrenheit

const (
	// Celsius temperature unit
	Celsius TemperatureUnit = iota
	// Fahrenheit temperature unit
	Fahrenheit
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL