infected_3d

Untitled

Dec 19th, 2022
771
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.79 KB | None | 0 0
  1. # SPDX-FileCopyrightText: 2020 Dan Halbert for Adafruit Industries
  2. # SPDX-License-Identifier: MIT
  3.  
  4. """
  5. Read cycling speed and cadence data from a peripheral using the standard BLE
  6. Cycling Speed and Cadence (CSC) Service.
  7. """
  8.  
  9. import time
  10.  
  11. import adafruit_ble
  12. from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
  13. from adafruit_ble.services.standard.device_info import DeviceInfoService
  14. from adafruit_ble_cycling_speed_and_cadence import CyclingSpeedAndCadenceService
  15.  
  16. # PyLint can't find BLERadio for some reason so special case it here.
  17. ble = adafruit_ble.BLERadio()  # pylint: disable=no-member
  18.  
  19.  
  20. while True:
  21.     print("Scanning...")
  22.     # Save advertisements, indexed by address
  23.     advs = {}
  24.     for adv in ble.start_scan(ProvideServicesAdvertisement, timeout=5):
  25.         if CyclingSpeedAndCadenceService in adv.services:
  26.             print("found a CyclingSpeedAndCadenceService advertisement")
  27.             # Save advertisement. Overwrite duplicates from same address (device).
  28.             advs[adv.address] = adv
  29.  
  30.     ble.stop_scan()
  31.     print("Stopped scanning")
  32.     if not advs:
  33.         # Nothing found. Go back and keep looking.
  34.         continue
  35.  
  36.     # Connect to all available CSC sensors.
  37.     cyc_connections = []
  38.     for adv in advs.values():
  39.         try:
  40.             cyc_connections.append(ble.connect(adv))
  41.             print("Connected", len(cyc_connections))
  42.         except AttributeError:
  43.             print("err")
  44.  
  45.     # Print out info about each sensors.
  46.     for conn in cyc_connections:
  47.         if conn.connected:
  48.             if DeviceInfoService in conn:
  49.                 dis = conn[DeviceInfoService]
  50.                 try:
  51.                     manufacturer = dis.manufacturer
  52.                 except AttributeError:
  53.                     manufacturer = "(Manufacturer Not specified)"
  54.                 print("Device:", manufacturer)
  55.             else:
  56.                 print("No device information")
  57.  
  58.     print("Waiting for data... (could be 10-20 seconds or more)")
  59.     # Get CSC Service from each sensor.
  60.     cyc_services = []
  61.     for conn in cyc_connections:
  62.  
  63.         cyc_services.append(conn[CyclingSpeedAndCadenceService])
  64.  
  65.     # Read data from each sensor once a second.
  66.     # Stop if we lose connection to all sensors.
  67.     while True:
  68.         still_connected = False
  69.         for conn, svc in zip(cyc_connections, cyc_services):
  70.             if conn.connected:
  71.                 still_connected = True
  72.                 wheel_revs = str(svc.measurement_values)
  73.                 print(wheel_revs)
  74.                 if wheel_revs != None:
  75.                     file = open("/var/www/html/info.txt", "w")
  76.                     file.write(wheel_revs)
  77.                     file.close()
  78.  
  79.         if not still_connected:
  80.             break
  81.         time.sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment