Shutdown/Reboot HTML Buttons
DISCLAIMER: I am not responsible for the loss of life, limb, Raspberry Pi, or the love of your family. Proceed at your own risk. Back up your stuff, yada, yada, yada.
1. Give www-data access to run the commands
First off, we need to give the webpage (www-data) access to run the shutdown and reboot commands.
From the command line type
Scroll down until you see this:
Code:
#includedir /etc/sudoers.d
pi ALL=(ALL) NOPASSWD: ALL
Directly below that type in
Code:
www-data ALL=NOPASSWD: /sbin/reboot, /sbin/shutdown
Press Ctrl-X to save, Y to confirm, and then Enter to confirm the file name
2. Create the PHP pages that will be run by the buttons
I did this step through the GUI, but I suppose you can do it through the command line as well.
Navigate to /var/www/ (if you're running Jessie, it might be /var/www/html)
Create a new empty file, name it reboot.php, and inside it type the following
PHP:
<?php system('sudo /sbin/reboot'); ?>
Create a new empty file named shutdown.php, and inside it type
PHP:
<?php system('sudo /sbin/shutdown -h now'); ?>
3. Add the buttons to the maintenance panel
Still in /var/www/ or /var/www/html/, open the maintenance-panel.php page
Near line 59 you'll seen an unordered list (designated by <ul>). At the bottom of this list, after the line <li><a href="#reprogram-arduino"><span>Reprogram <span class="boardMoniker">controller</span></span></a></li> enter the following
Code:
<li><a href="#shutdown"><span>Shutdown</span></a></li>
Following the closure of that list (designated by </ul>) enter
Code:
<div id="shutdown">
<script type="text/javascript">
function shutdownonclick()
{
shutdown_window = window.open("shutdown.php",
"shutdown_window","status=1,width=350,height=150");
setTimeout("shutdown_window.close()", 2000);
}
function rebootonclick()
{
reboot_window = window.open("reboot.php",
"reboot_window","status=1,width=350,height=150");
setTimeout("reboot_window.close()", 2000);
}
</script>
<button><a href="javascript: shutdownonclick()">Shutdown</a></button>
<br>
<br>
<button><a href="javascript: rebootonclick()">Reboot</a></button>
</div>
4. Test it
Refreshing your BrewPi page should be all that is required for this to work, but maybe you'll have to reboot the whole Pi as well.
How these buttons work
It took me about 48 hours to figure out how I wanted these to work. Originally I just created the php pages, and linked to them. The problem was that if the pages were open on my phone, I didn't want them to refresh when opening Safari after an extended duration and having them rerun the command. Instead, the buttons use javascript to create a new window with the PHP page, wait two seconds so that the command can be run, and then closes the page. The beauty of this is that the script is run on the webpage rather than the server, so once the Pi shutdown or begins rebooting, the page can finish processing the script to close the window.
Shutdown/Boot Physical Button
For this I really just used these instructions:
http://www.instructables.com/id/Simple-Raspberry-Pi-Shutdown-Button/
I used Pi pin 5 (on my RPi 2 ver B that was GPIO pin 3) and Pi pin 6 (a ground). Using these (maybe just using 5 with any ground) gives you the ability to boot it when it is off but hasn't been unplugged. I used female jumper wires on the pins, and then soldered the ends to a momentary switch (specifically this one:
https://www.adafruit.com/product/559).
Following the switch directions and code leaves you with a floating GPIO pin, and this caused me to get false triggers when the freezer compressor would kick on, shutting down the BrewPi. I added a pull up resistor to the circuit, connecting the GPIO pin to a +3.3v pin.