Newbie Question

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
gmccauley
 
Posts: 5
Joined: Mon Feb 03, 2014 3:34 pm

Newbie Question

Post by gmccauley »

Hi all,

I'm VERY new to this world, but it interests me greatly. I'm an IT geek by both profession and hobby. I'm currently a Systems Engineer for (don't laugh) Microsoft products, specifically Active Directory, Exchange, and System Center. I have a heavy scripting/coding background, but primarily VBScript/PowerShell/VB.NET, so new to python, but code is code once you learn the syntax.. :-)

OK, enough about me and on to the question: I did a quick search and I haven't been able to find anything so any help will be greatly appreciated.

Using the BBIO library, I know that I can setup a GPIO pin for output and set it to high by using:
GPIO.setup("P8_11", GPIO.OUT)
GPIO.output("P8_11", GPIO.HIGH)

My question is can I query the pin later in code to see if it's output is currently high or low? or do I have to keep that state in a variable and check the variable?

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: Newbie Question

Post by adafruit_support_mike »

One of my best friends in college wrote transport-layer code for Exchange. ;-)
gmccauley wrote:My question is can I query the pin later in code to see if it's output is currently high or low? or do I have to keep that state in a variable and check the variable?
It kind of depends on how you want to communicate with the chip.

Our Python-based BBIO library doesn't say it supports reading a pin that's defined as as OUTPUT, and there's no guarantee that it will preserve pin state if you write the pin then set it to INPUT to read it. The best advice I have is "try it and see", but I'm leery of relying on behavior that isn't formally specified. That kind of code tends to break in obscure ways some time later.

If you use the filesystem interface, there will be a directory under '/sys/class/gpio' for each pin you've enabled. If you enable pin 23, you'll have be able to configure and inspect it through the interface files in '/sys/class/gpio/gpio23/'. The pin's value would be '/sys/class/gpio/gpio23/value', which should be readable regardless of whether you've defined the pin as an input or output.

gmccauley
 
Posts: 5
Joined: Mon Feb 03, 2014 3:34 pm

Re: Newbie Question

Post by gmccauley »

Thanks for the response Mike.

again, I'm very new to this world, but the question arose as I was playing around last night. Basically, I had a single LED powered by pin P8_11. Using flask, I had a webpage that I wanted to display the state of the LED (on or off) and a button to toggle state. I was able to accomplish that via using a variable to hold the state, but I got thinking what if something outside of my program were to change the state of that pin? Then the results on my webpage would be wrong.

To be honest, I'm not even sure that's possible, but that's what led to my question.

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: Newbie Question

Post by adafruit_support_mike »

That's a perfectly valid concern. Hardware opens up a whole new world of possible bugs. ;-)

gmccauley
 
Posts: 5
Joined: Mon Feb 03, 2014 3:34 pm

Re: Newbie Question

Post by gmccauley »

adafruit_support_mike wrote:That's a perfectly valid concern. Hardware opens up a whole new world of possible bugs. ;-)
Thanks Mike! I guess I'll start working on my own library to read the state of the pin.

User avatar
scotteh
 
Posts: 6
Joined: Tue Jan 28, 2014 1:52 am

Re: Newbie Question

Post by scotteh »

I find this too ironic. I too am an IT geek by profession and hobby but focus on the networking side of things. I too am also a novice with lots of programming experience, except python, and am also working on a similar project but using bottlepy instead of flask. After reading this post and looking in /sys/class/gpio folder when I use Adafruit_BBIO to define an input or output pin I noticed a gpio68 folder was created; which if you check the pin reference table corresponds to pin I'm using, P8_10. I also noticed within that folder are several files, one of which is named "value" that contains a "1" if the pin is high and a "0" if the pin is low. There is also another file named "direction" that contains "out" or "in" depending upon how you setup the GPIO. Below is quick python script I wrote to read the pin state value from the /sys/class/gpio/gpio68/value file. A quick update to my web page code and now I'm easily checking the current pin state whenever the page is accessed. Hope this helps. Scott

fo = open("/sys/class/gpio/gpio68/value", "ro")
#print fo.name
line = fo.readline()
#print line
word = line[0]
if word == "1":
print "high"
elif word == "0":
print "low"
else:
print "unknown"
fo.close()

gmccauley
 
Posts: 5
Joined: Mon Feb 03, 2014 3:34 pm

Re: Newbie Question

Post by gmccauley »

Thanks Scott! That's exactly what I was planning to do but didn't get a chance to try it out just yet.

Locked
Please be positive and constructive with your questions and comments.

Return to “Beagle Bone & Adafruit Beagle Bone products”