Skip to content

Commit

Permalink
Add simple script to synchronize time and read values from sensors
Browse files Browse the repository at this point in the history
It also allows to set units to Celsius or Fahrenheit
  • Loading branch information
jacekkow committed Mar 30, 2020
1 parent 3dae6ca commit c89bb90
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
17 changes: 16 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
Note: use `python3` if your system defaults to Python 2.
## Usage
## Usage (library)
Instantiate client with Lywsd02 mac address
Expand Down Expand Up @@ -59,6 +59,21 @@ with client.connect():
print(client.battery)
```

## Usage (helper script)

This library also installs a simple command-line utility called `lywsd02`.

It allows to synchronize time and read data from devices:

```bash
lywsd02 sync 3F:59:C8:80:70:BE
lywsd02 read 3F:59:C8:80:70:BE 3F:59:C8:80:70:BF
```

Consult `lywsd02 -h` to get information on all possible actions.

Note that you may need to replace `lywsd02` with `~/.local/bin/lywsd02` or some other path, depending on your installation settings.

## Available properties

* `client.temperature` – Sensor's temperature data in Celsius degrees. Updates with timeout (See below)
Expand Down
29 changes: 29 additions & 0 deletions scripts/lywsd02
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!python

import argparse
import lywsd02

parser = argparse.ArgumentParser()
parser.add_argument('action', help='Action to perform, either: sync - synchronize time with this machine, read - read current values from device, setc/setf - set temperature unit on display to Celsius/Fahrenheit', choices=['sync', 'read', 'setc', 'setf'])
parser.add_argument('mac', help='MAC address of LYWSD02 device', nargs='+')
args = parser.parse_args()

for mac in args.mac:
try:
client = lywsd02.Lywsd02Client(mac)
if args.action == 'sync':
print('Synchronizing time of {}'.format(mac))
client.time = datetime.datetime.now()
elif args.action == 'read':
print('Fetching data from {}'.format(mac))
data = client.data
print('Temperature: {}°C'.format(data.temperature))
print('Humidity: {}%'.format(data.humidity))
print('Battery: {}%'.format(client.battery))
print()
elif args.action == 'setc':
client.units = 'C'
elif args.action == 'setf':
client.units = 'F'
except Exception as e:
print(e)
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
'Programming Language :: Python :: 3.7',
],
install_requires=['bluepy==1.3.0'],
scripts=['scripts/lywsd02'],
author_email='mikhail.baranov@gmail.com',
description='Lywsd02 BLE sendor Library',
long_description_content_type='text/x-rst',
Expand Down

0 comments on commit c89bb90

Please sign in to comment.