51 lines
972 B
Go
51 lines
972 B
Go
package main
|
|
|
|
import (
|
|
bh1750 "github.com/d2r2/go-bh1750"
|
|
i2c "github.com/d2r2/go-i2c"
|
|
logger "github.com/d2r2/go-logger"
|
|
"time"
|
|
)
|
|
|
|
var lg = logger.NewPackageLogger("main",
|
|
logger.ErrorLevel,
|
|
)
|
|
|
|
func main() {
|
|
defer logger.FinalizeLogger()
|
|
|
|
logger.ChangePackageLogLevel("i2c", logger.ErrorLevel)
|
|
logger.ChangePackageLogLevel("bh1750", logger.ErrorLevel)
|
|
|
|
// Create new connection to I2C to bh1750
|
|
i2c, err := i2c.NewI2C(0x23, 1)
|
|
if err != nil {
|
|
lg.Fatal(err)
|
|
}
|
|
|
|
// Free I2C connection on exit
|
|
defer i2c.Close()
|
|
|
|
// Setup sensor
|
|
sensor := bh1750.NewBH1750()
|
|
|
|
// Reset sensor prior to use
|
|
err = sensor.Reset(i2c)
|
|
if err != nil {
|
|
lg.Fatal(err)
|
|
}
|
|
|
|
// Read sensor value over time
|
|
for {
|
|
resolution := bh1750.HighResolution
|
|
lux, err := sensor.MeasureAmbientLight(i2c, resolution)
|
|
if err != nil {
|
|
lg.Fatal(err)
|
|
}
|
|
|
|
println("Illuminance:", lux, "lx")
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
}
|
|
}
|