Run and shutdown PC remotely

Foreword

After a LAN party I had to recognize that my pc’s power button broke. Since then I was only able to power on the oc by WakeOnLan… until i got into homeoffice. The LAN cable, I used for my pc had to pay for the office pc. The pc cannot be powered on by WLAN. So a new ignition aid was needed. There was a Raspberry Pi 2 on my pc. So why not make use of this synergy and power on the PC wih the help of the raspberry pi?

Requirements

  • Raspberry Pi 2 (Or another version of the Pi with GPIO interface)
  • Breadboard
  • Relay-Module
  • Jumper

Mount motherboard

The motherboard has an interface to turn on th pc. It’s usually labeled with “Front Panel” and is attached to the front panel. After unpluggin the cables from the motherboard the pins become available. Specifications about which pins have which functionality depends on the motherboard. There only research helps. After the pins for Power and Ground are found, they shall be applied by jumpers.

Whole wiring

The relay is connected to a 5V and a Ground pin at the GPIO. The input signal to switch the relay is connected to pin 7 (GPIO 4). With this relay the motherboards Power and Ground pin become connected. The physical build is shown in the following figure.

Schaltung

Programming

The only thing missing is a programm to control the relay. For that I use Golang. Though it also runs with any other programming language, which has a GPIO library. Here comes the Go-Code:

package main

import (
	"log"
	"net/http"
	"time"

	rpio "github.com/stianeikeland/go-rpio"
)

var (
	powerPin = rpio.Pin(4)
)

func powerOff() {
	powerPin.High()
	time.Sleep(5 * time.Second)
	powerPin.Low()
}

func powerOn() {
	powerPin.High()
	time.Sleep(time.Second / 5)
	powerPin.Low()
}

func powerOnHandle(rw http.ResponseWriter, r *http.Request) {
	log.Println("Press power on")
	powerOn()
	rw.Write([]byte("PC hochgefahren/-runtergefahren"))
}

func powerOffHandle(rw http.ResponseWriter, r *http.Request) {
    log.Println("Hold power on")
    powerOff()
	rw.Write([]byte("PC getötet"))
}

func main() {
	err := rpio.Open()
	if err != nil {
		log.Fatal("Couldn't open gpio connection")
	}
	defer rpio.Close()
	powerPin.Output()

	http.HandleFunc("/pc/on", powerOnHandle)
	http.HandleFunc("/pc/off", powerOffHandle)
	log.Fatal(http.ListenAndServe(":8500", nil))
}

For clarification:

func powerOff() {
	powerPin.High()
	time.Sleep(5 * time.Second)
	powerPin.Low()
}

and

func powerOn() {
	powerPin.High()
	time.Sleep(time.Second / 5)
	powerPin.Low()
}

simulate a button press of 1/5 seconds (for simple power on/off) and 5 seconds (for immidiate power off). This functionalities shall be accessible in means of a HttpServer:

http.HandleFunc("/pc/on", powerOnHandle)
http.HandleFunc("/pc/off", powerOffHandle)

By requesting http://raspberrypi:8500/pc/on and http://raspberrypi:8500/pc/off the functionalities are executed. (where raspberrypi is the raspberry pi*s address)
After the programm is built, you should put the executable file in ~/.bashrc, so that it will be executed each startup and the service is continuously available.

Control with Android Smartphone

To remotely control the service with a smartphone, an app, which requests the http api, is needed. For that I make use of Http Shortcuts. With this you can send requests by widgets on your homescreen. Therefor I created two shortcuts.

Power On Power Off
Power On Power Off
You can see hometoolservice as an in-app constant, which contains the adress raspberrypi:8500. These Shortcuts can be converted to widgets on the homescreen.

Conclusion

In this article I showed, how you can power on and off your pc with your smartphone using an raspberry pi and a relay module. The same concept applies to other device’s power supplies. For example, you can turn a light, which is attached to a wall socket, on and of by cutting one wire of the cable and mount them to the relay’s switch. Therfor you need to adapt the code. Many youtubers explain how the light is supposed to be attached exactly. Attention! Working with wall sockets power is extremely dangerous.

Postscript

The provided code doesn’t have any security implementation. Everyone in the network who has access to the pi, can power on and off your pc. Hence the service shouldn’t be accessible to the public network in this state.
Moreover there is no guarantee for correctness of the circuitry. This project is pure experimental.

comments powered by Disqus