connection + notifs current

This commit is contained in:
leo 2023-06-15 17:53:12 +02:00
commit f03dced25c
Signed by: leo
GPG Key ID: 0DD993BFB2B307DB
3 changed files with 78 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
__pycache__

54
main.py Normal file
View File

@ -0,0 +1,54 @@
# -*- coding: utf-8 -*-
import argparse
import asyncio
import logging
import struct
from bleak import BleakClient, BleakScanner
from bleak.backends.characteristic import BleakGATTCharacteristic
from bleak.uuids import normalize_uuid_str
import myUUIDs
logger = logging.getLogger(__name__)
def current_meas_handler(characteristic: BleakGATTCharacteristic, data: bytearray):
value = struct.unpack("<I", data)[0]
logger.info("%s: %f", characteristic.description, value * 1.0e-6)
async def main():
logger.info("starting scan...")
device = await BleakScanner.find_device_by_address("84:F7:03:1B:C6:A2", cb=dict(use_bdaddr=False))
if device is None:
logger.error("could not find device with address")
return
logger.info("connecting to device...")
async with BleakClient(device) as client:
logger.info("Connected")
current_svc_handle = 0
services = client.services.services
metrology_svcs = [services[i] for i in services if services[i].uuid == normalize_uuid_str(myUUIDs.METROLOGY_SERVICE)]
current_meas_svc = next(s for s in metrology_svcs if s.get_characteristic(myUUIDs.ELECTRIC_CURRENT_CHAR))
for c in current_meas_svc.characteristics:
await client.start_notify(c, current_meas_handler)
await asyncio.Event().wait()
# await client.stop_notify(args.characteristic)
if __name__ == "__main__":
logging.basicConfig(
level="INFO",
format="%(asctime)-15s %(name)-8s %(levelname)s: %(message)s",
)
loop = asyncio.get_event_loop()
loop.create_task(main())
loop.run_forever()

23
myUUIDs.py Normal file
View File

@ -0,0 +1,23 @@
METROLOGY_SERVICE = "1878"
CONFIGURATION_SERVICE = "1879"
METROLOGY_RANGE_SERVICE = "1880"
ELECTRIC_CURRENT_CHAR = "2AEE"
VOLTAGE_CHAR = "2B18"
SAMPLING_RATE_CHAR = "2C12"
ZERO_CALI_CHAR = "2C13"
ZERO_CALI_NSAMP = "2C14"
ELECTRIC_CURRENT_RANGE_CHAR = "2AEF"
AUTO_RANGE_CHAR = "2C15"
CHAR_PRES_FORMAT = "2904"
PPM_UNIT_UUID = "27C4"
PERCENT_UNIT_UUID = "27AD"
POWER_UNIT_UUID = "2726"
TESLA_UNIT_UUID = "272D"
AMPERE_UNIT_UUID = "2704"
VOLTS_UNIT_UUID = "2728"
HERTZ_UNIT_UUID = "2722"
SECONDS_UNIT_UUID = "2703"
UNITLESS_UNIT_UUID = "2700"
NSP_DESC_MAIN = "0106"
NSP_DESC_INSIDE = "010B"
NSP_DESC_OUTSIDE = "010C"