BLE Support
Next we'll set up BLE support with the BlueZ Bluetooth protocol stack. Type this and hit return:
sudo apt-get install bluez-hcidump
Adafruit Blinka BLEio
"Old MacDonald had a snake, BLEeeee-iiiiii-BLEeeee-iiiiii-oooooo!"
Next, we'll install the Adafruit Blinka bleio library, which is required for CircuitPython BLE to work properly.
In the ssh terminal type:
pip3 install adafruit-blinka-bleio
Bluetooth Group
In order to have the Pi scan for BLE devices we need to increase the permissions granted to the pi user.
To add the user to the bluetooth group do:
sudo usermod -a -G bluetooth pi
(Or, whatever your username is if not the default.)
Now, the pi user won't actually have these new permissions until the next time we log in, so type exit
and then re-login to the ssh with
Enter your password when promted, then you can check the groups the pi user is in by typing:
groups
pi@raspberrypiBlinkaBridge:~ $ groups pi adm dialout cdrom sudo audio video plugdev games users input netdev bluetooth gpio i2c spi
Now, we'll set permissions so that we can do comprehensive BLE scanning.
Type the following lines into the ssh terminal, pressing enter after each one:
sudo chown :bluetooth /usr/bin/hcidump /usr/bin/hcitool sudo chmod o-x /usr/bin/hcidump /usr/bin/hcitool sudo setcap 'cap_net_raw,cap_net_admin+eip' /usr/bin/hcitool sudo setcap 'cap_net_raw,cap_net_admin+eip' /usr/bin/hcidump
Install Python libraries
Now you're ready to install the Python support library for the BLE BroadcastNet bridge. Run the following command to install it:
pip3 install adafruit-circuitpython-ble-broadcastnet
Add Examples
Let's add some example code to try by cloning a Git repository locally.
Type cd
to go to your home directory on the Pi.
Note, you can type pwd
(print working directory) to see where you are at any time:
pi@raspberrypiBlinkaBridge:~ $ pwd /home/pi
Here we will clone the example directory. Type:
git clone https://github.com/adafruit/Adafruit_CircuitPython_BLE_BroadcastNet.git
Once it finishes copying the files, you can have a look. Go to the directory by typing cd Adafruit_CircuitPython_BLE_BroadcastNet
(Hint, you can press Tab to complete that long directory name after you type a couple of the letters).
Check out the contents by typing ls -1
pi@raspberrypiBlinkaBridge:~/Adafruit_CircuitPython_BLE_BroadcastNet $ ls -1 adafruit_ble_broadcastnet.py CODE_OF_CONDUCT.md docs examples LICENSE README.rst requirements.txt setup.py
The adafruit_ble_broadcastnet.py file is the Python library we'll be using when we run the code. The code we want to run on the Pi is inside the /examples directory. Let's go there by typing:
cd ./examples/
ble_broadcastnet_blinka_bridge.py is the one we'll run on the Pi (after setting up the secrets.py file below), while the others are examples we can run on different microcontrollers such as the CLUE, Circuit Playground Bluefruit, Feather nRF52840, Feather Sense, and ItsyBitsy nRF52840.
- ble_broadcastnet_battery_level_neopixel.py
- ble_broadcastnet_battery_level.py
- ble_broadcastnet_cpb.py
- ble_broadcastnet_expo_backoff.py
- ble_broadcastnet_multisensor.py
- ble_broadcastnet_simpletest.py
Adafruit IO
In order to used the Adafruit IO Internet of Things service (which is absolutely free to use) you'll need to log in with your Adafruit account. If you don't already have an Adafruit login, create one here.
If you haven't used Adafruit IO before, check out this guide for more info.
Once you have logged into your account, there are two pieces of information you'll need to place in your secrets.py file: Adafruit IO username
, and Adafruit IO key
. Head to io.adafruit.com and simply click the View AIO Key link on the left hand side of the Adafruit IO page to get this information.
Shhh Secrets
The ble_broadcastnet_blinka_bridge.py program will need to authenticate your Adafruit IO credentials in order to run. So, we'll create a file called secrets.py to contain this info.
Inside is a Python dictionary named secrets
with a line for each entry. Each entry has an entry name (say 'aio_username'
) and then a colon to separate it from the entry key 'daphne_von_person'
secrets = { 'aio_username' : '_your_aio_username_', 'aio_key' : '_your_big_huge_super_long_aio_key_' }
Use a text editor to create and save this file into the /Adafruit_CircuitPython_BLE_Broadcast/examples directory. Since you are ssh'd into the Pi, the easiest way to do this is with the nano text editor. Type:
nano secrets.py
In the nano editor that launches, copy and paste the secrets text from above, replacing the username and key with your own, then save & exit.
Your /examples directory should now look like this:
- ble_broadcastnet_battery_level_neopixel.py
- ble_broadcastnet_battery_level.py
- ble_broadcastnet_blinka_bridge.py
- ble_broadcastnet_cpb.py
- ble_broadcastnet_expo_backoff.py
- ble_broadcastnet_multisensor.py
- ble_broadcastnet_simpletest.py
- secrets.py
Service Identity
Next, we'll install the service_identity
module, which is used behind the scenes for certificate verification. Type this into the ssh terminal and press return:
pip3 install service_identity
Launch BroadcastNet Bridge
We're now ready to run the broadcast bridge software on the Pi! When this runs it will look for BLE sensors, collect their message data, and then send it to your Adafruit IO account as a uniquely named feed.
To run it, type this in the ssh shell and press return:
python3 ble_broadcastnet_blinka_bridge.py
If your Feather Sense and/or CLUE boards you set up earlier are powered on and within range, you will start to see the measurement values being received successfully:
bridge-dca63202680a-sensor-fdc492775544 [{'key': 'missed-message-count', 'value': 0}, {'key': 'acceleration-0-x', 'value': -5.6099700927734375}, {'key': 'acceleration-0-y', 'value': 2.15712833404541}, {'key': 'acceleration-0-z', 'value': 7.956132888793945}, {'key': 'pressure-0', 'value': 992.184814453125}] Done logging measurement to IO. Took 0.628571397000087 seconds bridge-dca63202680a-sensor-fdc492775544 [{'key': 'missed-message-count', 'value': 2}, {'key': 'acceleration-0-x', 'value': -5.6506500244140625}, {'key': 'acceleration-0-y', 'value': 2.1284141540527344}, {'key': 'acceleration-0-z', 'value': 7.968097686767578}, {'key': 'pressure-0', 'value': 992.178955078125}] Done logging measurement to IO. Took 1.1094765150000967 seconds bridge-dca63202680a-sensor-fdc492775544 [{'key': 'missed-message-count', 'value': 1}, {'key': 'magnetic-0-x', 'value': -0.9061675071716309}, {'key': 'magnetic-0-y', 'value': -21.879562377929688}, {'key': 'magnetic-0-z', 'value': -100.5845947265625}, {'key': 'temperature-0', 'value': 24.0244140625}] Done logging measurement to IO. Took 0.6365861880001376 seconds bridge-dca63202680a-sensor-fdc492775544 [{'key': 'missed-message-count', 'value': 0}, {'key': 'acceleration-0-x', 'value': -5.626718521118164}, {'key': 'acceleration-0-y', 'value': 2.1152544021606445}, {'key': 'acceleration-0-z', 'value': 7.981258392333984}, {'key': 'pressure-0', 'value': 992.23876953125}] Done logging measurement to IO. Took 0.6315450570000394 seconds
If you open your Adafruit IO Feeds page, you'll see this feed there.
Let's have a look at how to work with your feed data.
Page last edited March 08, 2024
Text editor powered by tinymce.