Use a Button to Execute Commands on a Raspberry Pi

Introduction: Use a Button to Execute Commands on a Raspberry Pi

I am sharing the project i did for the 2016 fll competition with the hackground team where i made a mouse trap that detected when a mouse entered and sent you an email.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: Lets Go!

the first thing you need to do is download andrewH7's python code which will be perfect for what we are doing

link to andrewH7's instructable

once you download this to your raspberry pi you must edit the code so it works

Change gpio_pin_number=YOUR_CHOSEN_GPIO_NUMBER_HERE

To gpio_pin_number=21

and once you do that you can change os.system("sudo shutdown -h now")

to any command you want within the parenthesis

now you are finished all you need to do now is run the script and connect pin 21 on the gpio to ground

1 Person Made This Project!

Recommendations

  • Backyard Contest

    Backyard Contest
  • Silly Hats Speed Challenge

    Silly Hats Speed Challenge
  • Finish It Already Speed Challenge

    Finish It Already Speed Challenge

3 Discussions

0
GuidoMonstrey
GuidoMonstrey

9 months ago

This is a good script that i did use in my project "satellite antenna" and comes from Project Arthur - Apollo50. Idid put a servo and the Speaker pHat for playing a ;wav.....i am using Raspberry pi Zero W.
i did change the GPIO into: gpio_pin_number=5
I did change: os.system("sudo shutdown -h now")
into : os.system("sudo python :home/pi/my_python:servo-2.py")

Now, i did put the "off-button.py" into the " /etc/rc.local " and it is working but just for one push on the button in front. (normaly, i think it is used for shutdown)
There is already another line above the exit 0 and it looks like :
python /home/pi/play-sound-apollo.py &
python /home/pi/my_python/servo-2.py &
So, two scripts are running and it is working only on the first push of the red button.
My question is: can i change the script or a script , so it is working every time i push the button in front? What must i put in the script please?
Into : 'off-button.py' or into 'play-sound-apollo.py' ?

script ' play-sound-apollo.py' below :
________________________________________
import pygame.mixer
from pygame.mixer import sound
from gpiozero import button
from signal import pause

pygame.mixer.init()

btn_eagle = Button(5)

eagle = pygame.mixer.Sound("/home/pi/gpio-music-box/samples/Eagle-Has-Landed-Audio.wav")
#above in one line

btn_eagle.when_pressed = eagle.play

pause()
_____________________________

_____________________________

script ' servo-2.py below :
____________________
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)
servoPin=11
GPIO.setup(servoPin, GPIO.OUT)
pwm.start(7)
for i in range(0,90) :
DC=1./18.*(i)+2
pwm.ChangeDutyCycle(DC
time.sleep(.02)
for i in range(90,0,-1)
DC=1/18*i+2
pwm.ChangeDutyCycle(DC)
time.sleep(.02)
pwm.stop()

_________________________________

Sorry for this long question.......
grts
Guido Monstrey Belgium

0
DIY Hacks and How Tos

Nice intro to Raspberry pi project. I am actually using this in a project that I am working on right now.

0
punkinlauncher
punkinlauncher

Reply 3 years ago

cool