Saturday, February 14, 2015

Raspberry Pi 2 and RPi.GPIO...

Raspberry Pi 2

So, you just bought a new Raspberry Pi 2 Model B. The one with 4 cores and 1GB of RAM. You want to run some python code that uses RPi.GPIO. You have the January 2015 Raspbian. You should be good to go, right?

RPi.GPIO

Let's revisit RPi.GPIO, the python module for interfacing with hardware on the Pi. Yes, it does come pre-installed. Latest Raspbian (January 2015) includes RPi.GPIO 0.5.9. But then, you try to run your script and it tells you




pi@raspberrypi ~ $ sudo python myscript.py
RuntimeError: This module can only be run on a Raspberry Pi!

Hmm, ok, whatever, this is a Raspberry Pi! So we will have to upgrade the RPi.GPIO python module. While at it, let's install the python dev, setuptools and pip:




pi@raspberrypi ~ $ sudo apt-get install python-dev python-setuptools
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
  libexpat1-dev libssl-dev libssl-doc python-pkg-resources python2.7-dev
Suggested packages:
  python-distribute python-distribute-doc
The following NEW packages will be installed:
  libexpat1-dev libssl-dev libssl-doc python-dev python-pkg-resources
  python-setuptools python2.7-dev
0 upgraded, 7 newly installed, 0 to remove and 23 not upgraded.
Need to get 32.1 MB of archives.
After this operation, 43.5 MB of additional disk space will be used.
Do you want to continue [Y/n]? y
Get:1 http://mirrordirector.raspbian.org/raspbian/ wheezy/main libexpat1-dev armhf 2.1.0-1+deb7u1 [210 kB]
Get:2 http://mirrordirector.raspbian.org/raspbian/ wheezy/main python-dev all 2.7.3-4+deb7u1 [920 B]
Get:3 http://mirrordirector.raspbian.org/raspbian/ wheezy/main libssl-dev armhf 1.0.1e-2+rvt+deb7u14 [1,505 kB]
Get:4 http://mirrordirector.raspbian.org/raspbian/ wheezy/main libssl-doc all 1.0.1e-2+rvt+deb7u14 [1,206 kB]
Get:5 http://mirrordirector.raspbian.org/raspbian/ wheezy/main python2.7-dev armhf 2.7.3-6+deb7u2 [28.7 MB]
Get:6 http://mirrordirector.raspbian.org/raspbian/ wheezy/main python-pkg-resources all 0.6.24-1 [63.6 kB]
Get:7 http://mirrordirector.raspbian.org/raspbian/ wheezy/main python-setuptools all 0.6.24-1 [449 kB]
Fetched 32.1 MB in 2min 3s (260 kB/s)
Selecting previously unselected package libexpat1-dev.
(Reading database ... 88962 files and directories currently installed.)
Unpacking libexpat1-dev (from .../libexpat1-dev_2.1.0-1+deb7u1_armhf.deb) ...
Selecting previously unselected package libssl-dev.
Unpacking libssl-dev (from .../libssl-dev_1.0.1e-2+rvt+deb7u14_armhf.deb) ...
Selecting previously unselected package libssl-doc.
Unpacking libssl-doc (from .../libssl-doc_1.0.1e-2+rvt+deb7u14_all.deb) ...
Selecting previously unselected package python2.7-dev.
Unpacking python2.7-dev (from .../python2.7-dev_2.7.3-6+deb7u2_armhf.deb) ...
Selecting previously unselected package python-dev.
Unpacking python-dev (from .../python-dev_2.7.3-4+deb7u1_all.deb) ...
Selecting previously unselected package python-pkg-resources.
Unpacking python-pkg-resources (from .../python-pkg-resources_0.6.24-1_all.deb) ...
Selecting previously unselected package python-setuptools.
Unpacking python-setuptools (from .../python-setuptools_0.6.24-1_all.deb) ...
Processing triggers for man-db ...
Setting up libexpat1-dev (2.1.0-1+deb7u1) ...
Setting up libssl-dev (1.0.1e-2+rvt+deb7u14) ...
Setting up libssl-doc (1.0.1e-2+rvt+deb7u14) ...
Setting up python2.7-dev (2.7.3-6+deb7u2) ...
Setting up python-dev (2.7.3-4+deb7u1) ...
Setting up python-pkg-resources (0.6.24-1) ...
Setting up python-setuptools (0.6.24-1) ...

pi@raspberrypi ~ $ sudo easy_install pip
Searching for pip
Reading http://pypi.python.org/simple/pip/
Best match: pip 6.0.8
Downloading https://pypi.python.org/packages/source/p/pip/pip-6.0.8.tar.gz#md5=2332e6f97e75ded3bddde0ced01dbda3
Processing pip-6.0.8.tar.gz
Running pip-6.0.8/setup.py -q bdist_egg --dist-dir /tmp/easy_install-pPyrAv/pip-6.0.8/egg-dist-tmp-UwhkN5
warning: no previously-included files found matching '.coveragerc'
warning: no previously-included files found matching '.mailmap'
warning: no previously-included files found matching '.travis.yml'
warning: no previously-included files found matching 'pip/_vendor/Makefile'
warning: no previously-included files found matching 'tox.ini'
warning: no previously-included files found matching 'dev-requirements.txt'
no previously-included directories found matching '.travis'
no previously-included directories found matching 'docs/_build'
no previously-included directories found matching 'contrib'
no previously-included directories found matching 'tasks'
no previously-included directories found matching 'tests'
Adding pip 6.0.8 to easy-install.pth file
Installing pip script to /usr/local/bin
Installing pip2.7 script to /usr/local/bin
Installing pip2 script to /usr/local/bin

Installed /usr/local/lib/python2.7/dist-packages/pip-6.0.8-py2.7.egg
Processing dependencies for pip
Finished processing dependencies for pip


pip install RPi.GPIO

Now, if we try to install RPi.GPIO, it'll complain that it is already installed. So we will have to use the --upgrade option:



pi@raspberrypi ~ $ sudo pip install RPi.GPIO
Requirement already satisfied (use --upgrade to upgrade): RPi.GPIO in /usr/lib/python2.7/dist-packages

Let's try --upgrade

pi@raspberrypi ~ $ sudo pip install --upgrade RPi.GPIO
Collecting RPi.GPIO from https://pypi.python.org/packages/source/R/RPi.GPIO/RPi.GPIO-0.5.10.tar.gz#md5=f1c67de40bf99e0612d75538b06a2181
  Downloading RPi.GPIO-0.5.10.tar.gz
Installing collected packages: RPi.GPIO
  Found existing installation: RPi.GPIO 0.5.9
    DEPRECATION: Uninstalling a distutils installed project (RPi.GPIO) has been deprecated and will be removed in a future version. This is due to the fact that uninstalling a distutils project will only partially uninstall the project.
    Uninstalling RPi.GPIO-0.5.9:
      Successfully uninstalled RPi.GPIO-0.5.9
  Running setup.py install for RPi.GPIO
    building 'RPi.GPIO' extension
    gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c source/py_gpio.c -o build/temp.linux-armv7l-2.7/source/py_gpio.o
    gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c source/c_gpio.c -o build/temp.linux-armv7l-2.7/source/c_gpio.o
    gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c source/cpuinfo.c -o build/temp.linux-armv7l-2.7/source/cpuinfo.o
    gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c source/event_gpio.c -o build/temp.linux-armv7l-2.7/source/event_gpio.o
    gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c source/soft_pwm.c -o build/temp.linux-armv7l-2.7/source/soft_pwm.o
    gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c source/py_pwm.c -o build/temp.linux-armv7l-2.7/source/py_pwm.o
    gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c source/common.c -o build/temp.linux-armv7l-2.7/source/common.o
    gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c source/constants.c -o build/temp.linux-armv7l-2.7/source/constants.o
    gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-z,relro build/temp.linux-armv7l-2.7/source/py_gpio.o build/temp.linux-armv7l-2.7/source/c_gpio.o build/temp.linux-armv7l-2.7/source/cpuinfo.o build/temp.linux-armv7l-2.7/source/event_gpio.o build/temp.linux-armv7l-2.7/source/soft_pwm.o build/temp.linux-armv7l-2.7/source/py_pwm.o build/temp.linux-armv7l-2.7/source/common.o build/temp.linux-armv7l-2.7/source/constants.o -o build/lib.linux-armv7l-2.7/RPi/GPIO.so
Successfully installed RPi.GPIO-0.5.10
pi@raspberrypi ~ $ 

OK, we are now ready to get to work. 0.5.10 works with the Raspberry Pi 2 model B.

Francois
@f_dion

4 comments:

Robert Murphy said...

I just did all of that and get this result

robert@MAGPIE /tmp $ rpio
Script not started as root. Running sudo...
Traceback (most recent call last):
File "/usr/local/bin/rpio", line 5, in
pkg_resources.run_script('RPIO==0.10.1', 'rpio')
File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 499, in run_script
self.require(requires)[0].run_script(script_name, ns)
File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 1235, in run_script
execfile(script_filename, namespace, namespace)
File "/usr/local/lib/python2.7/dist-packages/RPIO-0.10.1-py2.7-linux-armv7l.egg/EGG-INFO/scripts/rpio", line 353, in
main()
File "/usr/local/lib/python2.7/dist-packages/RPIO-0.10.1-py2.7-linux-armv7l.egg/EGG-INFO/scripts/rpio", line 147, in main
import RPIO
File "/usr/local/lib/python2.7/dist-packages/RPIO-0.10.1-py2.7-linux-armv7l.egg/RPIO/__init__.py", line 115, in
import RPIO._GPIO as _GPIO
SystemError: This module can only be run on a Raspberry Pi!


still.

Peniel Kidanemariam said...

i have the same problem robert

Francois Dion said...

RPIO hasn't been updated in two years, so it doesn't know about the Pi 2. The instructions here are for RPi.GPIO, a different module. The code for RPIO is here:

https://github.com/metachris/RPIO

It will need several changes to support the Pi 2. I would recommend to contact the author and open a bug on that github page.

Peniel Kidanemariam said...

i installed rpio from git, it works now. From this pythonhosted.org/RPIO.