User avatar
Grumpy Mike
Posts: 890
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

Re: WiringPython - WiringPi for Python

Tue Oct 16, 2012 4:38 pm

OK thanks.
It has been that long since I used it I forgot that you have to launch it with sudo from a command line. Launching it from IDLE kindly suppresses the segmentation fault message that would have been so helpful. As it was it just quit.
It still works, thanks.

bitbitbit
Posts: 30
Joined: Thu Aug 30, 2012 3:18 am

Re: WiringPython - WiringPi for Python

Wed Oct 17, 2012 4:46 am

texy wrote:The method I use for my LCD's is slightly different, but still uses wiringpi for python. Try this :

Code: Select all

#!/usr/bin/python
import wiringpi
from time import sleep

LED  = 1  # gpio pin 12 = wiringpi no. 1

wiringpi.wiringPiSetup()
wiringpi.pinMode(LED, 1) # output
while True:
    wiringpi.digitalWrite(LED, 1)  # Turn on light
    sleep(2)
    wiringpi.digitalWrite(LED, 0)  # Turn off
    sleep(2)
...you may need to format it correctly for python, this is just hand cut & paste.

Texy
It worked!! :lol: :lol: :lol: :lol: :lol: :lol: :lol: thanks!!

texy
Forum Moderator
Forum Moderator
Posts: 5145
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: WiringPython - WiringPi for Python

Wed Oct 17, 2012 6:28 am

What I would like to know is if using the wiringpi method of pin ID works for v2 boards without further coding when using GPIO 21 & 27. Apparently it is automatic for C but untested for python.
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

User avatar
Grumpy Mike
Posts: 890
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

Re: WiringPython - WiringPi for Python

Wed Oct 17, 2012 10:26 am

texy wrote:What I would like to know is if using the wiringpi method of pin ID works for v2 boards without further coding when using GPIO 21 & 27. Apparently it is automatic for C but untested for python.
Texy
Just tried it and it does not work with Python. :(
Tried it on pins 2, 8 & 9 corresponding to GPIO 27 / 21, 2 / 0 and 3 / 1
It works on other pins though, just checking nothing else was causing the problem.

texy
Forum Moderator
Forum Moderator
Posts: 5145
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: WiringPython - WiringPi for Python

Wed Oct 17, 2012 10:31 am

We either need gadgetoid to fix it (not heard from him for a while) or some clever person to write the code check which version board is being used and assign the correct pin.
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

User avatar
Grumpy Mike
Posts: 890
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

Re: WiringPython - WiringPi for Python

Wed Oct 17, 2012 11:58 am

Well this will check for what board you have:-

Code: Select all

# Board revision test
print"Hi from Python :- Board revision finder"
# open file
a=0
fin = open('/proc/cpuinfo')
boardRevision = -1
while True: # go through the file line by line
 line = fin.readline()
 if not line: break # end if reached the end of the file
 if "Revision" in line:
     rev = line[11:15]
     print "revision number ", rev
     if rev == "0002" or rev == "0003" :
         boardRevision = 1
     if rev == "0004" or rev == "0005" or rev == "0006" :
         boardRevision = 2
     print "Board revision ", boardRevision
fin.close()
if boardRevision == -1: print "Error can't find board revision"

texy
Forum Moderator
Forum Moderator
Posts: 5145
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: WiringPython - WiringPi for Python

Wed Oct 17, 2012 12:26 pm

Great! That code can then be used to assign the correct GPIO pin for 21/27, 0/2 and 1/3 depending on if its a revision 1 or 2. However, if wiringpi for python can utilise Gordon's latest wiringpi code, then it shouldn't be needed as its taken care of in the background (if I,m understanding it corrrectly).

Cheers,
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

User avatar
Grumpy Mike
Posts: 890
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

Re: WiringPython - WiringPi for Python

Wed Oct 17, 2012 7:25 pm

as its taken care of in the background (if I,m understanding it corrrectly).
Yes but only if you use the wiring Pi numbering system. I much prefer to use real GPIO numbers.

texy
Forum Moderator
Forum Moderator
Posts: 5145
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: WiringPython - WiringPi for Python

Wed Oct 17, 2012 8:05 pm

At the end of the day it doesn't matter what system you use as long as you understand what your doing ;-)
And as using the wiring pi numbering system has this advantage, if indeed it works, then I'm happy using it.
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

User avatar
Grumpy Mike
Posts: 890
Joined: Sat Sep 10, 2011 7:49 pm
Location: Manchester (England England)
Contact: Website

Re: WiringPython - WiringPi for Python

Thu Oct 18, 2012 11:15 am

texy wrote:At the end of the day it doesn't matter what system you use as long as you understand what your doing ;-)
Texy
Well I think adding an unnecessary layer of abstraction helps you not understand what you are doing and provides a barrier for further learning.

User avatar
[email protected]
Posts: 1979
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: WiringPython - WiringPi for Python

Sat Oct 20, 2012 12:12 pm

Grumpy Mike wrote:
as its taken care of in the background (if I,m understanding it corrrectly).
Yes but only if you use the wiring Pi numbering system. I much prefer to use real GPIO numbers.
Just remember that now you need to know if you're running on a Rev 1 or Rev 2 board though as some GPIO pin numbers changed between revisions.

-Gordon
--
Gordons projects: https://projects.drogon.net/

devnull
Posts: 65
Joined: Sat Dec 24, 2011 7:46 am

Re: WiringPython - WiringPi for Python

Sat Oct 20, 2012 12:24 pm

This is a message to Gadgetoid I guess, would it be possible to update the python wiringPi port so that it includes the latest changes to wiringPi ??

I really need the interrupt feature which was added a few months ago and would really appreciate if the latest wiringPi could be ported to python again.

Many Thanks

Pete
> /dev/null 2>&1

texy
Forum Moderator
Forum Moderator
Posts: 5145
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: WiringPython - WiringPi for Python

Wed Oct 24, 2012 11:48 am

I have made contact with Gadgetoid, and he is on the case, so watch this space ;)

Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2810
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: WiringPython - WiringPi for Python

Fri Nov 09, 2012 9:28 am

Grumpy Mike wrote:Well this will check for what board you have:-

Code: Select all

# Board revision test
--------------SNIP------------
Just tried this and wondered why it gave me so much grief - until I realised that having overvolted, it changes the output in revision from 0002 to 1000002. Had to modify the script so it works in all cases.
Basically changed the slice in line 11 so it takes the last four non \n characters, which should work for all boards :) Also removed a = 0 which doesn't do anything :) (I expect that's legacy from something else). Here's the updated code...

Code: Select all

# Board revision test
print"Hi from Python :- Board revision finder"
# open file

fin = open('/proc/cpuinfo')
boardRevision = -1
while True: # go through the file line by line
    line = fin.readline()
    if not line: break # end if reached the end of the file
    if "Revision" in line:
        rev = line[-5:-1]
        print "revision number ", rev
        if rev == "0002" or rev == "0003":
            boardRevision = 1
        if rev == "0004" or rev == "0005" or rev == "0006":
            boardRevision = 2
        print "Board revision ", boardRevision
fin.close()
if boardRevision == -1: 
    print "Error can't find board revision"
Alex Eames RasPi.TV, RasP.iO

salawinder
Posts: 7
Joined: Tue Nov 13, 2012 10:51 pm

Re: WiringPython - WiringPi for Python

Tue Nov 13, 2012 11:00 pm

Hi, really new to this, and having some problems, wonder if I can get a pointer in the right direction?

I am running Raspbmc and am calling a pair of python scripts from keypresses. The scripts need to set a GPIO pin to high or low to turn on or off an LED. I have installed WiringPi & WiringPi-Python, but cannot get it to work...

Form an SSH window logged in as pi I can switch the LED on & Off with:

Code: Select all

gpio export 0 out
gpio mode 0 out
gpio write 0 1

gpio write 0 0
but in python I cannot get it to work. Still in SSH I have entered:

Code: Select all

import wiringpi
wiringpi.wiringPiSetup
wiringpi.pinMode(0,1)
wiringpi.digitalWrite(0,1)
but the LED does not change. I am sure it is probably an easy solution, but I am new to all this and have been trying everything I can find for a couple of hours with no luck.

rickseiden
Posts: 411
Joined: Thu Aug 02, 2012 12:21 pm
Location: Buffalo, NY, USA

Re: WiringPython - WiringPi for Python

Wed Nov 14, 2012 12:51 am

gadgetoid wrote: Bit shifting:
wiringpi.shiftOut(1,2,0,123) // Shift out 123 to data pin 1, clock pin 2
I just started using wiringPi with C, and had written a subroutine to handle shifting the data out. Now I find out that it's built in, and I tried it and it works! I learned something from this thread!
There are 10 types of people in this world. Those that understand binary, and those that don't.

texy
Forum Moderator
Forum Moderator
Posts: 5145
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: WiringPython - WiringPi for Python

Wed Nov 14, 2012 8:01 am

salawinder wrote:Hi, really new to this, and having some problems, wonder if I can get a pointer in the right direction?

I am running Raspbmc and am calling a pair of python scripts from keypresses. The scripts need to set a GPIO pin to high or low to turn on or off an LED. I have installed WiringPi & WiringPi-Python, but cannot get it to work...

Form an SSH window logged in as pi I can switch the LED on & Off with:

Code: Select all

gpio export 0 out
gpio mode 0 out
gpio write 0 1

gpio write 0 0
but in python I cannot get it to work. Still in SSH I have entered:

Code: Select all

import wiringpi
wiringpi.wiringPiSetup
wiringpi.pinMode(0,1)
wiringpi.digitalWrite(0,1)
but the LED does not change. I am sure it is probably an easy solution, but I am new to all this and have been trying everything I can find for a couple of hours with no luck.
Which pin is the LED connected to? GPIO-0 is pin 11, ie the 5th pin down from pin 1

Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

User avatar
[email protected]
Posts: 1979
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website

Re: WiringPython - WiringPi for Python

Wed Nov 14, 2012 8:07 am

texy wrote:
salawinder wrote:Hi, really new to this, and having some problems, wonder if I can get a pointer in the right direction?

I am running Raspbmc and am calling a pair of python scripts from keypresses. The scripts need to set a GPIO pin to high or low to turn on or off an LED. I have installed WiringPi & WiringPi-Python, but cannot get it to work...

Form an SSH window logged in as pi I can switch the LED on & Off with:

Code: Select all

gpio export 0 out
gpio mode 0 out
gpio write 0 1

gpio write 0 0
but in python I cannot get it to work. Still in SSH I have entered:

Code: Select all

import wiringpi
wiringpi.wiringPiSetup
wiringpi.pinMode(0,1)
wiringpi.digitalWrite(0,1)
but the LED does not change. I am sure it is probably an easy solution, but I am new to all this and have been trying everything I can find for a couple of hours with no luck.
Which pin is the LED connected to? GPIO-0 is pin 11, ie the 5th pin down from pin 1

Texy
in this context, it's wiringPi pin 0 -which is bcm-gpio pin 17.

-Gordon
--
Gordons projects: https://projects.drogon.net/

BlackJack
Posts: 288
Joined: Sat Aug 04, 2012 8:28 am
Contact: Website

Re: WiringPython - WiringPi for Python

Wed Nov 14, 2012 8:44 am

@salawinder: You have to actually *call* the setup function. The call operator in Python is a (possibly empty) sequence of arguments in parenthesis. Just writing the name of a function results in the function object itself but does not call it automagically.

Code: Select all

while not self.asleep():
    sheep += 1

salawinder
Posts: 7
Joined: Tue Nov 13, 2012 10:51 pm

Re: WiringPython - WiringPi for Python

Wed Nov 14, 2012 10:13 am

Gordon & Texy: the pin numbering is very confusing, however I am using physical pin 11, which I believe is GPIO 0 - the code used at the SSH command prompt works - is the python code talking to a different pin? Incidentally it is a rev. 2.0 board.

BlackJack: I apologise, but while I understand every word in your reply, the reply itself may as well be in Chinese! :D I do have some programming experience, but am very new to Linux & python, so please use simple explanations/examples & I might figure it out!

Incidentally, if it helps, I am following instructions on Gordon's site and http://www.sirmc.net/view/9001/

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2810
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: WiringPython - WiringPi for Python

Wed Nov 14, 2012 10:22 am

salawinder wrote:BlackJack: I apologise, but while I understand every word in your reply, the reply itself may as well be in Chinese! :D I do have some programming experience, but am very new to Linux & python, so please use simple explanations/examples & I might figure it out!
I think what he meant was

Code: Select all

wiringpi.wiringPiSetup
should be

Code: Select all

wiringpi.wiringPiSetup()
Without the () you have not called the function to set up wiringpi within your python script :D
Of course, if it's not set up, it won't work.
Alex Eames RasPi.TV, RasP.iO

salawinder
Posts: 7
Joined: Tue Nov 13, 2012 10:51 pm

Re: WiringPython - WiringPi for Python

Wed Nov 14, 2012 10:35 am

Alex, Thanks for the clarification. The suggestion has made a difference, but unfortunately not a good one....


When entering the line with the parenthesis, I now get a permission denied error. I am running python as the user 'pi' because I am running this code from within XBMC, so it cannot be run as root.

I thought one of the main advantages of wiringpi was that it did not need root? I have reviously got the LED working with RPi.GPIO, but that needed root permissions also, which is how I found wiringpi.

texy
Forum Moderator
Forum Moderator
Posts: 5145
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England

Re: WiringPython - WiringPi for Python

Wed Nov 14, 2012 10:49 am

You need to use sudo :

Code: Select all

sudo python myprog.py
Texy
Various male/female 40- and 26-way GPIO header for sale here ( IDEAL FOR YOUR PiZero ):
https://www.raspberrypi.org/forums/viewtopic.php?f=93&t=147682#p971555

salawinder
Posts: 7
Joined: Tue Nov 13, 2012 10:51 pm

Re: WiringPython - WiringPi for Python

Wed Nov 14, 2012 11:44 am

Texy, that might be a problem then. I SSH'd into the pi and tested the theory manually, and it worked. I created a file (pwroff.sh) with the following:

Code: Select all

sudo python /home/pi/.xbmc/userdata/pyscript/pwroff.py
and called that from the pi user, and it worked. However when I do the same from within XBMC I get an error in the log:

Code: Select all

SyntaxError: ('invalid syntax', ('/home/pi/.xbmc/userdata/pyscript/pwroff.sh', 1, 11, 'sudo python /home/pi/.xbmc/userdata/pyscript/pwroff.py\n'))
I appreciate this appears to be an issue caused by XBMC, but that's why I'm trying to use wiringpi, I thought it did not need root/sudo to work?

salawinder
Posts: 7
Joined: Tue Nov 13, 2012 10:51 pm

Re: WiringPython - WiringPi for Python

Wed Nov 14, 2012 12:13 pm

Got it!

It has taken me four days to turn a light on, but I have got it....

I didn't realise that the script started by XBMC had to be a python script, but was launched as pi rather than root. I have added

Code: Select all

import subprocess
subprocess.call("sudo python /home/pi/.xbmc/userdata/pyscript/pwroff.py", shell=True)
to the script launched by XBMC and now the LED works as expected. I can't believe it took me this long, and that it was so difficult to get a process launched with elevated privileges from within XBMC, but it is a solution at least!

Thanks for the help.

Return to “Python”

Who is online

Users browsing this forum: No registered users and 14 guests