Giter Site home page Giter Site logo

btscale's Introduction

Go package to facilitate data access and management of Bluetooth-based scales

Github Release GoDoc Go Report Card Build/Test Status

This package allows to extract structured data from bluetooth-based remote scales and provides a management interface to control said devices. Usage is fairly trivial (see examples directory for a simple console logger implementation and a tool for controlling basic functions).

Features

  • Control of basic settings (via multiple interfaces)
    • Status
    • Battery level
    • Weight unit
    • Measurement precision
    • Buzzer
  • Extraction of scale data (both channel and handler concept supported)
    • Timestamp
    • Weight / Unit
  • Timer functionality
  • REST API wrapper (optional) to support remote interaction with scale functions

Installation

go get -u github.com/fako1024/btscale

API summary

The following API interfaces / methods are exposed:

// Basic denotes a basic coffee scale
type Basic interface {

	// ConnectionStatus returns the current connection status of the scale device
	ConnectionStatus() ConnectionStatus

	// BatteryLevel returns the current battery level
	BatteryLevel() float64

	// BatteryLevelRaw returns the current battery level in its raw form
	BatteryLevelRaw() int

	// Unit returns the current weight unit
	Unit() Unit

	// SetUnit sets the weight unit
	SetUnit(unit Unit) error

	// Tare tares the scale
	Tare() error

	// TogglePrecision toggles the weight precision between 0.1 and 0.01
	TogglePrecision() error

	// SetStateChangeHandler defines a handler function that is called upon state change
	SetStateChangeHandler(fn func(status ConnectionStatus))

	// SetStateChangeChannel defines a handler function that is called upon state change
	SetStateChangeChannel(ch chan ConnectionStatus)

	// SetDataHandler defines a handler function that is called upon retrieval of data
	SetDataHandler(fn func(data DataPoint))

	// SetDataChannel defines a handler function that is called upon retrieval of data
	SetDataChannel(ch chan DataPoint)

	// Close terminates the connection to the device
	Close() error
}

// Buzzer denotes audible signaling functionality
type Buzzer interface {

	// IsBuzzingOnTouch returs if the buzzer  (on user interaction) is on / off
	IsBuzzingOnTouch() bool

	// ToggleBuzzingOnTouch turns the buzzer (on user interaction) on / off
	ToggleBuzzingOnTouch() error

	// Buzz requests the scale to beep / buzz n times
	Buzz(n int) error
}

// Timer denotes timer / stopwatch functionality
type Timer interface {

	// StartTimer starts the timer / stopwatch
	StartTimer() error

	// StopTimer stops the timer / stopwatch
	StopTimer() error

	// ResetTimer resets the timer / stopwatch
	ResetTimer() error

	// ElapsedTime returns the current timer value
	ElapsedTime() time.Duration
}

// WithTimer denotes a scale with timer functionality
type WithTimer interface {
	Basic
	Timer
}

// WithBuzzer denotes a scale with buzzer functionality
type WithBuzzer interface {
	Basic
	Buzzer
}

// Scale denotes the "default" scale containing all functionality
type Scale interface {
	Basic
	Buzzer
	Timer
}

Example

// Initialize a simple logger for convenience
log := logrus.StandardLogger()

// Initialize a new Felicita bluetooth scale
s, err := felicita.New()
if err != nil {
	log.Fatalf("Error opening Felicita scale: %s", err)
}

// Start up the REST API on port 8090 (all interfaces)
api.New(s, ":8090")

// Set a data channel to continuously log incoming data
dataChan := make(chan scale.DataPoint, 256)
s.SetDataChannel(dataChan)

// Setup a state handler to notify upon connection status change
s.SetStateChangeHandler(func(status scale.ConnectionStatus) {
	log.Warnf("State change: %v", status)
})

// Setup a signal channel to gracefully disconnect the bluetooth device upon termination
sigChan := make(chan os.Signal)
signal.Notify(sigChan, syscall.SIGTERM)
signal.Notify(sigChan, os.Interrupt)
go func() {
	<-sigChan
	log.Infof("Got signal, terminating connection to device")
	s.Close()
	os.Exit(0)
}()

for v := range dataChan {
	log.Warnf("Read DATA from Channel: %v, %v, %v, %v, %v", v, s.ConnectionStatus(), s.BatteryLevel(), s.IsBuzzingOnTouch(), s.ElapsedTime())
}

For additional examples please refer to the cmd folder.

btscale's People

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

btscale's Issues

Getting started with the Felicita scale

Howdee fako1024,

I got a Felicita myself and I have been trying to connect with it using bluetooth. I started trying to connect with web bluetooth from within a javascript page but so far I have not much luck as clearly you need some service UUIDs or something like that.
Then I stumbled upon your solution and thought to give that a try. Unfortunately, eventhough I have a zillion years of dev experience, I don't know anything about Go. So, before I dive into that and learn Go I wanted to first see if I can get your stuff to work quickly on my Mac just to see if your stuff does connect with my Arc. But I got stuck. Installed Go (go version go1.22.1 darwin/arm64) and then I tried go get -u github.com/fako1024/btscale as you wrote in the readme but that immediately gave me some errors as this is cleary deprecated or something like that. I did go install ...@latest and that seemed to work. But then I'm stuck. What should I do then?
I tried go run scaletool and that seemed to get some packages but then... absolute silence (on the command line). Same with logger.

Could you get me started a bit before I need to learn Go? Also, have you tried to use javascript to connect to the scale?

Kind regards, and hopefully we can connect a bit around this topic. You can also mail me directly (address in my github profile).

Cheers and already thanks for the work you already put into this,
Danny

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.