RasPi server on/off control

Goal:
To turn on a PC server remotely from a Raspberry Pi web page.

first steps:

sudo apt-get update
sudo apt-get install python-dev
sudo apt-get install python-rpi.gpio

Make sure that your RPi.GPIO installation is working before you move on! Mine did not!

The Raspberry Pi is a 24/7 web server now. I have a Windows Home Server (Intel Atom 330 PC board) that I would like to be able to turn on. I do not want to leave the PC server on all the time. Using GPIO on the RasPi I will use a 2N7000 transistor to act like a momentary button push.

So far it is working.

GPIO #21 (pin 13) to the Drain of the 2N7000. Ground to ground. Signal to LED cathode (-).

python script name: switch.py


from time import sleep
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(13, GPIO.OUT)


while 1:

GPIO.output(13, False)
sleep(.1)
GPIO.output(13, True)
break
(the last four lines need to be spaced 5 spaces to the right to work)

This turns an LED on for 1/10th of a second then breaks the loop. When run in terminal it runs then returns to the prompt.

To perform a hard boot you only need to rename the file and then change the sleep from (.1) to (7) seconds. I named it hardboot.py.

The next phase is to figure out how to make this work in a WordPress page with a password!

Installed WordPress plugin “insert PHP”

Created page:
[insert_php]
echo

“;

if(isset($_POST['use_button']))
{
$command = “sudo python /switch.py 2>&1″;
$pid = popen( $command,”r”);
while( !feof( $pid ) )
{
echo fread($pid, 256);
flush();
ob_flush();
usleep(100000);
}
pclose($pid);
}

[/insert_php]

I have tried numerous ways to have this “Button” activate the python script. It appears the only way to have this button work would be to assign it root privileges. That would be a huge security mistake.

The script can be activated through either SSH or RDP. So it looks like the best solution is to use SSH remotely with a strong password. Switch.py and hardboot.py work when used through SSH just fine.

For the minimal commands you have to place the scripts in /home/pi
sudo python switch.py on|off
sudo python hardboot.py hard power off then use switch.py to restart.

Leave a Reply