kokein
Posts: 2
Joined: Thu Aug 16, 2018 7:05 pm

Shutdown command after Button press

Fri Aug 17, 2018 9:15 am

Hello,

Can You help me, how to convert this code, to work with my RaPi 3B+ ?
I know that it's for ZeroPi and i cant make it work.
I want to make a simple python script, to ba able to shutdown my RaPi after pressing GPIO17 button (that is on my LCD)
And this is nice and simple in my opionion...

Code: Select all

from gpiozero import Button
import os
Button(17).wait_for_press()
os.system("sudo halt")
But have no idea why there is such difference in Zero GPIO's commands, and RaPi3B+ type of importing buttons like:

Code: Select all

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(27, GPIO.IN) 
All the best !
Nick

ElEscalador
Posts: 621
Joined: Tue Dec 15, 2015 4:55 pm
Location: Detroit, MI USA
Contact: Website

Re: Shutdown command after Button press

Fri Aug 17, 2018 11:17 am

There is no difference in the code needed, it's just that the two examples are using different libraries to do the same thing (I don't know python too well, so I cannot speak for the code actually working). You could use different libraries or different languages... but if any work on a pi3 they would work on a zero and vice versa.
My Autonomous Robot Project and a few of my other projects below.

https://lloydbrombach.wordpress.com/

hippy
Posts: 3930
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Shutdown command after Button press

Fri Aug 17, 2018 12:48 pm

kokein wrote:
Fri Aug 17, 2018 9:15 am
Can You help me, how to convert this code, to work with my RaPi 3B+ ?
I know that it's for ZeroPi and i cant make it work.
If it works on a Pi Zero then it should also work on a Pi 3B+. You will need the GPIO Zero library installed, but that may be included with the standard Raspbian distribution. So long as you connect to the GPIO on the 3B+ as you did on the Zero it should work.

When you say it doesn't work on the 3B+; what does it do, or not do, what error messages do you get ?

User avatar
PeterO
Posts: 4262
Joined: Sun Jul 22, 2012 4:14 pm

Re: Shutdown command after Button press

Fri Aug 17, 2018 1:26 pm

It looks like OP thinks gpiozero can only be used on a PiZero ! Which is understandable and shows the poor choice of library names !

PeterO
Last edited by PeterO on Fri Aug 17, 2018 3:20 pm, edited 2 times in total.
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),Aeromodelling,1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
davidcoton
Posts: 3212
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK

Re: Shutdown command after Button press

Fri Aug 17, 2018 2:06 pm

PeterO wrote:
Fri Aug 17, 2018 1:26 pm
It looks like OP thinks gpiozero can only be used on a PiZero ! Which is understandable and shows the poor choice of library names !

PeterO
FTFY (because I couldn't parse it first time).

@kokein: Try the same code that works on the zero (using gpiozero library) on the Pi3B+. It should just work, as stated above gpiozero works on all current Pi models.
"Thanks for saving my life." See https://www.raspberrypi.org/forums/viewtopic.php?p=1327656#p1327656
“Raspberry Pi is a trademark of the Raspberry Pi Foundation”

User avatar
PeterO
Posts: 4262
Joined: Sun Jul 22, 2012 4:14 pm

Re: Shutdown command after Button press

Fri Aug 17, 2018 3:21 pm

davidcoton wrote:
Fri Aug 17, 2018 2:06 pm
FTFY (because I couldn't parse it first time).
You should have seen it before I fixed (some of) the typos. My "space bar thumbs" are often a bit to quick !
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),Aeromodelling,1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

kokein
Posts: 2
Joined: Thu Aug 16, 2018 7:05 pm

Re: Shutdown command after Button press

Wed Aug 22, 2018 8:16 am

Thank You all for support !

In deed, i thougt that "zero" is for "ZeroPi" - and now i know that is only library :) that work on RaPi3B+ (also)
Thats great, becaose i found verry nice code do controll my GPIO buttons - and its more easy then RPi.GPIO as GPIO

Too bad that its not nice explained - and most of all tutorials go to "RPi.GPIO as GPIO".

I think i can share it for other beginers ;)
https://github.com/scruss/shutdown_button

And one more link with more infos and pictures: http://scruss.com/blog/2017/10/21/combi ... pberry-pi/

All the best !
Nick

Return to “Beginners”