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-04 21:11:24 +01:00
parent 4a3cd53cf3
commit 6bd43f6414

208
main.go
View File

@@ -1,6 +1,7 @@
package main package main
import ( import (
"encoding/binary"
"flag" "flag"
"fmt" "fmt"
"log" "log"
@@ -12,22 +13,34 @@ import (
"github.com/currantlabs/gatt/examples/option" "github.com/currantlabs/gatt/examples/option"
) )
var done = make(chan struct{}) const discoveryTimeout = 4 * time.Second
const connectionTimeout = 4 * time.Second
func onStateChanged(d gatt.Device, s gatt.State) { var mifloraModeChangeData = []byte{0xa0, 0x1f}
fmt.Println("State:", s)
switch s { 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 discoveryDone = make(chan gatt.Peripheral)
var connectionDone = make(chan struct{})
func onStateChanged(device gatt.Device, state gatt.State) {
fmt.Println("State:", state)
switch state {
case gatt.StatePoweredOn: case gatt.StatePoweredOn:
fmt.Println("Scanning...") fmt.Println("Scanning...")
d.Scan([]gatt.UUID{}, false) device.Scan([]gatt.UUID{}, false)
return return
default: default:
d.StopScanning() device.StopScanning()
} }
} }
func onPeriphDiscovered(p gatt.Peripheral, a *gatt.Advertisement, rssi int) { func onPeriphDiscovered(p gatt.Peripheral, a *gatt.Advertisement, rssi int) {
id := strings.ToUpper(flag.Args()[0]) id := strings.ToUpper(flag.Args()[0])
fmt.Println(p.ID())
if strings.ToUpper(p.ID()) != id { if strings.ToUpper(p.ID()) != id {
return return
} }
@@ -35,14 +48,25 @@ func onPeriphDiscovered(p gatt.Peripheral, a *gatt.Advertisement, rssi int) {
// Stop scanning once we've got the peripheral we're looking for. // Stop scanning once we've got the peripheral we're looking for.
p.Device().StopScanning() p.Device().StopScanning()
fmt.Printf("\nPeripheral ID:%s, NAME:(%s)\n", p.ID(), p.Name()) discoveryDone <- p
fmt.Println(" Local Name =", a.LocalName) }
fmt.Println(" TX Power Level =", a.TxPowerLevel)
fmt.Println(" Manufacturer Data =", a.ManufacturerData)
fmt.Println(" Service Data =", a.ServiceData)
fmt.Println("")
p.Device().Connect(p) func findServiceByUUID(services []*gatt.Service, u gatt.UUID) *gatt.Service {
for _, service := range services {
if service.UUID().Equal(u) {
return service
}
}
return nil
}
func findCharacteristicByUUID(characteristics []*gatt.Characteristic, u gatt.UUID) *gatt.Characteristic {
for _, characteristic := range characteristics {
if characteristic.UUID().Equal(u) {
return characteristic
}
}
return nil
} }
func onPeriphConnected(p gatt.Peripheral, err error) { func onPeriphConnected(p gatt.Peripheral, err error) {
@@ -54,117 +78,56 @@ func onPeriphConnected(p gatt.Peripheral, err error) {
} }
// Discovery services // Discovery services
ss, err := p.DiscoverServices(nil) services, err := p.DiscoverServices([]gatt.UUID{mifloraServiceUUID})
if err != nil { if err != nil {
fmt.Printf("Failed to discover services, err: %s\n", err) fmt.Printf("Failed to discover services, err: %s\n", err)
return return
} }
for _, s := range ss { if len(services) == 1 {
msg := "Service: " + s.UUID().String() mifloraService := findServiceByUUID(services, mifloraServiceUUID)
if len(s.Name()) > 0 {
msg += " (" + s.Name() + ")"
}
fmt.Println(msg)
if s.UUID().String() != "0000120400001000800000805f9b34fb" { chars, err := p.DiscoverCharacteristics(nil, mifloraService)
fmt.Printf("Skipping uninteresting service\n")
continue
}
// Discovery characteristics
cs, err := p.DiscoverCharacteristics(nil, s)
if err != nil { if err != nil {
fmt.Printf("Failed to discover characteristics, err: %s\n", err) fmt.Printf("Failed to discover characteristics, err: %s\n", err)
continue return
} }
for _, c := range cs { mifloraVersionBatteryChar := findCharacteristicByUUID(chars, mifloraCharVersionBatteryUUID)
msg := " Characteristic " + c.UUID().String() bytes, err := p.ReadCharacteristic(mifloraVersionBatteryChar)
if len(c.Name()) > 0 {
msg += " (" + c.Name() + ")"
}
fmt.Println(msg)
if c.UUID().String() == "00001a0000001000800000805f9b34fb" {
err := p.WriteCharacteristic(c, []byte{0xa0, 0x1f}, false)
if err != nil {
fmt.Printf("Failed to write characteristic, err: %s\n", err)
continue
}
}
if c.UUID().String() == "00001a0200001000800000805f9b34fb" {
b, err := p.ReadCharacteristic(c)
if err != nil { if err != nil {
fmt.Printf("Failed to read characteristic, err: %s\n", err) fmt.Printf("Failed to read characteristic, err: %s\n", err)
continue return
}
fmt.Printf("Battery level: %d%%\n", uint8(bytes[0]))
fmt.Printf("Firmware version: %s\n", string(bytes[2:]))
// for the newer models a magic number must be written before we can read the current data
if string(bytes[2:]) >= "2.6.6" {
mifloraModeChangeChar := findCharacteristicByUUID(chars, mifloraCharModeChangeUUID)
err2 := p.WriteCharacteristic(mifloraModeChangeChar, mifloraModeChangeData, false)
if err2 != nil {
fmt.Printf("Failed to write characteristic, err: %s\n", err2)
return
} }
// fmt.Printf(" value %x | %q\n", b, b)
fmt.Printf(" battery level: %d%%\n", b[0])
fmt.Printf(" firmware version: %s\n", string(b[2:]))
} }
if c.UUID().String() == "00001a0100001000800000805f9b34fb" { mifloraSensorDataChar := findCharacteristicByUUID(chars, mifloraCharReadSensorDataUUID)
b, err := p.ReadCharacteristic(c) bytes2, err3 := p.ReadCharacteristic(mifloraSensorDataChar)
if err != nil { if err3 != nil {
fmt.Printf("Failed to read characteristic, err: %s\n", err) fmt.Printf("Failed to read characteristic, err: %s\n", err3)
continue return
} }
fmt.Printf(" temparature: %f °C\n", float64((int32(b[1]) << 8) + int32(b[0])) / 10.0) fmt.Printf("Temparature: %f °C\n", float64(binary.LittleEndian.Uint16(bytes2[0:2]))/10.0)
fmt.Printf(" brightness: %d lux\n", (int32(b[6]) << 24) + (int32(b[5]) << 16) + (int32(b[4]) << 8) + int32(b[3])) fmt.Printf("Brightness: %d lux\n", binary.LittleEndian.Uint32(bytes2[3:7]))
fmt.Printf(" moisture: %d %%\n", int32(b[7])) fmt.Printf("Moisture: %d %%\n", uint8(bytes2[7]))
fmt.Printf(" conductivity: %d µS/cm\n", (int32(b[9]) << 8) + int32(b[8])) fmt.Printf("Conductivity: %d µS/cm\n", binary.LittleEndian.Uint16(bytes2[8:10]))
} }
fmt.Printf("Skipping uninteresting char\n")
continue
// // Discovery descriptors
// ds, err := p.DiscoverDescriptors(nil, c)
// if err != nil {
// fmt.Printf("Failed to discover descriptors, err: %s\n", err)
// continue
// }
//
// for _, d := range ds {
// msg := " Descriptor " + d.UUID().String()
// if len(d.Name()) > 0 {
// msg += " (" + d.Name() + ")"
// }
// fmt.Println(msg)
//
// // Read descriptor (could fail, if it's not readable)
// b, err := p.ReadDescriptor(d)
// if err != nil {
// fmt.Printf("Failed to read descriptor, err: %s\n", err)
// continue
// }
// fmt.Printf(" value %x | %q\n", b, b)
// }
//
// // Subscribe the characteristic, if possible.
// if (c.Properties() & (gatt.CharNotify | gatt.CharIndicate)) != 0 {
// f := func(c *gatt.Characteristic, b []byte, err error) {
// fmt.Printf("notified: % X | %q\n", b, b)
// }
// if err := p.SetNotifyValue(c, f); err != nil {
// fmt.Printf("Failed to subscribe characteristic, err: %s\n", err)
// continue
// }
// }
}
fmt.Println()
}
fmt.Printf("Waiting for 5 seconds to get some notifiations, if any.\n")
time.Sleep(5 * time.Second)
} }
func onPeriphDisconnected(p gatt.Peripheral, err error) { func onPeriphDisconnected(p gatt.Peripheral, err error) {
fmt.Println("Disconnected") fmt.Println("Disconnected")
close(done) close(connectionDone)
} }
func main() { func main() {
@@ -173,20 +136,45 @@ func main() {
log.Fatalf("usage: %s [options] peripheral-id\n", os.Args[0]) log.Fatalf("usage: %s [options] peripheral-id\n", os.Args[0])
} }
d, err := gatt.NewDevice(option.DefaultClientOptions...) device, err := gatt.NewDevice(option.DefaultClientOptions...)
if err != nil { if err != nil {
log.Fatalf("Failed to open device, err: %s\n", err) log.Fatalf("Failed to open device, err: %s\n", err)
return return
} }
// Register handlers. // Register discovery handler
d.Handle( device.Handle(gatt.PeripheralDiscovered(onPeriphDiscovered))
gatt.PeripheralDiscovered(onPeriphDiscovered),
device.Init(onStateChanged)
var peripheral gatt.Peripheral
select {
case peripheral = <-discoveryDone:
fmt.Println("Discovery done")
case <-time.After(discoveryTimeout):
// fmt.Println("Discovery timed out")
log.Fatalf("Discovery timed out\n")
device.StopScanning()
device.Stop()
}
fmt.Printf("Discovered peripheral ID:%s, NAME:(%s)\n", peripheral.ID(), peripheral.Name())
// Register connection handlers
device.Handle(
gatt.PeripheralConnected(onPeriphConnected), gatt.PeripheralConnected(onPeriphConnected),
gatt.PeripheralDisconnected(onPeriphDisconnected), gatt.PeripheralDisconnected(onPeriphDisconnected),
) )
d.Init(onStateChanged) device.Connect(peripheral)
<-done
fmt.Println("Done") select {
case <-connectionDone:
fmt.Println("Connection done")
case <-time.After(connectionTimeout):
fmt.Println("Connection timed out")
}
device.Stop()
} }