diff --git a/PC_CONTROL_CODE/docker/.env b/PC_CONTROL_CODE/docker/.env new file mode 100644 index 0000000..ff1f847 --- /dev/null +++ b/PC_CONTROL_CODE/docker/.env @@ -0,0 +1,3 @@ +baud_rate= +serial_adapter= +wait_time= \ No newline at end of file diff --git a/PC_CONTROL_CODE/docker/Dockerfile b/PC_CONTROL_CODE/docker/Dockerfile new file mode 100644 index 0000000..072ee3f --- /dev/null +++ b/PC_CONTROL_CODE/docker/Dockerfile @@ -0,0 +1,28 @@ +FROM alpine:latest + +# https://docs.docker.com/reference/dockerfile/#environment-replacement +ENV MD1200BAUD=38400 +ENV SERIALADAPTER=/dev/ttyUSB0 +ENV EPPYSLEEPY=300 + +# VOLUME [""] + +RUN apk update && \ + apk add python3 py3-pip + +RUN mkdir /etc/MD1200FAN/ +WORKDIR /etc/MD1200FAN/ + +COPY ./mainDocker.py /etc/MD1200FAN/ +# COPY ./requirements.txt /etc/MD1200FAN/ + +RUN python3 -m venv venv && \ + venv/bin/python3 -m pip install --upgrade pip && \ + venv/bin/pip3 install PySerial + # venv/bin/pip3 install -r requirements.txt + +# VOLUME ["/etc/MD1200FAN/"] + +CMD ["venv/bin/python3", "mainDocker.py"] + +# CMD ["/etc/MD1200FAN/venv/bin/python3", "/etc/MD1200FAN/mainDocker.py"] \ No newline at end of file diff --git a/PC_CONTROL_CODE/docker/docker-compose.yml b/PC_CONTROL_CODE/docker/docker-compose.yml new file mode 100644 index 0000000..4b11330 --- /dev/null +++ b/PC_CONTROL_CODE/docker/docker-compose.yml @@ -0,0 +1,15 @@ +--- + +services: + mdfanchanger: + container_name: MD_Fan_Changer + build: ./ + image: dm1200fanchanger:V1 + environment: + - baud_rate=${baud_rate:-38400} + - serial_adapter=${serial_adapter:-"/dev/ttyUSB0"} + - wait_time=${wait_time:-300} + devices: + - /dev/ttyUSB0:/dev/ttyUSB0 + restart: unless-stopped + privileged: false diff --git a/PC_CONTROL_CODE/docker/mainDocker.py b/PC_CONTROL_CODE/docker/mainDocker.py new file mode 100644 index 0000000..22185ba --- /dev/null +++ b/PC_CONTROL_CODE/docker/mainDocker.py @@ -0,0 +1,141 @@ +import serial, time, os + +# CONST +if os.environ["MD1200BAUD"]: + MD1200BAUD = int(os.environ["MD1200BAUD"]) +else: + MD1200BAUD = 38400 + +if os.environ["SERIALADAPTER"]: + SERIALADAPTER = os.environ["SERIALADAPTER"] +else: + SERIALADAPTER = "/dev/ttyUSB0" + +GETTEMP = "_temp_rd" +SETFANPRCNT = "set_speed" + +if os.environ["EPPYSLEEPY"]: + EPPYSLEEPY = int(os.environ["EPPYSLEEPY"]) +else: + EPPYSLEEPY = 300 # 5 minutes + +# init +MDserial = serial.Serial( + port=SERIALADAPTER,\ + baudrate=MD1200BAUD,\ + parity=serial.PARITY_NONE,\ + stopbits=serial.STOPBITS_ONE,\ + bytesize=serial.EIGHTBITS,\ + timeout=1) + + + +def getTemp(inpMDreturning): + MDict = {} + + # Sanitise output + MDsanit = inpMDreturning.splitlines() + + #if there is smth do smth + if inpMDreturning: + + for line in MDsanit: + + if ">" in line or "b'" in line: + continue + + matchstm = line[2:6] + + match matchstm: + case "BP_1": + MDict["bp1"] = int(line[12:14]) + case "BP_2": + MDict["bp2"] = int(line[12:14]) + case "SIM0": + MDict["sim0"] = int(line[12:14]) + case "SIM1": + MDict["sim1"] = int(line[12:14]) + case "EXP0": + MDict["exp0"] = int(line[12:14]) + case "EXP1": + MDict["exp1"] = int(line[12:14]) + case "AVG": + MDict["avg"] = int(line[12:14]) + case _: + continue + return MDict + + +def setSpeed(inSpeeDict: dict): + + bpavrg = 0 + # Some safe fan speedvalue + defoutprntg = 27 + # default + outfanprcntg = 0 + + # Decide on fan speeds + LOW_FAN_TRSHD = 21 + HIGH_FAN_TRSHD = 40 + TEMP_FACTOR = 19 + + # get backplanbe average + if inSpeeDict["bp1"] and inSpeeDict["bp2"]: + bpavrg = (inSpeeDict["bp1"] + inSpeeDict["bp2"]) /2 + + outfanprcntg = int((bpavrg / (HIGH_FAN_TRSHD - LOW_FAN_TRSHD)) * TEMP_FACTOR) + + # Set fan speed + if outfanprcntg >= 20: + MDserial.write(("set_speed " + str(outfanprcntg) + " \n\r").encode()) + print(f"setting {outfanprcntg}%") + return 0 + else: + # Set default value + MDserial.write(("set_speed " + str(defoutprntg) + " \n\r").encode()) + return 1 + + # If something goes super wrong + return -1 + + + +# Check if UART is used +# Not neede because when defining MDserial it gets automatically opened +# Will leave it here anyway +# try: +# MDserial.open() +# except serial.serialutil.SerialException: +# # MDserial.close() +# # MDserial.open() +# print("Port allready opened.\nTry closing it first") + +while True: + # https://stackoverflow.com/questions/52578122/not-able-to-send-the-enter-command-on-pyserial + MDserial.write("_temp_rd\n\r".encode()) + time.sleep(1) + MDreturning = MDserial.read_until(" >").decode() + + MDtempDict = getTemp(MDreturning) + setSpeedrcode = setSpeed(MDtempDict) + + # good + if setSpeedrcode == 0: + # print("Were mint") + time.sleep(EPPYSLEEPY) + # not good + elif setSpeedrcode == 1: + print("Ambigous temperature readings.\nFalling back to safe values.") + time.sleep(EPPYSLEEPY) + # very not good + elif setSpeedrcode == -1: + print("o nyo") + exit() + # very very very not good + else: + print("idk") + exit() + + +print("closing port") +MDserial.close() diff --git a/PC_CONTROL_CODE/main.py b/PC_CONTROL_CODE/main.py index 11a2a6a..f9fd630 100644 --- a/PC_CONTROL_CODE/main.py +++ b/PC_CONTROL_CODE/main.py @@ -5,8 +5,8 @@ MD1200BAUD = 38400 SERIALADAPTER = "/dev/ttyUSB0" GETTEMP = "_temp_rd" SETFANPRCNT = "set_speed" -EPYSLEEPY = 300 # 5 minutes -#EPYSLEEPY = 150 # 2,5 minutes +EPPYSLEEPY = 300 # 5 minutes +#EPPYSLEEPY = 150 # 2,5 minutes # init MDserial = serial.Serial( @@ -112,11 +112,11 @@ while True: # good if setSpeedrcode == 0: # print("Were mint") - time.sleep(EPYSLEEPY) + time.sleep(EPPYSLEEPY) # not good elif setSpeedrcode == 1: print("Ambigous temperature readings.\nFalling back to safe values.") - time.sleep(EPYSLEEPY) + time.sleep(EPPYSLEEPY) # very not good elif setSpeedrcode == -1: print("o nyo") diff --git a/PC_CONTROL_CODE/systemd/MD1200Fans.example.service b/PC_CONTROL_CODE/systemd/MD1200Fans.example.service index 2015c41..6e1a2e9 100644 --- a/PC_CONTROL_CODE/systemd/MD1200Fans.example.service +++ b/PC_CONTROL_CODE/systemd/MD1200Fans.example.service @@ -1,6 +1,6 @@ [Unit] -Description=Netflow to InfluxDB script -After=multi-user.target network.target network-online.target +Description=Adjust MD1200/MD1220 fan speeds +After=multi-user.target # Place in /etc/systemd/system/ [Service] @@ -10,14 +10,13 @@ Type=simple Restart=on-failure # EnvironmentFile=/etc/NetFlux/netflow.env # User=myuser -WorkingDirectory=/etc/NetFlux/HQ/ -ExecStart=/etc/NetFlux/HQ/venv/bin/python3 /etc/NetFlux/HQ/HQnetflow.py --serve-in-foreground - +WorkingDirectory=/etc/MD1200FAN/ +ExecStart=/etc/MD1200FAN/venv/bin/python3 /etc/MD1200FAN/main.py --serve-in-foreground #StandardInput=tty-force # Log file will be create if it doesn't exist -StandardOutput=append:/var/log/HQNetFlowInflux.log -StandardError=append:/var/log/HQNetFlowInflux.errlog +StandardOutput=append:/var/log/MD1200FAN.py.log +StandardError=append:/var/log/MD1200FAN.py.errlog # StandardOutput=syslog # StandardError=syslog diff --git a/PC_CONTROL_CODE/systemd/MD1200Fans.service b/PC_CONTROL_CODE/systemd/MD1200Fans.service index 9598904..f8c19d9 100644 --- a/PC_CONTROL_CODE/systemd/MD1200Fans.service +++ b/PC_CONTROL_CODE/systemd/MD1200Fans.service @@ -1,6 +1,6 @@ [Unit] -Description=Netflow to InfluxDB script -After=multi-user.target network.target network-online.target +Description=Adjust MD1200/MD1220 fan speeds +After=multi-user.target # Place in /etc/systemd/system/ [Service] @@ -11,12 +11,12 @@ Restart=on-failure # EnvironmentFile=/etc/NetFlux/netflow.env # User=myuser WorkingDirectory=/dir/to/script/ -ExecStart=/dir/to/script'sVENV/venv/bin/python3 /dir/to/script/NetFlowCollect.py --serve-in-foreground +ExecStart=/dir/to/script'sVENV/venv/bin/python3 /dir/to/script/main.py --serve-in-foreground #StandardInput=tty-force # Log file will be create if it doesn't exist -StandardOutput=append:/var/log/NetFlowCollect.log -StandardError=append:/var/log/NetFlowCollect.errlog +StandardOutput=append:/var/log/MD1200FAN.py.log +StandardError=append:/var/log/MD1200FAN.py.errlog # StandardOutput=syslog # StandardError=syslog diff --git a/README.md b/README.md index 2b4d41b..5698169 100644 --- a/README.md +++ b/README.md @@ -4,9 +4,40 @@ A set of scripts that automagically set fan speed on a MD1200 (probably MD1220 a ## PC -Working working working. +### Docker -So far seems to work. +In .env file change + +```serial_adapter``` which is a serial port you're using. +On linux it is /dev/ttyUSBx and on windows it is COMx + +```wait_time``` is the interval in which script is checking temperature. By default it is 300 seconds, which is 5 minutes. + +### Systemd + +First create virtual enviroment + +``` +python3 -m venv venv +``` + +Then install required modules +``` +venv/bin/pip3 install PySerial +``` +After that you just need to change a few things +```SERIALADAPTER``` to a port you're using. + +On linux it is /dev/ttyUSBx and on windows it is COMx + + +```EPPYSLEEPY``` is the interval in which script is checking temperature. By default it is 300 seconds, which is 5 minutes. + +### Proxmox LXC + +You can also run it in LXC container on your Proxmox host. Just follow the [systemd](###Systemd) instructions. + +Here you will also need to add ```/dev/ttyUSBx``` to your LXC container. You do it under Resources -> Add -> Device Passthrough -> ```/dev/ttyUSBx``` as Device Path. ## STM32F103C6T6