From a8eae250ce1c9c3a1e3773ace4ed5a7811d389fd Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Thu, 21 Nov 2019 15:18:13 -0500 Subject: [PATCH 1/4] update for CPy 5.0.0-beta.0; not tested --- BLE_Client_Server/client.py | 48 ++++++++++++++++++++++--------------- BLE_Client_Server/server.py | 20 +++++++++------- 2 files changed, 41 insertions(+), 27 deletions(-) diff --git a/BLE_Client_Server/client.py b/BLE_Client_Server/client.py index 99a602f5e..7add804d3 100644 --- a/BLE_Client_Server/client.py +++ b/BLE_Client_Server/client.py @@ -1,6 +1,7 @@ from time import sleep -from adafruit_ble.uart_client import UARTClient -from adafruit_ble.scanner import Scanner +from adafruit_ble import BLERadio +from adafruit_ble.advertising.standard import ProvideServicesAdvertisement +from adafruit_ble.services.nordic import UARTService from adafruit_bluefruit_connect.packet import Packet from adafruit_bluefruit_connect.button_packet import ButtonPacket from adafruit_bluefruit_connect.color_packet import ColorPacket @@ -35,31 +36,38 @@ button_packet = ButtonPacket("1", True) # Transmits pressed button 1 -scanner = Scanner() # BLE Scanner -uart_client = UARTClient() # BLE Client +ble = BLERadio() -while True: - uart_addresses = [] - pixels[0] = BLUE # Blue LED indicates disconnected status - pixels.show() +uart_connection = None +# See if any existing connections are providing UARTService. +if ble.connected: + for connection in ble.connections: + if UARTService in connection: + uart_connection = connection + break - # Keep trying to find target UART peripheral - while not uart_addresses: - uart_addresses = uart_client.scan(scanner) - for address in uart_addresses: - if TARGET in str(address): - uart_client.connect(address, 5) # Connect to target +while True: + if not uart_connection: + pixels[0] = BLUE # Blue LED indicates disconnected status + pixels.show() + for adv in ble.start_scan(ProvideServicesAdvertisement, timeout=5): + if UARTService in adv.services: + uart_connection = ble.connect(adv) + break + # Stop scanning whether or not we are connected. + ble.stop_scan() - while uart_client.connected: # Connected + while uart_connection and uart_connection.connected: switch.update() if switch.fell: # Check for button press try: - uart_client.write(button_packet.to_bytes()) # Transmit press + uart_connection.write(button_packet.to_bytes()) # Transmit press except OSError: - pass + uart_connection = None + continue # Check for LED status receipt - if uart_client.in_waiting: - packet = Packet.from_stream(uart_client) + if uart_connection.in_waiting: + packet = Packet.from_stream(uart_connection) if isinstance(packet, ColorPacket): if fancy.CRGB(*packet.color).pack() == GREEN: # Color match # Green indicates on state @@ -79,3 +87,5 @@ color_index += fade_direction sleep(0.02) + + uart_connection = None diff --git a/BLE_Client_Server/server.py b/BLE_Client_Server/server.py index 6684bb060..d1882ed54 100644 --- a/BLE_Client_Server/server.py +++ b/BLE_Client_Server/server.py @@ -1,5 +1,7 @@ from time import sleep -from adafruit_ble.uart_server import UARTServer +from adafruit_ble import BLERadio +from adafruit_ble.advertising.standard import ProvideServicesAdvertisement +from adafruit_ble.services.nordic import UARTService from adafruit_bluefruit_connect.packet import Packet from adafruit_bluefruit_connect.button_packet import ButtonPacket from adafruit_bluefruit_connect.color_packet import ColorPacket @@ -13,17 +15,19 @@ solenoid.direction = Direction.OUTPUT solenoid.value = False -uart_server = UARTServer() +ble = BLERadio() +uart_service = UARTService() +advertisement = ProvideServicesAdvertisement(uart_service) while True: - uart_server.start_advertising() # Advertise when not connected. + ble.start_advertising(advertisement) # Advertise when not connected. - while not uart_server.connected: # Wait for connection + while not ble.connected: # Wait for connection pass - while uart_server.connected: # Connected - if uart_server.in_waiting: # Check BLE commands - packet = Packet.from_stream(uart_server) + while uart_service.connected: # Connected + if uart_service.in_waiting: # Check BLE commands + packet = Packet.from_stream(uart_service) if isinstance(packet, ButtonPacket): if packet.button == '1' and packet.pressed: solenoid.value = True # Activate solenoid for 1 second @@ -35,7 +39,7 @@ # Color: red = off, green = on color_packet = ColorPacket((255 * int(not led_on), 255 * led_on, 0)) try: - uart_server.write(color_packet.to_bytes()) # Transmit state color + uart_service.write(color_packet.to_bytes()) # Transmit state color except OSError: pass From 11ba63eb79b24a2bccd0a5aa35d6608e4f56e8f5 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Sun, 24 Nov 2019 18:33:31 -0500 Subject: [PATCH 2/4] fix bugs --- BLE_Client_Server/client.py | 5 ++++- BLE_Client_Server/server.py | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/BLE_Client_Server/client.py b/BLE_Client_Server/client.py index 7add804d3..be1fe20f0 100644 --- a/BLE_Client_Server/client.py +++ b/BLE_Client_Server/client.py @@ -1,10 +1,13 @@ from time import sleep + from adafruit_ble import BLERadio from adafruit_ble.advertising.standard import ProvideServicesAdvertisement from adafruit_ble.services.nordic import UARTService + from adafruit_bluefruit_connect.packet import Packet from adafruit_bluefruit_connect.button_packet import ButtonPacket from adafruit_bluefruit_connect.color_packet import ColorPacket + from neopixel import NeoPixel from board import NEOPIXEL, SWITCH from adafruit_debouncer import Debouncer @@ -61,7 +64,7 @@ switch.update() if switch.fell: # Check for button press try: - uart_connection.write(button_packet.to_bytes()) # Transmit press + uart_connection[UARTService].write(button_packet.to_bytes()) # Transmit press except OSError: uart_connection = None continue diff --git a/BLE_Client_Server/server.py b/BLE_Client_Server/server.py index d1882ed54..f9167bb2b 100644 --- a/BLE_Client_Server/server.py +++ b/BLE_Client_Server/server.py @@ -1,10 +1,13 @@ from time import sleep + from adafruit_ble import BLERadio from adafruit_ble.advertising.standard import ProvideServicesAdvertisement from adafruit_ble.services.nordic import UARTService + from adafruit_bluefruit_connect.packet import Packet from adafruit_bluefruit_connect.button_packet import ButtonPacket from adafruit_bluefruit_connect.color_packet import ColorPacket + from board import A0, D13 from analogio import AnalogIn from digitalio import DigitalInOut, Direction @@ -25,7 +28,7 @@ while not ble.connected: # Wait for connection pass - while uart_service.connected: # Connected + while ble.connected: # Connected if uart_service.in_waiting: # Check BLE commands packet = Packet.from_stream(uart_service) if isinstance(packet, ButtonPacket): From 90e39cf2f2b38785a35e7191ebe433f009d13eca Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Mon, 25 Nov 2019 18:42:32 -0500 Subject: [PATCH 3/4] fix uart_service errors --- BLE_Client_Server/client.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/BLE_Client_Server/client.py b/BLE_Client_Server/client.py index be1fe20f0..4d28a2749 100644 --- a/BLE_Client_Server/client.py +++ b/BLE_Client_Server/client.py @@ -61,16 +61,17 @@ ble.stop_scan() while uart_connection and uart_connection.connected: + uart_service = uart_connection[UARTService] switch.update() if switch.fell: # Check for button press try: - uart_connection[UARTService].write(button_packet.to_bytes()) # Transmit press + uart_service.write(button_packet.to_bytes()) # Transmit press except OSError: uart_connection = None continue # Check for LED status receipt if uart_connection.in_waiting: - packet = Packet.from_stream(uart_connection) + packet = Packet.from_stream(uart_service) if isinstance(packet, ColorPacket): if fancy.CRGB(*packet.color).pack() == GREEN: # Color match # Green indicates on state From 80e5c2dd53a8ed34bd214d53f8e2cb6e3a202ad4 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Thu, 17 Sep 2020 15:12:29 -0400 Subject: [PATCH 4/4] more rewrite and initial smoke test --- BLE_Client_Server/client.py | 36 ++++++++++++++---------------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/BLE_Client_Server/client.py b/BLE_Client_Server/client.py index 4d28a2749..e8b72281b 100644 --- a/BLE_Client_Server/client.py +++ b/BLE_Client_Server/client.py @@ -29,36 +29,30 @@ gradients = {'Off': [(0.0, RED), (0.75, ORANGE)], 'On': [(0.0, GREEN), (1.0, AQUA)]} + palette = fancy.expand_gradient(gradients['Off'], 30) gamma_levels = (0.25, 0.3, 0.15) color_index = 1 fade_direction = 1 -TARGET = 'a0:b4:c2:d0:e7:f2' # CHANGE TO YOUR BLE ADDRESS - button_packet = ButtonPacket("1", True) # Transmits pressed button 1 ble = BLERadio() -uart_connection = None -# See if any existing connections are providing UARTService. -if ble.connected: - for connection in ble.connections: - if UARTService in connection: - uart_connection = connection - break - while True: - if not uart_connection: - pixels[0] = BLUE # Blue LED indicates disconnected status - pixels.show() - for adv in ble.start_scan(ProvideServicesAdvertisement, timeout=5): - if UARTService in adv.services: - uart_connection = ble.connect(adv) - break - # Stop scanning whether or not we are connected. - ble.stop_scan() + uart_connection = None + + pixels[0] = BLUE # Blue LED indicates disconnected status + pixels.show() + + for adv in ble.start_scan(ProvideServicesAdvertisement): + if UARTService in adv.services: + uart_connection = ble.connect(adv) + break + + # Stop scanning when connected. + ble.stop_scan() while uart_connection and uart_connection.connected: uart_service = uart_connection[UARTService] @@ -70,7 +64,7 @@ uart_connection = None continue # Check for LED status receipt - if uart_connection.in_waiting: + if uart_service.in_waiting: packet = Packet.from_stream(uart_service) if isinstance(packet, ColorPacket): if fancy.CRGB(*packet.color).pack() == GREEN: # Color match @@ -91,5 +85,3 @@ color_index += fade_direction sleep(0.02) - - uart_connection = None