ggarciacompanys
Published

DIY PC Stats Monitor

DIY Arduino Project with Python to Display your PC's stats (CPU temp, CPU load, GPU temp and GPU load)

IntermediateShowcase (no instructions)3,986
DIY PC Stats Monitor

Things used in this project

Story

Read more

Schematics

Schematics

Quite simple

Code

PC_Stats_Monitor

Arduino
#include <UTFTGLUE.h>              //use GLUE class and constructor
#include <string.h>
UTFTGLUE myGLCD(0,A2,A1,A3,A4,A0); //initiate screen

void setup()
{
  randomSeed(analogRead(0));
  Serial.begin(115200);
  Serial.setTimeout(1);
  
  //Print the basic layout of the Display
  myGLCD.InitLCD(0);
  myGLCD.setFont(BigFont);
  
  myGLCD.setColor(255, 255, 255);
  myGLCD.fillRect(0, 0, 319, 479);
  
  myGLCD.setBackColor(255, 255, 255);
  myGLCD.setColor(0, 0, 0);
  
  myGLCD.print("CPU TEMP: ", LEFT, 30);
  myGLCD.print("C ",300, 30);

  myGLCD.print("CPU LOAD: ", LEFT, 30+135);
  myGLCD.print("%", 300, 30+135);
  
  myGLCD.print("GPU TEMP: ", LEFT, 30+135*2);
  myGLCD.print("C", 300, 30+135*2);
  
  myGLCD.print("GPU LOAD: ", LEFT, 30+135*3);
  myGLCD.print("%", 300, 30+135*3);

  myGLCD.setFont(SevenSegNumFont);
  
  //End of the basic layout
}

void loop()
{
  String x= " ";

  char str[20];
  char s[2] = ",";
  char *token;
  int aux[4];

while (!Serial.available()); //Get data from the PC
  x = Serial.readString();
  x.toCharArray(str,20);
  token = strtok(str, s); //Conversions to split the string and get the data of each field.
  int i=0;
  while( token != NULL ) {
  aux[i]=atoi(token);
  i++;
  token = strtok(NULL, s); 
 }
 
 
  String cputemp, cpuload, gputemp, gpuload;
  String bcputemp, bcpuload, bgputemp, bgpuload;
  
  bcputemp=cputemp;
  cputemp=String(aux[0]);
  if( bcputemp != cputemp){ //If the new read is different do a new print
    if(aux[0]<10){ //Due to limitations of my screen I can't afford to clear the string so I print a "0" if the temp is lower than 10
      cputemp="0"+cputemp;
    }
    if(aux[0]==100){ //Same but for 3 digits
      cputemp="99";
    }
    myGLCD.print(cputemp, 200, 30-10);
  }
  
  bcpuload=cpuload;
  cpuload=String(aux[1]);
  if( bcpuload != cpuload){
    if(aux[1]<10){
      cpuload="0"+cpuload;
    }
    if(aux[1]==100){
      cpuload="99";
    }
    myGLCD.print(cpuload, 200, 30+135*1-10);
  }

  bgputemp=gputemp;
  gputemp=String(aux[2]);
  if( bgputemp != gputemp){
    if(aux[2]<10){
      gputemp="0"+gputemp;
    }
    if(aux[2]==100){
      gputemp="99";
    }
    myGLCD.print(gputemp, 200, 30+135*2-10);
  }
  
  bgpuload=gpuload;
  gpuload=String(aux[3]);
  if( bgpuload != gpuload){
    if(aux[3]<10){
      gpuload="0"+gpuload;
    }
    if(aux[3]==100){
      gpuload="99";
    }
    myGLCD.print(gpuload, 200, 30+135*3-10);
  }

  
  delay (1000);
}

Python Script

Python
More info in my github: https://github.com/bykape/Stat_Screen_Monitor
#main Python script

import GPUtil
import psutil
import wmi
import math
import serial
import time


arduino = serial.Serial(port='COM8', baudrate=115200, timeout=.1) #open serial port

def write_read(x): #function to send data to the arduino board
    arduino.write(bytes(x,'utf-8'))


while 1:
      w = wmi.WMI(namespace="root\OpenHardwareMonitor") #open Hardware Monitor
      temperature_infos = w.Sensor()
      value=0

      for sensor in temperature_infos: #Search for the CPU temperature. Due to lack of OpenHardwareMonitor limitation with 11th Intel Family I only get the core 0 temp.
        #It is posible to do the mean of all your processor temps. 
          if sensor.SensorType==u'Temperature' and sensor.Parent==u'/lpc/nct6798d' and sensor.Name==u'Temperature #1':
              value=sensor.Value
    
      gpu = GPUtil.getGPUs()[0]
      
      A = str(round(value,2))
      B = str(round(psutil.cpu_percent(),2))
      C = str(round(gpu.temperature,2))
      D = str(round(gpu.load*100,2))
      finalString = A + "," + B + "," + C + "," + D + "\n" #Create the String to send to arduino
      write_read(finalString) #calling the send function

Credits

ggarciacompanys
0 projects • 1 follower
Contact

Comments

Please log in or sign up to comment.