Android app to boot my computer from anywhere in the world

So you're probably wondering why I created an application for my phone to boot my computer from anywhere in the world. Well, two reasons : 

  1. Because I can and it's fun to do
  2. Because I actually needed it

I needed it because I wanted to host a Plex server on my computer. To put it into a nutshell, Plex is free media application software that enables you to create and manage your own Netflix-like platform. It enables you to create your own VOD platform and share your video content with your friends and family. Content can be watched through a web browser or using the Plex mobile application.
The server can be deployed in various ways, such as on Synology NAS, a Raspberry Ri, a MAC or a Windows computer. I chose not to install it on my Raspberry Pi, but rather on my personal Windows desktop computer mainly because of two reasons:

1) Video Transcoding: The Raspebrry pi 3 doesn't have a Graphics card and has a cheap CPU. Because of this, your Plex server would not be able to achieve fast video transcodings. Like most VOD platforms, Plex can adapt the quality of the stream to your internet connection's bandwidth. To do so, Plex  transcodes your video in real time and sends the stream of the transcoded video. Therefore, having a good CPU is a bare minimum and having a Graphics card is ideal, especially if your media uses the H.265 Codec.

2) Storage: The raspberry pi 3 can only be connected to storage using USB 2 external drives, (read rates of 480Mb/s), whereas my desktop computer can count on SATA III (6Gb/s) and internal drives.

The only downside of not using a Raspberry Pi for hosting Plex are the financial and environmental aspects: the tiny computer only consumes about 18 kWh in a year which would be around 11€ in France. A desktop computer on the other hand, even idle most of the time, would consume much more power. A rough estimation I made suggested my computer would cost around 350€ per year, while being idle.

So my dilemma was that I wanted to have the benefits of a powerful machine to host a Plex server but I did not want to pay the costs of a powerful server running all the time. So I needed a way to wake my computer from my Raspberry pi, which is always on. First, I started looking on the Playstore if what I needed didn't already exist. All I could find where applications for WakeOnLan. They enable you to send a magic packet for WOL from your phone. However, these packets are sent to the MAC address of the device, on layer 2, so they are non routable. This means that even if my phone were connected to my Wireguard VPN (hosted on my Raspberry Pi), I could not use one of these apps to wake my computer. It would only work if my phone is connected by Wifi to the same network as my desktop computer, which is not very usefull. So I decided to develop an app that can boot up or shut down my computer remotely when I want to use Plex. 

Here is how I did it:

 

1) First I developed a Python script that acts as a server. It is deployed on my raspberry pi and was set up as a service. You can find the code on my Github repository here.
This script binds to a socket and receives strings from my Android app, to trigger certain actions:
- broadcast a magic packet to trigger wakeOnLan to boot my computer up. Since the Raspberry pi is always on the same LAN as the computer to wake up, it will work.
- send a remote command to shutdown the computer

2) Then  I developed a basic Android application using Java and Android Studio.
This application contains two buttons. One to send a request to boot up, and the other one to shut down the remote machine. It looks like this:

 



I also added some functions to check that the computer as indeed started or shut down and how long it took.


3) I set up a VPN on my Raspberry pi. I used to have OpenVPN but now I use Wireguard. 
Since I do not handle authentication and security in my python server, the only protection I have is the firewall. Therefore, I connect my phone to my VPN on my raspberry pi to be able to send requests to the python server hosted on that same raspberry pi.