sgp30

package
v0.0.0-...-bd62a88 Latest Latest
Warning

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

Go to latest
Published: May 28, 2021 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package sgp30 controls a Sensiron SGP30 Gas Sensor over I²C.

Datasheet

https://cdn.sparkfun.com/assets/c/0/a/2/e/Sensirion_Gas_Sensors_SGP30_Datasheet.pdf

Example
package main

import (
	"fmt"
	"log"
	"time"

	"periph.io/x/periph/conn/i2c/i2creg"
	"periph.io/x/periph/host"

	"github.com/bcl/air-sensors/sgp30"
)

func main() {
	// Make sure periph is initialized.
	if _, err := host.Init(); err != nil {
		log.Fatal(err)
	}

	// Open a handle to the first available I²C bus:
	bus, err := i2creg.Open("")
	if err != nil {
		log.Fatal(err)
	}
	defer bus.Close()

	d, err := sgp30.New(bus, ".sgp30_baseline", 30*time.Second)
	if err != nil {
		log.Fatal(err)
	}
	defer d.Halt() //nolint

	sn, err := d.GetSerialNumber()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("Serial Number: %X\n", sn)

	// Start measuring air quality
	if err = d.StartMeasurements(); err != nil {
		log.Fatal(err)
	}

	for {
		time.Sleep(1 * time.Second)
		if co2, tvoc, err := d.ReadAirQuality(); err != nil {
			log.Fatal(err)
		} else {
			fmt.Printf("CO2 : %d ppm\nTVOC: %d ppb\n", co2, tvoc)
		}
	}
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dev

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

Dev holds the connection and error details for the device as well as the path to the baseline file and how often to save it.

func New

func New(i i2c.Bus, baselineFile string, baselineInterval time.Duration) (*Dev, error)

New returns a SGP30 device struct for communicating with the device

If baselineFile is passed the baseline calibration data will be read from the file at startup, and new data will be saved at baselineInterval interval when ReadAirQuality is called.

eg. pass 30 * time.Second to save the baseline data every 30 seconds

func (*Dev) GetFeatures

func (d *Dev) GetFeatures() (uint8, uint8, error)

GetFeatures returns the 8 bit product type, and 8 bit product version

func (*Dev) GetSerialNumber

func (d *Dev) GetSerialNumber() (uint64, error)

GetSerialNumber returns the 48 bit serial number of the device

func (*Dev) Halt

func (d *Dev) Halt() error

Halt implements conn.Resource.

func (*Dev) ReadAirQuality

func (d *Dev) ReadAirQuality() (uint16, uint16, error)

ReadAirQuality returns the CO2 and TVOC readings as 16 bit values CO2 is in ppm and TVOC is in ppb

If a baselineFile was passed to New the baseline data will be saved to disk every baselineInterval

func (*Dev) ReadBaseline

func (d *Dev) ReadBaseline() ([6]byte, error)

ReadBaseline returns the 6 data bytes for the measurement baseline These values should be saved to disk and restore using SetBaseline when the program restarts.

func (*Dev) SetBaseline

func (d *Dev) SetBaseline(baseline []byte) error

SetBaseline sets the measurement baseline data bytes The values should have been previously read from the device using ReadBaseline

NOTE: The data order for setting it is TVOC, CO2 even though the order when reading is CO2, TVOC. This assumes that the baseline data passed in is CO2, TVOC

func (*Dev) StartMeasurements

func (d *Dev) StartMeasurements() error

StartMeasurements sends the Inlet Air Quality command to start measuring ReadAirQuality needs to be called every second after this has been sent

Note that for 15s after the measurements have started the readings will return 400ppm CO2 and 0ppb TVOC

Jump to

Keyboard shortcuts

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