1
0
mirror of https://github.com/cmur2/miflorad.git synced 2026-04-02 16:18:34 +02:00
This commit is contained in:
cn
2018-12-07 14:22:59 +01:00
parent 70e02ae11d
commit 5efa9af573
3 changed files with 42 additions and 39 deletions

1
.gitignore vendored
View File

@@ -1 +1,2 @@
mifloractl
miflorad miflorad

View File

@@ -7,6 +7,8 @@ import (
"strings" "strings"
"time" "time"
"miflorad/common"
"github.com/currantlabs/gatt" "github.com/currantlabs/gatt"
"github.com/currantlabs/gatt/examples/option" "github.com/currantlabs/gatt/examples/option"
) )
@@ -58,7 +60,7 @@ func onPeriphConnected(p gatt.Peripheral, err error) {
// Discover services and characteristics // Discover services and characteristics
{ {
_, err := p.DiscoverServices([]gatt.UUID{mifloraServiceUUID}) _, err := p.DiscoverServices([]gatt.UUID{common.MifloraServiceUUID})
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "Failed to discover services, err: %s\n", err) fmt.Fprintf(os.Stderr, "Failed to discover services, err: %s\n", err)
return return
@@ -72,32 +74,32 @@ func onPeriphConnected(p gatt.Peripheral, err error) {
} }
} }
metaData, err := mifloraRequestVersionBattery(p) metaData, err := common.MifloraRequestVersionBattery(p)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "Failed to request version battery, err: %s\n", err) fmt.Fprintf(os.Stderr, "Failed to request version battery, err: %s\n", err)
return return
} }
fmt.Fprintf(os.Stdout, "Battery level: %d%%\n", metaData.battery_level) fmt.Fprintf(os.Stdout, "Battery level: %d%%\n", metaData.BatteryLevel)
fmt.Fprintf(os.Stdout, "Firmware version: %s\n", metaData.firmware_version) 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 // for the newer models a magic number must be written before we can read the current data
if metaData.firmware_version >= "2.6.6" { if metaData.FirmwareVersion >= "2.6.6" {
err2 := mifloraRequestModeChange(p) err2 := common.MifloraRequestModeChange(p)
if err2 != nil { if err2 != nil {
fmt.Fprintf(os.Stderr, "Failed to request mode change, err: %s\n", err2) fmt.Fprintf(os.Stderr, "Failed to request mode change, err: %s\n", err2)
return return
} }
} }
sensorData, err3 := mifloraRequstSensorData(p) sensorData, err3 := common.MifloraRequstSensorData(p)
if err3 != nil { if err3 != nil {
fmt.Fprintf(os.Stderr, "Failed to request sensor data, err: %s\n", err3) fmt.Fprintf(os.Stderr, "Failed to request sensor data, err: %s\n", err3)
return return
} }
fmt.Fprintf(os.Stdout, "Temparature: %.1f °C\n", sensorData.temperature) fmt.Fprintf(os.Stdout, "Temparature: %.1f °C\n", sensorData.Temperature)
fmt.Fprintf(os.Stdout, "Brightness: %d lux\n", sensorData.brightness) fmt.Fprintf(os.Stdout, "Brightness: %d lux\n", sensorData.Brightness)
fmt.Fprintf(os.Stdout, "Moisture: %d %%\n", sensorData.moisture) fmt.Fprintf(os.Stdout, "Moisture: %d %%\n", sensorData.Moisture)
fmt.Fprintf(os.Stdout, "Conductivity: %d µS/cm\n", sensorData.conductivity) fmt.Fprintf(os.Stdout, "Conductivity: %d µS/cm\n", sensorData.Conductivity)
} }
func onPeriphDisconnected(p gatt.Peripheral, err error) { func onPeriphDisconnected(p gatt.Peripheral, err error) {

View File

@@ -1,4 +1,4 @@
package main package common
import ( import (
"encoding/binary" "encoding/binary"
@@ -8,27 +8,27 @@ import (
) )
type VersionBatteryResponse struct { type VersionBatteryResponse struct {
firmware_version string // as "x.y.z" FirmwareVersion string // as "x.y.z"
battery_level uint8 // in percent 0-100 BatteryLevel uint8 // in percent 0-100
} }
type SensorDataResponse struct { type SensorDataResponse struct {
temperature float64 // in degree C Temperature float64 // in degree C
brightness uint32 // in lux Brightness uint32 // in lux
moisture uint8 // in percent 0-100 Moisture uint8 // in percent 0-100
conductivity uint16 // in µS/cm Conductivity uint16 // in µS/cm
} }
func mifloraGetModeChangeData() []byte { func MifloraGetModeChangeData() []byte {
return []byte{0xa0, 0x1f} return []byte{0xa0, 0x1f}
} }
var mifloraServiceUUID = gatt.MustParseUUID("00001204-0000-1000-8000-00805f9b34fb") var MifloraServiceUUID = gatt.MustParseUUID("00001204-0000-1000-8000-00805f9b34fb")
var mifloraCharModeChangeUUID = gatt.MustParseUUID("00001a00-0000-1000-8000-00805f9b34fb") var MifloraCharModeChangeUUID = gatt.MustParseUUID("00001a00-0000-1000-8000-00805f9b34fb")
var mifloraCharReadSensorDataUUID = gatt.MustParseUUID("00001a01-0000-1000-8000-00805f9b34fb") var MifloraCharReadSensorDataUUID = gatt.MustParseUUID("00001a01-0000-1000-8000-00805f9b34fb")
var mifloraCharVersionBatteryUUID = gatt.MustParseUUID("00001a02-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 { for _, service := range services {
if service.UUID().Equal(u) { if service.UUID().Equal(u) {
return service return service
@@ -37,7 +37,7 @@ func findServiceByUUID(services []*gatt.Service, u gatt.UUID) *gatt.Service {
return nil 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 { for _, characteristic := range characteristics {
if characteristic.UUID().Equal(u) { if characteristic.UUID().Equal(u) {
return characteristic return characteristic
@@ -46,13 +46,13 @@ func findCharacteristicByUUID(characteristics []*gatt.Characteristic, u gatt.UUI
return nil return nil
} }
func mifloraRequestVersionBattery(p gatt.Peripheral) (VersionBatteryResponse, error) { func MifloraRequestVersionBattery(p gatt.Peripheral) (VersionBatteryResponse, error) {
mifloraService := findServiceByUUID(p.Services(), mifloraServiceUUID) mifloraService := FindServiceByUUID(p.Services(), MifloraServiceUUID)
if mifloraService == nil { if mifloraService == nil {
return VersionBatteryResponse{}, errors.New("Failed to get the miflora service") return VersionBatteryResponse{}, errors.New("Failed to get the miflora service")
} }
mifloraVersionBatteryChar := findCharacteristicByUUID(mifloraService.Characteristics(), mifloraCharVersionBatteryUUID) mifloraVersionBatteryChar := FindCharacteristicByUUID(mifloraService.Characteristics(), MifloraCharVersionBatteryUUID)
if mifloraVersionBatteryChar == nil { if mifloraVersionBatteryChar == nil {
return VersionBatteryResponse{}, errors.New("Failed to get the version battery characteristic") 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 return VersionBatteryResponse{string(bytes[2:]), uint8(bytes[0])}, nil
} }
func mifloraRequestModeChange(p gatt.Peripheral) error { func MifloraRequestModeChange(p gatt.Peripheral) error {
mifloraService := findServiceByUUID(p.Services(), mifloraServiceUUID) mifloraService := FindServiceByUUID(p.Services(), MifloraServiceUUID)
if mifloraService == nil { if mifloraService == nil {
return errors.New("Failed to get the miflora service") return errors.New("Failed to get the miflora service")
} }
mifloraModeChangeChar := findCharacteristicByUUID(mifloraService.Characteristics(), mifloraCharModeChangeUUID) mifloraModeChangeChar := FindCharacteristicByUUID(mifloraService.Characteristics(), MifloraCharModeChangeUUID)
if mifloraModeChangeChar == nil { if mifloraModeChangeChar == nil {
return errors.New("Failed to discover the mode change characteristic") 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 { if err != nil {
return err return err
} }
@@ -84,13 +84,13 @@ func mifloraRequestModeChange(p gatt.Peripheral) error {
return nil return nil
} }
func mifloraRequstSensorData(p gatt.Peripheral) (SensorDataResponse, error) { func MifloraRequstSensorData(p gatt.Peripheral) (SensorDataResponse, error) {
mifloraService := findServiceByUUID(p.Services(), mifloraServiceUUID) mifloraService := FindServiceByUUID(p.Services(), MifloraServiceUUID)
if mifloraService == nil { if mifloraService == nil {
return SensorDataResponse{}, errors.New("Failed to get the miflora service") return SensorDataResponse{}, errors.New("Failed to get the miflora service")
} }
mifloraSensorDataChar := findCharacteristicByUUID(mifloraService.Characteristics(), mifloraCharReadSensorDataUUID) mifloraSensorDataChar := FindCharacteristicByUUID(mifloraService.Characteristics(), MifloraCharReadSensorDataUUID)
if mifloraSensorDataChar == nil { if mifloraSensorDataChar == nil {
return SensorDataResponse{}, errors.New("Failed to discover the sensor data characteristic") return SensorDataResponse{}, errors.New("Failed to discover the sensor data characteristic")
} }
@@ -101,9 +101,9 @@ func mifloraRequstSensorData(p gatt.Peripheral) (SensorDataResponse, error) {
} }
return SensorDataResponse{ return SensorDataResponse{
temperature: float64(binary.LittleEndian.Uint16(bytes[0:2])) / 10.0, Temperature: float64(binary.LittleEndian.Uint16(bytes[0:2])) / 10.0,
brightness: binary.LittleEndian.Uint32(bytes[3:7]), Brightness: binary.LittleEndian.Uint32(bytes[3:7]),
moisture: uint8(bytes[7]), Moisture: uint8(bytes[7]),
conductivity: binary.LittleEndian.Uint16(bytes[8:10]), Conductivity: binary.LittleEndian.Uint16(bytes[8:10]),
}, nil }, nil
} }