aosong

package module
v0.0.0-...-2d6230e Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2019 License: MIT Imports: 7 Imported by: 2

README

Aosong Electronics DHT12, AM2320 humidity and temperature sensors

Build Status Go Report Card GoDoc MIT License

DHT12 (pdf reference) and AM2320 (pdf reference) are relatively cheap and popular among Arduino and Raspberry PI developers. Both sensors may operate via i2c-bus interface: image

Here is a library written in Go programming language for Raspberry PI and clones, which gives you in the output relative humidity and temperature values (making all necessary i2c-bus interacting and values computing).

Pay attention that this library only employ i2c-bus interaction approach. Other option to work with the sensors - specific "single bus communication" protocol is not implemented here.

Golang usage

func main() {
	// Create new connection to i2c-bus on 1 line with address 0x5C.
	// Use i2cdetect utility to find device address over the i2c-bus
	i2c, err := i2c.NewI2C(0x5C, 1)
	if err != nil {
		log.Fatal(err)
	}
	defer i2c.Close()

	sensor := aosong.NewSensor(aosong.DHT12)

	log.Printf("Sensor type = %v\n", sensor.GetSensorType())
	rh, t, err := sensor.ReadRelativeHumidityAndTemperature(i2c)
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("Relative humidity = %v%%\n", rh)
	log.Printf("Temperature in celsius = %v*C\n", t)
}

Getting help

GoDoc documentation

Installation

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

Troubleshooting

  • How to obtain fresh Golang installation to RPi device (either any RPi clone): If your RaspberryPI golang installation taken by default from repository is outdated, you may consider to install actual golang manually from official Golang site. Download tar.gz file containing armv6l in the name. Follow installation instructions.

  • How to enable I2C bus on RPi device: If you employ RaspberryPI, use raspi-config utility to activate i2c-bus on the OS level. Go to "Interfacing Options" menu, to active I2C bus. Probably you will need to reboot to load i2c kernel module. Finally you should have device like /dev/i2c-1 present in the system.

  • How to find I2C bus allocation and device address: Use i2cdetect utility in format "i2cdetect -y X", where X may vary from 0 to 5 or more, to discover address occupied by peripheral device. To install utility you should run apt install i2c-tools on debian-kind system. i2cdetect -y 1 sample output:

         0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
    00:          -- -- -- -- -- -- -- -- -- -- -- -- --
    10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    70: -- -- -- -- -- -- 76 --    
    

Contribute authors

Contact

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

License

Go-aosong is licensed under MIT License.

Documentation

Index

Constants

View Source
const (
	CMD_AM2320_READ_REGISTERS  byte = 0x03 // Reading register data
	CMD_AM2320_WRITE_REGISTERS byte = 0x10 // Write  multiple registers
)

AM2320 command byte's sequences

View Source
const (
	DHT12_HUM_INT    = 0x00
	DHT12_HUM_SCALE  = 0x01
	DHT12_TEMP_INT   = 0x02
	DHT12_TEMP_SCALE = 0x03
	DHT12_CHECKSUM   = 0x04
	DHT12_DATA_BYTES = 5
	DHT12_DATA_START = DHT12_HUM_INT
)

DHT12 sensor memory map

Variables

This section is empty.

Functions

This section is empty.

Types

type Sensor

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

func NewSensor

func NewSensor(sensorType SensorType) *Sensor

func (*Sensor) GetSensorType

func (v *Sensor) GetSensorType() SensorType

func (*Sensor) ReadRelativeHumidityAndTemperature

func (v *Sensor) ReadRelativeHumidityAndTemperature(i2c *i2c.I2C) (humidity float32,
	temperature float32, err error)

type SensorAM2320

type SensorAM2320 struct {
}

SensorAM2320 specific type

func (*SensorAM2320) ReadRelativeHumidityAndTemperatureMult10

func (v *SensorAM2320) ReadRelativeHumidityAndTemperatureMult10(i2c *i2c.I2C) (humidity int16,
	temperature int16, err error)

type SensorDHT12

type SensorDHT12 struct {
}

SensorDHT12 specific type

func (*SensorDHT12) ReadRelativeHumidityAndTemperatureMult10

func (v *SensorDHT12) ReadRelativeHumidityAndTemperatureMult10(i2c *i2c.I2C) (humidity int16,
	temperature int16, err error)

type SensorInterface

type SensorInterface interface {
	ReadRelativeHumidityAndTemperatureMult10(i2c *i2c.I2C) (humidity int16, temperature int16, err error)
}

Abstract Aosong Electronics sensor interface to control and gather data via I2C-bus.

type SensorType

type SensorType int

SensorType identify which Aosong Electronics humidity and temperature sensor is used. DHT12, AM2320 are supported.

const (
	// Aosong Electronics humidity and temperature sensor model DHT12.
	DHT12 SensorType = iota
	// Aosong Electronics humidity and temperature sensor model AM2320.
	AM2320
)

func (SensorType) String

func (v SensorType) String() string

Implement Stringer interface.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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