[PYTHON] Power on / off your PC with raspberry pi

pc_power_unit.png

In my home environment, my main windows machine is always running, and I also run pi for a quick test. There is one ubuntu machine with the main machine down for heavy processing and testing, and although it is powerless, it has a GTX650. Most of the time, it's pretty much in the cloud, and if you keep it running, the electricity bill will be high and the room will get hotter, so I hardly move it. But sometimes I use it when I need GPU or machine power. Also, when I was using it from the outside and hung it, nothing happened, so I wanted to do something about it.

So, I created a unit and a script that can turn the power on and off directly from pi.

A little explanation

code

power.py


#!/usr/bin/env python

import RPi.GPIO as GPIO
import time
import subprocess

#out put gpio pin
OUTPUT=16

#host ip
HOST="Destination IP"

#initialize
def init():
        GPIO.setmode(GPIO.BOARD)
        GPIO.setup(OUTPUT,GPIO.OUT,initial=GPIO.LOW)

#finalize
def finalize():
        GPIO.cleanup()

#gpio power on
def on():
        GPIO.output(OUTPUT,GPIO.HIGH)
        time.sleep(2)
        GPIO.output(OUTPUT,GPIO.LOW)

#gpio power off
def off():
        GPIO.output(OUTPUT,GPIO.HIGH)
        time.sleep(10)
        GPIO.output(OUTPUT,GPIO.LOW)

#ping check
def ping_check():
    pcheck = subprocess.Popen(["ping", "-c", "1", HOST],stdout = subprocess.PIPE,stderr = subprocess.PIPE )
    time.sleep(10)
    pcheck_out, pcheck_error = pcheck.communicate()
    out_line = pcheck_out.split("\n")
    for i in range(0,len(out_line)):
        print out_line[i]
    if "1 received, 0% packet loss" in pcheck_out:
        print "ping success!!"
    else:
        print "--ping fail--"

#power on main
def power_on():
        print "power on y/n"
        input_line = raw_input()
        if input_line == "y":
                print "power on please wait"
                on()
                time.sleep(120)
                print "ping check please wait"
                ping_check()
        elif input_line == "n":
                print "power on cancel"
        else:
                print "input error"

#power off main
def power_off():
        print "--CAUTION---"
        print "power off? y/n"
        input_line = raw_input()
        if input_line == "y":
                print "power off please wait"
                off()
                print "ping check please wait"
                ping_check()
        elif input_line == "n":
                print "power off cancel"
        else:

                print "input error"

#main
if __name__ == '__main__':
        try:
                init()
                print "please input power on/off "
                input_l = raw_input()
                if input_l == "on":
                        power_on()
                elif input_l == "off":
                        power_off()
                else:
                        print "input error"
                finalize()
        except:
                finalize()

circuit diagram

pc_pwer_unit.png

Recommended Posts

Power on / off your PC with raspberry pi
Power on / off Raspberry pi on Arduino
Play with your Ubuntu desktop on your Raspberry Pi 4
Control power on / off of USB port of Raspberry Pi
Programming normally with Node-RED programming on Raspberry Pi 3
Power SG-90 servo motor with raspberry pi
Working with sensors on Mathematica on Raspberry Pi
Working with GPS on Raspberry Pi 3 Python
pigpio on Raspberry pi
Enjoy electronic work with GPIO on Raspberry Pi
DigitalSignage with Raspberry Pi
Cython on Raspberry Pi
Connect to MySQL with Python on Raspberry Pi
Record temperature and humidity with systemd on Raspberry Pi
Run LEDmatrix interactively with Raspberry Pi 3B + on Slackbot
Try debugging Python on Raspberry Pi with Visual Studio.
Control brushless motors with GPIOs on Raspberry Pi Zero
Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python
Troubleshoot with installing OpenCV on Raspberry Pi and capturing
Mutter plants with Raspberry Pi
Introduced pyenv on Raspberry Pi
Use NeoPixel on Raspberry Pi
Install OpenCV4 on Raspberry Pi 3
Install TensorFlow 1.15.0 on Raspberry Pi
Make an air conditioner integrated PC "airpi" with Raspberry Pi 3!
[Raspberry Pi] Stepping motor control with Raspberry Pi
Testing uart communication on Raspberry Pi
Use vl53l0x with Raspberry Pi (python)
Servo motor control with Raspberry Pi
MQTT on Raspberry Pi and Mac
raspberry pi 4 centos7 install on docker
Serial communication with Raspberry Pi + PySerial
Install ghoto2 on Raspberry Pi (memo)
Try using ArUco on Raspberry Pi
Try L Chika with raspberry pi
OpenCV installation procedure on Raspberry Pi
VPN server construction with Raspberry Pi
Try moving 3 servos with Raspberry Pi
Detect switch status on Raspberry Pi 3
Install OpenMedia Vault 5 on Raspberry Pi 4
Using a webcam with Raspberry Pi
L Chika on Raspberry Pi C #
Build wxPython on Ubuntu 20.04 on raspberry pi 4
Control the motor with a motor driver using python on Raspberry Pi 3!
Let's access your Raspberry Pi from outside your home with VPN (WireGuard)
Graph display of household power consumption with 3GPI and Raspberry Pi
Measure SIM signal strength with Raspberry Pi
Pet monitoring with Rekognition and Raspberry pi
"Honwaka Notification Lamp" on Raspberry Pi Part 2
Detect "brightness" using python on Raspberry Pi 3!
Connect to VPN with your smartphone and turn off / on the server
Detect analog signals with A / D converter using python on Raspberry Pi 3!
USB boot on Raspberry Pi 4 Model B
Hello World with Raspberry Pi + Minecraft Pi Edition
"Honwaka Notification Lamp" on Raspberry Pi Part 1
Build a Tensorflow environment with Raspberry Pi [2020]
Enable UART + serial communication on Raspberry Pi
Adafruit Python BluefruitLE works on Raspberry Pi.
Use python on Raspberry Pi 3 to light the LED with switch control!
Get BITCOIN LTP information with Raspberry PI
Operate Minecraft Pi Edition from your PC