Created
March 11, 2025 11:11
-
-
Save sonpython/808a4d65acfac51cdc27c64e0625779f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# script to auto turn off and on the fan of CUBE pi case based on CPU temp | |
import time | |
import os | |
from CubeRaspberryLib import CubeRaspberry | |
# Khởi tạo bot CUBE Pi | |
bot = CubeRaspberry(i2c_bus=1) | |
# Ngưỡng nhiệt độ | |
TEMP_ON = 50 # Bật quạt khi nhiệt độ >= 50°C | |
TEMP_OFF = 45 # Tắt quạt khi nhiệt độ <= 45°C | |
def get_cpu_temperature(): | |
"""Lấy nhiệt độ CPU hiện tại""" | |
try: | |
temp_str = os.popen("vcgencmd measure_temp").readline() | |
temp = float(temp_str.replace("temp=", "").replace("'C\n", "")) | |
return temp | |
except Exception as e: | |
print("Lỗi khi đọc nhiệt độ:", e) | |
return 0 # Trả về 0 nếu lỗi | |
def main(): | |
fan_on = False # Trạng thái quạt | |
bot.set_Fan(0) # đầu tiên tắt quạt | |
while True: | |
temp = get_cpu_temperature() | |
print(f"Nhiệt độ CPU: {temp:.2f}°C") | |
if temp >= TEMP_ON and not fan_on: | |
print("⚙️ Bật quạt...") | |
bot.set_Fan(1) | |
fan_on = True | |
elif temp <= TEMP_OFF and fan_on: | |
print("🛑 Tắt quạt...") | |
bot.set_Fan(0) | |
fan_on = False | |
time.sleep(5) # Kiểm tra mỗi 5 giây | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment