Documentation
¶
Overview ¶
Package zh07 provides a driver for Winsen ZH06 and ZH07 laser dust sensors.
The ZH06 and ZH07 are laser dust sensor modules used to check air quality by measuring particulate matter concentrations (PM1.0, PM2.5, and PM10).
This package supports two communication modes:
- Initiative upload mode: The sensor continuously broadcasts readings
- Question and answer mode: Readings are requested on demand
Example usage:
// Create a sensor instance for Q&A mode
sensor := zh07.NewZH07q(&zh07.Config{RW: rw})
if err := sensor.Init(); err != nil {
log.Fatal(err)
}
// Read sensor data
reading, err := sensor.Read()
if err != nil {
log.Fatal(err)
}
fmt.Printf("PM1.0: %d, PM2.5: %d, PM10: %d\n", reading.PM1, reading.PM25, reading.PM10)
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrChecksumMismatch is returned when the calculated checksum doesn't match the received checksum ErrChecksumMismatch = errors.New("checksum mismatch") // ErrInvalidFrame is returned when the received data frame is invalid ErrInvalidFrame = errors.New("invalid data frame") // ErrSensorCommunication is returned when communication with the sensor fails ErrSensorCommunication = errors.New("sensor communication failed") )
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// RW is the ReadWriter interface for communicating with the sensor
RW *bufio.ReadWriter
}
Config holds configuration options for sensor instances.
type Reading ¶
type Reading struct {
PM1 int // Mass Concentration PM1.0 [μg/m³]
PM25 int // Mass Concentration PM2.5 [μg/m³]
PM10 int // Mass Concentration PM10 [μg/m³]
}
Reading represents a sensor reading with particulate matter concentrations.
type SensorInterface ¶
type SensorInterface interface {
// Init initializes the sensor and sets the communication mode
Init() error
// CalculateChecksum computes the checksum for data validation
CalculateChecksum() int
// IsReadingValid checks if the received data has a valid checksum
IsReadingValid() bool
// Read returns a sensor reading or an error
Read() (*Reading, error)
}
SensorInterface defines the common interface for ZH07 sensors.
type ZH07i ¶
type ZH07i struct {
// contains filtered or unexported fields
}
ZH07i implements the SensorInterface for initiative upload mode. In this mode, the sensor continuously broadcasts readings.
func (*ZH07i) CalculateChecksum ¶
CalculateChecksum calculates the checksum from the payload. The checksum is calculated by adding all the first 30 bytes of the data received; the last 2 bytes are the checksum.
func (*ZH07i) IsReadingValid ¶
IsReadingValid checks if the calculated checksum matches the payload checksum.
type ZH07q ¶
type ZH07q struct {
// contains filtered or unexported fields
}
ZH07q implements the SensorInterface for question and answer mode. In this mode, readings are requested on demand.
func (*ZH07q) CalculateChecksum ¶
CalculateChecksum calculates the checksum from the payload.
func (*ZH07q) IsReadingValid ¶
IsReadingValid checks if the calculated checksum matches the payload checksum.
Pinout
