diff --git a/.gitignore b/.gitignore index 67aa06c..a4ef343 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ +mifloractl miflorad diff --git a/main.go b/cmd/mifloractl/main.go similarity index 88% rename from main.go rename to cmd/mifloractl/main.go index 919b713..ecfee68 100644 --- a/main.go +++ b/cmd/mifloractl/main.go @@ -7,6 +7,8 @@ import ( "strings" "time" + "miflorad/common" + "github.com/currantlabs/gatt" "github.com/currantlabs/gatt/examples/option" ) @@ -58,7 +60,7 @@ func onPeriphConnected(p gatt.Peripheral, err error) { // Discover services and characteristics { - _, err := p.DiscoverServices([]gatt.UUID{mifloraServiceUUID}) + _, err := p.DiscoverServices([]gatt.UUID{common.MifloraServiceUUID}) if err != nil { fmt.Fprintf(os.Stderr, "Failed to discover services, err: %s\n", err) return @@ -72,32 +74,32 @@ func onPeriphConnected(p gatt.Peripheral, err error) { } } - metaData, err := mifloraRequestVersionBattery(p) + metaData, err := common.MifloraRequestVersionBattery(p) if err != nil { fmt.Fprintf(os.Stderr, "Failed to request version battery, err: %s\n", err) return } - fmt.Fprintf(os.Stdout, "Battery level: %d%%\n", metaData.battery_level) - fmt.Fprintf(os.Stdout, "Firmware version: %s\n", metaData.firmware_version) + fmt.Fprintf(os.Stdout, "Battery level: %d%%\n", metaData.BatteryLevel) + fmt.Fprintf(os.Stdout, "Firmware version: %s\n", metaData.FirmwareVersion) // for the newer models a magic number must be written before we can read the current data - if metaData.firmware_version >= "2.6.6" { - err2 := mifloraRequestModeChange(p) + if metaData.FirmwareVersion >= "2.6.6" { + err2 := common.MifloraRequestModeChange(p) if err2 != nil { fmt.Fprintf(os.Stderr, "Failed to request mode change, err: %s\n", err2) return } } - sensorData, err3 := mifloraRequstSensorData(p) + sensorData, err3 := common.MifloraRequstSensorData(p) if err3 != nil { fmt.Fprintf(os.Stderr, "Failed to request sensor data, err: %s\n", err3) return } - fmt.Fprintf(os.Stdout, "Temparature: %.1f °C\n", sensorData.temperature) - fmt.Fprintf(os.Stdout, "Brightness: %d lux\n", sensorData.brightness) - fmt.Fprintf(os.Stdout, "Moisture: %d %%\n", sensorData.moisture) - fmt.Fprintf(os.Stdout, "Conductivity: %d µS/cm\n", sensorData.conductivity) + fmt.Fprintf(os.Stdout, "Temparature: %.1f °C\n", sensorData.Temperature) + fmt.Fprintf(os.Stdout, "Brightness: %d lux\n", sensorData.Brightness) + fmt.Fprintf(os.Stdout, "Moisture: %d %%\n", sensorData.Moisture) + fmt.Fprintf(os.Stdout, "Conductivity: %d µS/cm\n", sensorData.Conductivity) } func onPeriphDisconnected(p gatt.Peripheral, err error) { diff --git a/common.go b/common/common.go similarity index 51% rename from common.go rename to common/common.go index f2ee3f9..867059a 100644 --- a/common.go +++ b/common/common.go @@ -1,4 +1,4 @@ -package main +package common import ( "encoding/binary" @@ -8,27 +8,27 @@ import ( ) type VersionBatteryResponse struct { - firmware_version string // as "x.y.z" - battery_level uint8 // in percent 0-100 + FirmwareVersion string // as "x.y.z" + BatteryLevel uint8 // in percent 0-100 } type SensorDataResponse struct { - temperature float64 // in degree C - brightness uint32 // in lux - moisture uint8 // in percent 0-100 - conductivity uint16 // in µS/cm + Temperature float64 // in degree C + Brightness uint32 // in lux + Moisture uint8 // in percent 0-100 + Conductivity uint16 // in µS/cm } -func mifloraGetModeChangeData() []byte { +func MifloraGetModeChangeData() []byte { return []byte{0xa0, 0x1f} } -var mifloraServiceUUID = gatt.MustParseUUID("00001204-0000-1000-8000-00805f9b34fb") -var mifloraCharModeChangeUUID = gatt.MustParseUUID("00001a00-0000-1000-8000-00805f9b34fb") -var mifloraCharReadSensorDataUUID = gatt.MustParseUUID("00001a01-0000-1000-8000-00805f9b34fb") -var mifloraCharVersionBatteryUUID = gatt.MustParseUUID("00001a02-0000-1000-8000-00805f9b34fb") +var MifloraServiceUUID = gatt.MustParseUUID("00001204-0000-1000-8000-00805f9b34fb") +var MifloraCharModeChangeUUID = gatt.MustParseUUID("00001a00-0000-1000-8000-00805f9b34fb") +var MifloraCharReadSensorDataUUID = gatt.MustParseUUID("00001a01-0000-1000-8000-00805f9b34fb") +var MifloraCharVersionBatteryUUID = gatt.MustParseUUID("00001a02-0000-1000-8000-00805f9b34fb") -func findServiceByUUID(services []*gatt.Service, u gatt.UUID) *gatt.Service { +func FindServiceByUUID(services []*gatt.Service, u gatt.UUID) *gatt.Service { for _, service := range services { if service.UUID().Equal(u) { return service @@ -37,7 +37,7 @@ func findServiceByUUID(services []*gatt.Service, u gatt.UUID) *gatt.Service { return nil } -func findCharacteristicByUUID(characteristics []*gatt.Characteristic, u gatt.UUID) *gatt.Characteristic { +func FindCharacteristicByUUID(characteristics []*gatt.Characteristic, u gatt.UUID) *gatt.Characteristic { for _, characteristic := range characteristics { if characteristic.UUID().Equal(u) { return characteristic @@ -46,13 +46,13 @@ func findCharacteristicByUUID(characteristics []*gatt.Characteristic, u gatt.UUI return nil } -func mifloraRequestVersionBattery(p gatt.Peripheral) (VersionBatteryResponse, error) { - mifloraService := findServiceByUUID(p.Services(), mifloraServiceUUID) +func MifloraRequestVersionBattery(p gatt.Peripheral) (VersionBatteryResponse, error) { + mifloraService := FindServiceByUUID(p.Services(), MifloraServiceUUID) if mifloraService == nil { return VersionBatteryResponse{}, errors.New("Failed to get the miflora service") } - mifloraVersionBatteryChar := findCharacteristicByUUID(mifloraService.Characteristics(), mifloraCharVersionBatteryUUID) + mifloraVersionBatteryChar := FindCharacteristicByUUID(mifloraService.Characteristics(), MifloraCharVersionBatteryUUID) if mifloraVersionBatteryChar == nil { return VersionBatteryResponse{}, errors.New("Failed to get the version battery characteristic") } @@ -65,18 +65,18 @@ func mifloraRequestVersionBattery(p gatt.Peripheral) (VersionBatteryResponse, er return VersionBatteryResponse{string(bytes[2:]), uint8(bytes[0])}, nil } -func mifloraRequestModeChange(p gatt.Peripheral) error { - mifloraService := findServiceByUUID(p.Services(), mifloraServiceUUID) +func MifloraRequestModeChange(p gatt.Peripheral) error { + mifloraService := FindServiceByUUID(p.Services(), MifloraServiceUUID) if mifloraService == nil { return errors.New("Failed to get the miflora service") } - mifloraModeChangeChar := findCharacteristicByUUID(mifloraService.Characteristics(), mifloraCharModeChangeUUID) + mifloraModeChangeChar := FindCharacteristicByUUID(mifloraService.Characteristics(), MifloraCharModeChangeUUID) if mifloraModeChangeChar == nil { return errors.New("Failed to discover the mode change characteristic") } - err := p.WriteCharacteristic(mifloraModeChangeChar, mifloraGetModeChangeData(), false) + err := p.WriteCharacteristic(mifloraModeChangeChar, MifloraGetModeChangeData(), false) if err != nil { return err } @@ -84,13 +84,13 @@ func mifloraRequestModeChange(p gatt.Peripheral) error { return nil } -func mifloraRequstSensorData(p gatt.Peripheral) (SensorDataResponse, error) { - mifloraService := findServiceByUUID(p.Services(), mifloraServiceUUID) +func MifloraRequstSensorData(p gatt.Peripheral) (SensorDataResponse, error) { + mifloraService := FindServiceByUUID(p.Services(), MifloraServiceUUID) if mifloraService == nil { return SensorDataResponse{}, errors.New("Failed to get the miflora service") } - mifloraSensorDataChar := findCharacteristicByUUID(mifloraService.Characteristics(), mifloraCharReadSensorDataUUID) + mifloraSensorDataChar := FindCharacteristicByUUID(mifloraService.Characteristics(), MifloraCharReadSensorDataUUID) if mifloraSensorDataChar == nil { return SensorDataResponse{}, errors.New("Failed to discover the sensor data characteristic") } @@ -101,9 +101,9 @@ func mifloraRequstSensorData(p gatt.Peripheral) (SensorDataResponse, error) { } return SensorDataResponse{ - temperature: float64(binary.LittleEndian.Uint16(bytes[0:2])) / 10.0, - brightness: binary.LittleEndian.Uint32(bytes[3:7]), - moisture: uint8(bytes[7]), - conductivity: binary.LittleEndian.Uint16(bytes[8:10]), + Temperature: float64(binary.LittleEndian.Uint16(bytes[0:2])) / 10.0, + Brightness: binary.LittleEndian.Uint32(bytes[3:7]), + Moisture: uint8(bytes[7]), + Conductivity: binary.LittleEndian.Uint16(bytes[8:10]), }, nil }