sgp30

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

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

Go to latest
Published: Aug 10, 2021 License: MIT Imports: 5 Imported by: 0

README

go-sgp30

A Go library to read eCO2 (Equivalent Carbon Dioxide) and TVOC (Total Volatile Organic Compounds) sensor data from a SGP30 Sensirion Gas Platform digital sensor.

A huge amount of this code is based on ataboo's library however this one is compatible with the go-i2c library, simplifies a few things, and adds support for setting the humidity compensation.

Implementation checked against documentation here https://cdn.sos.sk/productdata/b2/66/3af4ba1f/sgp30.pdf

This was written against the I2C interface of a Raspberry Pi using the github.com/d2r2/go-i2c library although it is not a dependency - you just have to meet the following interface:

type Bus interface {
	ReadBytes(buf []byte) (int, error)
	WriteBytes(buf []byte) (int, error)
}
Example usage
package main

import (
	"log"

	"github.com/g-wilson/go-sgp30"

	"github.com/d2r2/go-i2c"
	"github.com/kr/pretty"
)

const sensorAddress = 0x10

func main() {
	bus, err := i2c.NewI2C(sensorAddress, 1)
	if err != nil {
		log.Fatal(err)
	}
	defer bus.Close()

	sensor := sgp30.New(bus)
		.WithHumidity(0)

	err = sensor.Start()
	if err != nil {
		log.Fatal(err)
	}

	for {
		select {
		case <-time.Tick(time.Second):
			m, err := sensor.Measure()
			if err != nil {
				log.Println(err)
			} else {
				log.Printf("sgp30 measurement: eco2: %v tvoc: %v\n", m.ECO2, m.TVOC)
			}

		case <-time.Tick(time.Hour):
			m, err := sensor.GetBaseline()
			if err != nil {
				log.Println(err)
			} else {
				log.Printf("sgp30 baseline: eco2: %v tvoc: %v\n", m.ECO2, m.TVOC)
			}

		}
}

Documentation

Index

Constants

View Source
const (
	GetBaseline          uint16 = 0x2015
	GetFeatureSetVersion uint16 = 0x202f
	GetSerialID          uint16 = 0x3682
	InitAirQuality       uint16 = 0x2003
	MeasureAirQuality    uint16 = 0x2008
	SetBaseline          uint16 = 0x201e
	SetHumidity          uint16 = 0x2061
	MeasureRawSignals    uint16 = 0x2050
	MeasureTest          uint16 = 0x2032
)

Variables

View Source
var ValidFeaturesets = []uint16{0x0020, 0x0022}

ValidFeaturesets are the supported featuresets of the SGP30.

Functions

This section is empty.

Types

type Bus

type Bus interface {
	ReadBytes(buf []byte) (int, error)
	WriteBytes(buf []byte) (int, error)
}

Bus is the interface for I2C communication.

type Measurement

type Measurement struct {
	ECO2 uint16
	TVOC uint16
}

Measurement encapsulates air quality readings from the sensor.

type SGP30Sensor

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

SGP30Sensor provides methods to interact with the sensor.

func New

func New(i2cbus Bus) *SGP30Sensor

New creates a Sensor and validates the connection.

func (*SGP30Sensor) GetBaseline

func (s *SGP30Sensor) GetBaseline() (Measurement, error)

GetBaseline returns a measurement representing the baseline readings. > For best performance and faster startup times, the current baseline needs to be persistently stored before power off and set again accordingly after boot up. > Approximately in the first 60min of operation after init the call will fail unless a previous baseline was restored.

func (*SGP30Sensor) GetFeatureSet

func (s *SGP30Sensor) GetFeatureSet() (uint16, error)

GetFeatureSet returns the featureset value directly from the sensor.

func (*SGP30Sensor) GetSerial

func (s *SGP30Sensor) GetSerial() (uint64, error)

GetSerial returns the ID value directly from the sensor.

func (*SGP30Sensor) InitAirQuality

func (s *SGP30Sensor) InitAirQuality() (err error)

InitAirQuality prepares the sensor for taking measurements. > Call init to initialize or re-initialize the indoor air quality algorithm. > Call init to reset all SGP baselines. The initialization takes up to around 15 seconds, during which measurements will not change.

func (*SGP30Sensor) Measure

func (s *SGP30Sensor) Measure() (Measurement, error)

Measure returns air quality measurements, using the most recently sampled value if available. Otherwise, directly measures from the sensor.

func (*SGP30Sensor) SetBaseline

func (s *SGP30Sensor) SetBaseline(ECO2 uint16, TVOC uint16) error

SetBaseline sets the baseline values of the sensor > The baseline value must be exactly as returned by get_baseline and should only be set if it’s less than one week old.

func (*SGP30Sensor) SetHumidity

func (s *SGP30Sensor) SetHumidity(absHumidity uint16) error

SetHumidity configures the humidity compensation. Set to zero to disable. > Call set_humidity to a value greater than 0 and smaller than 256000 mg/m3 to enable the humidity compensation feature, or write 0 to disable it. > The absolute humidity in g/m3 can be retrieved by measuring the relative humidity and temperature and converting the value to absolute humidity > With AH in g/m3, RH in 0-100%, and t in °C > Note: the value in g/m3 has to be multiplied by 1000 to convert to mg/m3 and any remaining decimal places have to be rounded and removed since the interface does not support floating point numbers. > Note: The humidity compensation is disabled by setting the value to 0.

func (*SGP30Sensor) Start

func (s *SGP30Sensor) Start() error

Start intialises the sensor according to the manufacturer documentation, including reseting the baseline compensation algorithm and polling at the recommended 1 second interval.

For the first 15s the sensor is in an initialization phase during which measurements return fixed values of 400 ppm CO2eq and 0 ppb TVOC.

Use `.New().WithHumidity()` and `.WithBaseline()` to set compensation values immediately after init.

> If no stored baseline is available after initializing the baseline algorithm, the sensor has to run for 12 hours until the baseline can be stored. > This will ensure an optimal behavior for preceding startups. Reading out the baseline prior should be avoided unless a valid baseline is restored first. > Once the baseline is properly initialized or restored, the current baseline value should be stored approximately once per hour. > While the sensor is off, baseline values are valid for a maximum of seven days.

func (*SGP30Sensor) Stop

func (s *SGP30Sensor) Stop()

Stop ends sampling. Restarting sampling requires re-initialisation of the baseline compensation, so you probably don't want to do this.

func (*SGP30Sensor) WithBaseline

func (s *SGP30Sensor) WithBaseline(bl Measurement) *SGP30Sensor

WithBaseline restores the baseline to a previous known value in the init proccess. Calling this after `Start()` will have no effect until the next time `Start()` is called. Call `GetBseline()` every hour after an initial 12 hour calibration phase to obtain the value. > After a power-up or soft reset, the baseline of the baseline correction algorithm can be restored by sending first an > “Init_air_quality” command followed by a “Set_baseline” command

func (*SGP30Sensor) WithHumidity

func (s *SGP30Sensor) WithHumidity(hm uint16) *SGP30Sensor

WithHumidity sets the humidity compensation value in the init proccess. Calling this after `Start()` will have no effect until the next time `Start()` is called. > Call set_humidity to a value greater than 0 and smaller than 256000 mg/m3 to enable the humidity compensation feature, or write 0 to disable it. Changes in humidity can be provided using the `.SetHumidity()` method during sampling.

Jump to

Keyboard shortcuts

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