Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update BLE remote control switch; not tested #937

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 24 additions & 18 deletions BLE_Client_Server/client.py
@@ -1,9 +1,13 @@
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

from neopixel import NeoPixel
from board import NEOPIXEL, SWITCH
from adafruit_debouncer import Debouncer
Expand All @@ -25,41 +29,43 @@

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

scanner = Scanner() # BLE Scanner
uart_client = UARTClient() # BLE Client
ble = BLERadio()

while True:
uart_addresses = []
uart_connection = None

pixels[0] = BLUE # Blue LED indicates disconnected status
pixels.show()

# 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
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_client.connected: # Connected
while uart_connection and uart_connection.connected:
uart_service = uart_connection[UARTService]
switch.update()
if switch.fell: # Check for button press
try:
uart_client.write(button_packet.to_bytes()) # Transmit press
uart_service.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_service.in_waiting:
packet = Packet.from_stream(uart_service)
if isinstance(packet, ColorPacket):
if fancy.CRGB(*packet.color).pack() == GREEN: # Color match
# Green indicates on state
Expand Down
23 changes: 15 additions & 8 deletions BLE_Client_Server/server.py
@@ -1,8 +1,13 @@
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

from board import A0, D13
from analogio import AnalogIn
from digitalio import DigitalInOut, Direction
Expand All @@ -13,17 +18,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 ble.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
Expand All @@ -35,7 +42,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

Expand Down