mirror of
https://github.com/cmur2/miflorad.git
synced 2026-04-02 16:18:34 +02:00
wep
This commit is contained in:
109
common.go
Normal file
109
common.go
Normal file
@@ -0,0 +1,109 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
"github.com/currantlabs/gatt"
|
||||||
|
)
|
||||||
|
|
||||||
|
type VersionBatteryResponse struct {
|
||||||
|
firmware_version string // as "x.y.z"
|
||||||
|
battery_level 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
|
||||||
|
}
|
||||||
|
|
||||||
|
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")
|
||||||
|
|
||||||
|
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 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)
|
||||||
|
if mifloraVersionBatteryChar == nil {
|
||||||
|
return VersionBatteryResponse{}, errors.New("Failed to get the version battery characteristic")
|
||||||
|
}
|
||||||
|
|
||||||
|
bytes, err := p.ReadCharacteristic(mifloraVersionBatteryChar)
|
||||||
|
if err != nil {
|
||||||
|
return VersionBatteryResponse{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return VersionBatteryResponse{string(bytes[2:]), uint8(bytes[0])}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
if mifloraModeChangeChar == nil {
|
||||||
|
return errors.New("Failed to discover the mode change characteristic")
|
||||||
|
}
|
||||||
|
|
||||||
|
err := p.WriteCharacteristic(mifloraModeChangeChar, mifloraGetModeChangeData(), false)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
if mifloraSensorDataChar == nil {
|
||||||
|
return SensorDataResponse{}, errors.New("Failed to discover the sensor data characteristic")
|
||||||
|
}
|
||||||
|
|
||||||
|
bytes, err := p.ReadCharacteristic(mifloraSensorDataChar)
|
||||||
|
if err != nil {
|
||||||
|
return SensorDataResponse{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
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]),
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
129
main.go
129
main.go
@@ -1,10 +1,8 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@@ -16,21 +14,20 @@ import (
|
|||||||
const discoveryTimeout = 4 * time.Second
|
const discoveryTimeout = 4 * time.Second
|
||||||
const connectionTimeout = 4 * time.Second
|
const connectionTimeout = 4 * time.Second
|
||||||
|
|
||||||
var mifloraModeChangeData = []byte{0xa0, 0x1f}
|
type DiscoveryResult struct {
|
||||||
|
p gatt.Peripheral
|
||||||
|
a *gatt.Advertisement
|
||||||
|
rssi int
|
||||||
|
}
|
||||||
|
|
||||||
var mifloraServiceUUID = gatt.MustParseUUID("00001204-0000-1000-8000-00805f9b34fb")
|
var discoveryDone = make(chan DiscoveryResult)
|
||||||
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{})
|
var connectionDone = make(chan struct{})
|
||||||
|
|
||||||
func onStateChanged(device gatt.Device, state gatt.State) {
|
func onStateChanged(device gatt.Device, state gatt.State) {
|
||||||
fmt.Println("State:", state)
|
fmt.Fprintln(os.Stderr, "State:", state)
|
||||||
switch state {
|
switch state {
|
||||||
case gatt.StatePoweredOn:
|
case gatt.StatePoweredOn:
|
||||||
fmt.Println("Scanning...")
|
fmt.Fprintln(os.Stderr, "Scanning...")
|
||||||
device.Scan([]gatt.UUID{}, false)
|
device.Scan([]gatt.UUID{}, false)
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
@@ -40,7 +37,7 @@ func onStateChanged(device gatt.Device, state gatt.State) {
|
|||||||
|
|
||||||
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())
|
// fmt.Fprintln(os.Stderr, p.ID())
|
||||||
if strings.ToUpper(p.ID()) != id {
|
if strings.ToUpper(p.ID()) != id {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -48,98 +45,77 @@ 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()
|
||||||
|
|
||||||
discoveryDone <- p
|
discoveryDone <- DiscoveryResult{p, a, rssi}
|
||||||
}
|
|
||||||
|
|
||||||
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) {
|
||||||
fmt.Println("Connected")
|
fmt.Fprintln(os.Stderr, "Connected")
|
||||||
defer p.Device().CancelConnection(p)
|
// defer p.Device().CancelConnection(p)
|
||||||
|
|
||||||
if err := p.SetMTU(500); err != nil {
|
if err := p.SetMTU(500); err != nil {
|
||||||
fmt.Printf("Failed to set MTU, err: %s\n", err)
|
fmt.Fprintf(os.Stderr, "Failed to set MTU, err: %s\n", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Discovery services
|
// Discover services and characteristics
|
||||||
services, err := p.DiscoverServices([]gatt.UUID{mifloraServiceUUID})
|
{
|
||||||
|
_, err := p.DiscoverServices([]gatt.UUID{mifloraServiceUUID})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Failed to discover services, err: %s\n", err)
|
fmt.Fprintf(os.Stderr, "Failed to discover services, err: %s\n", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if len(services) == 1 {
|
for _, service := range p.Services() {
|
||||||
mifloraService := findServiceByUUID(services, mifloraServiceUUID)
|
_, err := p.DiscoverCharacteristics(nil, service)
|
||||||
|
|
||||||
chars, err := p.DiscoverCharacteristics(nil, mifloraService)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Failed to discover characteristics, err: %s\n", err)
|
fmt.Fprintf(os.Stderr, "Failed to discover characteristics, err: %s\n", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
mifloraVersionBatteryChar := findCharacteristicByUUID(chars, mifloraCharVersionBatteryUUID)
|
metaData, err := mifloraRequestVersionBattery(p)
|
||||||
bytes, err := p.ReadCharacteristic(mifloraVersionBatteryChar)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Failed to read characteristic, err: %s\n", err)
|
fmt.Fprintf(os.Stderr, "Failed to request version battery, err: %s\n", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Printf("Battery level: %d%%\n", uint8(bytes[0]))
|
fmt.Fprintf(os.Stdout, "Battery level: %d%%\n", metaData.battery_level)
|
||||||
fmt.Printf("Firmware version: %s\n", string(bytes[2:]))
|
fmt.Fprintf(os.Stdout, "Firmware version: %s\n", metaData.firmware_version)
|
||||||
|
|
||||||
// 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 string(bytes[2:]) >= "2.6.6" {
|
if metaData.firmware_version >= "2.6.6" {
|
||||||
mifloraModeChangeChar := findCharacteristicByUUID(chars, mifloraCharModeChangeUUID)
|
err2 := mifloraRequestModeChange(p)
|
||||||
err2 := p.WriteCharacteristic(mifloraModeChangeChar, mifloraModeChangeData, false)
|
|
||||||
if err2 != nil {
|
if err2 != nil {
|
||||||
fmt.Printf("Failed to write characteristic, err: %s\n", err2)
|
fmt.Fprintf(os.Stderr, "Failed to request mode change, err: %s\n", err2)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mifloraSensorDataChar := findCharacteristicByUUID(chars, mifloraCharReadSensorDataUUID)
|
sensorData, err3 := mifloraRequstSensorData(p)
|
||||||
bytes2, err3 := p.ReadCharacteristic(mifloraSensorDataChar)
|
|
||||||
if err3 != nil {
|
if err3 != nil {
|
||||||
fmt.Printf("Failed to read characteristic, err: %s\n", err3)
|
fmt.Fprintf(os.Stderr, "Failed to request sensor data, err: %s\n", err3)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Printf("Temparature: %f °C\n", float64(binary.LittleEndian.Uint16(bytes2[0:2]))/10.0)
|
fmt.Fprintf(os.Stdout, "Temparature: %.1f °C\n", sensorData.temperature)
|
||||||
fmt.Printf("Brightness: %d lux\n", binary.LittleEndian.Uint32(bytes2[3:7]))
|
fmt.Fprintf(os.Stdout, "Brightness: %d lux\n", sensorData.brightness)
|
||||||
fmt.Printf("Moisture: %d %%\n", uint8(bytes2[7]))
|
fmt.Fprintf(os.Stdout, "Moisture: %d %%\n", sensorData.moisture)
|
||||||
fmt.Printf("Conductivity: %d µS/cm\n", binary.LittleEndian.Uint16(bytes2[8:10]))
|
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) {
|
||||||
fmt.Println("Disconnected")
|
fmt.Fprintln(os.Stderr, "Disconnected")
|
||||||
close(connectionDone)
|
close(connectionDone)
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
if len(flag.Args()) != 1 {
|
if len(flag.Args()) != 1 {
|
||||||
log.Fatalf("usage: %s [options] peripheral-id\n", os.Args[0])
|
fmt.Fprintf(os.Stderr, "Usage: %s [options] peripheral-id\n", os.Args[0])
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
device, 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)
|
fmt.Fprintf(os.Stderr, "Failed to open device, err: %s\n", err)
|
||||||
return
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Register discovery handler
|
// Register discovery handler
|
||||||
@@ -147,19 +123,19 @@ func main() {
|
|||||||
|
|
||||||
device.Init(onStateChanged)
|
device.Init(onStateChanged)
|
||||||
|
|
||||||
var peripheral gatt.Peripheral
|
var discoveryResult DiscoveryResult
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case peripheral = <-discoveryDone:
|
case discoveryResult = <-discoveryDone:
|
||||||
fmt.Println("Discovery done")
|
fmt.Fprintln(os.Stderr, "Discovery done")
|
||||||
case <-time.After(discoveryTimeout):
|
case <-time.After(discoveryTimeout):
|
||||||
// fmt.Println("Discovery timed out")
|
fmt.Fprintln(os.Stderr, "Discovery timed out")
|
||||||
log.Fatalf("Discovery timed out\n")
|
|
||||||
device.StopScanning()
|
device.StopScanning()
|
||||||
device.Stop()
|
device.Stop()
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Discovered peripheral ID:%s, NAME:(%s)\n", peripheral.ID(), peripheral.Name())
|
fmt.Fprintf(os.Stderr, "Discovered peripheral ID:%s, NAME:(%s), RSSI:%d\n", discoveryResult.p.ID(), discoveryResult.p.Name(), discoveryResult.rssi)
|
||||||
|
|
||||||
// Register connection handlers
|
// Register connection handlers
|
||||||
device.Handle(
|
device.Handle(
|
||||||
@@ -167,14 +143,19 @@ func main() {
|
|||||||
gatt.PeripheralDisconnected(onPeriphDisconnected),
|
gatt.PeripheralDisconnected(onPeriphDisconnected),
|
||||||
)
|
)
|
||||||
|
|
||||||
device.Connect(peripheral)
|
device.Connect(discoveryResult.p)
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-connectionDone:
|
case <-connectionDone:
|
||||||
fmt.Println("Connection done")
|
fmt.Fprintln(os.Stderr, "Connection done")
|
||||||
case <-time.After(connectionTimeout):
|
case <-time.After(connectionTimeout):
|
||||||
fmt.Println("Connection timed out")
|
fmt.Fprintln(os.Stderr, "Connection timed out")
|
||||||
|
fmt.Fprintln(os.Stderr, "A")
|
||||||
|
// device.CancelConnection(discoveryResult.p)
|
||||||
|
fmt.Fprintln(os.Stderr, "B")
|
||||||
}
|
}
|
||||||
|
|
||||||
device.Stop()
|
fmt.Fprintln(os.Stderr, "C")
|
||||||
|
// device.Stop()
|
||||||
|
fmt.Fprintln(os.Stderr, "D")
|
||||||
}
|
}
|
||||||
|
|||||||
4
notes.md
4
notes.md
@@ -6,6 +6,10 @@ select-attribute /org/bluez/hci0/dev_C4_7C_8D_66_D5_27/service0002/char0003
|
|||||||
select-attribute 00002a00-0000-1000-8000-00805f9b34fb
|
select-attribute 00002a00-0000-1000-8000-00805f9b34fb
|
||||||
read 0003
|
read 0003
|
||||||
|
|
||||||
|
hcitool dev
|
||||||
|
https://bbs.archlinux.org/viewtopic.php?id=193813
|
||||||
|
modprobe -r btusb; modprobe btusb
|
||||||
|
|
||||||
08:38:52::$ gatttool --device=C4:7C:8D:66:D5:27 --char-read -a 0x03
|
08:38:52::$ gatttool --device=C4:7C:8D:66:D5:27 --char-read -a 0x03
|
||||||
Characteristic value/descriptor: 46 6c 6f 77 65 72 20 63 61 72 65
|
Characteristic value/descriptor: 46 6c 6f 77 65 72 20 63 61 72 65
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user