sht3x

package module
v0.0.0-...-074abc2 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2018 License: MIT Imports: 13 Imported by: 3

README

Sensirion SHT3x relative humidity and temperature sensor's family

Build Status Go Report Card GoDoc MIT License

SHT30, SHT31, SHT35 (general specification, alert mode specification) high accuracy temperature and relative humidity sensor. Easily integrated with Arduino and Raspberry PI via i2c communication interface: image

This sensor has extra feature - integrated heater which could be helpfull in some specific application (such as periodic condensate removal, for example).

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

Golang usage

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

	sensor := sht3x.NewSHT3X()

	temp, rh, err := sensor.ReadTemperatureAndRelativeHumidity(i2c, sht3x.REPEATABILITY_LOW)
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("Temperature and relative humidity = %v*C, %v%%", temp, rh)

Getting help

GoDoc documentation

Installation

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

Troubleshoting

  • 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 mannualy 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 "Interfaceing 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 --    
    

Contact

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

License

Go-sht3x is licensed under MIT License.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Measure values in "single shot mode".
	CMD_SINGLE_MEASURE_HIGH_CSE   = []byte{0x2C, 0x06} // Single Measure of Temp. and Hum.; High precise; Clock stretching enabled
	CMD_SINGLE_MEASURE_MEDIUM_CSE = []byte{0x2C, 0x0D} // Single Measure of Temp. and Hum.; Medium precise; Clocl stretching enabled
	CMD_SINGLE_MEASURE_LOW_CSE    = []byte{0x2C, 0x10} // Single Measure of Temp. and Hum.; Low precise; Clock stretching enabled
	CMD_SINGLE_MEASURE_HIGH       = []byte{0x24, 0x00} // Single Measure of Temp. and Hum.; High precise
	CMD_SINGLE_MEASURE_MEDIUM     = []byte{0x24, 0x0B} // Single Measure of Temp. and Hum.; Medium precise
	CMD_SINGLE_MEASURE_LOW        = []byte{0x24, 0x16} // Single Measure of Temp. and Hum.; Low precise

	// Measure values in "periodic acquisition mode".
	CMD_PERIOD_MEASURE_05MPS_HIGH   = []byte{0x20, 0x32} // Periodic Measure of Temp. and Hum.; 0.5 Measurements Per Second; High precise
	CMD_PERIOD_MEASURE_05MPS_MEDIUM = []byte{0x20, 0x24} // Periodic Measure of Temp. and Hum.; 0.5 Measurements Per Second; Medium precise
	CMD_PERIOD_MEASURE_05MPS_LOW    = []byte{0x20, 0x2F} // Periodic Measure of Temp. and Hum.; 0.5 Measurements Per Second; Low precise
	CMD_PERIOD_MEASURE_1MPS_HIGH    = []byte{0x21, 0x30} // Periodic Measure of Temp. and Hum.; 1 Measurements Per Second; High precise
	CMD_PERIOD_MEASURE_1MPS_MEDIUM  = []byte{0x21, 0x26} // Periodic Measure of Temp. and Hum.; 1 Measurements Per Second; Medium precise
	CMD_PERIOD_MEASURE_1MPS_LOW     = []byte{0x21, 0x2D} // Periodic Measure of Temp. and Hum.; 1 Measurements Per Second; Low precise
	CMD_PERIOD_MEASURE_2MPS_HIGH    = []byte{0x22, 0x36} // Periodic Measure of Temp. and Hum.; 2 Measurements Per Second; High precise
	CMD_PERIOD_MEASURE_2MPS_MEDIUM  = []byte{0x22, 0x20} // Periodic Measure of Temp. and Hum.; 2 Measurements Per Second; Medium precise
	CMD_PERIOD_MEASURE_2MPS_LOW     = []byte{0x22, 0x2B} // Periodic Measure of Temp. and Hum.; 2 Measurements Per Second; Low precise
	CMD_PERIOD_MEASURE_4MPS_HIGH    = []byte{0x23, 0x34} // Periodic Measure of Temp. and Hum.; 4 Measurements Per Second; High precise
	CMD_PERIOD_MEASURE_4MPS_MEDIUM  = []byte{0x23, 0x22} // Periodic Measure of Temp. and Hum.; 4 Measurements Per Second; Medium precise
	CMD_PERIOD_MEASURE_4MPS_LOW     = []byte{0x23, 0x29} // Periodic Measure of Temp. and Hum.; 4 Measurements Per Second; Low precise
	CMD_PERIOD_MEASURE_10MPS_HIGH   = []byte{0x27, 0x37} // Periodic Measure of Temp. and Hum.; 10 Measurements Per Second; High precise
	CMD_PERIOD_MEASURE_10MPS_MEDIUM = []byte{0x27, 0x21} // Periodic Measure of Temp. and Hum.; 10 Measurements Per Second; Medium precise
	CMD_PERIOD_MEASURE_10MPS_LOW    = []byte{0x27, 0x2A} // Periodic Measure of Temp. and Hum.; 10 Measurements Per Second; Low precise

	// Alert management commands.
	// Works in conjunction with "periodic acquisition mode".
	// Equation must be respected: HIGH SET > HIGH CLEAR > LOW CLEAR > LOW SET.
	CMD_ALERT_READ_HIGH_SET    = []byte{0xE1, 0x1F} // Read high defined level, exceeding which alert is triggered ON
	CMD_ALERT_READ_HIGH_CLEAR  = []byte{0xE1, 0x14} // Read high defined level, falling behind which alert is triggering OFF
	CMD_ALERT_READ_LOW_CLEAR   = []byte{0xE1, 0x09} // Read low defined level, exceeding which alert is triggered OFF
	CMD_ALERT_READ_LOW_SET     = []byte{0xE1, 0x02} // Read low defined level, falling behind which alert is triggering ON
	CMD_ALERT_WRITE_HIGH_SET   = []byte{0x61, 0x1D} // Write high level, exceeding which alert is triggered ON
	CMD_ALERT_WRITE_HIGH_CLEAR = []byte{0x61, 0x16} // Write high level, falling behind which alert is triggering OFF
	CMD_ALERT_WRITE_LOW_CLEAR  = []byte{0x61, 0x0B} // Write low level, exceeding which alert is triggered OFF
	CMD_ALERT_WRITE_LOW_SET    = []byte{0x61, 0x00} // Write low level, falling behind which alert is triggering ON

	// Heater management commands.
	CMD_ENABLE_HEATER  = []byte{0x30, 0x6D} // Switch heater on
	CMD_DISABLE_HEATER = []byte{0x30, 0x66} // Switch heater off

	// Status register commands.
	CMD_READ_STATUS_REG  = []byte{0xF3, 0x2D} // Read status register
	CMD_CLEAR_STATUS_REG = []byte{0x30, 0x41} // Clear status register

	// Other commands.
	CMD_PERIOD_FETCH = []byte{0xE0, 0x00} // Read data after being measured by periodic acquisition mode command
	CMD_ART          = []byte{0x2B, 0x32} // Activate "accelerated response time"
	CMD_BREAK        = []byte{0x30, 0x93} // Interrupt "periodic acqusition mode" and return to "single shot mode"
	CMD_RESET        = []byte{0x30, 0xA2} // Soft reset command
)

Command byte's sequences

Functions

This section is empty.

Types

type MeasureRepeatability

type MeasureRepeatability int

MeasureRepeatability used to define measure precision.

const (
	RepeatabilityLow    MeasureRepeatability = iota + 1 // Low precision
	RepeatabilityMedium                                 // Medium precision
	RepeatabilityHigh                                   // High precision
)

func (MeasureRepeatability) GetMeasureTime

func (v MeasureRepeatability) GetMeasureTime() time.Duration

GetMeasureTime define how long to wait for the measure process to complete according to specification.

func (MeasureRepeatability) String

func (v MeasureRepeatability) String() string

String define stringer interface.

type PeriodicMeasure

type PeriodicMeasure int

PeriodicMeasure identify pause between subsequent measures in "periodic data acquisition" mode.

const (
	PeriodicHalfMPS PeriodicMeasure = iota + 1 // 1 measurement per each 2 seconds
	Periodic1MPS                               // 1 measurement per second
	Periodic2MPS                               // 2 measurements per second
	Periodic4MPS                               // 4 measurements per second
	Periodic10MPS                              // 10 measurements per second
)

func (PeriodicMeasure) GetWaitDuration

func (v PeriodicMeasure) GetWaitDuration() time.Duration

GetWaitDuration identify pause between measures depending on PeriodicMeasure value.

func (PeriodicMeasure) String

func (v PeriodicMeasure) String() string

String define stringer interface.

type SHT3X

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

SHT3X is a sensor itself.

func NewSHT3X

func NewSHT3X() *SHT3X

NewSHT3X return new sensor instance.

func (*SHT3X) Break

func (v *SHT3X) Break(i2c *i2c.I2C) error

Break interrupt "periodic data acquisition mode" and return sensor to "single shot mode".

func (*SHT3X) CheckCommandFailed

func (v *SHT3X) CheckCommandFailed(i2c *i2c.I2C) (bool, error)

CheckCommandFailed return last command status: failed (true) or not (false).

func (*SHT3X) CheckResetDetected

func (v *SHT3X) CheckResetDetected(i2c *i2c.I2C) (bool, error)

CheckResetDetected return system reset detected : found (true) or not (false).

func (*SHT3X) CheckWrittenChecksumIsIncorrect

func (v *SHT3X) CheckWrittenChecksumIsIncorrect(i2c *i2c.I2C) (bool, error)

CheckWrittedChecksumIsIncorrect return last command status: not correct (true) correct (false).

func (*SHT3X) FetchTemperatureAndRelativeHumidity

func (v *SHT3X) FetchTemperatureAndRelativeHumidity(i2c *i2c.I2C) (temp float32, hum float32, err error)

FetchTemperatureAndRelativeHumidity wait for uncompensated temperature and humidity values and convert them to float values (Celsius and related humidity).

func (*SHT3X) FetchTemperatureAndRelativeHumidityWithContext

func (v *SHT3X) FetchTemperatureAndRelativeHumidityWithContext(parent context.Context,
	i2c *i2c.I2C) (temp float32, hum float32, err error)

FetchTemperatureAndRelativeHumidityWithContext wait for uncompensated temperature and humidity values and convert them to float values (Celsius and related humidity). Use context parameter, since operation is time consuming (can take up to 2 seconds, waiting for results).

func (*SHT3X) FetchUncompTemperatureAndHumidity

func (v *SHT3X) FetchUncompTemperatureAndHumidity(i2c *i2c.I2C) (ut uint16, uh uint16, err error)

FetchUncompTemperatureAndHumidity return uncompensated temperature and humidity obtained from sensor.

func (*SHT3X) FetchUncompTemperatureAndHumidityWithContext

func (v *SHT3X) FetchUncompTemperatureAndHumidityWithContext(parent context.Context,
	i2c *i2c.I2C) (ut uint16, uh uint16, err error)

FetchUncompTemperatureAndHumidityWithContext return uncompensated temperature and humidity obtained from sensor. Use context parameter, since operation is time consuming (can take up to 2 seconds, waiting for results).

func (*SHT3X) GetAlertPendingStatus

func (v *SHT3X) GetAlertPendingStatus(i2c *i2c.I2C) (bool, error)

GetAlertPendingStatus return alert pending status: found (true) or not (false).

func (*SHT3X) GetHeaterStatus

func (v *SHT3X) GetHeaterStatus(i2c *i2c.I2C) (bool, error)

GetHeaterStatus return heater status: enabled (true) or disabled (false).

func (*SHT3X) GetHumidityAlertStatus

func (v *SHT3X) GetHumidityAlertStatus(i2c *i2c.I2C) (bool, error)

GetHumidityAlertStatus return humidity alert pending status: found (true) or not (false).

func (*SHT3X) GetTemperatureAlertStatus

func (v *SHT3X) GetTemperatureAlertStatus(i2c *i2c.I2C) (bool, error)

GetTemperatureAlertStatus return humidity alert pending status: found (true) or not (false).

func (*SHT3X) ReadAlertHighClear

func (v *SHT3X) ReadAlertHighClear(i2c *i2c.I2C) (float32, float32, error)

ReadAlertHighClear read sensor alert HIGH CLEAR limits for temperature and humidity.

func (*SHT3X) ReadAlertHighSet

func (v *SHT3X) ReadAlertHighSet(i2c *i2c.I2C) (float32, float32, error)

ReadAlertHighSet read sensor alert HIGH SET limits for temperature and humidity.

func (*SHT3X) ReadAlertLowClear

func (v *SHT3X) ReadAlertLowClear(i2c *i2c.I2C) (float32, float32, error)

ReadAlertLowClear read sensor alert LOW CLEAR limits for temperature and humidity.

func (*SHT3X) ReadAlertLowSet

func (v *SHT3X) ReadAlertLowSet(i2c *i2c.I2C) (float32, float32, error)

ReadAlertLowSet read sensor alert LOW SET limits for temperature and humidity.

func (*SHT3X) ReadStatusReg

func (v *SHT3X) ReadStatusReg(i2c *i2c.I2C) (uint16, error)

ReadStatusReg return status register flags. You should use constants of type StatusRegFlag to distinguish individual states received from sensor.

func (*SHT3X) ReadTemperatureAndRelativeHumidity

func (v *SHT3X) ReadTemperatureAndRelativeHumidity(i2c *i2c.I2C,
	precision MeasureRepeatability) (float32, float32, error)

ReadTemperatureAndRelativeHumidity returns humidity and temperature obtained from sensor in "single shot mode".

func (*SHT3X) ReadUncompTemperatureAndHumidity

func (v *SHT3X) ReadUncompTemperatureAndHumidity(i2c *i2c.I2C,
	precision MeasureRepeatability) (uint16, uint16, error)

ReadUncompTemperatureAndHumidity returns uncompensated humidity and temperature obtained from sensor in "single shot mode".

func (*SHT3X) Reset

func (v *SHT3X) Reset(i2c *i2c.I2C) error

Reset reboot a sensor.

func (*SHT3X) SetHeaterStatus

func (v *SHT3X) SetHeaterStatus(i2c *i2c.I2C, enableHeater bool) error

SetHeaterStatus enable or disable heater.

func (*SHT3X) StartPeriodicTemperatureAndHumidityMeasure

func (v *SHT3X) StartPeriodicTemperatureAndHumidityMeasure(i2c *i2c.I2C,
	period PeriodicMeasure, precision MeasureRepeatability) error

StartPeriodicTemperatureAndHumidityMeasure send command to the sensor to start continuous measurement process of temperature and humidity with the pace defined by period parameter. Measurement process should be interrupted by Break command. Use Fetch... methods to read results.

func (*SHT3X) WriteAlertHighClear

func (v *SHT3X) WriteAlertHighClear(i2c *i2c.I2C, temp, hum float32) error

WriteAlertHighClear write alert HIGH CLEAR limits for temperature and humidity to the sensor.

func (*SHT3X) WriteAlertHighSet

func (v *SHT3X) WriteAlertHighSet(i2c *i2c.I2C, temp, hum float32) error

WriteAlertHighSet write alert HIGH SET limits for temperature and humidity to the sensor.

func (*SHT3X) WriteAlertLowClear

func (v *SHT3X) WriteAlertLowClear(i2c *i2c.I2C, temp, hum float32) error

WriteAlertLowClear write alert LOW CLEAR limits for temperature and humidity to the sensor.

func (*SHT3X) WriteAlertLowSet

func (v *SHT3X) WriteAlertLowSet(i2c *i2c.I2C, temp, hum float32) error

WriteAlertLowSet write alert LOW SET limits for temperature and humidity to the sensor.

type StatusRegFlag

type StatusRegFlag uint16

StatusRegFlag determine sensor states. It shows various sensor pending events and returns heater status.

const (
	ALERT_PENDING         StatusRegFlag = 0x8000
	HEATER_ENABLED        StatusRegFlag = 0x2000
	HUMIDITY_ALERT        StatusRegFlag = 0x0800
	TEMPERATURE_ALERT     StatusRegFlag = 0x0400
	RESET_DETECTED        StatusRegFlag = 0x0010
	COMMAND_FAILED        StatusRegFlag = 0x0002
	WRITE_DATA_CRC_FAILED StatusRegFlag = 0x0001
)

func (StatusRegFlag) String

func (v StatusRegFlag) String() string

String define stringer interface.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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