import matplotlib.pyplot as plt from time import time import asyncio from bleak import BleakClient, BleakScanner import pandas as pd from argparse import ArgumentParser from tqdm.auto import tqdm from config import MILLIS_UUID, WEIGHT_UUID READING_TIME = 30 async def read_ble(reading_time=READING_TIME): device = await BleakScanner.find_device_by_name("Smaage") assert device is not None, "No device found" timestamps = [] raw_weights = [] async with BleakClient(device.address) as client: print(f"Connected to {device}") # Read the characteristic value pbar = tqdm(desc="reading data", total=reading_time) time_start = time() time_passed = 0 last_time_passed = 0 while time_passed < reading_time: try: # Read the sensor data millis = await client.read_gatt_char(MILLIS_UUID) millis = int.from_bytes(millis, byteorder='little') # Adjust based on your data format weight = await client.read_gatt_char(WEIGHT_UUID) weight = int.from_bytes(weight, byteorder='little') # Adjust based on your data format timestamps.append(millis) raw_weights.append(weight) except Exception as e: print(f"Error reading data: {e}") break time_passed = time() - time_start time_delta = (time_passed - last_time_passed) last_time_passed = time_passed pbar.update(time_delta) pbar.close() df = pd.DataFrame({"timestamps": timestamps, "weights": raw_weights}) print(df['weights'].describe()) plt.plot(timestamps, raw_weights) plt.show() if __name__ == '__main__': parser = ArgumentParser() parser.add_argument('-t', default=READING_TIME, type=int, help="reading time in sec") args = parser.parse_args() asyncio.run(read_ble(args.t))