Aula Jazmati
Published © MIT

Raspberry Pi Fan Controller Using Hexabitz Mosfet Module

This project has the ability to only turn the fan on when needed by monitoring the core temperature and CPU usage.

IntermediateFull instructions provided3 hours795
Raspberry Pi Fan Controller Using Hexabitz Mosfet Module

Things used in this project

Story

Read more

Schematics

Raspberry Pi Fan Controller System

Raspberry Pi Fan Controller System Schematic

Code

The code

Python
import psutil
import serial
import time
from tkinter import *
import re, subprocess
ser = serial.Serial(        
               port='/dev/ttyUSB0',
               baudrate = 921600,
               parity=serial.PARITY_NONE,
               stopbits=serial.STOPBITS_ONE,
               bytesize=serial.EIGHTBITS,
               timeout=1
           )
print(ser.name)
root=Tk()
root.title("RPi Fan Controller")
ser.write ('\r'.encode())
msg1=ser.read(2000)
print (msg1)
#led_state = False
old_input_state = True # pulled-up
def check_CPU_temp():
    temp = None
    err, msg = subprocess.getstatusoutput('vcgencmd measure_temp')
    if not err:
        m = re.search(r'-?\d\.?\d*', msg)   # a solution with a  regex 
        try:
            temp = float(m.group())
        except:
            pass
    return temp, msg
def start():
  temp, msg = check_CPU_temp()
  #cpu_pc = psutil.cpu_percent(interval=2)
  lable= Label (root,width=24,fg="plum",bg="black")
  lable.config(font=("Courier",20))
  lable.config(text= msg)
  #lable.config(text= cpu_pc)
  lable.update()
  lable.pack()
def start2():
  cpu_pc = psutil.cpu_percent(interval=1)
  lable= Label (root,width=24,fg="bisque",bg="black")
  lable.config(font=("Courier",20))
  lable.config(text= 'CPU:' + str(cpu_pc))
  lable.update()
  lable.pack()
def fanon():
    ser.write ('on 10000'.encode())
    ser.write ('\r'.encode())
    msg3=ser.read(2000)
    print(msg3) 
def fanoff():
    ser.write ('off'.encode())
    ser.write ('\r'.encode())
    msg2=ser.read(1000)
    print(msg2)
button=Button(root,text='Stop',bg ='lightgray',width=50,command=root.destroy)
button.pack()
button2 =Button(root,text='CPU Usage',bg ='bisque',width=50,command=start2)
button2.pack()
B22=Button(root,text='Raspberry Pi Core Temperature', bg ='plum',width=50,command=start)
B22.pack()
button3 =Button(root,text='Fan/on',bg ='pink',width=50,command=fanon)
button3.pack()
Bsample4=Button(root,text='Fan/off', bg ='cyan',width=50,command=fanoff)
Bsample4.pack()
try:
   while (1):
      temp, msg = check_CPU_temp()
 
      print (temp)
      print ("full message:    ", msg)
      cpu_pc = psutil.cpu_percent(interval=2)
      print ('CPU:', (cpu_pc))
      print(psutil.sensors_temperatures())
      if (cpu_pc >= 45 or temp >45):
        new_input_state = False
        if new_input_state == False and old_input_state == True:
            ser.write ('on 10000'.encode())
            ser.write ('\r'.encode())
            msg3=ser.read(2000)
            print(msg3)
        else:
            new_input_state = True
            #time.sleep(1)
            ser.write ('off'.encode())
            ser.write ('\r'.encode())
            msg2=ser.read(1000)
            print(msg2)
      root.mainloop()

except KeyboardInterrupt:
    print ("Good bye ^_^")

MOSFET Switch (H0FR7)-Firmware

Credits

Aula Jazmati

Aula Jazmati

49 projects β€’ 192 followers
(PhD) in Electronic Engineering 2023 πŸ’‘πŸ•ŠοΈ

Comments