Bluetooth Sensor Capture - Adafruit Circuit Playground Bluefruit¶
The following example demonstrates using an Adafruit Circuit Playground Bluefruit as a wireless sensor. The CircuitPython program uses only the onboard sensor hardware. The companion Python program runs on a host computer (e.g. desktop or laptop) and receives data. One example use case is to capture accelerometer motion sensor data for analysis.
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_ble
module which is implemented for both the microcontroller and for host Python.
iOS apps
- Bluefruit Connect iOS App for iPad, iPhone, and Apple Watch.
This appears to be a somewhat general user interface tool. Adafruit has a related tutorial.
- Bluefruit Playground iOS App for iPhone.
This appears to be more of a remote control demo rather than a general purpose utility.
CircuitPython Remote Sensor¶
Direct download: cpb_ble_sensor.py.
1# cpb_ble_sensor.py
2
3# Provide a remote sensing service over Bluetooth Low-Energy (BLE).
4# This runs on an Adafruit Circuit Playground Bluefruit.
5
6# This example works with either:
7# 1. host_ble_receiver.py running on a host computer
8# 2. the Plotter function of the Bluefruit Connect iOS app
9
10# ----------------------------------------------------------------
11# Import the standard Python time functions.
12import time
13
14# Import the board-specific input/output library.
15from adafruit_circuitplayground import cp
16
17# Import the Adafruit Bluetooth library. Technical reference:
18# https://circuitpython.readthedocs.io/projects/ble/en/latest/api.html
19from adafruit_ble import BLERadio
20from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
21from adafruit_ble.services.nordic import UARTService
22
23# ----------------------------------------------------------------
24# Initialize global variables for the main loop.
25
26ble = BLERadio()
27uart = UARTService()
28advertisement = ProvideServicesAdvertisement(uart)
29
30print("Radio:", ble)
31ble.name = 'CPB-accel-1'
32print("Radio name:", ble.name)
33
34print("Advertisement:", advertisement)
35print("UART:", uart)
36
37# Flags for detecting state changes.
38advertised = False
39connected = False
40
41# The sensor sampling rate is precisely regulated using the following timer variables.
42sampling_timer = 0.0
43last_time = time.monotonic()
44sampling_interval = 0.10
45
46# ----------------------------------------------------------------
47# Begin the main processing loop.
48
49while True:
50
51 # Read the accelerometer at regular intervals. Measure elapsed time and
52 # wait until the update timer has elapsed.
53 now = time.monotonic()
54 interval = now - last_time
55 last_time = now
56 sampling_timer -= interval
57 if sampling_timer < 0.0:
58 sampling_timer += sampling_interval
59 x, y, z = cp.acceleration
60 else:
61 x = None
62
63 if not advertised:
64 ble.start_advertising(advertisement)
65 print("Waiting for connection.")
66 advertised = True
67
68 if not connected and ble.connected:
69 print("Connection received.")
70 connected = True
71 cp.red_led = True
72
73 if connected:
74 if not ble.connected:
75 print("Connection lost.")
76 connected = False
77 advertised = False
78 cp.red_led = False
79 else:
80 if x is not None:
81 uart.write(b"%.3f,%.3f,%.3f\n" % (x, y, z))
Host Sensor Receiver¶
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_receiver.py.
1# Connect to a remote sensing service over Bluetooth Low-Energy (BLE). This
2# script runs in Python 3 on a desktop or laptop. It scans for a connection,
3# the prints incoming data to the console 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 print(uart_service.readline().decode("utf-8").rstrip())