Python GPIO

I am just experimenting with GPIO on my vrsion 2 Pi.
I am using Debian Wheezy which I have just updated in hopes of
overcoming the problems I have encountered.
In trying to run the initial program to detect a button press I get a
syntax error with 'import' as in "sudo import RPi.GPIO as GPIO" and
"sudo import time".
Not using sudo does not help.
Can anyone help, lots of reading has not helped.
Malcolm Smith
--
T M Smith 
Using an Iyonix and RISC OS 5.20 in the North Riding of Yorkshire
Reply to
T M Smith
Loading thread data ...
Install Raspbian not Debian.
However the import commands should be inside a Python program.
You may then have to use sudo to run the program.
Gordon
Reply to
Gordon Henderson
I suspect Malcolm is already using Raspbian.
If the program is in a file called something.py and he's trying to run it by: directory/something.py like ./something.py
then he'll probably need #!/usr/bin/python
as the first like of the file to indicate it's a Python program.
If he's running it via 'python something.py' then this shouldn't be necessary.
Try it without - if you get 'permission denied' then you may need it.
Theo
Reply to
Theo Markettos
Is that within a python shell or at the normal command prompt? In a python shell it doesn't understand sudo. At the normal command prompt sudo and/or the shell won't understand import ... etc.
You need to create a file with the following content (and set the permissions to make it executable).
#!/usr/bin/env python import time import RPi.GPIO as GPIO # Set GPIO numbering (mandatory) GPIO.setmode(GPIO.BCM)

GPIO.cleanup()
--
Cheers 
Dave.
Reply to
Dave Liquorice
Any reason why you use env when you're not actually using it to manipulate the environment? I mean it could just be #!/usr/bin/python
Reply to
A. Dumas
Yes
They are
That is present though I did not know its purpose
Removed the sudo's and 'import' problem has gone away.
But I now have 'Runtime error: no access to /dev/mem'
Malcolm
--
T M Smith 
Using an Iyonix and RISC OS 5.20 in the North Riding of Yorkshire
Reply to
T M Smith
Typed in an IDLE3 window, saved and results are displayed in Python shell window.
That is new to me?
This code is different to the code given in 'Magpie' BCM replacing BOARD. cleanup not in demo program.
Malcolm
--
T M Smith 
Using an Iyonix and RISC OS 5.20 in the North Riding of Yorkshire
Reply to
T M Smith
One reason is that env is cleverer about searching for things. For example, Python is /usr/local/bin/python on FreeBSD. If you hardcode the bang path, it won't find it. Likewise /usr/bin/perl v /bin/perl (on some systems)
The trouble with env is it doesn't pass arguments, so #!/usr/bin/env perl -w doesn't work
Theo
Reply to
Theo Markettos
I suspect your main problem is that it is hard to run as root from IDLE, and GPIO stuff needs to be run as root. If, as I think others have suggested, you create your program file, and then go to a Terminal to use sudo to run it, things should work.
I just grabbed the program text from the MagPi that I think you're using:
#!/usr/bin/python import time import RPi.GPIO as GPIO GPIO.setmode(GPIO.BOARD) GPIO.setup(11, GPIO.IN) while True: mybutton = GPIO.input(11) if mybutton == False: print "giggle" time.sleep(.2)
...entered it into the nano editor [I'm running via ssh -- no GUI --, so I can't use IDLE myself] to create 'gpiotest.py', made that file executable, and from the Terminal did "sudo ./gpiotest.py".
Works fine (despite not actually having anything connected to the pins (:-)).
-- Pete --
Reply to
Pete
Ah yes. I don't suppose the location of env itself differs too much? :)
Is that shebang-line-specific? Because argument passing is supported according to the man pages for env on Raspbian and OS X, and it works on the commandline on both ("env ls" vs. "env ls -l").
Thanks.
Reply to
A. Dumas
"sudo python"
upi@raspberrypi ~ $ sudo python Python 2.7.3 (default, Jan 13 2013, 11:20:46) [GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more information.
you may want to write a python script instead of using the command-line...
--
umop apisdn
Reply to
Jasen Betts
It's based on presumption the path to env is more predictable than the path to python.
--
umop apisdn
Reply to
Jasen Betts
TBH I hadn't noticed, The only bit of python I have "done" (aka. stole from the net somewhere) is to switch off the LED on the camera. B-)
I don't have an itch to detect putton pushes and switch LEDs on an off. One Pi runs XBMC the other is working as a webcam producing stills and time lapse mp4's. Having to encode with mencoder that takes about an hour as the Pi's hardware encoding produces really naff output even if it does it in about 1/30th the time.
--
Cheers 
Dave.
Reply to
Dave Liquorice
get a
and
command
I donno from yoru orginal post there isn't enough information and something very fundemental going wrong.
BCM uses the numbers on the Pi's SOC chip to reference the PIO's. BOARD uses the pin numbers of the header, these vary between model A and B and B rev1 and B rev2 boards. In theory using BCM makes the code portable but you have to do the chip GPIO number to physical pin number mapping yourself in hardware.
.cleanup() resets any PIO's etc that have been adjusted by the program to the default state. Just good practice really, if another program comes along and finds things non-default is that just mess left behind or is something else using that PIO? cleanup() can be told to only clean up some or all of the PIOs that have been used.
--
Cheers 
Dave.
Reply to
Dave Liquorice
You'r getting there. B-)
At the normal command prompt "sudo " should work. You might need to give the full path or prefix with ~/ or ./ depending on where the script file is.
--
Cheers 
Dave.
Reply to
Dave Liquorice
Thanks.
Reply to
A. Dumas
It's shebang and OS specific. It works on Linux, on Mac OS, but not on FreeBSD. Or roughly that, I forget the permutations. The issue is that the shell parses the command into 'env' 'ls' '-l' while shebang parses it into 'env' 'ls -l' - and then there's no such executable as 'ls -l'. shebang is supposed to take exactly one trailing argument and execute it, so technically this is correct (but unhelpful).
Theo
Reply to
Theo Markettos
Thanks for all the help from everyone. I am now able to run the two demo programs.
Malcolm
--
T M Smith 
Using an Iyonix and RISC OS 5.20 in the North Riding of Yorkshire
Reply to
T M Smith
Agreed - I do it in all my python and rexx programs.
Reply to
Bob Martin

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.