Bluetooth Remote Control - Adafruit Circuit Playground Bluefruit¶
The following example demonstrates sending command strings to an Adafruit Circuit Playground Bluefruit over Bluetooth. The CircuitPython program just prints out messages. The companion Python program runs on a host computer (e.g. desktop or laptop) and accepts and transmits command strings. One example use case is to puppet a robot wirelessly.
Prerequisites
host computer with Python 3 Installation including BLE libraries
Adafruit Circuit Playground Bluefruit microcontroller board
Related Information
Site pages
Tutorials
Technical Documentation
- Adafruit BLE Library - API Reference
This reference applies to
adafruit_blemodule which is implemented for both the microcontroller and for host Python.
CircuitPython Remote Control Receiver¶
Direct download: cpb_ble_receiver.py.
1# cpb_ble_receiver.py
2
3# Provide a remotely controlled service over Bluetooth Low-Energy (BLE).
4# This runs on an Adafruit Circuit Playground Bluefruit.
5
6# Please see host_ble_console.py for the laptop side of the connection.
7
8# ----------------------------------------------------------------
9# Import the standard Python time functions.
10import time
11
12# Import the board-specific input/output library.
13from adafruit_circuitplayground import cp
14
15# Import the Adafruit Bluetooth library. Technical reference:
16# https://circuitpython.readthedocs.io/projects/ble/en/latest/api.html
17from adafruit_ble import BLERadio
18from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
19from adafruit_ble.services.nordic import UARTService
20
21# ----------------------------------------------------------------
22# Initialize global variables for the main loop.
23
24ble = BLERadio()
25uart = UARTService()
26advertisement = ProvideServicesAdvertisement(uart)
27
28print("Radio:", ble)
29ble.name = 'CPB-robot-1'
30print("Radio name:", ble.name)
31
32print("Advertisement:", advertisement)
33print("UART:", uart)
34
35# Flags for detecting state changes.
36advertised = False
37connected = False
38
39# The sensor sampling rate is precisely regulated using the following timer variables.
40sampling_timer = 0.0
41last_time = time.monotonic()
42sampling_interval = 0.50
43
44# ----------------------------------------------------------------
45# Begin the main processing loop.
46
47while True:
48
49 # Read the event loop at regular intervals. Measure elapsed time and
50 # wait until the update timer has elapsed.
51 now = time.monotonic()
52 interval = now - last_time
53 last_time = now
54 sampling_timer -= interval
55 if sampling_timer < 0.0:
56 sampling_timer += sampling_interval
57 x, y, z = cp.acceleration
58 print("tilt: ", x, y, z)
59 else:
60 x = None
61
62 if not advertised:
63 ble.start_advertising(advertisement)
64 print("Waiting for connection.")
65 advertised = True
66
67 if not connected and ble.connected:
68 print("Connection received.")
69 connected = True
70 cp.red_led = True
71
72 if connected:
73 if not ble.connected:
74 print("Connection lost.")
75 connected = False
76 advertised = False
77 cp.red_led = False
78 else:
79 if uart.in_waiting > 0:
80 command = uart.readline()
81 print("Received", command)
Host Control Transmitter¶
The host-side script uses a version of the Adafruit Bluetooth libraries for regular Python so the essential code uses the same interface.
Direct download: host_ble_console.py.
1# Send commands to a remote device service over Bluetooth Low-Energy (BLE). This
2# script runs in Python 3 on a desktop or laptop. It scans for a connection,
3# then transfers console commands to the remote device until the connection breaks.
4
5# It assumes the following packages have been installed:
6#
7# pip3 install adafruit-blinka-bleio
8# pip3 install adafruit-circuitpython-ble
9
10# ----------------------------------------------------------------
11# Import the Adafruit Bluetooth library, part of Blinka. Technical reference:
12# https://circuitpython.readthedocs.io/projects/ble/en/latest/api.html
13
14from adafruit_ble import BLERadio
15from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
16from adafruit_ble.services.nordic import UARTService
17
18# ----------------------------------------------------------------
19# Initialize global variables for the main loop.
20ble = BLERadio()
21uart_connection = None
22
23# ----------------------------------------------------------------
24# Begin the main processing loop.
25
26while True:
27 if not uart_connection:
28 print("Trying to connect...")
29 for adv in ble.start_scan(ProvideServicesAdvertisement):
30 print("Found advertisement: ", adv)
31 print(" name:", adv.complete_name)
32 if UARTService in adv.services:
33 uart_connection = ble.connect(adv)
34 print("Connected with ", uart_connection)
35 break
36 ble.stop_scan()
37
38 if uart_connection and uart_connection.connected:
39 uart_service = uart_connection[UARTService]
40 while uart_connection.connected:
41 command = input("> ")
42 uart_service.write(command.encode() + b"\n")