Using a Raspberry Pi to control a togglable power strip that a laptop-server is plugged in to.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

27 lines
747 B

import socket
from os.path import exists
from re import match
server_ip = "127.0.0.1"
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.connect((server_ip,12894))
except:
exit()
if exists("/sys/class/power_supply/BAT1/energy_now"):
now,full = None,None
with open("/sys/class/power_supply/BAT1/energy_now","r") as f:
now = eval(f.read())
with open("/sys/class/power_supply/BAT1/energy_full","r") as f:
full = eval(f.read())
percentage = str(int(now/full*100))
else:
# Not a laptop, assume debugging
percentage = ""
while not match(r"\d{1,3}",percentage):
percentage = input("Enter the fake percentage to send: ")
sock.sendall(bytes(percentage, 'utf-8'))
sock.close()