Raspberry Pi Safe Shutdown Button Script

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey welcome back guys this is jason km4ack today let's talk about a safe shutdown button for your raspberry pi stick around and we'll get right to it real quick before we get going today i've got to give a shout out to these guys they're my latest patrons over on patreon if you'd like to help support the channel i'll leave a link to patreon down in the description below now in this video we'll be covering both the hardware installation of the button and the software that we're going to need to install to get everything working correctly but first let's talk about why you would want this shutdown button to begin with well if you're like me you run your raspberry pi headless as such in order for me to shut the raspberry pi down i have to remote into it using my vnc server and then i can click on the raspberry pi logo in the top left corner click shutdown and then go ahead and safely shut the raspberry pi down but this requires me to log into it each and every time i want to shut the pie down well when i'm getting ready to say go out into the field and do a podo i want to just be able to grab a raspberry pi off the shelf and go ahead and get that into the backpack sometimes i'm just too lazy to actually want to log into it and shut it down the proper way now you could i suppose just pull the power cord for the raspberry pi and go ahead and put it in your backpack the trouble with that is you run a risk of corrupting your sd card so i definitely don't recommend you just disconnect power from your pi although i have to admit uh in certain times of frustration i have been guilty of doing that myself but what i wanted to do was be able to just reach up push a button on the pie have it shut down in a safe manner without me having to log into it and then go ahead and pull it and throw it in my bag quick and easy i'm just basically cutting out that one necessary step of logging into the raspberry pi so when i started on this uh project i went over to amazon and i found this package of buttons now i happen to have wire laying around in the shack from some other projects if you don't have that you can also get the wiring you need off of amazon as well and guys i'll leave links to all of this down in the description below after i had all of the necessary components in my hands i went ahead and started modifying the case that i use so i simply drilled out a hole large enough for the button to fit through and went ahead and inserted that into the top of the case once i got it inserted there then i went ahead and soldered one wire on each of the terminals underneath and then connected that up to the gpio pins once that was done we simply snapped the case back our the top of the case back onto the base of the case and we're ready to go so now let's go ahead and jump over to the raspberry pi and i'll show you how quick and easy this is to get this script installed okay so once you're on your raspberry pi go ahead and open up your web browser and we're going to navigate to github.com forward slash km4 ack once that page loads up you want to come right here and click on the pi scripts repository inside the pi scripts repository we're just going to scroll down the script is almost at the very bottom of the list here but it's called safe shutdown v1 dot zip now that just means version one that way if i change it somewhere in the future you'll be able to know that there was a change made to the script let's just go ahead and click on that and when the next screen comes up we'll just go ahead and click the download button right here give that just a couple of seconds to download at this point we can go ahead and close out of the browser and we'll open up our file explorer go ahead and navigate over to the downloads directory and you should see that safe shutdown script uh the zip file for it so we're just gonna right click on that and say extract here and that will give you a new folder right here in the downloads directory let's go ahead and open up this safe shutdown directory now you'll see four files here initially and we're going to kind of walk through what each of these is doing the install script is what we're going to run in a minute to actually install everything uh but you see another one here called the km4 ack shutdown so i'm just going to go ahead and right click on that and say open in a text editor and this is a simple python script that is going to monitor the gpio pins and once it detects that the button has been pressed it's going to simply run sudo shutdown dash h now and that's it the pi will then be shut down so let's close out of that particular file and let's look at this shutdown dot service file here so what we're doing here is we're running this uh script as a system d service that way it is automatically started in the background every time you boot the raspberry pi and if something happens in the script word to crash it will always be restarted after a two second delay so that's the only two files that we really need to make all of this happen let's go ahead and right click and close that one and then in addition to that you'll see a pin out dot png file now this will also open by default when we click the install here in just a minute but i did want to go ahead and show it to you here this shows you the two pins that it uses by default here uh that we need to connect the button to so if you don't make any modifications to the script you'll connect your wires to these two pins on your raspberry pi board so it's bcm 23 and a ground and it doesn't matter uh which way you have those wires turned you don't have to worry about those being reversed okay so let's go ahead and we're going to install this now you could double click on this and say execute in terminal i'm going to go about it just a little bit of a different way only because if you do it this way you don't get to see any output on the screen should we run into any errors so i'm just going to go ahead and open the terminal window here and let's navigate to the downloads directory and if we run an ls command that shows you the two things that are in there now so let's go ahead and move to the safe hyphen shutdown directory so that's cd space safe hyphen shutdown we'll run the ls command one more time and that shows us that we've got that install script here ready for us so let's run dot forward slash install now it will tell you here there is a warning that says if you wire up your raspberry pi incorrectly you can permanently damage the raspberry pi you assume all responsibility uh by doing this project go ahead and press any key to continue and accept and you'll see just a couple of little things here it does tell you that the script has been installed and enabled and it reminds you which uh gpio pins we're going to connect to and then it tells you that the reference image should open up which it did over here on the left hand side and it also gives you some information if you want to disable this from running at boot then you will run this command here system all right sudo system ctl disable km4 hyphen shutdown dot service if you run that command then the then the script the shutdown script will not be started and your button will not work if you have disabled it and you want to re-enable it at some point in the future beyond that then you would run sudo system ctl enable km4 hyphen shutdown dot service reboot the pi and you would be ready to go your button would work because the script would be running in the background so there you have it guys there's how you can install a shutdown button on your raspberry pi and make it quick and easy to safely power everything down i hope you enjoyed the video be sure to give us a thumbs up if you found this information helpful and we will see you guys on the next one until then 7-3
Info
Channel: KM4ACK
Views: 2,663
Rating: undefined out of 5
Keywords: raspberry pi button, raspberry pi safe shutdown button, raspberry pi, raspberry pi projects, shutdown switch, safe shutdown, raspberry pi power, raspberry pi shutdown, safe shutdown raspberry pi, raspberry pi power button, raspberry pi power switch, raspberry pi safe shutdown, raspberry pi shutdown button
Id: fyHYSLbhLgU
Channel Id: undefined
Length: 9min 38sec (578 seconds)
Published: Fri Nov 05 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.