Possible bug in the proposed fshut.py script

Uninterruptible Power intelligent Supply Module
Post Reply
nikosdion
Posts: 1
Joined: Thu Jan 16, 2014 4:33 pm

Possible bug in the proposed fshut.py script

Post by nikosdion » Thu Jan 16, 2014 4:40 pm

Hello guys,

First of all, thank you for an excellent product!

I think I may have found a little bug in the proposed fshut.py script, page 45 of the preliminary version of the User Manual. The os.system command seems to be unrecognised by Python 2.7.3 in my fairly recent version of Raspbian. I'm not a Python developer (I'm a professional PHP developer), but searching around I found out that os.system is deprecated in favour of subprocess.call. So my (tested!) take on the fshut.py script is:

Code: Select all

# Import the libraries to use time delays, send os commands and access GPIO pins 
import RPi.GPIO as GPIO 
import time 
import subprocess
 
GPIO.setmode(GPIO.BCM) # Set pin numbering to board numbering 
GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Setup pin 27 as an input 
while True: # Setup a while loop to wait for a button press 
  if(GPIO.input(27)==0): # Setup an if loop to run a shutdown command when button press sensed 
    cmd="sudo shutdown -h now"
    pid=subprocess.call(cmd, shell=True) # Send shutdown command to os
    break 
  time.sleep(1) # Allow a sleep time of 1 second to reduce CPU usage
I hope it will be useful to you and to fellow UPiS users.

User avatar
Pimaster
Site Admin
Posts: 1615
Joined: Fri Sep 14, 2012 9:50 am

Re: Possible bug in the proposed fshut.py script

Post by Pimaster » Thu Jan 16, 2014 5:56 pm

Dear nikosdion,

Thank you for your script and good words. We are not python developers. We are embedded hardware and software developers. We just provide this script as an example. Your seems to be better - thank you !!! There is also possibility to build such script based o interrupts. Please experiment and share with us and other UPiS users. We will add your script version (after internal testing) in the next manual - if you allow it.
Warmest Regards
Pi Master
Warmest Regards
PiM
---
Designing with Mentor Graphics PADS - www.pads.com
Please read and follow the PiForum rules
http://www.forum.pimodules.com/viewtopic.php?f=13&t=196
---

mike506
Posts: 2
Joined: Tue Jan 28, 2014 6:24 am

Re: Possible bug in the proposed fshut.py script

Post by mike506 » Tue Jan 28, 2014 6:38 am

I just want to make the developers (and others) aware that there is a difference in the script depending on the version of the Raspberry Pi that you have. I spent the better part of a day trying to figure out why my scripts were not working. Then I found it...I had a Version 1 Pi! For the readers that have a version 1 Raspberry Pi, the script needs to use PIN 21. Once I figured this out and fixed the script, I was able to use the shutdown button with no problems.

So.. for fshut.py ... Raspberry Pi Version 1 - use GPIO pin 21
Version 2 - Use GPIO pin 27

I have not checked any other stuff this change in GPIO pins might impact ... maybe the developers could comment. (specifically, does it impact any of the serial port settings?)

Regards to all the UPiS users,
-Mike

n0ahg
Posts: 51
Joined: Sat May 24, 2014 1:33 am

Re: Possible bug in the proposed fshut.py script

Post by n0ahg » Tue Dec 23, 2014 8:40 pm

Here's a quick hack using interrupts (uses board pin numbering so will work on Rev 1 and Rev 2 boards without change)

Code: Select all

# Import the libraries to send os commands and access GPIO pins
import RPi.GPIO as GPIO
import subprocess

GPIO.setmode(GPIO.BOARD) # Set pin numbering to board numbering
GPIO.setup(13, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Setup pin 13 as an input

try:
    GPIO.wait_for_edge(13, GPIO.FALLING) # Wait for a button press
    pid=subprocess.call("sudo shutdown -h now", shell=True) # Send shutdown command to os

except KeyboardInterrupt:
    GPIO.cleanup()       # clean up GPIO on CTRL+C exit
GPIO.cleanup()           # clean up GPIO on normal exit

Post Reply