Skip to content

Instantly share code, notes, and snippets.

@dkarchmer
Created September 16, 2018 18:22
Show Gist options
  • Save dkarchmer/c1dbe040ab7e9c1c8d814beef3c9dc6c to your computer and use it in GitHub Desktop.
Save dkarchmer/c1dbe040ab7e9c1c8d814beef3c9dc6c to your computer and use it in GitHub Desktop.
import argparse
import time
import sys
from iotile.core.hw import HardwareManager
def build_parser():
parser = argparse.ArgumentParser(description="Print broadcast readings from a specific device")
parser.add_argument('-d', '--dev', type=lambda x: int(x, 0), help="Optional, the UUID of the device you want to see broadcasts from (0x21 for example)")
parser.add_argument('-i', '--interval', type=float, default=1.0, help="The minimum interval in seconds between two reported readings (only applies when -d is given), defaults to 1s")
parser.add_argument('-p', '--port', default="bled112", help="The port to use, defaults to bled112")
return parser
def main():
parser = build_parser()
args = parser.parse_args()
with HardwareManager(port=args.port) as hw:
hw.enable_broadcasting()
while True:
found_reading = False
for report in hw.iter_broadcast_reports(blocking=False):
print(report)
if args.dev is not None and report.origin != args.dev:
continue
if not (args.dev and found_reading):
reading = report.visible_readings[0]
print("%04X: %d" % (reading.stream, reading.value))
found_reading = True
time.sleep(args.interval)
if __name__ == "__main__":
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment