(this is a cross post from Communicating over UART0 with HCI uart BLE dongle since I originally, mistakenly, posted it on Balena Cloud)
I am trying to use the BluePy Scanner on a Raspberry Pi 3 B+ with a nRF52840-Dongle programmed as a Bluetooth: HCI-uart.
I am able to get the Bluepy Scanner code to work with the onboard BLE, but I am so far unable to get it to communicate with the Bluetooth HCI dongle that is attached to the UART0. I have connected UART0_TXD (GPIO14), UART0_RXD (GPIO15), UART0_CTS (GPIO16), and UART0_RTS (GPIO17).
How do I go about attaching a HCI ble device to the RPi’s UART and then use it in my python scanner program?
The nRF52840-Dongle has the following default UART settings.
- Baudrate: 1Mbit/s
- 8 bits, no parity, 1 stop bit
- Hardware Flow Control (RTS/CTS) enabled
Following is my bluepyScanner code.
from bluepy.btle import Scanner, DefaultDelegate
class ScanDelegate(DefaultDelegate):
def __init__(self):
DefaultDelegate.__init__(self)
def handleDiscovery(self, dev, isNewDev, isNewData):
if isNewDev:
print("Discovered device", dev.addr)
elif isNewData:
print("Received new data from", dev.addr)
scanner = Scanner().withDelegate(ScanDelegate())
devices = scanner.scan(10.0)
for dev in devices:
print("Device {} ({}), RSSI={} dB".format(dev.addr, dev.addrType, dev.rssi))
for (adtype, desc, value) in dev.getScanData():
print(" {} = {}".format(desc, value))
with open('bluepyscanlog.txt', 'a') as the_file:
the_file.write("{}={}\n".format(desc, value))
And the Dockerfile.template for bluepyScanner.
FROM balenalib/%%BALENA_MACHINE_NAME%%-python:3-stretch-run
# enable container init system.
ENV INITSYSTEM on
# use `install_packages` if you need to install dependencies,
# for instance if you need git, just uncomment the line below.
# RUN install_packages git
RUN pip install --upgrade pip
RUN apt-get update && apt-get install -yq --no-install-recommends build-essential
RUN install_packages \
build-essential \
bluez \
python-dbus \
python-dev \
libglib2.0-dev
# Set our working directory
WORKDIR /usr/src/bluepyScanner
# Copy requirements.txt first for better cache on later pushes
COPY requirements.txt requirements.txt
# pip install python deps from requirements.txt on the resin.io build server
RUN pip3 install -r requirements.txt
# This will copy all files in our root to the working directory in the container
COPY . ./
# Enable udevd so that plugged dynamic hardware devices show up in our container.
ENV UDEV=1
# main.py will run when container starts up on the device
CMD ["python3","-u","src/bluepyScanner.py"]
The only item in requirements.txt is
bluepy
Following is my docker-compose.yml
version: '2.1'
services:
bluepy_scanner:
privileged: true
restart: always
build: ./bluepy_scanner
devices:
- "/dev/ttyS0:/dev/ttyS0"
network_mode: host
cap_add:
- NET_ADMIN
labels:
io.balena.features.supervisor-api: '1'
io.balena.features.dbus: "1"
io.balena.features.kernel-modules: "1"
io.balena.features.firmware: "1"
io.balena.features.balena-api: "1"
wifi-connect:
build: ./wifi-connect
network_mode: "host"
labels:
io.balena.features.dbus: '1'
cap_add:
- NET_ADMIN
environment:
DBUS_SYSTEM_BUS_ADDRESS: "unix:path=/host/run/dbus/system_bus_socket"
I have set the following Device configurations.
I am trying to use the BluePy Scanner on a Raspberry Pi 3 B+ with a nRF52840-Dongle programmed as a Bluetooth: HCI-uart.I am able to get the Bluepy Scanner code to work with the onboard BLE, but I am so far unable to get it to communicate with the Bluetooth HCI dongle that is attached to the UART0. I have connected UART0_TXD (GPIO14), UART0_RXD (GPIO15), UART0_CTS (GPIO16), and UART0_RTS (GPIO17).
How do I go about attaching a HCI ble device to the RPi’s UART and then use it in my python scanner program?
The nRF52840-Dongle has the following default UART settings.
- Baudrate: 1Mbit/s
- 8 bits, no parity, 1 stop bit
- Hardware Flow Control (RTS/CTS) enabled
Following is my bluepyScanner code.
from bluepy.btle import Scanner, DefaultDelegate
class ScanDelegate(DefaultDelegate):
def __init__(self):
DefaultDelegate.__init__(self)
def handleDiscovery(self, dev, isNewDev, isNewData):
if isNewDev:
print("Discovered device", dev.addr)
elif isNewData:
print("Received new data from", dev.addr)
scanner = Scanner().withDelegate(ScanDelegate())
devices = scanner.scan(10.0)
for dev in devices:
print("Device {} ({}), RSSI={} dB".format(dev.addr, dev.addrType, dev.rssi))
for (adtype, desc, value) in dev.getScanData():
print(" {} = {}".format(desc, value))
with open('bluepyscanlog.txt', 'a') as the_file:
the_file.write("{}={}\n".format(desc, value))
And the Dockerfile.template for bluepyScanner.
FROM balenalib/%%BALENA_MACHINE_NAME%%-python:3-stretch-run
# enable container init system.
ENV INITSYSTEM on
# use `install_packages` if you need to install dependencies,
# for instance if you need git, just uncomment the line below.
# RUN install_packages git
RUN pip install --upgrade pip
RUN apt-get update && apt-get install -yq --no-install-recommends build-essential
RUN install_packages \
build-essential \
bluez \
python-dbus \
python-dev \
libglib2.0-dev
# Set our working directory
WORKDIR /usr/src/bluepyScanner
# Copy requirements.txt first for better cache on later pushes
COPY requirements.txt requirements.txt
# pip install python deps from requirements.txt on the resin.io build server
RUN pip3 install -r requirements.txt
# This will copy all files in our root to the working directory in the container
COPY . ./
# Enable udevd so that plugged dynamic hardware devices show up in our container.
ENV UDEV=1
# main.py will run when container starts up on the device
CMD ["python3","-u","src/bluepyScanner.py"]
The only item in requirements.txt is
bluepy
Following is my docker-compose.yml
version: '2.1'
services:
bluepy_scanner:
privileged: true
restart: always
build: ./bluepy_scanner
devices:
- "/dev/ttyS0:/dev/ttyS0"
network_mode: host
cap_add:
- NET_ADMIN
labels:
io.balena.features.supervisor-api: '1'
io.balena.features.dbus: "1"
io.balena.features.kernel-modules: "1"
io.balena.features.firmware: "1"
io.balena.features.balena-api: "1"
wifi-connect:
build: ./wifi-connect
network_mode: "host"
labels:
io.balena.features.dbus: '1'
cap_add:
- NET_ADMIN
environment:
DBUS_SYSTEM_BUS_ADDRESS: "unix:path=/host/run/dbus/system_bus_socket"
I have set the following Device configurations.