简介
之前手头上有小熊派的开发板, 有一个E53_IA1模块, 刚好用到树莓派上, 使用GO进行控制,实现智慧农业模块功能。
模块介绍
模块电路介绍
按硬件分成五块, 其中四块在本次用上了, 分别是
1. 补光模块(LED灯)
2. SHT30(温湿度传感器)
3. BH1750 光照强度传感器
4. 电机部分
5. AT24C02(EEPROM, 本次没用上)
注意: 除了电机和LED部分, 其余都在同一个I2C总线上。
树莓派接线
代码
1. SHT30部分
Go实现树莓派I2C读取SHT30温度&湿度传感器
2. bh1750 部分
Go实现树莓派读取bh1750光照强度
3. LED部分
package sensor
import (
"periph.io/x/conn/v3/gpio"
"periph.io/x/conn/v3/gpio/gpioreg"
)
type LEDSensor struct {
pin gpio.PinIO
onLevel gpio.Level
offLevel gpio.Level
}
func NewLEDSensor(pinName string, highIsOn bool) (*LEDSensor, error) {
var (
sensor = &LEDSensor{}
err error
)
if err = sensor.init(pinName, highIsOn); nil != err {
return nil, err
}
return sensor, nil
}
func (sensor *LEDSensor) init(pinName string, highIsOn bool) (err error) {
sensor.pin = gpioreg.ByName(pinName)
if nil == sensor.pin {
return SensorPinCantFoundError
}
if highIsOn {
sensor.onLevel = gpio.High
sensor.offLevel = gpio.Low
} else {
sensor.onLevel = gpio.Low
sensor.offLevel = gpio.High
}
///* 默认关灯 */
//if err = sensor.pin.Out(sensor.offLevel); nil != err {
// return
//}
return
}
func (sensor *LEDSensor) GetState() OnOffState {
//level := sensor.pin.Read()
if sensor.onLevel == sensor.pin.Read() {
return OnOffState_ON
}
return OnOffState_OFF
}
func (sensor *LEDSensor) SetState(state OnOffState) (err error) {
if OnOffState_ON == state {
err = sensor.pin.Out(sensor.onLevel)
} else {
err = sensor.pin.Out(sensor.offLevel)
}
return
}
func (sensor *LEDSensor) Toggle() (err error) {
if sensor.GetState() == OnOffState_ON {
err = sensor.SetState(OnOffState_OFF)
} else {
err = sensor.SetState(OnOffState_ON)
}
return
}
4. 电机部分
package sensor
import (
"fmt"
"periph.io/x/conn/v3/gpio"
"periph.io/x/conn/v3/gpio/gpioreg"
)
type MotorSensor struct {
opt MotorOption
pin gpio.PinOut
currDutyCycle uint8
oned bool
}
func NewMotorSensor(opt MotorOption) (*MotorSensor, error) {
var (
sensor = &MotorSensor{
opt: opt,
currDutyCycle: opt.DutyCycle,
}
)
if opt.DutyCycle > 100 {
return nil, fmt.Errorf("duty clycle %s", OutOfRangeError.Error())
}
sensor.pin = gpioreg.ByName(opt.PinNum.String())
if nil == sensor.pin {
return nil, CantFindPinError
}
return sensor, nil
}
func (sensor *MotorSensor) Init() error {
return nil
}
func (sensor *MotorSensor) Destroy() error {
return nil
}
func (sensor *MotorSensor) Control(isOpen bool) (err error) {
if isOpen == sensor.oned {
return
}
err = sensor.control(isOpen)
if nil != err {
return
}
sensor.oned = isOpen
return
}
func (sensor *MotorSensor) control(isOpen bool) (err error) {
if isOpen {
err = sensor.pin.Out(gpio.High)
//err = sensor.pin.PWM(gpio.DutyMax/gpio.Duty((100-sensor.currDutyCycle)+1), physic.Hertz*physic.Frequency(sensor.opt.Frequency))
} else {
err = sensor.pin.Out(gpio.Low)
err = sensor.pin.Halt()
}
return
}
func (sensor *MotorSensor) SetDutyCycle(duty uint8) error {
//return UnsupportedError
if 100 < duty {
return fmt.Errorf("duty clycle %s", OutOfRangeError.Error())
}
if sensor.currDutyCycle == duty {
return nil
}
sensor.currDutyCycle = duty
if sensor.oned {
if err := sensor.control(sensor.oned); nil != err {
return err
}
}
return nil
}