Start your computer remotely using Raspberry Pi

Why do that?






What do you need to pull this off?













How control is done using Raspberry Pi:




The following is a map of these pins:






Note 2: You MUST run this as super-user.


import RPi.GPIO as GPIO
import time


def switchPortState(portMapElement):
    return pe

portMap = {}
portMap[0] = [2,False]
portMap[1] = [3,False]
portMap[2] = [5,False]
portMap[3] = [6,False]

for i in range(len(portMap)):
    GPIO.setup(portMap[i][0], GPIO.OUT)

while True:
    for i in range(len(portMap)):
    portMap[i] = switchPortState(portMap[i])




chmod +x

then run the script using

sudo ./









Connecting the power-wires to the Relay Module:









import RPi.GPIO as GPIO
import time
import argparse

#initialize GPIO pins

parser = argparse.ArgumentParser()

args = parser.parse_args()

#initialize the port that you'll use
GPIO.setup(int(args.port), GPIO.OUT)

if args.length == "long":
elif args.length == "short":

Save this script as, say, “”, and make it executable as we did before:

chmod +x


sudo ./

Running the program from a web browser




Using superuser’s sudo without having to put the password everytime


To do this, run the command

sudo visudo


www-data ALL=(myuser) NOPASSWD: ALL


Installing Apache web-server


sudo apt-get install apache2
sudo apt-get install php5
sudo apt-get install libapache2-mod-php5
sudo a2enmod php5
sudo a2enmod alias
sudo service apache2 restart







Creating the webpage:


cd /var/www/

Now create the new php page using the command

sudo nano control.php

And use the script

<!DOCTYPE html>
<title>Control page</title>
 <select name="switchlen">

 echo("This is long");
 $command = "sudo -u myuser sudo /usr/bin/python3 /home/myuser/ -len long > debug.log 2>&1";
 echo("This is short");
 $command = "sudo -u myuser sudo /usr/bin/python3 /home/myuser/ -len short > debug.log 2>&1";
 $output = shell_exec($command);
 echo("Script return: ");

Don’t forget to change the path of the script to the correct path of your script “”, and change “myuser” to the username you’re using in your Raspberry Pi. After you’re done, press Ctrl+X to save and exit.


sudo chown www-data:www-data control.php



To test the new php page, go to the link:



Controlling your computer from outside your home





13 thoughts on “Start your computer remotely using Raspberry Pi”

    1. Well, in the Python script, you can choose what GPIO port number to use. For example, if you choose GPIO5 (based on this schematic), you’ll see that it’s pin 29. So, you connect pin 29 to the corresponding pin in the relay that you want to control. Now this is not enough, because you need to have a common digital ground between the relay and the Raspberry Pi. So you choose a ground pin on the Raspberry Pi (pin 9, for example), and you connect it to the GND pin on the relay. That’s all! That’s the minimal thing to do.

      On the other hand, for a relay with 4 switches, use 4 GPIOx outputs + 1 GND, so 5 cables.

  1. Great tutorial. I just wonder if I can install teamviewer in Raspbian instead of creating a web server. 



    1. Sure you can. But there are two issues here:

      1. I personally don’t trust closed-source software to be the way I control my network; worse case scenario if I need graphical access, I’d use VNC behind an SSH tunnel
      2. Which one is easier, to just have to get any browser in your network to switch your computer, which can be set as a homepage with a button to switch, or have to start a TeamViewer session every time you want to do something?

      In anyway, using TeamViewer will not make it much easier even in setting it up, because you won’t have an interface to interact with, but you’ll have the whole desktop. So you’re gonna have to make a script probably that you should at least double-click to turn on/off your computer.

      Possible? Yes. Recommended? I wouldn’t do it.

  2. Hello,

    Can you explain how you did all the rest for setting access from outside the network, the OpenVPN and stuff please?


    1. Hi there Mauro,

      Sorry, I can’t explain the details because it’s really lengthy. But I can tell you that I have an OpenVPN server on some public server, and my home devices connect to that and I tunnel through it to them.


  3. Duh – a much simplier solution is to plug the server into a "smart switch" that you can control via the Internet. Set the server bios to "full on after power failure". To turn server on turn smart switch off and then back on (simulates power failure). Shut server down normally when done.


  4. Hi Samer,

    Thank you so much for creating this amazing tutorial. Scripts are working as expected and turning on my machines when I am running them via terminal.

    However when I open the website and select SHORT or LONG option but nothing happens and get a message as “script return: null” on the webpage.

    Any ideas what could be going wrong?


    1. It’s not an error. It’s just how you want to display the result. In the PHP, IIRC, I would direct the python script to a debug file, which will make the stdout of the script null in the PHP output. You shouldn’t care about that. Just ensure that the python script is being called. That’s all you should care about.

Leave a Reply to Samer Cancel reply


