CircuitPython BLU UART on Seeed Studio XIAO nRF52840/ESP32S3 Sense, connect to Raspberry Pi as Serial Console.
Implement CircuitPython BLU UART on
Seeed Studio XIAO nRF52840 Sense/XIAO ESP32S3 Sense
running CircuitPython 9.2.7.
Library needed:
~ adafruit_ble To install adafruit_ble on CircuitPython device using circup:
circup install adafruit_ble
Read "Install and using CircUp (CircuitPython library updater) to install
CircuitPython libraries" for more about CircUp.Code:
cpy_ble_uart.py
"""
Circuitpython 9.2.7 BLE exercise
Run on XIAO nRF52840 Sense/ESP32S3 Sense
Act as bridge between BLE UART and UART
To make it run as standalone (without REPL connection),
copy to CircuitPython device, name "code.py".
"""
import os, sys
import time
import board, busio
import digitalio
from adafruit_ble import BLERadio
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
from adafruit_ble.services.nordic import UARTService
#------------------------
print("========================================")
print(sys.implementation[0], os.uname()[3],
"\nrun on", os.uname()[4])
print("========================================")
led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT
#blink LED three to indicate program start
for i in range(3):
led.value = False
time.sleep(0.25)
led.value = True
time.sleep(0.25)
# Setup hardware UART with baud rate.
uart = busio.UART(board.TX, board.RX, baudrate=115200)
ble = BLERadio()
uart_service = UARTService()
advertisement = ProvideServicesAdvertisement(uart_service)
name = "T_"+os.uname()[1]
name = name[:8] # In my test using Android/Serial Bluetooth Terminal,
# if complete_name > 8 char,
# it will not be shown in SCAN.
advertisement.complete_name = name
print("complete_name:", advertisement.complete_name)
def blink_led():
global next_blink_tick
if time.monotonic() > next_blink_tick:
led.value = not led.value
next_blink_tick = time.monotonic() + next_blink_delay
led.value = True
while True:
# In my test on XIAO nRF52840 Sense and XIAO ESP32S3 Sense
# nRD52840 no problem if/if NO delay.
# For ESP32S3, it's easier to connect if delay here,
# otherwise, sometimes connection failed: gatt status 133.
time.sleep(1.0)
ble.start_advertising(advertisement)
print("Waiting to connect")
next_blink_delay = 0.5
next_blink_tick = time.monotonic() + next_blink_delay
while not ble.connected:
blink_led()
print("Connected")
led.value = True
next_blink_delay = 1
next_blink_tick = time.monotonic() + next_blink_delay
while ble.connected:
blink_led()
# If data is received over BLE, forward it to UART.
if uart_service.in_waiting:
ble_data = uart_service.readline()
if ble_data:
print("Received from BLE:", ble_data)
uart.write(ble_data)
# If data is received over UART, forward it to BLE.
if uart.in_waiting:
uart_data = uart.read(uart.in_waiting)
if uart_data:
print("Received from UART:", uart_data)
uart_service.write(uart_data)
Test with Android Serial Bluetooth Terminal App:
Connect XIAO UART to FTDI232 USB-to-Serial adapter.
- Connect (in Serial Bluetooth Terminal App) to BLE device of XIAO BLE.
- Send text from Android to XIAO via BLE UART, XIAO pass the received
text to UART, FTDI232 USB-to-Serial adapter convert the UART data to
another USB port, using Arduino IDE Serial Monitor to receive the text.
- Reverse the direction to send text from Arduino IDE Aerial Monitor to XIAO, and to Android via BLE UART.
- Reverse the direction to send text from Arduino IDE Aerial Monitor to XIAO, and to Android via BLE UART.
To make it run as standalone (without REPL connection), copy to
CircuitPython device, name "code.py".
The Second part, connect XIAO to Raspberry Pi (tested on 4B running 64-bit Raspberry Pi OS bookworm), act as BLE UART Serial Console.
The Second part, connect XIAO to Raspberry Pi (tested on 4B running 64-bit Raspberry Pi OS bookworm), act as BLE UART Serial Console.
Before all, have to enable Serial Interface in Raspberry Pi using raspi-config.
sudo raspi-config
- Connect to Raspberry Pi Serial Console via BLE UART using Serial
Bluetooth Terminal App.- Login user/password.
- Then, you can enter command in Serial Bluetooth Terminal App.
Comments
Post a Comment