air-sensors

Air Quality Sensor library
git clone https://www.brianlane.com/git/air-sensors
Log | Files | Refs | README | LICENSE

example_test.go (1340B)


      1 // Copyright 2020 by Brian C. Lane <bcl@brianlane.com>. All rights reserved.
      2 // Use of this source code is governed under the Apache License, Version 2.0
      3 // that can be found in the LICENSE file.
      4 
      5 package pmsa003i_test
      6 
      7 import (
      8 	"fmt"
      9 	"log"
     10 	"time"
     11 
     12 	"periph.io/x/periph/conn/i2c/i2creg"
     13 	"periph.io/x/periph/host"
     14 
     15 	"github.com/bcl/air-sensors/pmsa003i"
     16 )
     17 
     18 func Example() {
     19 	// Make sure periph is initialized.
     20 	if _, err := host.Init(); err != nil {
     21 		log.Fatal(err)
     22 	}
     23 
     24 	// Open a handle to the first available I²C bus:
     25 	bus, err := i2creg.Open("")
     26 	if err != nil {
     27 		log.Fatal(err)
     28 	}
     29 	defer bus.Close()
     30 
     31 	d, err := pmsa003i.New(bus)
     32 	if err != nil {
     33 		log.Fatal(err)
     34 	}
     35 
     36 	for {
     37 		time.Sleep(1 * time.Second)
     38 
     39 		// Read the PMSA003i sensor data
     40 		r, err := d.ReadSensor()
     41 		if err != nil {
     42 			// Checksum failures could be transient
     43 			fmt.Println(err)
     44 		} else {
     45 			fmt.Println()
     46 			fmt.Printf("PM1.0  %3d μg/m3\n", r.EnvPm1)
     47 			fmt.Printf("PM2.5  %3d μg/m3\n", r.EnvPm2_5)
     48 			fmt.Printf("PM10   %3d μg/m3\n", r.EnvPm10)
     49 			fmt.Println("Counters in 0.1L of air")
     50 			fmt.Printf("%d > 0.3μm\n", r.Cnt0_3)
     51 			fmt.Printf("%d > 0.5μm\n", r.Cnt0_5)
     52 			fmt.Printf("%d > 1.0μm\n", r.Cnt1)
     53 			fmt.Printf("%d > 2.5μm\n", r.Cnt2_5)
     54 			fmt.Printf("%d > 5.0μm\n", r.Cnt5)
     55 			fmt.Printf("%d > 10μm\n", r.Cnt10)
     56 			fmt.Println()
     57 		}
     58 	}
     59 }