All Projects → ttu → Ruuvitag Sensor

ttu / Ruuvitag Sensor

Licence: mit
Python library for communicating with RuuviTag BLE Sensor Beacon and for decoding sensor data from broadcasted data

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Ruuvitag Sensor

Norepeat
The norepeat package contains some magical function, tools
Stars: ✭ 111 (-18.38%)
Mutual labels:  pypi
Swarmlib
This repository implements several swarm optimization algorithms and visualizes them. Implemented algorithms: Particle Swarm Optimization (PSO), Firefly Algorithm (FA), Cuckoo Search (CS), Ant Colony Optimization (ACO), Artificial Bee Colony (ABC), Grey Wolf Optimizer (GWO) and Whale Optimization Algorithm (WOA)
Stars: ✭ 121 (-11.03%)
Mutual labels:  pypi
Huobi
火币的行情交易的python实现
Stars: ✭ 129 (-5.15%)
Mutual labels:  pypi
Linuxacademy Dl
Download videos from Linux Academy (linuxacademy.com) for personal offline use
Stars: ✭ 111 (-18.38%)
Mutual labels:  pypi
Best Of Python
🏆 A ranked list of awesome Python open-source libraries and tools. Updated weekly.
Stars: ✭ 1,869 (+1274.26%)
Mutual labels:  pypi
Gml
Auto Data Science - Python Library.
Stars: ✭ 125 (-8.09%)
Mutual labels:  pypi
Nimble Arduino
A fork of the NimBLE library structured for compilation with Ardruino, designed for use with ESP32.
Stars: ✭ 108 (-20.59%)
Mutual labels:  bluetooth-le
Pylightxl
A light weight, zero dependency, minimal functionality excel read/writer python library
Stars: ✭ 134 (-1.47%)
Mutual labels:  pypi
Rectorch
rectorch is a pytorch-based framework for state-of-the-art top-N recommendation
Stars: ✭ 121 (-11.03%)
Mutual labels:  pypi
Exabgp
The BGP swiss army knife of networking
Stars: ✭ 1,713 (+1159.56%)
Mutual labels:  pypi
Tinvest
Тинькофф Инвестиции, tinkoff, python, aiohttp, requests, pydantic
Stars: ✭ 115 (-15.44%)
Mutual labels:  pypi
Freenom Dns Updater
A tool to update freenom's dns records
Stars: ✭ 117 (-13.97%)
Mutual labels:  pypi
Itunes Iap
Apple iTunes In-app purchase verification tool
Stars: ✭ 126 (-7.35%)
Mutual labels:  pypi
Simplebluetoothleterminal
Android terminal app for Bluetooth LE devices using custom serial profiles
Stars: ✭ 111 (-18.38%)
Mutual labels:  bluetooth-le
Box
Python dictionaries with advanced dot notation access
Stars: ✭ 1,804 (+1226.47%)
Mutual labels:  pypi
Yakutils
🐃 Yet another toolbox of Python 3 helper functions.
Stars: ✭ 111 (-18.38%)
Mutual labels:  pypi
Purerpc
Asynchronous pure Python gRPC client and server implementation supporting asyncio, uvloop, curio and trio
Stars: ✭ 125 (-8.09%)
Mutual labels:  pypi
Py3 Pinterest
Fully fledged Python Pinterest client
Stars: ✭ 133 (-2.21%)
Mutual labels:  pypi
Rx Book
xgrommx.github.io/rx-book
Stars: ✭ 1,660 (+1120.59%)
Mutual labels:  reactive-extensions
Photoshop Python Api
Python API for Photoshop.
Stars: ✭ 126 (-7.35%)
Mutual labels:  pypi

RuuviTag Sensor Python Package

Build Status PyPI

RuuviTag Sensor is a Python library for communicating with RuuviTag BLE Sensor Beacon and for decoding sensord data from broadcasted eddystone-url.

Requirements

Installation

Install latest released version

$ pip install ruuvitag_sensor

Install latest developement version

$ pip install git+https://github.com/ttu/ruuvitag-sensor
# Or clone this repository and install locally
$ pip install -e .

Full installation guide for Raspberry PI & Raspbian

Usage

RuuviTag sensors can be identified using MAC addresses.

Get sensor datas with callback

get_datas calls the callback every time when a RuuviTag sensor broadcasts data. This method is the preferred way to use the library.

from ruuvitag_sensor.ruuvi import RuuviTagSensor

def handle_data(found_data):
    print('MAC ' + found_data[0])
    print(found_data[1])

RuuviTagSensor.get_datas(handle_data)

Optional list of macs and run flag can be passed to the get_datas function. Callback is called only for macs in the list and setting run flag to false will stop execution. If run flag is not passed, function will execute forever.

from ruuvitag_sensor.ruuvi import RuuviTagSensor, RunFlag

counter = 10
# RunFlag for stopping execution at desired time
run_flag = RunFlag()

def handle_data(found_data):
    print('MAC ' + found_data[0])
    print(found_data[1])
    global counter
    counter = counter - 1
    if counter < 0:
        run_flag.running = False

# List of macs of sensors which will execute callback function
macs = ['AA:2C:6A:1E:59:3D', 'CC:2C:6A:1E:59:3D']

RuuviTagSensor.get_datas(handle_data, macs, run_flag)
Get data for specified sensors

get_data_for_sensors will collect latest data from sensors for specified duration.

from ruuvitag_sensor.ruuvi import RuuviTagSensor

# List of macs of sensors which data will be collected
# If list is empty, data will be collected for all found sensors
macs = ['AA:2C:6A:1E:59:3D', 'CC:2C:6A:1E:59:3D']
# get_data_for_sensors will look data for the duration of timeout_in_sec
timeout_in_sec = 4

datas = RuuviTagSensor.get_data_for_sensors(macs, timeout_in_sec)

# Dictionary will have lates data for each sensor
print(datas['AA:2C:6A:1E:59:3D'])
print(datas['CC:2C:6A:1E:59:3D'])

NOTE: This method shouldn't be used for a long duration with short timeout. get_data_for_sensors will start and stop a new BLE scanning process with every method call. For a long running processes it is recommended to use get_datas-method with a callback.

Get data from sensor

NOTE: For a single sensor it is recommended to use RuuviTagSensor.get_datas or RuuviTagSensor.get_data_for_sensors functions instead of RuuviTag-class.

from ruuvitag_sensor.ruuvitag import RuuviTag

sensor = RuuviTag('AA:2C:6A:1E:59:3D')

# update state from the device
state = sensor.update()

# get latest state (does not get it from the device)
state = sensor.state

print(state)
RuuviTagReactive

Reactive wrapper and background process for RuuviTagSensor get_datas. Optional MAC address list can be passed on initializer and execution can be stopped with stop function.

from ruuvitag_sensor.ruuvi_rx import RuuviTagReactive

ruuvi_rx = RuuviTagReactive()

# Print all notifications
ruuvi_rx.get_subject().\
    subscribe(print)

# Print only last data every 10 seconds for F4:A5:74:89:16:57
ruuvi_rx.get_subject().\
    filter(lambda x: x[0] == 'F4:A5:74:89:16:57').\
    buffer_with_time(10000).\
    subscribe(lambda datas: print(datas[len(datas) - 1]))

# Execute only every time when temperature changes for F4:A5:74:89:16:57
ruuvi_rx.get_subject().\
    filter(lambda x: x[0] == 'F4:A5:74:89:16:57').\
    map(lambda x: x[1]['temperature']).\
    distinct_until_changed().\
    subscribe(lambda x: print('Temperature changed: {}'.format(x)))

# Close all connections and stop bluetooth communication
ruuvi_rx.stop()

More samples and simple HTTP server under examples directory.

Check official documentation from RxPy GitHub and RxPY Public API

Find sensors

find_ruuvitags function will exeute forever and when new RuuviTag sensor is found it will print it's MAC address and state at that moment. This function can be used with a command line applications. Logging must be enabled and set to print to console.

from ruuvitag_sensor.ruuvi import RuuviTagSensor
import ruuvitag_sensor.log

ruuvitag_sensor.log.enable_console()

RuuviTagSensor.find_ruuvitags()
Using different Bluetooth device

If you have multiple Bluetooth devices installed, device to be used might not be the default (Linux: hci0). Device can be passed with bt_device parameter.

from ruuvitag_sensor.ruuvi import RuuviTagSensor
from ruuvitag_sensor.ruuvitag import RuuviTag

sensor = RuuviTag('F4:A5:74:89:16:57', 'hci1')

RuuviTagSensor.find_ruuvitags('hci1')

datas = RuuviTagSensor.get_data_for_sensors(bt_device='hci1')

RuuviTagSensor.get_datas(lambda x: print('%s - %s' % (x[0], x[1]), bt_device=device))
Parse data
from ruuvitag_sensor.data_formats import DataFormats
from ruuvitag_sensor.decoder import get_decoder

full_data = '043E2A0201030157168974A51F0201060303AAFE1716AAFE10F9037275752E76692F23416A5558314D417730C3'
data = full_data[26:]

# convert_data returns tuple which has Data Format type and encoded data
(data_format, encoded) = DataFormats.convert_data(data)

sensor_data = get_decoder(data_format).decode_data(encoded)

print(sensor_data)
# {'temperature': 25.12, 'identifier': '0', 'humidity': 26.5, 'pressure': 992.0}
Data Formats

Example data has data from 4 sensors with different firmwares.

  • 1st is Data Format 2 (URL) so identifier is None as sensor doesn't broadcast any identifier data
  • 2nd is Data Format 4 (URL) and it has an identifier character
  • 3rd is Data Format 3 (RAW)
  • 4th is Data Format 5 (RAW v2)
{
'CA:F7:44:DE:EB:E1': { 'data_format': 2, 'temperature': 22.0, 'humidity': 28.0, 'pressure': 991.0, 'identifier': None }, 
'F4:A5:74:89:16:57': { 'data_format': 4, 'temperature': 23.24, 'humidity': 29.0, 'pressure': 991.0, 'identifier': '0' },
'A3:GE:2D:91:A4:1F': { 'data_format': 3, 'battery': 2899, 'pressure': 1027.66, 'humidity': 20.5, 'acceleration': 63818.215675463696, 'acceleration_x': 200.34, 'acceleration_y': 0.512, 'acceleration_z': -200.42, 'temperature': 26.3},
'CB:B8:33:4C:88:4F': { 'data_format': 5, 'battery': 2.995, 'pressure': 1000.43, 'mac': 'cbb8334c884f', 'measurement_sequence_number': 2467, 'acceleration_z': 1028, 'acceleration': 1028.0389097694697, 'temperature': 22.14, 'acceleration_y': -8, 'acceleration_x': 4, 'humidity': 53.97, 'tx_power': 4, 'movement_counter': 70 }
}
Note on Data Format 2 and 4

There is no reason to use Data Format 2 or 4.

Original reasoning to use the URL-encoded data was to use Google's Nearby notifications to let users to view the tags without the need to install any app. Since the Nearby has been discontinued there isn't any benefit in using Eddystone format anymore.

Logging and Print to console

Logging can be enabled by importing ruuvitag_sensor.log. Console print can be enabled by calling ruuvitag_sensor.log.enable_console(). Command line application has console logging enabled by default.

from ruuvitag_sensor.ruuvi import RuuviTagSensor
import ruuvitag_sensor.log

ruuvitag_sensor.log.enable_console()

datas = RuuviTagSensor.get_data_for_sensors()

print(datas)
Command line application
$ python ruuvitag_sensor -h

usage: ruuvitag_sensor [-h] [-g MAC_ADDRESS] [-d BT_DEVICE] [-f] [-l] [-s] [--version]

optional arguments:
  -h, --help            show this help message and exit
  -g MAC_ADDRESS, --get MAC_ADDRESS
                        Get data
  -d BT_DEVICE, --device BT_DEVICE
                        Set Bluetooth device id (default hci0)
  -f, --find            Find broadcasting RuuviTags
  -l, --latest          Get latest data for found RuuviTags
  -s, --stream          Stream broadcasts from all RuuviTags
  --version             show program's version number and exit

BlueZ

BlueZ works only on Linux. Windows and macOS supports are only for testing and url decoding.

BlueZ tools require superuser rights.

Install BlueZ.

$ sudo apt-get install bluez bluez-hcidump

Ruuvitag_sensor package uses internally hciconfig, hcitool and hcidump. These tools are deprecated. In case tools are missing, older version of BlueZ is required (Issue)

BlueZ limitations

The ruuvitag-sensor use BlueZ to listen broadcasted BL information (uses hciconf, hcitool, hcidump). Implementation does not handle well unexpected errors or changes, e.g. when adapter is busy, rebooted or powered down.

In case of errors, application tries to exit immediately, so it can be automatically restarted.

Bleson

Current state and known bugs in issue #78.

Bleson works with Linux, macOS and partially with Windows.

Requires Python 3.

Bleson is not installed automatically with ruuvitag_sensor package. Install it manually from GitHub.

$ pip install git+https://github.com/TheCellule/python-bleson

Add environment variable RUUVI_BLE_ADAPTER with value Bleson. E.g.

$ export RUUVI_BLE_ADAPTER="Bleson"

NOTE: On macOS only Data Format 5 works as macOS doesn't advertise MAC-address and only DF5 has MAC in sensor payload. RuuviTag-class doesn't work with macOS.

NOTE: On Windows Bleson requires Python 3.6. Unfortunately on Windows, Bleson doesn't send any payload for advertised package, so it is still unusable.

Examples

Examples are in examples directory, e.g.

Changelog

Changelog

Developer notes

Notes for developers who are interested in developing RuuviTag Sensor package or interested in it's internal functionality.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

Licensed under the MIT License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].