Controlling nRF52840 Express Feather from Raspberry Pi

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
travis_kumberland
 
Posts: 13
Joined: Tue Aug 10, 2021 1:29 pm

Controlling nRF52840 Express Feather from Raspberry Pi

Post by travis_kumberland »

Hello,

I'm working on my first Feather project, just got a nRF52840 Express. I've successfully got to the point where i've updated the bootloader, setup arduino IDE support, installed the iOS bluefruit Connect app....and I just successfully tested the Arduino Example: Bluefruit Examples->peripheral->neopixel...where I can control the color of the neopixel on the Feather from the bluefruit app on my iphone.

What I'm trying to figure out next (my question), is how I can accomplish the same task (control the color/brightness of the Feather's single neopixel), but instead of via the bluefruit iOS app...do so from a Linux box, e.g. Raspberry Pi with BLE support. I searched around (apologies if I missed something obvious) but got a bit lost in all the API documentation. In essence my questions are:

1) Is there an example already made (or close) that does what I'm trying to do?

2) If no to (1), would the best solution be something like?
a) Keep the Arduino neopixel example loaded on the Feather untouched.
b) Decode the command protocol for the Arduino program, i.e. what commands need to be sent over BLE to do various tasks like changing colors/brightness.
c) Figure out how to run a program from the linux box to: i) connect to the feather ii) send those commands defined in (b).

Thanks for any help getting me on the right track here!

User avatar
mikeysklar
 
Posts: 13824
Joined: Mon Aug 01, 2016 8:10 pm

Re: Controlling nRF52840 Express Feather from Raspberry Pi

Post by mikeysklar »

Good question of how to control a NeoPixel from a Linux box. We have a guide that details how to connect up a BLE board to a linux box.

https://learn.adafruit.com/circuitpytho ... y-computer
Adafruit CircuitPython supports using Bluetooth Low Energy (BLE) to communicate wirelessly with BLE devices, phones, tablets, and with other CircuitPython boards. Adafruit provides many libraries to make this easy and to support specific devices.

Now you can use those same libraries (or write your own) on any host computer--Windows, Mac, or Linux--that has BLE hardware. Most modern computers, especially laptops, already have Bluetooth hardware built in. If not, you can plug in a USB adapter such as Adafruit's Bluetooth 4.0 USB Module.

The Adafruit Blinka bleio library makes this possible. It is a regular Python library that runs on desktop Python, not on CircuitPython boards. It re-implements the _bleio module that is part of CircuitPython: all our BLE libraries are ultimately based on _bleio.

User avatar
travis_kumberland
 
Posts: 13
Joined: Tue Aug 10, 2021 1:29 pm

Re: Controlling nRF52840 Express Feather from Raspberry Pi

Post by travis_kumberland »

Thanks!

I followed the instructions...and am trying to get the UART example to work: https://learn.adafruit.com/circuitpytho ... rt-example

Following the example code I am able to successfully connect to the Feather...also reading seems to work...however I haven't successfully been able to write to the Feather. I've tried different combinations of ubuntu laptop, Raspberry Pi 3B...(both virtualenv and native installs) with no success. Below is my stack trace from an ipython session (note running from a normal python session or from command line results in the same thing except only displays Bad file description bit. Apologies, but my google fu failed me...any help appreciated.

OSError: [Errno 9] Bad file descriptor

Traceback (most recent call last):
File "/usr/lib/python3.7/asyncio/selector_events.py", line 251, in _add_reader
key = self._selector.get_key(fd)
File "/usr/lib/python3.7/selectors.py", line 192, in get_key
raise KeyError("{!r} is not registered".format(fileobj)) from None
KeyError: '0 is not registered'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/pi/.virtualenvs/python37/bin/ipython", line 8, in <module>
sys.exit(start_ipython())
File "/home/pi/.virtualenvs/python37/lib/python3.7/site-packages/IPython/__init__.py", line 126, in start_ipython
return launch_new_instance(argv=argv, **kwargs)
File "/home/pi/.virtualenvs/python37/lib/python3.7/site-packages/traitlets/config/application.py", line 846, in launch_instance
app.start()
File "/home/pi/.virtualenvs/python37/lib/python3.7/site-packages/IPython/terminal/ipapp.py", line 356, in start
self.shell.mainloop()
File "/home/pi/.virtualenvs/python37/lib/python3.7/site-packages/IPython/terminal/interactiveshell.py", line 564, in mainloop
self.interact()
File "/home/pi/.virtualenvs/python37/lib/python3.7/site-packages/IPython/terminal/interactiveshell.py", line 547, in interact
code = self.prompt_for_code()
File "/home/pi/.virtualenvs/python37/lib/python3.7/site-packages/IPython/terminal/interactiveshell.py", line 475, in prompt_for_code
**self._extra_prompt_options())
File "/home/pi/.virtualenvs/python37/lib/python3.7/site-packages/prompt_toolkit/shortcuts/prompt.py", line 1021, in prompt
set_exception_handler=set_exception_handler, in_thread=in_thread
File "/home/pi/.virtualenvs/python37/lib/python3.7/site-packages/prompt_toolkit/application/application.py", line 892, in run
self.run_async(pre_run=pre_run, set_exception_handler=set_exception_handler)
File "/usr/lib/python3.7/asyncio/base_events.py", line 584, in run_until_complete
return future.result()
File "/home/pi/.virtualenvs/python37/lib/python3.7/site-packages/prompt_toolkit/application/application.py", line 816, in run_async
return await _run_async2()
File "/home/pi/.virtualenvs/python37/lib/python3.7/site-packages/prompt_toolkit/application/application.py", line 792, in _run_async2
result = await _run_async()
File "/home/pi/.virtualenvs/python37/lib/python3.7/site-packages/prompt_toolkit/application/application.py", line 722, in _run_async
read_from_input
File "/usr/lib/python3.7/contextlib.py", line 112, in __enter__
return next(self.gen)
File "/home/pi/.virtualenvs/python37/lib/python3.7/site-packages/prompt_toolkit/input/vt100.py", line 170, in _attached_input
loop.add_reader(fd, callback_wrapper)
File "/usr/lib/python3.7/asyncio/selector_events.py", line 324, in add_reader
return self._add_reader(fd, callback, *args)
File "/usr/lib/python3.7/asyncio/selector_events.py", line 254, in _add_reader
(handle, None))
File "/usr/lib/python3.7/selectors.py", line 359, in register
self._selector.register(key.fd, poller_events)
OSError: [Errno 9] Bad file descriptor

If you suspect this is an IPython 7.27.0 bug, please report it at:
https://github.com/ipython/ipython/issues
or send an email to the mailing list at [email protected]

You can print a more detailed traceback right now with "%tb", or use "%debug"
to interactively debug it.

Extra-detailed tracebacks for bug-reporting purposes can be enabled via:
%config Application.verbose_crash=True

User avatar
mikeysklar
 
Posts: 13824
Joined: Mon Aug 01, 2016 8:10 pm

Re: Controlling nRF52840 Express Feather from Raspberry Pi

Post by mikeysklar »

On the BLE UART example page there are two programs.

The first needs to go on your nRF52 as code.py. The second will be saved on your linux machine as ble_eval_client.py.

Is that the setup you have going? Including the same link you provided just for clarity.

https://learn.adafruit.com/circuitpytho ... rt-example

User avatar
travis_kumberland
 
Posts: 13
Joined: Tue Aug 10, 2021 1:29 pm

Re: Controlling nRF52840 Express Feather from Raspberry Pi

Post by travis_kumberland »

Thanks for writing back, so originally I was not following the example exactly as stated...but rather trying to send raw UART commands to the Feather with another program that I wrote running on the Feather itself.

However in order to debug my issue, I have followed your advise of following the example exactly as stated here: https://learn.adafruit.com/circuitpytho ... rt-example

The good news is that both the client (my linux box or raspberry pi) and the server (the Feather) establish the connection as expected...I can see that it does from the Feather's perspective via the serial console....and from the linux box's perspective the script gets to the connected bit of the code. However after the the connection is established I get the same stack trace I linked above in my previous post (on the linux box)...and the serial console shows the lost connection from the Feather's perspective.

Note I have validated that my Circuitpython install is working correctly as I have successfully run the blinking NeoPixel example....and I did copy the necessary libraries for this new example as well onto the Feather's circuitpython lib.

A bit stuck now, appreciate any help you can offer.

User avatar
mikeysklar
 
Posts: 13824
Joined: Mon Aug 01, 2016 8:10 pm

Re: Controlling nRF52840 Express Feather from Raspberry Pi

Post by mikeysklar »

@travis_kumberland,

On the Feather nRF52 side which version of CircuitPython are you running, which bootloader and what release date of libraries?

User avatar
travis_kumberland
 
Posts: 13
Joined: Tue Aug 10, 2021 1:29 pm

Re: Controlling nRF52840 Express Feather from Raspberry Pi

Post by travis_kumberland »

Here's what I have:

circuit python libraries: adafruit-circuitpython-bundle-7.x-mpy-20210922
circuit python: adafruit-circuitpython-feather_nrf52840_express-en_US-7.0.0.uf2
bootloader: I tried both of these (with the above config for cp and libraries):
- feather_nrf52840_express_bootloader-0.3.2_s140_6.1.1.zip
- feather_nrf52840_express_bootloader-0.6.2_s140_6.1.1.zip

User avatar
mikeysklar
 
Posts: 13824
Joined: Mon Aug 01, 2016 8:10 pm

Re: Controlling nRF52840 Express Feather from Raspberry Pi

Post by mikeysklar »

Okay, things on the nRF52 controller side look as current as possible.

On the Pi side how are you executing the code? Can you cut out all the virtualenvs and other iPython stuff and just call python3 <script>? The errors you are seeing are not any I have come across.

User avatar
travis_kumberland
 
Posts: 13
Joined: Tue Aug 10, 2021 1:29 pm

Re: Controlling nRF52840 Express Feather from Raspberry Pi

Post by travis_kumberland »

Yep, here's what I get (note I named the script (for the raspberry pi side) ble.py):

----------------------------------
pi@raspberrypi:~ $ python3 ble.py
Trying to connect...
Connected
Eval: 2+2
2+2Traceback (most recent call last):
File "ble.py", line 24, in <module>
uart_service.write(b'\n')
File "/home/pi/.local/lib/python3.7/site-packages/adafruit_ble/services/nordic.py", line 99, in write
self._tx.write(buf)
File "/home/pi/.local/lib/python3.7/site-packages/adafruit_ble/characteristics/stream.py", line 34, in write
self.bound_characteristic.value = buf[offset : offset + 20]
File "/home/pi/.local/lib/python3.7/site-packages/_bleio/common.py", line 579, in value
response=self.properties & Characteristic.WRITE,
File "/home/pi/.local/lib/python3.7/site-packages/_bleio/common.py", line 140, in await_bleak
return future.result(timeout)
File "/usr/lib/python3.7/concurrent/futures/_base.py", line 432, in result
return self.__get_result()
File "/usr/lib/python3.7/concurrent/futures/_base.py", line 384, in __get_result
raise self._exception
File "/home/pi/.local/lib/python3.7/site-packages/bleak/backends/bluezdbus/client.py", line 812, in write_gatt_char
assert_reply(reply)
File "/home/pi/.local/lib/python3.7/site-packages/bleak/backends/bluezdbus/utils.py", line 23, in assert_reply
raise BleakDBusError(reply.error_name)
bleak.exc.BleakDBusError: org.bluez.Error.NotPermitted
----------------------------------------------------------------------------------------------------------------------------

So I searched around and came across this thread: https://githubmemory.com/repo/hbldh/bleak/issues/512

My raspberry pi was running bluez 5.50...so I took the hail marry advise and upgraded to bluez to 5.51. I now get a different error:

-----------------------------------------------------------------------------------------------
pi@raspberrypi:~ $ python3 ble.py
Trying to connect...
Traceback (most recent call last):
File "ble.py", line 12, in <module>
for adv in ble.start_scan(ProvideServicesAdvertisement):
File "/home/pi/.local/lib/python3.7/site-packages/adafruit_ble/__init__.py", line 258, in start_scan
active=active,
File "/home/pi/.local/lib/python3.7/site-packages/_bleio/common.py", line 248, in start_scan
self._scan_for_interval(self._SCAN_INTERVAL)
File "/home/pi/.local/lib/python3.7/site-packages/_bleio/common.py", line 140, in await_bleak
return future.result(timeout)
File "/usr/lib/python3.7/concurrent/futures/_base.py", line 432, in result
return self.__get_result()
File "/usr/lib/python3.7/concurrent/futures/_base.py", line 384, in __get_result
raise self._exception
File "/home/pi/.local/lib/python3.7/site-packages/_bleio/common.py", line 357, in _scan_for_interval
await self._scanner.start()
File "/home/pi/.local/lib/python3.7/site-packages/bleak/backends/bluezdbus/scanner.py", line 145, in start
assert_reply(reply)
File "/home/pi/.local/lib/python3.7/site-packages/bleak/backends/bluezdbus/utils.py", line 23, in assert_reply
raise BleakDBusError(reply.error_name)
bleak.exc.BleakDBusError: org.freedesktop.DBus.Error.UnknownObject
--------------------------------------------------------------------------------------------------------------------

Thanks again for any ideas!

User avatar
mikeysklar
 
Posts: 13824
Joined: Mon Aug 01, 2016 8:10 pm

Re: Controlling nRF52840 Express Feather from Raspberry Pi

Post by mikeysklar »

Let's try these two example scripts from the Adafruit_CircuitPython_BLE library repo. This should be slightly different than the code you had been running. One of them has been updated in the last 9 days.

ble_uart_echo_client.py
ble_uart_echo_test.py

https://github.com/adafruit/Adafruit_Ci ... /examples/

I think it can be run either way (pi or nRF52).

Let me know if this makes any difference.

User avatar
travis_kumberland
 
Posts: 13
Joined: Tue Aug 10, 2021 1:29 pm

Re: Controlling nRF52840 Express Feather from Raspberry Pi

Post by travis_kumberland »

Ok here's what I got...a couple notes:

1) Executed on pi (and a linux box) this script: https://github.com/adafruit/Adafruit_Ci ... _client.py

2) Executed on feather this script: https://github.com/adafruit/Adafruit_Ci ... ho_test.py

3) Note on (2), I added a one-liner to print "connected yay", so I could see that the connection was successful from the feathers end.

Here's the output

####### pi output

disconnected, scanning
connected
echo
echo
echo
Traceback (most recent call last):
File "code2.py", line 17, in <module>
uart.write(b"echo")
File "/home/pi/.local/lib/python3.7/site-packages/adafruit_ble/services/nordic.py", line 99, in write
self._tx.write(buf)
File "/home/pi/.local/lib/python3.7/site-packages/adafruit_ble/characteristics/stream.py", line 34, in write
self.bound_characteristic.value = buf[offset : offset + 20]
File "/home/pi/.local/lib/python3.7/site-packages/_bleio/common.py", line 579, in value
response=self.properties & Characteristic.WRITE,
File "/home/pi/.local/lib/python3.7/site-packages/_bleio/common.py", line 140, in await_bleak
return future.result(timeout)
File "/usr/lib/python3.7/concurrent/futures/_base.py", line 432, in result
return self.__get_result()
File "/usr/lib/python3.7/concurrent/futures/_base.py", line 384, in __get_result
raise self._exception
File "/home/pi/.local/lib/python3.7/site-packages/bleak/backends/bluezdbus/client.py", line 812, in write_gatt_char
assert_reply(reply)
File "/home/pi/.local/lib/python3.7/site-packages/bleak/backends/bluezdbus/utils.py", line 23, in assert_reply
raise BleakDBusError(reply.error_name)
bleak.exc.BleakDBusError: org.bluez.Error.NotPermitted


##### Feather output

connected yay
connected yay
connected yay
connected yay
connected yay
connected yay
*** Note stops printing after the failure above on the pi

User avatar
mikeysklar
 
Posts: 13824
Joined: Mon Aug 01, 2016 8:10 pm

Re: Controlling nRF52840 Express Feather from Raspberry Pi

Post by mikeysklar »

Okay, glad to see there were a few lines of UART data that made it through before the script on the linux side crashed.

Can you try upgrading your bluez package to 5.61 which is the current release. It does look like it is failing in that area.

http://www.bluez.org/release-of-bluez-5-61/

One bit of testing you can try with bluez is to see if you can communicate without the scripts.

Code: Select all

sudo bluetoothctl
scan on 
connect <BLE MAC ADDR>
scan off
info
list-attributes
menu gatt

User avatar
travis_kumberland
 
Posts: 13
Joined: Tue Aug 10, 2021 1:29 pm

Re: Controlling nRF52840 Express Feather from Raspberry Pi

Post by travis_kumberland »

Wanted to post resolution for anyone else that runs into similar problems. It was indeed a bluez issue, upgrading to 5.61 worked. Turns out the first time I upgraded it didnt install correctly. Thanks @mikeysklar for all your help!

User avatar
mikeysklar
 
Posts: 13824
Joined: Mon Aug 01, 2016 8:10 pm

Re: Controlling nRF52840 Express Feather from Raspberry Pi

Post by mikeysklar »

Awesome! Thank you for the confirmation. Good luck.

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”