Problem with gpio in Python


27 posts   Page 1 of 2   1, 2
by andy01842 » Wed Dec 26, 2012 12:02 pm
I am trying to setup python to use the gpio. I have downloaded RPi.GPIO-0.4.1a.tar.gz
and then run
tar xvzf RPi.GPIO-0.4.1a.tar.gz
changed to the directory and run
sudo python setup.py install

but get the error

source/py_gpio.c:23:20: fatal error: Python.h: No such file or directory
compilation terminated.
error: command 'gcc' failed with exit status 1

I have a dir source with a file py_gpio.c Help please I am lost where dose the 23:20 come from?
tnx Andy
Posts: 4
Joined: Fri Oct 05, 2012 9:33 am
by croston » Wed Dec 26, 2012 1:29 pm
If you are using Raspbian then RPi.GPIO should already be installed.
User avatar
Posts: 449
Joined: Sat Nov 26, 2011 12:33 pm
Location: Blackpool
by andy01842 » Wed Dec 26, 2012 3:05 pm
The first 2 lines of the python progarm I am trying to run are

import time
import RPi.GPIO as GPIO

the error i get when run is


Traceback (most recent call last):
File "/home/pi/temp/test.py", line 2, in <module>
import RPi.GPIO as GPIO
SetupException: No access to /dev/mem. Try running as root!

as far as I can tell thier is no GPIO lib on my pi so am trying to down load and set it up.
How do I run a python prog as root?
tnx Andy
Posts: 4
Joined: Fri Oct 05, 2012 9:33 am
by croston » Wed Dec 26, 2012 3:20 pm
andy01842 wrote:How do I run a python prog as root?

Code: Select all
sudo python myprogram.py
where myprogram.py is the name of your script.
User avatar
Posts: 449
Joined: Sat Nov 26, 2011 12:33 pm
Location: Blackpool
by andy01842 » Wed Dec 26, 2012 4:06 pm
Thanks that has got me a lot closer, now got a problem with the python program.
How would I run a python program from the IDLE on my pi desktop as root, so that I can sort my program which will be a long job with my programming skill or should I say lack of skill hi hi
Tnx Andy
Posts: 4
Joined: Fri Oct 05, 2012 9:33 am
by techpaul » Wed Dec 26, 2012 4:22 pm
click the menu 'button' bottom left

click on run

type sudo idle

or sudo idle3 for python3
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/
Posts: 1510
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
by mistletoekid » Wed Dec 26, 2012 5:39 pm
I'm connected to my Pi via Xming and Putty and a Win XP PC; I tried entering sudo idle at the Pi's terminal prompt but got an error message as follows:
PuTTY X11 proxy: wrong authentication protocol attemptedTraceback (most recent call last):
File "/usr/bin/idle", line 5, in <module>
main()
File "/usr/lib/python2.7/idlelib/PyShell.py", line 1406, in main
root = Tk(className="Idle")
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1688, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: couldn't connect to display "localhost:10.0"

Anyone got any ideas what to do next, please?
Posts: 4
Joined: Wed Dec 26, 2012 5:17 pm
by techpaul » Wed Dec 26, 2012 5:56 pm
idle is ONLY availble when running x-windows graphical terminal NOT command prompt
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/
Posts: 1510
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
by texy » Wed Dec 26, 2012 6:03 pm
I think you can only run tkinter locally, not via vnc, or remotely. That's why it comes up with the display error.
I could be wrong though, but that was my experience with tightvnc.
Texy
"2.8inch TFT LCD + Touch screen" add-on boards for sale here :
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=93&t=65566
50p goes to the Foundation ;-)
Forum Moderator
Forum Moderator
Posts: 2385
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England
by mistletoekid » Wed Dec 26, 2012 6:21 pm
Thanks for a v quick reply TechPaul (and Texy's comments) which helps my understanding but leaves me doubting if what I'm trying to do can be done at all.

Is there another way to run Idle as root via the remote set up I described - behind all this I have exactly the same problem as Andy01842 - cannot import RPi.GPIO errors when trying to run a Python Program from inside IDLE.
Posts: 4
Joined: Wed Dec 26, 2012 5:17 pm
by texy » Wed Dec 26, 2012 7:01 pm
If you write a simple python script without using tkinter but using RPi.GPIO what is the error message?
I use puTTy to ssh into my Pi's and from the command line I have no trouble using
Code: Select all
sudo python myprog.py

run a python program. This is without X started.
Texy
"2.8inch TFT LCD + Touch screen" add-on boards for sale here :
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=93&t=65566
50p goes to the Foundation ;-)
Forum Moderator
Forum Moderator
Posts: 2385
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England
by mistletoekid » Wed Dec 26, 2012 10:27 pm
It works OK from the command line (outside of IDLE) for me also - what I am trying to do is to run the GPIO program from within IDLE. I want to do this so I can then try extending the given code and learn about Python at the same time without having to keep switchng in and out of the IDE.

To answer your question though, if I try to run a simple import command as a program from within IDLE it says :
........SetUpException: No access to /dev/mem. Try running as root!
>>>
Posts: 4
Joined: Wed Dec 26, 2012 5:17 pm
by techpaul » Thu Dec 27, 2012 12:27 am
mistletoekid wrote:It works OK from the command line (outside of IDLE) for me also - what I am trying to do is to run the GPIO program from within IDLE. I want to do this so I can then try extending the given code and learn about Python at the same time without having to keep switchng in and out of the IDE.

To answer your question though, if I try to run a simple import command as a program from within IDLE it says :
........SetUpException: No access to /dev/mem. Try running as root!
>>>

If you are running Raspbian with LXDE desktop

click the menu 'button' bottom left of screen

on the programme menu click on 'Run' and type in the box
Code: Select all
sudo idle
or for python3
Code: Select all
sudo idle3
Then open your programme in idle now running as root. Running GPIO programmes this way WORKS have done this myself many times.

What distribution and desktop are you using?

Which IDLE are you trying to use IDLE or IDLE3
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/
Posts: 1510
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
by mistletoekid » Thu Dec 27, 2012 12:14 pm
Thanks for your patience and persistence with me on this one :-)
I've tried runninig the sudo idle command both from the LXTerminal Window started from the Pis GUI DEsktop and also by entering it (as you suggested) into a pop up run command window you get by clicking on the 'swift' icon bottom left of the screen. In both cases idle seems to run for about 2 seconds before giving the error message I reported earlier in this post. By the way there's no difference whether this is with Idle or Idle3.

Pi Details are as follows:
Linux raspberrypi 3.1.9+ #168 PREEMPT Sat Jul 14 18:56:31 BST 2012 armv6l
should be a standard wheezy set up.
I run Xming which has been set up per Simon the Pieman's instructions at:
http://simonthepiman.com/2-10-how_to_vi ... ows_PC.php
This seems to issue a startlxde command to the Pi via putty.
Posts: 4
Joined: Wed Dec 26, 2012 5:17 pm
by texy » Thu Dec 27, 2012 12:29 pm
Is there any reason why you are using such an old version of the OS? I strongly recommend you try the latest Wheezy Raspbian download. Note you will NOT need to install RPi.GPIO as it is already pre-installed.

Texy
"2.8inch TFT LCD + Touch screen" add-on boards for sale here :
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=93&t=65566
50p goes to the Foundation ;-)
Forum Moderator
Forum Moderator
Posts: 2385
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England
by bgreat » Thu Dec 27, 2012 1:17 pm
The problem you are having with "sudo idle" is related to using Ximg over an SSH tunnel via PuTTY.

You can confirm this by opening a LXTerminal window and entering the command:
Code: Select all
pi@raspberrypi ~ $ sudo idle
PuTTY X11 proxy: MIT-MAGIC-COOKIE-1 data did not matchTraceback (most recent call last):
...
_tkinter.TclError: couldn't connect to display "localhost:10.0"
pi@raspberrypi ~ $

A simple work around is to give root the proper authorization to use the X11 connection. Again, within a LXTerminal window:
Code: Select all
pi@raspberrypi ~ $ sudo xauth add `xauth list`

Note: The quote characters around `xauth list` are single back quotes (back-tick). On the US keyboard, they are on the same key as the ~ character. This command copies the active X authorization key from the current user to the root user, allowing root to have access to the X display.

Now, entering "sudo idle" will open the expected window.

Enjoy!
Bill
User avatar
Posts: 235
Joined: Mon Jan 23, 2012 2:09 pm
by texy » Thu Dec 27, 2012 1:35 pm
....I get this :

Code: Select all
pi@raspberrypi ~ $ sudo xauth add `xauth list`
xauth: (argv):1:  bad "add" command line



I,ve tried different combo's, but still the same error.

Texy
"2.8inch TFT LCD + Touch screen" add-on boards for sale here :
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=93&t=65566
50p goes to the Foundation ;-)
Forum Moderator
Forum Moderator
Posts: 2385
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England
by texy » Thu Dec 27, 2012 1:50 pm
...so I googled the xauth command, and found another way, although maybe not as slick :

Code: Select all
pi@raspberrypi ~ $ xauth list|grep `uname -n`
raspberrypi:1  MIT-MAGIC-COOKIE-1  c0c80fbc610fbc9da9ea93823c6b1e27
raspberrypi/unix:1  MIT-MAGIC-COOKIE-1  c0c80fbc610fbc9da9ea93823c6b1e27
raspberrypi:2  MIT-MAGIC-COOKIE-1  e21343b7a0c099e28c9f62435b63a545
raspberrypi/unix:2  MIT-MAGIC-COOKIE-1  e21343b7a0c099e28c9f62435b63a545
raspberrypi/unix:0  MIT-MAGIC-COOKIE-1  eb38e53ef0e69b378e3fb14e202721ff
raspberrypi:0  MIT-MAGIC-COOKIE-1  eb38e53ef0e69b378e3fb14e202721ff
pi@raspberrypi ~ $ DISPLAY=:1
pi@raspberrypi ~ $ sudo xauth add $DISPLAY . c0c80fbc610fbc9da9ea93823c6b1e27
pi@raspberrypi ~ $


..and now I am able to usetightvnc from my laptop to run tkinter python scripts.
Many thanks for this tip.

Texy
"2.8inch TFT LCD + Touch screen" add-on boards for sale here :
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=93&t=65566
50p goes to the Foundation ;-)
Forum Moderator
Forum Moderator
Posts: 2385
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England
by bgreat » Thu Dec 27, 2012 5:09 pm
Looks like you have multiple entries for xauth (multiple connections active/registered). I only had one, so I went for the simple command. I'm glad it pointed you in the right direction.

Here is a simple way to make all of the pi user connections available for root, bypassing the xauth command:
Code: Select all
sudo cp ~pi/.Xauthority ~root/


This just copies the X11 authority file directly to root without the xauth command management. This will make all active user pi X11 connections available for use by root without having to sort through and find the correct one.

Enjoy!
Bill
User avatar
Posts: 235
Joined: Mon Jan 23, 2012 2:09 pm
by Nipper » Sun Dec 30, 2012 12:09 am
I have been having a problem with GPIO. I type in the following lines into Python (tried both of them):

wget http://raspberry-gpio-python.googlecode ... 2.0.tar.gz
tar xvzf RPi.GPIO-0.2.0.tar.gz
cd RPi.GPIO-0.2.0
sudo python setup.py install
import RPi.GPIO as GPIO

I then press enter and I get "Syntax Error" message. I have retyped these 5 lines of code so many times now I am past frustration. Help...!
User avatar
Posts: 39
Joined: Tue Dec 25, 2012 4:55 pm
by bgreat » Sun Dec 30, 2012 6:08 am
First a question. What OS distribution are you running? If you are using Raspbian, you do not need to download or install the GPIO library.

Of the commands you indicate you are typing, only the last line is a python command. The rest are Linux commands that would be issued from a shell prompt before starting python.

To use the the GPIO module, you must run the python command as root, i.e. using 'sudo'

Interactive example, in a LXTerminal window or in text mode:
Code: Select all
pi@raspberrypi ~ $ sudo python
Python 2.7.3rc2 (default, May  6 2012, 20:02:25)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import RPi.GPIO as GPIO
>>> GPIO.setmode(GPIO.BCM)
>>> GPIO.setup(25, GPIO.OUT)
>>> GPIO.output(25, 1)
>>> GPIO.output(25, 0)
>>> GPIO.cleanup()
>>> quit()
pi@raspberrypi ~ $

This interactive example will set output pin 25 high (3.3v) then low (0v). Then the GPIO is released and return to the bash command prompt.

Enjoy!
Bill
User avatar
Posts: 235
Joined: Mon Jan 23, 2012 2:09 pm
by fdion » Mon Dec 31, 2012 4:01 am
mistletoekid wrote:Thanks for a v quick reply TechPaul (and Texy's comments) which helps my understanding but leaves me doubting if what I'm trying to do can be done at all.

Is there another way to run Idle as root via the remote set up I described - behind all this I have exactly the same problem as Andy01842 - cannot import RPi.GPIO errors when trying to run a Python Program from inside IDLE.


idle does run remotely through X forwarding. Set up http://raspberry-python.blogspot.com/2012/09/sidekick.html and then xauth list and then sudo su, then xauth add the cookie to your root session. idle should work with sudo remotely.

François
Francois
http://raspberry-python.blogspot.com - @f_dion
User avatar
Posts: 305
Joined: Sun Sep 16, 2012 2:33 pm
Location: North Carolina, USA
by mandresc » Thu Jan 03, 2013 12:53 am
Hi Andy

Before you install the RPi.GPIO-0.4.1a, run:

sudo apt-get install python-rpi.gpio
sudo apt-get install python-dev

Then install the RPi.GPIO-0.4.1a and is ready. http://pypi.python.org/pypi/RPi.GPIO to follow the commands.

That worked for me. I'm using raspbian From the uname -a , Linux raspberry 3.2.27 and I do not think you need the latest distro for this to run.

A
Posts: 1
Joined: Thu Jan 03, 2013 12:38 am
by croston » Thu Jan 03, 2013 8:38 am
mandresc wrote:Hi Andy

Before you install the RPi.GPIO-0.4.1a, run:

sudo apt-get install python-rpi.gpio
sudo apt-get install python-dev

Then install the RPi.GPIO-0.4.1a and is ready. http://pypi.python.org/pypi/RPi.GPIO to follow the commands.

That worked for me. I'm using raspbian From the uname -a , Linux raspberry 3.2.27 and I do not think you need the latest distro for this to run.

A

This is complete rubbish. RPi.GPIO is already installed in recent versions of Raspbian. If you have a really old version then the commands you would want are:
Code: Select all
sudo apt-get update
sudo apt-get install python-rpi.gpio

The python-dev package is only required if you are building a python module from source yourself.
User avatar
Posts: 449
Joined: Sat Nov 26, 2011 12:33 pm
Location: Blackpool
by tommycheok » Sun Mar 10, 2013 8:56 am
texy wrote:I think you can only run tkinter locally, not via vnc, or remotely. That's why it comes up with the display error.
I could be wrong though, but that was my experience with tightvnc.
Texy


can access with VNC with code :
Code: Select all
xhost +
quoted from DBryant
Posts: 21
Joined: Tue Feb 26, 2013 12:49 pm