motedem

package module
v0.0.0-...-286de71 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 31, 2019 License: MIT Imports: 8 Imported by: 0

README

This SDK is for a BLE device "MOTEDEM" and target to work on Raspberry Pi

Install

$ go get github.com/pkg/errors
$ go get github.com/go-ble/ble
import "github.com/artgrouplimited/motedem-sdk-go"

Usage

Basic setup

// Store all device connection
var devices map[string]*motedem.Device

func main() {
    // Setup on the Bluetooth LE
    // It must be call before anything BLE action
    motedem.BLESetup()

    devices = make(map[string]*motedem.Device, 0)

    // do your magic here

    c := make(chan os.Signal, 1)
    signal.Notify(c, os.Interrupt)

    // Waiting system signal such like Ctrl + C
    <-c
    for _, d := range devices {
        // Disconnect all devices to have a graceful exit
        d.Disconnect()
    }
}

Declare device

device := motedem.NewDevice("8c:14:7d:00:00:00")
Field Name Type Description
MAC string MAC address of motedem
Connected bool Is it connecting
Timeout time.Duration operation timeout

Connect and Disconnect

if err := device.Connect(); err != nil {
    // connect fail
}
if err := device.DisConnect(); err != nil {
    // disconnect fail
}

Get temperature

c, err := device.GetTemperature()
if err != nil {
    // err.Error()
    return
}

select {
case <-time.After(device.Timeout):
    // "Request timeout"
    return
case sensorData := <-c:
    fmt.Printf("Temperature: %f\n", sensorData.Temperature)
}
Temperature Result
Field Name Type Description
Temperature float64 current temperature data of the sensor

Learn IR Code

c, err := device.LearnAV()
// or 
c, err := device.LearnAC()
if err != nil {
    // err.Error()
    return
}

select {
case <-time.After(device.Timeout):
    // "Request timeout"
    return
case learnData := <-c:
    if learnData.Success {
        if learnData.HaveData {
            fmt.Printf("Learn Data: %s\n", learnData.Data)
        } else {

        }
    } else {
        fmt.Printf("Learn failed\n")
    }
}
LearnData
Field Name Type Description
Success bool is learn operation success
HaveData bool is there any data learnt
Data string learn data in hex string
Emit LearnData

You find can few of sample learn data at here

LearnData := ""
if err := device.EmitData(LearnData); err != nil {
    // err.Error()
    return
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BLESetup

func BLESetup() error

BLESetup must have to call at start of program

Types

type Device

type Device struct {
	MAC string

	Connected bool

	Timeout time.Duration
	// contains filtered or unexported fields
}

Device presenting device after scan

func NewDevice

func NewDevice(MAC string) *Device

NewDevice function

func (*Device) AddControlNotification

func (d *Device) AddControlNotification(c *notificationCallback)

AddControlNotification function

func (*Device) AddDataNotification

func (d *Device) AddDataNotification(c *notificationCallback)

AddDataNotification function

func (*Device) Connect

func (d *Device) Connect() error

Connect a BLE device

func (*Device) Disconnect

func (d *Device) Disconnect() error

Disconnect a device

func (*Device) DisconnectSync

func (d *Device) DisconnectSync() error

DisconnectSync function

func (*Device) Discover

func (d *Device) Discover() error

Discover profile

func (*Device) EmitData

func (d *Device) EmitData(irData string) error

EmitData function

func (*Device) GetTemperature

func (d *Device) GetTemperature() (chan SensorData, error)

GetTemperature function

func (*Device) LearnAC

func (d *Device) LearnAC() (chan LearnData, error)

LearnAC function

func (*Device) LearnAV

func (d *Device) LearnAV() (chan LearnData, error)

LearnAV function

type LearnData

type LearnData struct {
	Success  bool
	HaveData bool
	Data     string
}

LearnData struct

type ScanResult

type ScanResult struct {
	MAC  string `json:"MAC"`
	RSSI int    `json:"RSSI"`
	Name string `json:"name"`
}

ScanResult struct

func ScanDevice

func ScanDevice(duration int) ([]ScanResult, error)

ScanDevice BLE scan for a duration in Millisecond

type SensorData

type SensorData struct {
	Success     bool
	HaveData    bool
	Temperature float64
	Humidity    float64
}

SensorData struct

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL