Simple Ruuvitag is a hard frok and HEAVY simplification of ttu work ruuvitag-sensor
It uses bleson python lib, which leverages Python 3 native support for Bluetooth sockets. However, in order for python to have access to AF_SOCKET family, python needs to be compiled with lib-bluetooth or bluez.
pip install simple-ruuvitag
Simplest usage is using ruuvi_client internal state. The client will keep the latest state of all sensors that have been polled.
from simple_ruuvitag import RuuviTagClient
macs = ['CC:2C:6A:1E:59:3D', 'DD:2C:6A:1E:59:3D']
ruuvi_client = RuuviTagClient(mac_addresses=macs)
ruuvi_client.start()
last_datas = ruuvi_client.get_current_datas()
print(last_datas)
However this means that if a sensor doesn't get updated in a while, the state will contain
old data that might pollute your dataset. To clear the state use the flag consume=True
last_datas = ruuvi_client.get_current_datas(consume=True)
You cal also use a callback to handle data as it is recieved by the library.
Just pass a callback
parameter to the listen method
from simple_ruuvitag import RuuviTagClient
def handle_callback(mac_address, data):
print(f"Data from {mac_address}: {data}")
macs = ['CC:2C:6A:1E:59:3D', 'DD:2C:6A:1E:59:3D']
ruuvi_client = RuuviTagClient(callback=handle_callback, mac_addresses=macs)
ruuvi_client.start()
Although the bluetooth observer is running, data might stop coming through due to
duplication removal in bleson. This issue is fixed in TheCellule/python-bleson#40
and in simple-ruuvitag v0.0.6
If you still find this issue, simple-ruuvitag has a method for to re-scan:ruuvi_client.rescan()
which will restart the observer, and data should start flowing again.
ruuvi_client.start()
time.sleep(5)
last_datas = ruuvi_client.get_current_datas()
print(last_datas)
ruuvi_client.rescan()
time.sleep(5)
last_datas = ruuvi_client.get_current_datas()
print(last_datas)
time.sleep(5)
from simple_ruuvitag import RuuviTagClient
def handle_callback(mac_address, data):
print(f"Data from {mac_address}: {data}")
macs = ['CC:2C:6A:1E:59:3D', 'DD:2C:6A:1E:59:3D']
ruuvi_client = RuuviTagClient(callback=handle_callback, mac_addresses=macs)
ruuvi_client.start()
new_macs = ['CC:2C:6A:1E:59:3D']
ruuvi_client.set_mac_addresses(new_macs)
Right now this library should work with:
- Python Docker official images (after this PR docker-library/python#445)
- Latest Ubuntu versions
- Projects like HASS.io (after this PR home-assistant/docker-base#53)
- MAC OS from version 0.0.3
- Unfortunately only for Ruuvi data format 5. Update your tags and use mode RAW V2
- See how to update and change modes here - https://lab.ruuvi.com/ruuvitag-fw/
- Install bluez or lib-bluetooth depending on your platform
- Rebuild python.