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.
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.
Related
I have a python script that records audio from an I2S MEMS microphone, connected to a Raspberry PI 3.
This script runs as supposed to, when accessed from the terminal. The problem appears when i run it as a service in the background.
From what i have seen, the problem is that the script as service, has no access to a software_volume i have configured in asoundrc. The strange thing is that i can see this "device" in the list of devices using the get_device_info_by_index() function.
For audio capturing i use the pyaudio library and for making the script a service i have utilized the supervisor utility.
Any ideas what the problem might be and how i can make my script to have access to asoundrc when it runs as a service?
The ~/.asoundrc file is looked for the home directory of the current user (this is what ~ means).
Put it into the home directory of the user as which the service runs, or put the definitions into the global ALSA configuration file /etc/asound.conf.
I have a shell script which starts a simulation environment (ROS together with Underwater Simulation) on Ubuntu 12.04. In order to use the simulation environment in general ones component needs to communicate with the environment via TCP/IP.
If I start the script for the simulation on a standard terminal the simulation environment starts like it's supposed to. It also binds to a previously defined IP address, no errors or warning. I can work with it without any limitation. Now here comes my problem: I need to start the simulation in a detached screen (do to requirements). I use
screen -d -m -S UWSim bash -c 'export $USER=~ ; ~/uwsim_ws/uwsim_starter.sh'
I need to set the USER variable which is not set by default in a screen, otherwise the script doesn't find other subscripts of ROS. After executing the command above, at some point during the startup of ROS I get the following error:
...
Traceback (most recent call last):
File "/usr/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 99] Cannot assign requested address
...
Due to this error my components can't connect to the simulation. As I already mentioned: everything works perfectly when started in a standard terminal. My guess is that there are some export's missing. I already compared all exports in the terminal with the exports in the screen (this led me to set the USER variable), without any success.
I would do anything shell-related in the uwsim_starter.sh shell instead of inside your screen command. Thanks to that, your screen command would be simpler
then export $USER is NOT supposed to set $USER to anything ! use
export USER=~
instead
Also, Cannot assign requested address usually appears when another process is already listening at the same ip:port or *:port on the same server. Please check that using
netstat -plnt | grep <port number of your application>
and search for someone else in LISTEN mode
It tourned out, that the script tries to bind to an IP address which was not assinged to any NIC. When I start the script outside of a screen it uses localhost (like supposed), when startet inside, it uses some default address (no idea where this comes from). Anayway, thanks everybody for your feedback
I am extremely new to Python but discovered that I need it in order to programme my Digi ConnectPort TS1W MEI gateway. What I want to do is read from the serial port periodically then send this data wirelessly to the nearest access point. What I have started with is trying to open, read, write and close the serial port, and I have got the following code so far:
https://www.dropbox.com/sh/1yq4x09xi5w4dsm/CuuZCIjwRT
But the code was not designed for my model of gateway, and the error message I received is:
Traceback (most recent call last):
File “WEB/python/serial_loopback.py”, line 28, in ? import serial.
ImportError: No module named serial.
When running the following command through Telnet:
python serial_loopback.py
I'm not sure if any of you Python programmers out there would know how to go about fixing this sort of thing?
I think #msvalkon means you need to:
pip install pyserial
See installation docs:
pyserial#installation
So I have an SSH session to a windows XP box via WinSSHD (from Ubuntu). I coded up a small Python program to take a screenshot with pywin32. Everything works fine when I run the script on the XP box directly in a normal 'desktop' session but if I SSH in and run the script the script fails:
Traceback (most recent call last):
File "ss.py", line 38, in <module>
win32_ss()
File "ss.py", line 19, in win32_ss
cDC.BitBlt((0, 0),(w, h) , dcObj, (0, 0), win32con.SRCCOPY)
win32ui.error: BitBlt failed
I am assuming this is because the SSH session is a different session than the XP session and has no access to the 'desktop' display device. I found reference on how to do this (I think this is what I want to do) in linux:
DISPLAY=:0.0 import -window root /tmp/shot.png
I suppose what I'm looking for is how to do this in windows. Again, I'm using Python and can install any module needed. I currently am using pywin32 as this is much faster than PIL.
I think SSH is the problem. I am not sure if SSH is the best way to do this. The best way would be to do the screenshot work at Windows' end. I have done something similar recently. I wanted to take a screenshot of a webpage opened in Internet Explorer and I am using Selenium Webdriver for this purpose with Python bindings for it.
What I did was that I decided to do it with the help of TCP sockets. I just wrote a very simple Python script that opens a TCP socket on the Windows machine and listens for connections. As soon as it receives the connection, it does the work of taking the screenshot. So in your case, it can take the screenshot there and save it locally and then the script can SSH to the system where you want this file to be.
Hope this helps.
I am trying to install and start a simple CherryPy server as a Windows service.
Here is the script: (Removed some lines to cut it short. It's fully working when executing manually from the command-line)
app = AdminMediaHandler(django.core.handlers.wsgi.WSGIHandler())
logged_app = TransLogger(app)
server = wsgiserver.CherryPyWSGIServer( ('127.0.0.1', 8632), logged_app, server_name='localhost', numthreads=20 )
try:
server.start()
except KeyboardInterrupt:
server.stop()
I'm using sc.exe to install and start the service. Installation goes fine, but I can't seem to start the service.
The command used is: (note there're spaces in the paths, though I'm handeling this with double-quotes, and the binPath is working when executing its string manually through the command-line)
> sc.exe create "ServiceName" binPath= "\"C:\Path to Python\python.exe\" \"C:\Path to CherryPy Script\cherryserver.py\""
> sc.exe start "ServiceName"
I keep getting this error, no matter if attempting to start the service using sc.exe or through services.msc GUI:
[SC] StartService FAILED 1053:
The service did not respond to the start or control request in a timely fashion.
From what I understand, this is happenning because python.exe doesn't implement the Windows Service API.
I do not wish to create an .exe from the script, using py2exe.
I have found this answer that suggests to install the service using different tools than sc.exe, called srvany.exe & instsrv.exe. However, I can't find them in the Win2K Resource Kit website.
Does anybody know how to install & start this .py as a Windows succesfully?
Does anybody know
CherryPy ships with a module for starting as a Windows service. See this other SO question for instructions on how to install and run it. You'll probably want to switch from your current approach (of passing the Django app directly to the WSGIServer) and use cherrypy.tree.graft instead.
I prefer nssm for installing normal scripts as a service.
You can copy the nssm.exe in the C:\Windows\system32 or C:\Windows\SysWOW64 directory depending on your system. After that you are able to install a Service as follow:
nssm install yourservicename
For a python script you must set the application path to your python.exe and the argument is your script self.
Other common commands for start/stop/edit your service are:
nssm start yourservicename
nssm stop yourservicename
nssm edit yourservicename
I eventually used ServiceInstaller aka SMaster, as stated in this answer. The URL in the given answer is broken, and I couldn't find a working URL. I just had srunner.exe available locally beforehand.
Note there was another obstacle to overcome though, as ServiceInstaller can't handle files with spaces in their paths.
So, I used the old DOS path formatting for service registration.
Instead of registering C:\Program Files\MyApp\python.exe, I registered C:\PROGRA~1\MyApp\python.exe.