Update (7/15/2013): Added instructions for enabling use with Windows 8 (Which does not reply to ping requests by default)
Update (7/20/2013): Added ability to put Windows Computers to sleep using the Webapp
Update (12/15/2013): Added Ability to control multiple computers
Update (9/14/2015): Control can now be achieved over an encrypted (HTTPS) connection, and additional security improvements have been made.
I have a very power hungry desktop computer that I use for video editing and large file storage. When I’m travelling with my laptop, I obviously can’t carry all my files and data with me, so I occasionally use “remote desktop” to access my desktop from the road. I also use a Plex Media Server to stream content from my desktop computer. However, the computer has to be on for me to be able to do this. This means I need to leave the computer on all the time, just in case I need to access it. This wastes power, costs me money, and harms the environment. My Raspberry Pi, on the other hand, consumes a minuscule amount of power, and is always on anyways, serving other purposes. In this tutorial, I’ll show you how to run a simple mobile-friendly website from your Raspberry Pi that will allow you to remotely wake up your power-hungry computer, so you can remotely access it. When you’re done, just put it back to sleep from within the remote desktop service (or use the mobile app plus a small background server running on your desktop to put it back to sleep – this is described below). The app pings the computer from the Raspberry Pi to inform you of when the computer has woken up and established network connectivity. Follow along after the break…
When you’re done, here’s how this will work:
- From wherever you are in the world, navigate to your unique, dynamically updating URL (example.dyndns.org:5000)
- You’ll be presented with a screen that looks like this (note, you may get a security popup first, if you choose to use a self-signed certificate to enable HTTPS – this is described later):
- Next, you’ll enter a passphrase that you’ve pre-allowed. If the wrong phrase is entered, a “denial” note will pop up, and you’ll be able to try again. If you enter the right phrase, the Raspberry Pi will send a “Wake Up” command to the network interface of the computer that you want to wake up. It will then ping that computer and display the results in real-time, so that you know when the computer has finished booting and is accessible. That screen looks like this:
- Once you’ve received confirmation that the computer is alive, you can access it as you normally would, depending on what you’re doing (via SSH, FTP, or Windows Remote Desktop, for example).
For the purposes of the following explanation and tutorial, I’ll define a few terms:
- Remote Computer: This is the “power-hungry” computer located on the same network as your Raspberry Pi. I’m assuming you are using a hardwired Ethernet connection, since Wake-On-LAN (WOL) does not work over Wi-Fi.
- WOL Sever (Raspberry Pi): This is the low-power, always-on, micro server that will be running on the same network as the remote computer. You don’t have to use a Raspberry Pi; any low-power platform capable of serving up web pages, running PHP, and sending Wake-On-LAN magic packets will work fine.
- Client: This is the computer, phone, or other web-connected device that will view the WOL control webpage.
- Router: I assume you are using a home router with a fairly standard feature set, and that you have administrative access to it. It should support DHCP address reservations, NAT port forwarding, and ideally support for a dynamic DNS updating service (like DynDNS or NoIP).
- Dynamic DNS Service: This is a service that handles redirecting a URL to your home’s dynamic IP address. Since most ISPs assign dynamic WAN IP addresses, you need to use a service like this to ensure you can maintain access to your home network, even when the externally-facing IP address changes.
Here’s what you need to do to get this working:
- Ensure that the remote computer supports Wake-On-Lan (WOL) functionality. You may need to boot into the BIOS and permit the computer to be woken up via the network interface. This will look different on nearly every BIOS. LifeHacker has a good tutorial showing how to enable WOL functionality.
- Setup your Raspberry Pi with a linux distribution if you haven’t already, and get it connected to your network. Adafruit has a great set of tutorials that can help you do the initial setup for your Raspberry Pi.
- Log into your Router’s administration panel (in most scenarios you can do this by visiting your Gateway IP from your browser). Find the status page and locate the listing of attached clients. Identify your Raspberry Pi and your computer by their hostnames, and copy down their MAC addresses, and IP addresses. In the screenshot below, the hostname of the computer I want to control is “PHOENIX” and the Pi’s hostname is, appropriately, “raspberrypi.” This will vary by router, but the listing will look something like this (Note: I blurred out some of my personal info):
- Now, find the “DHCP Reservation” or “Static Leases” list in your router’s administration panel. You’ll configure your router to always assign your remote computer and the raspberry pi the same IP address every time they connect to the network. This ensures that you’ll be able to reliably forward data to them through the router. It will look something like this:
- Next, find the port forwarding configuration screen in your router’s administration panel. You’ll want to route the HTTP port (TCP 80) and/or HTTPS port (TCP 443). Note, you only need to forward port 443 if you plan to enable SSL encryption following the steps below. These ports will be used to send data to the raspberry pi. You’ll also need to forward any remote service ports that you want to use to access the remote computer. Importantly, many ISPs will block incoming traffic on port 80 and or 443, the default HTTP(S) ports, to prevent customers from hosting websites from their residences. But, you’re smarter than they are! Simply route an arbitrary external port, to the internal port 80 or 443 on your Raspberry Pi. In the screenshot below, you can see that I am routing external port 5000 to internal port 80. This means that when you eventually access the WOL server, you’ll need to specify the port, like this: example.dyndns.org:5000. Route the appropriate port for whatever services you want to access on the remote computer. In the screenshot below, I’ve routed port 3389, which is what Window’s uses for Remote Desktop. If you wanted to access an SSH server or SFTP server running on the remote computer, you could additionally route port 22. You can find a listing of ports used by various services on wikipedia.
- Next, you’ll want to sign up for a dynamic DNS updating service that your router supports. Different routers support different services. I chose to use DynDNS, since I’m grandfathered into their free account plan, and because my router supports it. Unfortunately, DynDNS no longer offers its free services, but noip.com does, and is supported by many routers. Sign up for a free account, and get a URL that looks something like this: yoursite.no-ip.org. NoIP even supports port 80 redirects, so you can configure your account to not require appending the non-standard port number to the end as described in the last step. Once you’ve got your dynamic DNS account, find the Dynamic DNS (DDNS) updating screen in your router’s administration panel, and enter your account info. Now, you’ll always be able to access your home network, without knowing the IP addresses, and the router will handle automatically updating where the URL directs when your dynamic IP changes!
- Now that your router is all set to handle everything, it’s time to get the Raspberry Pi setup. Access your Raspberry Pi over SSH and log in with the username (pi) and the password that you configured when you followed the setup tutorial linked from step 2. All the steps for how to configure your pi can be found in the README for this project’s GitHub Repo. Note that it includes optional steps for enabling SSL encryption. These additional security measures have been implemented by GitHub user felixrr. If you want to setup your WOL server to work over an encrypted connection, you need to ensure port 443 is forwarded, you need to follow the additional steps in the README, and you need to be aware that unsigned certificates will cause a warning in most browsers that will warn you about the certificate being unsigned (you can safely bypass these warnings).
If the remote computer is Windows 8, you will need to change some settings to make it reply to ping requests (or else you won’t know when it has woken up). By default, Window 8 does not reply to pings. Follow these instructions to make it reply.
I’ve added the ability to put the computer back to sleep using the mobile webapp. The new version of the code can still be found on GitHub. Follow the same instructions from above and in the README. For it to work, you’ll need to ensure that Curl is enabled for your PHP installation (you should have already done this when you ran “sudo apt-get install php5-curl”), and that your remote computer is running the Sleep-on-LAN application server:
Download the Sleep on LAN software and run it on your windows computer. Set it to launch at boot so that it will always be ready to go.
Note, with this updated version of the webapp, it will now ping the computer to check if it’s awake or not to decide if it should present you with a “Wake Up” or “Put to Sleep” button on the interface.
The app now includes a dropdown that allows you to pick a computer. The config file has been updated to include lists for computer names, IP addresses, and MAC addresses. You can now Wake/Sleep as many computers as you want! Thanks to GitHub user ahodgey for submitting a Pull Request with most of this functionality (I made some updates to his pull request, and merged it into the master repository branch).
The Raspberry Pi WOL software now supports SSL encryption, meaning you can connect over HTTPS using a self-signed certificate. Additional security measures have also been implemented to reduce the risk of XSS vulnerabilities. Thanks to GitHub user felixrr for the Pull Request! Make sure you read and perform the additional steps in the README, and that you port forward the HTTPS port, and enable HTTPS enforcing in the config file. If you don’t want to use HTTPS, simply skip those steps, forward the HTTP port, and don’t enforce HTTPS in the config file (this remains the default option).
You’re done! Put the computer to sleep, visit the dynamic URL that you chose, and you should be presented with the screen that was shown earlier in the tutorial. When you input your passphrase, it will be compared with the hash by the PHP script. If it is approved, the Raspberry Pi will send a command to the remote machine to wake it up. The display will update in real time to show you when the machine has woken up and successfully joined the network. You can then access it using any program whose ports you’ve forwarded. Enjoy!