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 ¶
- type Dev
- func (d *Dev) GetFeatures() (uint8, uint8, error)
- func (d *Dev) GetSerialNumber() (uint64, error)
- func (d *Dev) Halt() error
- func (d *Dev) ReadAirQuality() (uint16, uint16, error)
- func (d *Dev) ReadBaseline() ([6]byte, error)
- func (d *Dev) SetBaseline(baseline []byte) error
- func (d *Dev) StartMeasurements() error
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 ¶
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 ¶
GetFeatures returns the 8 bit product type, and 8 bit product version
func (*Dev) GetSerialNumber ¶
GetSerialNumber returns the 48 bit serial number of the device
func (*Dev) ReadAirQuality ¶
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 ¶
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 ¶
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 ¶
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