Main Restorations Software Audio/Jukebox/MP3 Everything Else Buy/Sell/Trade
Project Announcements Monitor/Video GroovyMAME Merit/JVL Touchscreen Meet Up Retail Vendors
Driving & Racing Woodworking Software Support Forums Consoles Project Arcade Reviews
Automated Projects Artwork Frontend Support Forums Pinball Forum Discussion Old Boards
Raspberry Pi & Dev Board controls.dat Linux Miscellaneous Arcade Wiki Discussion Old Archives
Lightguns Arcade1Up Try the site in https mode Site News

Unread posts | New Replies | Recent posts | Rules | Chatroom | Wiki | File Repository | RSS | Submit news

  

Author Topic: Pi shutdown with toggle switch  (Read 7249 times)

0 Members and 1 Guest are viewing this topic.

pbj

  • Trade Count: (+4)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 10893
  • Last login:Today at 08:46:53 am
  • Obey.
    • The Chris Burke Band
Pi shutdown with toggle switch
« on: November 12, 2015, 10:30:29 am »
So, for my purposes, the thing that really kills the Pi are the mandatory shutdowns unless you want to corrupt your SD card.  Which means I keep doing things like running DOS MAME cabinets or using an old Xbox.


Then I saw this:

http://www.piups.net/

Supposed to shut it down safely when power is cut, and the Pi will boot back up when power is restored.  Seems like that takes care of the issue. 

Surely there's got to be a cheaper way to accomplish this?





JDFan

  • Trade Count: (+1)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 3448
  • Last login:June 01, 2024, 11:31:20 pm
  • I want to build my own arcade controls!
Re: Pi shutdown with toggle switch
« Reply #1 on: November 12, 2015, 11:03:13 am »
THere is an instructable ( http://www.instructables.com/id/Simple-Raspberry-Pi-Shutdown-Button/ ) - that includes a python script to shut down the pi using the GPIO -- It can be used with a momentary switch wired to the GPIO so that when the button is pressed it runs the python script to shut down the pi properly - so does what you are wanting without buying anything other than a button (which you probably already have !)

pbj

  • Trade Count: (+4)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 10893
  • Last login:Today at 08:46:53 am
  • Obey.
    • The Chris Burke Band
Re: Pi shutdown with toggle switch
« Reply #2 on: November 12, 2015, 11:08:06 am »
I'm thinking more along the lines of this...

http://www.hackerspace-ffm.de/wiki/index.php?title=Raspi_EDLC_UPS

I want to be able to cut the Pi off from wall power and have it safely shut down.


pbj

  • Trade Count: (+4)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 10893
  • Last login:Today at 08:46:53 am
  • Obey.
    • The Chris Burke Band
Re: Pi shutdown with toggle switch
« Reply #3 on: November 12, 2015, 01:46:35 pm »
Maybe one of you Pi gurus will check in.  The idea is that the thing shuts down when GPIO 7 is no longer being fed 3V:

import RPi.GPIO as GPIO
import time
import os
GPIO.setmode(GPIO.BCM)
GPIO.setup(7, GPIO.IN,pull_up_down=GPIO.PUD_DOWN)
while True:
    print GPIO.input(7)
    if(GPIO.input(7) == False):
        os.system("sudo shutdown -h now")
        break
    time.sleep(1)



Think that will work?


DaOld Man

  • Trade Count: (+4)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 5147
  • Last login:May 29, 2024, 07:09:08 am
  • Wheres my coffee?
    • Skenny's Outpost
Re: Pi shutdown with toggle switch
« Reply #4 on: November 12, 2015, 07:33:17 pm »
I'm thinking more along the lines of this...

http://www.hackerspace-ffm.de/wiki/index.php?title=Raspi_EDLC_UPS

I want to be able to cut the Pi off from wall power and have it safely shut down.

I was thinking along these lines too, except have a 120 vac relay on the high side of the power supply to signal the Pi to shutdown when the relay turns off (Due to 120 VAC power outage.)
I was looking into this for my NES_frankenstein project, but the small relay I have gets pretty warm.
This would be very simple with the mausberry switch circuit, just run one of the PWR wires from mausberry thru a NO contact on the relay.
I was also trying to figure out how big a capacitor I would need on the 5 volt side to keep the pi powered long enough to shutdown. Didnt think about super caps.
This would probably be the simplest way to do it, if you are using the mausberry switch.
Of course the relay contact could also trigger shutdown from one of the GPIO pins.

pbj

  • Trade Count: (+4)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 10893
  • Last login:Today at 08:46:53 am
  • Obey.
    • The Chris Burke Band
Re: Pi shutdown with toggle switch
« Reply #5 on: November 13, 2015, 09:41:25 am »
Relays could do it, but I'd use a microrelay being switched by the power supply and not the AC power.  The issue is charging up the capacitor, and then you start back into the territory of that EDLC UPS circuit.

I've also contemplated using a backup battery, even AAs, but I'm not crazy about the idea of power being present at the Pi 24/7.  The beauty of the caps is that they're going to eventually wind down if that Pi manages to turn itself back on.

Keep at it, man, you're our only hope.





obcd

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 160
  • Last login:April 03, 2019, 11:44:36 am
  • I want to build my own arcade controls!
Re: Pi shutdown with toggle switch
« Reply #6 on: November 13, 2015, 12:58:24 pm »
You can make the linux root file system of the raspberry pi read only. If the SD card is no longer written 2, it will no longer corrupt if you pull the power. A problem that rises using that approach is obvious, what about the high scores? Maybe a small (cheap) usb stick can be used for that purpose. A normal setup on such a stick can still corrupt if the system is written during a powerdown. Some systems like the Nintendo Wii clearly give a message not to power down the system while the high scores are saved...Another approach is to use multiple save files with a crc check at the end of them. If a save file has a bad crc, simply revert to the one before it. You will only lose the stuff that was changed during the last save. You could even use the usb stick as medium with a number of writable sectors, and use no file system on it. This approach will again lessen the chanche of corruption. I found a tool for debian called fsprotect that does the job real nice. All writes are simply redirected to a ram based tmpfs file system. It uses aufs to combine the 2 filesystems (the root ext4 and the tmpfs) for it's purpose.
To get a kernel with aufs, you might need to recompile the kernel. Once you have that, the additional steps are simple.
first run : sudo update-initramfs -c -k `uname -r`
This will create an initrd file on the boot folder that is needed by fsprotect
Next, add the following line to the /boot/config.txt file:
initramfs initrd.img-x.yy.z
(x.yy.z depend upon your kernel version. uname -r will show that version)
Next, install the fsprotect package:
sudo apt-get install fsprotect
Next, add the following to the end of the /boot/cmdline.txt
fsprotect=128M
upon the next reboot, your root fs will be read only. You can run the command "sudo mount" to check this out.
Simply remove the fsprotect=128M and reboot if you need to make changes to the rootfs.
128M is the amount of ram memory used for the root fs overlay file.
I have instructions for the compilation of a new kernel, but it's a bit much to post this here.
There are other methods to make the rootfs read only, but fsprotect has the advantage that everything keeps working like there are no changes to the root fs.
You might just need to replace the logging system to prevent excessive writing to the rootfs.



DaOld Man

  • Trade Count: (+4)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 5147
  • Last login:May 29, 2024, 07:09:08 am
  • Wheres my coffee?
    • Skenny's Outpost
Re: Pi shutdown with toggle switch
« Reply #7 on: November 13, 2015, 02:02:24 pm »
Relays could do it, but I'd use a microrelay being switched by the power supply and not the AC power.  The issue is charging up the capacitor, and then you start back into the territory of that EDLC UPS circuit.

I've also contemplated using a backup battery, even AAs, but I'm not crazy about the idea of power being present at the Pi 24/7.  The beauty of the caps is that they're going to eventually wind down if that Pi manages to turn itself back on.

Keep at it, man, you're our only hope.

The reason I was thinking of the relay on the power supply high side was to isolate the caps from the relay. The caps will keep the relay on otherwise, unless you use blocking diodes, but a diode introduces voltage drop, even the schottky ones, however the drop across them is small. Have to account for all the current going through the diode too.
A relay on the high side would be isolated by the power supply itself, but the power supply would have to be hefty enough to charge the completely drained cap.
A relay would be de-engerized automatically on power failure, and a simple on/off switch in the power high side before relay would work.

@obcd: Interesting stuff man.

Here is a crude drawing of my proposed circuit:
« Last Edit: November 13, 2015, 02:24:28 pm by DaOld Man »

behrmr

  • Trade Count: (+1)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 459
  • Last login:April 23, 2021, 09:17:13 am
Re: Pi shutdown with toggle switch
« Reply #8 on: November 13, 2015, 02:31:31 pm »
Have you considered an inexpensive UPS and NUT on the Pi? 

Jamesbeat

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 261
  • Last login:February 11, 2019, 06:13:39 pm
  • I want to build my own arcade controls!
Re: Pi shutdown with toggle switch
« Reply #9 on: December 14, 2015, 06:12:28 pm »
Is it absolutely necessary to be able to pull the plug?

I set up my pi to shut down when exiting mame, and I have a dedicated (hidden) exit button mounted on the cabinet.
The same button is set to 'tab' when in a game, or 'esc' when in the main menu, so it serves double duty.

When I want to shut down the pi, I just quit to the main menu and hit the button. The pi shuts down in literally three seconds.

I'm not saying that it wouldn't be better to be able to just pull the plug, but this method is so fast that it practically shuts down in the time it takes to bend down and reach the plug to pull it out, and there's no extra hardware or batteries etc needed.

I did this by editing .profile to include 'sudo shutdown -h now' as the last instruction, so as soon as I quit mame, it executes the shutdown instruction.
Then all I had to do was map my button to the 'esc' or in mame.

Naturally, its best to get the machine set up properly before adding this line to .profile, otherwise the pi will shut down every time you quit mame. If you need to get back under the hood of the pi for any reason, you can always put the sd card in another machine and edit .profile to stop the pi shutting itself down.
« Last Edit: December 14, 2015, 06:24:36 pm by Jamesbeat »

opt2not

  • Trade Count: (+15)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 6173
  • Last login:April 02, 2024, 07:42:30 pm
Re: Pi shutdown with toggle switch
« Reply #10 on: December 14, 2015, 06:23:32 pm »
I'm with pbj on this as well. I'd like to kill the power without having to worry about shutting down beforehand.
Subscribed.  :cheers:

Slippyblade

  • Trade Count: (+2)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 3167
  • Last login:June 05, 2024, 10:30:57 am
  • And to the death god we say, "Not today!"
Re: Pi shutdown with toggle switch
« Reply #11 on: December 14, 2015, 06:31:18 pm »
Is it absolutely necessary to be able to pull the plug?

My main concern is not the ability to yank the plug.  It's not wanting a corrupted card in case of power outage, or cord trippage, or machine getting yanked off the shelf by the dog running by and catching a cord.

Jamesbeat

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 261
  • Last login:February 11, 2019, 06:13:39 pm
  • I want to build my own arcade controls!
Re: Pi shutdown with toggle switch
« Reply #12 on: December 14, 2015, 06:32:43 pm »
I'm with pbj on this as well. I'd like to kill the power without having to worry about shutting down beforehand.
Subscribed.  :cheers:

Is tapping a button and waiting three seconds really that bad?

I have actually pulled the plug on my cabinet many times and never had a corrupted card.
I guess mame just doesn't write to the card all that often.

If the card did get corrupted, it's a five minute job to reflash the sd card, so why not just try it and see if it is actually a problem?

opt2not

  • Trade Count: (+15)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 6173
  • Last login:April 02, 2024, 07:42:30 pm
Re: Pi shutdown with toggle switch
« Reply #13 on: December 14, 2015, 07:55:59 pm »
Is tapping a button and waiting three seconds really that bad?
<sigh> ok, I'll indulge this, this one time...

Yes, it IS really that bad.
Reason: I have my cabinets set-up like an arcade, they're permanently switched to the -On- position, connected to a surge-protected power bar, plugged into a wall outlet that is controlled by a wall-switch. When I want to play my cabinets in my game room, I walk in, flip the switch, everything powers up and I play. Easy-peasy, like how an Arcade Op would do.  When I'm done I walk away from the machines, flip my wall switch off, everything powers down and I don't have to think about it again. No worrying about corruption, whether I left something on, or whatnot. Peace-of-mind.

2 of my cabinets currently run off original boards (sometimes I put in a console, i.e. a Dreamcast, or sometimes I'll throw in a JAMMA-fied PC that runs WinXP), 1 of my cabinets has a permanent DOS box running AdvanceMame. This cabinet can be powered off without having to shut down the OS beforehand.  The downside to running AdvanceMame is that it's well out-of-date, so many of the newer games that came out in the 90's doesn't play correctly or at full speed - so this cabinet is mostly a classics pre-90's machine. My plan is to eventually replace this DOS box to some kind of non-volatile OS hardware, ONLY IF I can power-off by a flip of my wall-switch without corrupting things. I considered the ArcadeSD, but the price and the games it support doesn't fit my needs either.

I have actually pulled the plug on my cabinet many times and never had a corrupted card.
I guess mame just doesn't write to the card all that often.
This is great for you. Unfortunately a control group of one isn't really a valid field-test. Many people have reported SD card corruption, and that's enough for me to not support this until it's verified bulletproof for my needs. This goes along the lines of running my cabinets like an Op would.
I need hardware that I can rely on 100% of the time.

If the card did get corrupted, it's a five minute job to reflash the sd card, so why not just try it and see if it is actually a problem?
I don't buy things with the intention of testing if it'll break on me. Or with the intention of constantly having to repair it based on user error. I have enough original hardware to worry about breaking down as-is.

Jamesbeat

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 261
  • Last login:February 11, 2019, 06:13:39 pm
  • I want to build my own arcade controls!
Re: Pi shutdown with toggle switch
« Reply #14 on: December 14, 2015, 08:27:17 pm »
Fair enough.
In your particular use case, I agree that you would have to rely on the card not becoming corrupted, which I admit is not ideal, (which is why I still use my button).


« Last Edit: December 14, 2015, 08:31:07 pm by Jamesbeat »

DaOld Man

  • Trade Count: (+4)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 5147
  • Last login:May 29, 2024, 07:09:08 am
  • Wheres my coffee?
    • Skenny's Outpost
Re: Pi shutdown with toggle switch
« Reply #15 on: December 15, 2015, 10:03:42 am »
My main concern is like Slippyblade said.
With the NES console conversion I did using the Pi, it is extremely possible for someone (grandkids) to pull the pull before turning it off properly, also random power failures is a high possibility in some areas.
I experimented a little with a Pi B (only 2 usb ports)  and a 1000uf 35 VDC electrolytic cap.
I couldnt see any difference in the amount of time it took the led on the Pi to go out using the cap versus not using it. (pulling plug to test)
I did use a "old arcade type" power supply, so Im not real sure if the power supply may have allowed the cap to discharge through it. Maybe someone who has experience with these power supplies could shed some light on this.
Or it's quite possible that the 1000 uf cap is way to small, but Im concerned with real estate space on the NES console, so there's not a lot of space in there for a really big cap.
Has anyone else experimented with the cap/relay idea?

Jamesbeat

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 261
  • Last login:February 11, 2019, 06:13:39 pm
  • I want to build my own arcade controls!
Re: Pi shutdown with toggle switch
« Reply #16 on: December 15, 2015, 12:34:55 pm »
I bring my pi based bartop to our annual holiday party at work.
The outlet that I plug it into is controlled by a wall switch because it usually powers some lighting.

Despite the fact that I always tape over the switch, people still manage to lean on it and cut power to the cabinet numerous times.

Before I figured out how to shut down with a button, I just used to cut the power, because my cab has no provision for a keyboard.

Power must have been cut to my pi 20 or 30 times, and I am still on the original sd card that I installed my system on in 2012.
Anecdotal evidence, sure, but I really believe the situation is nowhere near as bad as people make out.

I understand why opt2not wouldn't want to pull the plug as the routine way of shutting down the machine, but I think worrying about the odd power cut is going a little far.


pbj

  • Trade Count: (+4)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 10893
  • Last login:Today at 08:46:53 am
  • Obey.
    • The Chris Burke Band
Re: Pi shutdown with toggle switch
« Reply #17 on: December 15, 2015, 12:42:46 pm »
So this "that thing you want to do?  you don't really want to do that" mentality kind of pervades the community associated with the Raspberry Pi.

I'm pretty much in the same situation as Opt2not.  I'd like to freshen up a cabinet that's been running AdvanceMAME and DOS for 10 years.

Maybe I was just REALLY unlucky, but I got a corrupt card every time I turned off my Pi at the power strip the four or five times I tested it. 

The reasons I want to flip it off and on with a toggle switch are beside the point.  This is something we SHOULD be able to easily do, and simplifying this functionality would open the Pi up to many more applications.






Jamesbeat

  • Trade Count: (0)
  • Full Member
  • ***
  • Offline Offline
  • Posts: 261
  • Last login:February 11, 2019, 06:13:39 pm
  • I want to build my own arcade controls!
Re: Pi shutdown with toggle switch
« Reply #18 on: December 15, 2015, 12:53:08 pm »
I was really talking about the odd chance of a power cut or an accident.

I agree that routinely cutting the power is not ideal, which is why I use a button myself.

If, like opt2not, a button doesn't fit your use case, then you will have to think of something else.

I was just offering an easy alternative that might be a good enough solution for some people.
It works great for me, but my situation is different because I'm only using one machine.