From 6d11f4e3984f04546926c5a19411fdfd248ddc2c Mon Sep 17 00:00:00 2001 From: Jonas_Jones <91549607+J-onasJones@users.noreply.github.com> Date: Mon, 21 Feb 2022 00:03:36 +0100 Subject: [PATCH] Uploaded main projects --- pc/main.py | 79 +++++++++++++++++++++++++ pi1/main.py | 57 ++++++++++++++++++ pi4/main.py | 165 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 301 insertions(+) create mode 100644 pc/main.py create mode 100644 pi1/main.py create mode 100644 pi4/main.py diff --git a/pc/main.py b/pc/main.py new file mode 100644 index 0000000..7954c12 --- /dev/null +++ b/pc/main.py @@ -0,0 +1,79 @@ +from ftplib import FTP +from io import BytesIO +from time import sleep, time +from xml.sax.handler import feature_namespace_prefixes + +launch_time = time() + +print_stats = True + +hostname = "172.24.1.193" +username = "testuser" +password = "uwu" + +ftp = FTP(hostname, username, password) +ftp.encoding = "utf-8" + +filename = "output.txt" + +while True: + start_time = time() + + with open(filename, "wb") as file: + # use FTP's RETR command to download the file + ftp.retrbinary(f"RETR {filename}", file.write) + + file = open(filename, 'r') + content = file.read() + content = content.split(";") + + fanmode = content[0] + slave_fanmode = content[1] + fanspeed = content[2] + fanspeed2 = content[3] + thermal = content[4] + slave_thermal = content[5] + cpu_usage = content[6] + cpu_freq = content[7] + cpu_up = content[8] + total_ram = content[9] + ram_usage = content[10] + ram_free = content[11] + ram_percent = content[12] + swap_percent = content[13] + disk_percent = content[14] + + mc_motd = content[15] + mc_version = content[16] + mc_players_max = content[17] + mc_players_on = content[18] + mc_version_brand = content[19] + mc_plugins = content[20] + mc_playerlist = content[21] + + sleep(2) + + stop_time = time() + + if print_stats: + print("---------- Uptime: " + str(cpu_up) + " ----------") + print("Loop time:\t" + str(round(stop_time - start_time, 2)) + "\ts") + print("CMD Uptime:\t" + str(round(stop_time - launch_time, 2)) + "\ts") + print("CPU") + print("\tusage:\t" + str(cpu_usage) + "\t%") + print("\tfreq:\t" + str(cpu_freq) + "\tMHz") + print("\ttemp:\t" + str(thermal) + "\tC") + print("RAM") + print("\tusage:\t" + str(ram_percent) + "\t%") + print("\tswap :\t" + str(swap_percent) + "\t%") + print("Disks") + print("\tusage:\t" + str(disk_percent) + "\t%") + print("FANMODE") + print("\tmain:\t" + str(fanmode) + "\t%") + print("\tslave:\t" + str(slave_fanmode) + "\t%") + print("\tglobal:\t" + str(fanspeed) + "\t%") + print("\t2ndary:\t" + str(fanspeed2) + "\t%") + + +# Close the Connection +ftp.quit() \ No newline at end of file diff --git a/pi1/main.py b/pi1/main.py new file mode 100644 index 0000000..94d7347 --- /dev/null +++ b/pi1/main.py @@ -0,0 +1,57 @@ +import RPi.GPIO as GPIO +import subprocess +import time + +# GPIO setup +GPIO.setmode(GPIO.BCM) +GPIO.setwarnings(False) + +# Powering Status Pin to indicate that control program is up +GPIO.setup(4, GPIO.OUT) +GPIO.output(4, True) + +# setting up communication pins for binary fanmode transmission +GPIO.setup(17, GPIO.OUT) +GPIO.setup(18, GPIO.OUT) + +# define default variables -> fan speed will be at max. if someting goes wrong with getting cpu temps +thermal = -99 +fanmode = 100 +i = 0 + +# function to get cpu temperature on linux and macOS and decide on fan speed. +def temp(thermal, fanmode): + thermal = str(round(float(subprocess.check_output("cat /sys/class/thermal/thermal_zone0/temp", shell=True).rstrip())/1000,2)) + if thermal <= 20: + fanmode = 0 + elif 20 < thermal <= 40: + fanmode = 25 + elif 40 < thermal <= 60: + fanmode = 50 + elif 60 < fanmode <= 70: + fanmode = 75 + elif 70 < fanmode: + fanmode = 100 + return thermal, fanmode + +# main loop +while True: + # get cpu temp + temp(thermal, fanmode) + if fanmode == 0 or fanmode == 25: + GPIO.output(17, False) + GPIO.output(18, True) + elif fanmode == 50: + GPIO.output(17, True) + GPIO.output(18, False) + elif fanmode == 75: + GPIO.output(17, True) + GPIO.output(18, True) + else: + GPIO.output(17, False) + GPIO.output(18, False) + # cpu temp will be updated every 5 seconds + time.sleep(5) + i += 1 + # log cpu temp and fan speed + print("----- Run Nr.:", i, "\nCpu temperature measured: ", thermal + "°C", "- Fanspeed: ", fanmode + "%") \ No newline at end of file diff --git a/pi4/main.py b/pi4/main.py new file mode 100644 index 0000000..84e99b4 --- /dev/null +++ b/pi4/main.py @@ -0,0 +1,165 @@ +from http import server +import RPi.GPIO as GPIO +import subprocess, os, time + +# GPIO setup +GPIO.setmode(GPIO.BCM) +GPIO.setwarnings(False) + +# Powering Status Pin to indicate that control program is up +GPIO.setup(4, GPIO.OUT) +GPIO.output(4, True) + +# setting up communication pins for binary fanmode transmission +GPIO.setup(17, GPIO.IN) +GPIO.setup(18, GPIO.IN) + +# setting up control pin for fancontrol +GPIO.setuo(4, GPIO.OUT) + +# define default variables -> fan speed will be at max. if someting goes wrong with getting cpu temps +thermal = -99 +fanmode = 100 +i = 0 + +file = open("output.txt", 'r+') + +cpu_usage = "--" +total_ram = "--.--" +ram_usage = "--.--" +ram_free = "--.--" + +# check mc server status too. Set to False to disable Minecraft server module +check_mcserver = True +mc_ip = "127.0.0.1" +mc_port = "25565" + +mc_motd = "-" +mc_version = 0 +mc_players_max = 0 +mc_players_on = 0 +mc_version_brand = "-" +mc_plugins = [] +mc_player_list = [] + +# function to get cpu temperature on linux and macOS and decide on fan speed. +def temp(thermal, fanmode): + thermal = str(round(float(subprocess.check_output("cat /sys/class/thermal/thermal_zone0/temp", shell=True).rstrip())/1000,2)) + if thermal <= 20: + fanmode = 0 + elif 20 < thermal <= 40: + fanmode = 25 + elif 40 < thermal <= 60: + fanmode = 50 + elif 60 < fanmode <= 70: + fanmode = 75 + elif 70 < fanmode: + fanmode = 100 + return thermal, fanmode + +# function to round values +def display(val): + return round(float(val),2) + +# function to get system informations +def sys_monitor(cpu_usage, total_ram, ram_usage, ram_free): + cpu_usage = str(round(float(os.popen('''grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage }' ''').readline()),2)) + total_ram = str(round(display(subprocess.check_output("free | awk 'FNR == 2 {print $2/1000000}'", shell=True)))) + ram_usage = str(subprocess.check_output("free | awk 'FNR == 2 {print $3/($3+$4)*100}'", shell=True)) + ram_free = str(100 - display(ram_usage)) + return cpu_usage, total_ram, ram_usage, ram_free + +# function to get server status if enabled +def mcserver(mc_motd, mc_version, mc_players_max, mc_players_on, mc_version_brand, mc_plugins, mc_player_list): + if check_mcserver: + # importing module mcstatus --> must be installed for this in order to work + from mcstatus import MinecraftServer + mcserver = MinecraftServer.lookup(mc_ip + ":" + mc_port) + status = mcserver.status() + mc_motd = status.description + mc_version = status.version.name + mc_players_max = status.players.max + mc_players_on = status.players.online + + # IMPORTANT! + # for the following code to work, the query must be enabled in the server config file ('server.properties' --> 'enable-query=True'). + # If you don't have access to that file but still want your server status, you can delete the following lines that contain the 'query' argument. + # This will provide you with a more limited status: + # the version brand (vanilla, fabric modded, bukkit, etc.), the plugin list (if any) and the list if players won't be visible in that case + query = mcserver.query() + mc_version = query.software.version + mc_version_brand = query.software.brand + mc_plugins = query.software.plugins + mc_player_list = query.players.names + + # if no query --> delete 'mc_version_brand', 'mc_plugins' and 'mc_player_list' from the return argument + # the line below should then look like THIS: return mc_motd, mc_version, mc_players_max, mc_players_on + return mc_motd, mc_version, mc_players_max, mc_players_on, mc_version_brand, mc_plugins, mc_player_list + + +while True: + # eecute temperature function + + temp(thermal, fanmode) + # determine fanmode of 2nd raspberry PI and guess CPU temps + if GPIO.input(17) and GPIO.input(18): + slave_fanmode = 75 + slave_thermal = "60 - 70" + elif GPIO.input(17) and not GPIO.input(18): + slave_fanmode = 50 + slave_thermal = "40 - 60" + elif not GPIO.input(17) and GPIO.input(18): + slave_fanmode = 25 + slave_thermal = "0 - 40" + elif not GPIO.input(17) and not GPIO.input(18): + slave_fanmode = 100 + slave_thermal = "70 - 100" + else: + slave_fanmode = 100 + slave_thermal = "-" + + # get other system infos and Minecraft Server stats + sys_monitor(cpu_usage, total_ram, ram_usage, ram_free) + mcserver(mc_motd, mc_version, mc_players_max, mc_players_on, mc_version_brand, mc_plugins, mc_player_list) + + # write all data to list + system_infos = [fanmode, slave_fanmode, thermal, slave_thermal, cpu_usage, total_ram, ram_usage, ram_free, mc_motd, mc_version, mc_players_max, mc_players_on, mc_version_brand, mc_plugins, mc_player_list] + + # delete all text in file and write list to file + file.truncate(0) + for e in system_infos: + file.write(e) + file.write(";") + + # determine the PI with higher CPU temperature + if slave_fanmode > fanmode: + fanspeed = slave_fanmode + else: + fanspeed = fanmode + if fanmode == 100: + GPIO.output(4, True) + time.sleep(5) + elif fanmode == 75: + while i < 12: + GPIO.output(4, True) + time.sleep(0.3) + GPIO.outut(4, False) + time.sleep(0.1) + i += 1 + elif fanmode == 50: + while i < 25: + GPIO.output(4, True) + time.sleep(0.1) + GPIO.output(4, False) + time.sleep(0.1) + i += 1 + elif fanmode == 25: + while i < 12: + GPIO.output(4, True) + time.sleep(0.1) + GPIO.outut(4, False) + time.sleep(0.3) + i += 1 + else: + GPIO.output(4, True) + time.sleep(5) \ No newline at end of file