heposheikki
Published © GPL3+

Linux pc/server performance monitor

A handy little project that allows you to see your system stats on an LCD display and execute commands by pressing a button on the arduino.

IntermediateFull instructions provided1,776
Linux pc/server performance monitor

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
Any Arduino board with 10 or more I/O pins will do.
×1
RGB Backlight LCD - 16x2
Adafruit RGB Backlight LCD - 16x2
×1
Rotary potentiometer (generic)
Rotary potentiometer (generic)
Any generic potentiometer will do.
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Breadboard (generic)
Breadboard (generic)
×1
Button (generic)
×2
Resistor 10k ohm
Resistor 10k ohm
×2

Software apps and online services

Linux Any Distro

Story

Read more

Schematics

Schematics

Code

The Python script for the host machine.

Python
This script reads the system stats, sends them to the arduino and handles executing commands when a button is pressed.
import psutil
import serial
import time
import os
ser = serial.Serial('/dev/ttyUSB0') #define serial port.
line = "nothing"; #create the variable where incoming communication is gonna be saved.
while True:
    sep = ',' 
    sep1 = '.'
    t = os.popen('uptime -p').read()[:-1] #open the file /proc/uptime and read it
    cpu = psutil.cpu_percent(interval=1) #reads the cpu åercetange every second
    if cpu <= 10: #if cpu percetange is less than 10, add a zero, eg. 5% -> 05%. This lessens the propability of a bug happening where ram has two "%" icons
        cpu = '0',str(cpu)
        cpu = str(cpu)
    cpu = str(cpu).replace('"', "")# Remove unecessary characters from string
    cpu = str(cpu).replace("'", "")# --||--
    cpu = str(cpu).replace(' ', "")# --||--
    cpu = 'CPU', str(cpu).split(sep1, 1)[0] #split the string at the first dot and remove anything behind it
    amount_of_ram = str(psutil.virtual_memory()[2]).split(sep1, 1)[0] # --||--
    ram = 'RAM', str(amount_of_ram) # Convert the variable to a string. (Why??)
    uptime = t #more spaghetti for the sake of it
    updisk = (str(uptime).split(sep, 1)[0],'/',psutil.disk_usage('/') [3], '%') #generate a string that contains the uptime and how much of the / drive is used. 
    downdisk = str(cpu) + '% ' + str(ram) + '%' #generate a string that contains the info regarding the RAM and the CPU. I know my naming scheme is stupid.
    downdisk = str(downdisk) #convert the variable to a string??
    upstr = str(updisk) #--||--
    upstr = upstr.replace("(", "") #replace all unecessary characters from the strings
    upstr = upstr.replace(",", "")
    upstr = upstr.replace("'", "")
    upstr = upstr.replace(")", "")
    upstr = upstr.replace("minutes", "m")
    upstr = upstr.replace("minute", "m")
    upstr = upstr.replace("hours", "h")
    upstr = upstr.replace("hour", "h")
    upstr = upstr.replace("days", "d")
    upstr = upstr.replace("day", "d")
    downdisk = downdisk.replace("(", "")
    downdisk = downdisk.replace(",", "")
    downdisk = downdisk.replace("'", "")
    downdisk = downdisk.replace(")", "")
    ser.write(b"*") # Let the arduino know we're about to write onto the second row
    ser.write(upstr.encode()) # Write the data 
    ser.write(b"#") # Let the arduino know we're about to write onto the first row
    ser.write(downdisk.encode())
    line = ser.readline() # Read the serial output
    if (not line == b'nothing\r\n'): # if false any buttons were'nt pressed, do nothing.
        if (line == b'reboot\r\n'): # A reboot was ordered by pressing a button
            os.system("reboot") #reboot the host system, replace the command if you wish to do something else when a button is pressed.
        if (line == b'shutdown\r\n'): # A shutdown was ordered
            os.system("shutdown now")    
    time.sleep(2) # Wait two seconds


# I'm sorry for the spaghetti and the excessive use of the commenting feature :)

Arduino code

C/C++
This is the code that runs on the Arduino
#include <Wire.h> 
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
String inData;
const int buttonPin = 6;
const int buttonPin2 = 7;
const int ledPin = 8;
int buttonState = 0; 
int buttonState2 = 0;

void setup() {
    pinMode(buttonPin2, INPUT);
    pinMode(buttonPin, INPUT);
    pinMode(ledPin, OUTPUT);
    Serial.begin(9600);
    lcd.begin(16, 2);
}

void loop() {
  ledPin == HIGH;
  buttonState = digitalRead(buttonPin);
  buttonState2 = digitalRead(buttonPin2);
  if (buttonState == HIGH) {
      Serial.println("reboot");
      delay(250);
    
  }
  if (buttonState2 == HIGH) {
      Serial.println("shutdown");
      delay(250);
    
  }

    while (Serial.available() > 0)
    {
        char recieved = Serial.read();
        inData += recieved; 
        
        if (recieved == '*')
        {
            inData.remove(inData.length() -1, 1);
            lcd.setCursor(0,0);
            lcd.print(inData);
            inData = ""; 
            
            if(inData == "DIS")
            {
              delay(0); 
            }
  Serial.println("nothing");
            
        } 
        
        if (recieved == '#')
        {
            inData.remove(inData.length() -1, 1);
            lcd.setCursor(0,1);
            lcd.print(inData);
            inData = ""; 
        }
    }
}

Credits

heposheikki

heposheikki

0 projects • 0 followers

Comments