Kaylee
Kaylee

Reputation: 33

Conflict between PyMonzo library and Adafruit_Neopixel library using Raspberry Pi 3

I am currently using both the PyMonzo library and the Adafruit_Neopixel library in my python on Raspberry Pi. In short, I can run the PyMonzo codes if I run as a normal user, but I need to run the Neopixel codes as root. However, PyMonzo codes give me errors when I run as root, while the Neopixel codes give me errors when I do not run as root.

I use the PyMonzo library to access the Monzo API, get my account information, and output that onto a screen. The library can be found here: https://github.com/pawelad/pymonzo I have managed to set up the API access following all the instructions in the readme, saving the client ID, client secret and auth code in the auth.py file. The codes work when I run them normally, i.e. python3 filename.py, and I can obtain my transaction data, balance, etc. with no problem.

However, I am also using the Adafruit Neopixel library to control some LED lights at the same time. The library can be found here: https://github.com/adafruit/Adafruit_NeoPixel Using this library apparently has two constraints: it has to be run in Python 3, and it has to be run as root. The documentation explains that "For NeoPixels to work on Raspberry Pi, you must run the code as root! Root access is required to access the RPi peripherals." Further documentation can be found here: https://cdn-learn.adafruit.com/downloads/pdf/neopixels-on-raspberry-pi.pdf

When I run sudo python3 filename.py, the error message:

Traceback (most recent call last):
File "filename.py", line 34, in 
monzo = MonzoAPI( )
File "/usr/local/lib/python3.5/dist-packages/pymonzo/monzo_api.py", line 106, in init
"To authenticate and use Monzo public API you need to pass "
ValueError: To authenticate and use Monzo public API you need to pass (or set as environment variables either the access token or all of client ID, client secret and authentication code. For more info see https://github.com/pawelad/pymonzo#authentication

If I run the code normally without using sudo, the error I get is:

Can't open /dev/mem: Operation not permitted
Traceback (most recent call last):
File "filename.py", line 66, in 
neopix.show( )
.....
RuntimeError: ws2811_init failed with code -5 (mmap( ) failed)
swig/python detected a memory leak of type 'ws2811_t *', no destructor found

Does anyone know if there is a way to deconflict between these two operations, perhaps a way that pyMonzo can be run as root?

I have tried running sudo chmod 666 /dev/mem, and running it without sudo.

Thank you very much.

Upvotes: 0

Views: 559

Answers (1)

brianfit
brianfit

Reputation: 1919

I don't know how you can run pyMonzo as root, but I know how you can run your python3 Neopixel code as not-root.

I faced a similar issue and found no joy in Adafruit's documentation. But if you go to the documentation of the rpi_ws281x library itself there's more info:

The GPIOs that can be used are limited by the hardware of the Pi and will vary based on the method used to drive them (PWM, PCM or SPI).

I found it advantageous to drive them with SPI as I was using audio on the board, and you need to disable that to use PWM or PCM.

Discovered then that if you run your Neopixels from pin GPIO 10 and use SPI instead of PWM or PCM, that brings with it this nifty little benefit:

SPI requires you to be in the gpio group if you wish to control your LEDs without root.

So add your user to the gpio group and hey presto, you're good to run without root.

Upvotes: 1

Related Questions