Using a Raspberry Pi Zero W to turn on a PC by pinging it's IP address
This is an explanation in case anyone else has this issue.
I ended up using a 470 Ohm resistor tied to the middle stem which was soldered into GPIO pin 24, with a red LED soldered to GPIO 18 pin and the ground next to it, tied the emitter pin (Leftmost with the flat facing you) to ground on the pi and the pc using the USB 2.0 pins and a ground pin header from an old xp machine, and used another of those headers to connect the Collector side (Rightmost one with the flat facing you) to the Left Pwr Sw header, (it was marked + on my motherboard).
I have a micro usb cable plugged into a scavenged USB 3.0 port from my windows 7 machine that is inside the case, which is plugged into the Pi Zero W, you can set the python script to run on reboot, but I am having some issue with the computer hardware so I did not, but you would simply enter
And this is the code you input, I have it in /code/ping.py, with a log file "/code/log.txt", and another for troubleshooting in "/code/gpioactivity.txt", but you can do whatever.
And a last note, this was mostly written in sporadic stages through BingGPT, I am not saying this is the best way to do it, but it seems to work great, just have other issues outside of this in the way.
import subprocess
import RPi.GPIO as GPIO
import time
from datetime import datetime, timedelta
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
GPIO.setup(24, GPIO.OUT)
"""
18 is LED, 24 is the added gpio that actually operates the power button.
"""
last_time = datetime.now()
offline_time = None
last_flush_time = datetime.now()
offline_count = 0
print()
print('This code is running 30sec pings when device is online, and 15sec pings when offline, not final 180 second sleep!')
print()
# CHANGE WHAT THIS PRINTS OR REMOVE IT BY MAKING IT A COMMENT
def is_device_online(ip_address):
# This function checks if a device with the given IP address is online by pinging it once
try:
output = subprocess.check_output(['ping', '-c', '1', ip_address])
print(output.decode('utf-8'))
# Make the line above a comment if you do not want to see it in the console
return True
except subprocess.CalledProcessError as e:
print(e.output.decode('utf-8'))
# Make the line above a comment if you do not want to see it in the console
return False
with open('log.txt', 'a') as f:
with open('/code/gpioactivity.txt', 'a') as g:
while True:
if is_device_online('192.168.0.127'):
# If the device is online, check if it was previously offline and log the duration of offline time
if offline_time:
online_time = datetime.now()
duration = online_time - offline_time
f.write(f'{online_time} - Device is back online after {duration}\n')
if (online_time - last_flush_time) > timedelta(weeks=1):
f.flush()
last_flush_time = online_time
offline_time = None
offline_count = 0
sleep_time = 30
print()
print(f'***WARNING*** Ping command set to sleep for {sleep_time} seconds ***WARNING***')
print()
time.sleep(sleep_time)
else:
# If the device is offline, log the time and use GPIO to restart it after 3 consecutive checks
print('Device is offline')
current_time = datetime.now()
if (current_time - last_time).seconds >= 180:
if not offline_time:
offline_time = current_time
f.write(f'{offline_time} - Device is offline\n')
if (offline_time - last_flush_time) > timedelta(weeks=1):
f.flush()
last_flush_time = offline_time
offline_count += 1
if offline_count >= 3:
GPIO.output(18, GPIO.HIGH)
GPIO.output(24, GPIO.HIGH)
time.sleep(10)
g.write(f'{datetime.now()} - GPIO used for 10 seconds\n')
print('\nGPIO used for 10 seconds\n')
# This is how long it holds the hard shutdown input
GPIO.output(18, GPIO.LOW)
GPIO.output(24, GPIO.LOW)
else:
GPIO.output(18, GPIO.HIGH)
GPIO.output(24, GPIO.HIGH)
time.sleep(1)
g.write(f'{datetime.now()} - GPIO used for 1 second\n')
print('\nGPIO used for 1 second\n')
# This is how long the normal startup output is held
GPIO.output(18, GPIO.LOW)
GPIO.output(24, GPIO.LOW)
time.sleep(15)
# This makes the code pause before repeating the pings, I accidentally removed this from the code in copying back and forth from the console.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now