From 17d734df530df6b3718c919d142837a13b8dd999 Mon Sep 17 00:00:00 2001 From: Taylor Helsper Date: Sun, 9 Feb 2020 19:36:09 -0600 Subject: [PATCH] Add RockPro64 Fan Controller --- .../etc/systemd/system/fan_control.service | 20 +++++++ rootfs/standard/usr/bin/fan_control.py | 55 +++++++++++++++++++ .../standard/usr/bin/mynode_post_upgrade.sh | 6 ++ setup/setup_base_image_rockpro64.txt | 8 --- setup/setup_device.sh | 6 ++ 5 files changed, 87 insertions(+), 8 deletions(-) create mode 100644 rootfs/rockpro64/etc/systemd/system/fan_control.service create mode 100755 rootfs/standard/usr/bin/fan_control.py diff --git a/rootfs/rockpro64/etc/systemd/system/fan_control.service b/rootfs/rockpro64/etc/systemd/system/fan_control.service new file mode 100644 index 00000000..8916252d --- /dev/null +++ b/rootfs/rockpro64/etc/systemd/system/fan_control.service @@ -0,0 +1,20 @@ +# myNode Fan Control (RockPro64) +# /etc/systemd/system/fan_control.service + +[Unit] +Description=myNode Fan Control + +[Service] +Type=simple +KillMode=control-group +ExecStart=/usr/bin/fan_control.py +User=root +Group=root +StandardOutput=syslog +StandardError=syslog +SyslogIdentifier=mynode_fan +Restart=always +RestartSec=30 + +[Install] +WantedBy=multi-user.target diff --git a/rootfs/standard/usr/bin/fan_control.py b/rootfs/standard/usr/bin/fan_control.py new file mode 100755 index 00000000..4f055546 --- /dev/null +++ b/rootfs/standard/usr/bin/fan_control.py @@ -0,0 +1,55 @@ +#!/usr/bin/python3 +from time import sleep + +current_fan_speed=0 + +def get_temp(): + with open('/sys/class/thermal/thermal_zone0/temp') as f: + results = f.read() + device_temp = int(int(results) / 1000) + print("Current temp: {}C".format(device_temp)) + return device_temp + +def set_fan_speed(percent): + global current_fan_speed + + if percent != current_fan_speed: + print("Setting fan speed: {}%".format(percent)) + fan_speed=int(float(percent) * 2.55) + current_fan_speed = percent + with open('/sys/class/hwmon/hwmon0/pwm1', 'w') as f: + f.write("{}".format(fan_speed)) + + +def calculate_fan_setting(temp): + if temp <= 50: + set_fan_speed(0) + elif temp >= 65 and temp < 70: + set_fan_speed(30) + elif temp >= 70 and temp < 75: + set_fan_speed(60) + elif temp >= 75: + set_fan_speed(100) + +def main_loop(): + # On start run fan for a bit + try: + set_fan_speed(60) + sleep(20) + set_fan_speed(0) + except: + pass + + while True: + try: + temp = get_temp() + calculate_fan_setting(temp) + except Exception as e: + print("Exception: {}".format(str(e))) + set_fan_speed(80) + finally: + sleep(10) + +# This is the main entry point for the program +if __name__ == "__main__": + main_loop() \ No newline at end of file diff --git a/rootfs/standard/usr/bin/mynode_post_upgrade.sh b/rootfs/standard/usr/bin/mynode_post_upgrade.sh index 56c01069..9d88ef39 100755 --- a/rootfs/standard/usr/bin/mynode_post_upgrade.sh +++ b/rootfs/standard/usr/bin/mynode_post_upgrade.sh @@ -418,6 +418,12 @@ apt-get remove -y tor apt-get install -y tor +# Enable fan control +if [ $IS_ROCKPRO64 = 1 ]; then + systemctl enable fan_control +fi + + # Enable any new/required services systemctl enable firewall systemctl enable invalid_block_check diff --git a/setup/setup_base_image_rockpro64.txt b/setup/setup_base_image_rockpro64.txt index 19c39088..8392d3a1 100644 --- a/setup/setup_base_image_rockpro64.txt +++ b/setup/setup_base_image_rockpro64.txt @@ -25,14 +25,6 @@ sudo apt-get -y upgrade sudo apt-get -y install network-manager tmux sudo apt-get -y install lua5.3 lua5.3-dev gcc make git -# Install fan controller -git clone https://github.com/tuxd3v/ats.git -cd ats -make -sudo make install -cd ../ -rm -rf ats - # Regenerate MAC Address for RockPro64 . /usr/lib/armbian/armbian-common CONNECTION="$(nmcli -f UUID,ACTIVE,DEVICE,TYPE connection show --active | tail -n1)" diff --git a/setup/setup_device.sh b/setup/setup_device.sh index ebecbb74..900448fc 100755 --- a/setup/setup_device.sh +++ b/setup/setup_device.sh @@ -525,6 +525,12 @@ sync sleep 1 +# Enable fan control +if [ $IS_ROCKPRO64 = 1 ]; then + systemctl enable fan_control +fi + + # Setup myNode Startup Script systemctl daemon-reload systemctl enable mynode