测试通过ibeacon识别

This commit is contained in:
suguo.yao 2021-12-21 16:22:49 +08:00
parent c08a167ccc
commit c813ec827b
3 changed files with 18 additions and 84 deletions

View File

@ -18,3 +18,12 @@ release: arm
ssh pi@192.168.0.21 "sudo systemctl stop ble.service" ssh pi@192.168.0.21 "sudo systemctl stop ble.service"
scp ./ble-ibeacon pi@192.168.0.21:~/ scp ./ble-ibeacon pi@192.168.0.21:~/
ssh pi@192.168.0.21 "sudo systemctl start ble.service" ssh pi@192.168.0.21 "sudo systemctl start ble.service"
.PHONY: release2
release2: arm
ssh pi@192.168.0.28 "sudo systemctl stop ble.service"
scp ./ble-ibeacon pi@192.168.0.28:~/
ssh pi@192.168.0.28 "sudo systemctl start ble.service"
release2: arm
scp ./ble-ibeacon pi@192.168.0.28:~/

View File

@ -16,6 +16,8 @@ func main() {
fmt.Printf("%v\n", cpu) fmt.Printf("%v\n", cpu)
fmt.Println(cpu.JSONString(true))
for _, proc := range cpu.Processors { for _, proc := range cpu.Processors {
fmt.Printf(" %v\n", proc) fmt.Printf(" %v\n", proc)
for _, core := range proc.Cores { for _, core := range proc.Cores {

89
main.go
View File

@ -6,14 +6,10 @@ import (
"errors" "errors"
"fmt" "fmt"
"log" "log"
"strconv"
"strings" "strings"
"time"
"github.com/influxdata/influxdb/client/v2"
"github.com/paypal/gatt" "github.com/paypal/gatt"
"github.com/paypal/gatt/examples/option" "github.com/paypal/gatt/examples/option"
"myschools.me/suguo/ble-ibeacon/influx"
) )
type IBeancon struct { type IBeancon struct {
@ -33,48 +29,14 @@ func NewiBeacon(data []byte) (*IBeancon, error) {
return beacon, nil return beacon, nil
} }
var (
chn chan *client.Point
)
func init() {
chn = make(chan *client.Point, 500)
}
func onPerhipheralDiscovered(p gatt.Peripheral, a *gatt.Advertisement, rssi int) { func onPerhipheralDiscovered(p gatt.Peripheral, a *gatt.Advertisement, rssi int) {
// if p.ID() != "78:2C:29:16:F2:78" { b, err := NewiBeacon(a.ManufacturerData)
// return if err == nil {
// } fmt.Println("UUID:", b.Uuid)
// if p.ID() != "90:98:38:F5:DA:60" { fmt.Println("Major:", b.Major)
// return fmt.Println("Minor:", b.Minor)
// } fmt.Println("RSSI:", rssi)
// if p.ID() != "E4:78:41:19:10:98" {
// return
// }
// if !(p.ID() == "C6:1A:05:02:06:C0" || p.ID() == "90:98:38:F5:DA:60") {
// return
// }
// fmt.Println("NAME:", a.LocalName, "T:", time.Now(), "MAC:", p.ID(), "RSSI:", rssi, "LEN:", len(a.ManufacturerData))
tags := make(map[string]string)
tags["mac"] = p.ID()
fields := make(map[string]interface{})
fields["name"] = a.LocalName
fields["rssi"] = rssi
fields["pl"] = strconv.Itoa(len(a.ManufacturerData)) //包长度
fields["power"] = a.TxPowerLevel
point, err := client.NewPoint(
"band",
tags,
fields,
time.Now(),
)
if err != nil {
log.Fatalln(err.Error())
} }
chn <- point
} }
func onStateChanged(device gatt.Device, s gatt.State) { func onStateChanged(device gatt.Device, s gatt.State) {
@ -88,51 +50,12 @@ func onStateChanged(device gatt.Device, s gatt.State) {
} }
func main() { func main() {
influx.Init(&influx.Config{
Address: "http://127.0.0.1:8086",
DBName: "wristband",
Username: "abc",
Password: "abc",
Timeout: 3,
})
// if err := influx.CreateDB("wristband", 10); err != nil {
// log.Fatalln("influx.CreateDB", err.Error())
// }
go func() {
db, err := influx.New()
if err != nil {
log.Fatalln(err.Error())
}
for {
bp, err := client.NewBatchPoints(client.BatchPointsConfig{
Database: "wristband",
Precision: "ms",
})
if err != nil {
log.Fatalln(err.Error())
}
for i := 1; i < 50; i++ {
if d, ok := <-chn; ok {
bp.AddPoint(d)
}
}
if err := db.Write(bp); err != nil {
log.Println("db.Write: ", err.Error())
}
}
}()
dev, err := gatt.NewDevice(option.DefaultClientOptions...) dev, 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)
} }
dev.Handle( dev.Handle(
gatt.PeripheralDiscovered(onPerhipheralDiscovered), gatt.PeripheralDiscovered(onPerhipheralDiscovered),
gatt.CentralConnected(func(c gatt.Central) { fmt.Println("Connect: ", c.ID()) }),
gatt.CentralDisconnected(func(c gatt.Central) { fmt.Println("Disconnect: ", c.ID()) }),
) )
dev.Init(onStateChanged) dev.Init(onStateChanged)
select {} select {}