Components and supplies
Resistor 10k ohm
Rotary potentiometer (generic)
RGB Backlight LCD - 16x2
Breadboard (generic)
Button (generic)
Jumper wires (generic)
Arduino UNO
Apps and platforms
Any Distro
Project description
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.
1import psutil 2import serial 3import time 4import os 5ser = serial.Serial('/dev/ttyUSB0') 6 #define serial port. 7line = "nothing"; #create the variable where incoming 8 communication is gonna be saved. 9while True: 10 sep = ',' 11 sep1 = 12 '.' 13 t = os.popen('uptime -p').read()[:-1] #open the file /proc/uptime and 14 read it 15 cpu = psutil.cpu_percent(interval=1) #reads the cpu åercetange every 16 second 17 if cpu <= 10: #if cpu percetange is less than 10, add a zero, eg. 18 5% -> 05%. This lessens the propability of a bug happening where ram has two "%" 19 icons 20 cpu = '0',str(cpu) 21 cpu = str(cpu) 22 cpu = str(cpu).replace('"', 23 "")# Remove unecessary characters from string 24 cpu = str(cpu).replace("'", 25 "")# --||-- 26 cpu = str(cpu).replace(' ', "")# --||-- 27 cpu = 'CPU', 28 str(cpu).split(sep1, 1)[0] #split the string at the first dot and remove anything 29 behind it 30 amount_of_ram = str(psutil.virtual_memory()[2]).split(sep1, 1)[0] 31 # --||-- 32 ram = 'RAM', str(amount_of_ram) # Convert the variable to a string. 33 (Why??) 34 uptime = t #more spaghetti for the sake of it 35 updisk = (str(uptime).split(sep, 36 1)[0],'/',psutil.disk_usage('/') [3], '%') #generate a string that contains the 37 uptime and how much of the / drive is used. 38 downdisk = str(cpu) + '% ' + 39 str(ram) + '%' #generate a string that contains the info regarding the RAM and the 40 CPU. I know my naming scheme is stupid. 41 downdisk = str(downdisk) #convert 42 the variable to a string?? 43 upstr = str(updisk) #--||-- 44 upstr = upstr.replace("(", 45 "") #replace all unecessary characters from the strings 46 upstr = upstr.replace(",", 47 "") 48 upstr = upstr.replace("'", "") 49 upstr = upstr.replace(")", 50 "") 51 upstr = upstr.replace("minutes", "m") 52 upstr = upstr.replace("minute", 53 "m") 54 upstr = upstr.replace("hours", "h") 55 upstr = upstr.replace("hour", 56 "h") 57 upstr = upstr.replace("days", "d") 58 upstr = upstr.replace("day", 59 "d") 60 downdisk = downdisk.replace("(", "") 61 downdisk = downdisk.replace(",", 62 "") 63 downdisk = downdisk.replace("'", "") 64 downdisk = downdisk.replace(")", 65 "") 66 ser.write(b"*") # Let the arduino know we're about to write onto 67 the second row 68 ser.write(upstr.encode()) # Write the data 69 ser.write(b"#") 70 # Let the arduino know we're about to write onto the first row 71 ser.write(downdisk.encode()) 72 73 line = ser.readline() # Read the serial output 74 if (not line == b'nothing\ \ 75'): 76 # if false any buttons were'nt pressed, do nothing. 77 if (line == b'reboot\ \ 78'): 79 # A reboot was ordered by pressing a button 80 os.system("reboot") 81 #reboot the host system, replace the command if you wish to do something else when 82 a button is pressed. 83 if (line == b'shutdown\ \ 84'): # A shutdown was 85 ordered 86 os.system("shutdown now") 87 time.sleep(2) # Wait 88 two seconds 89 90 91# I'm sorry for the spaghetti and the excessive use of the 92 commenting feature :)
Arduino code
c_cpp
This is the code that runs on the Arduino
1#include <Wire.h> 2#include <LiquidCrystal.h> 3 4LiquidCrystal 5 lcd(12, 11, 5, 4, 3, 2); 6String inData; 7const int buttonPin = 6; 8const 9 int buttonPin2 = 7; 10const int ledPin = 8; 11int buttonState = 0; 12int buttonState2 13 = 0; 14 15void setup() { 16 pinMode(buttonPin2, INPUT); 17 pinMode(buttonPin, 18 INPUT); 19 pinMode(ledPin, OUTPUT); 20 Serial.begin(9600); 21 lcd.begin(16, 22 2); 23} 24 25void loop() { 26 ledPin == HIGH; 27 buttonState = digitalRead(buttonPin); 28 29 buttonState2 = digitalRead(buttonPin2); 30 if (buttonState == HIGH) { 31 Serial.println("reboot"); 32 33 delay(250); 34 35 } 36 if (buttonState2 == HIGH) { 37 Serial.println("shutdown"); 38 39 delay(250); 40 41 } 42 43 while (Serial.available() > 0) 44 { 45 46 char recieved = Serial.read(); 47 inData += recieved; 48 49 50 if (recieved == '*') 51 { 52 inData.remove(inData.length() 53 -1, 1); 54 lcd.setCursor(0,0); 55 lcd.print(inData); 56 57 inData = ""; 58 59 if(inData == "DIS") 60 61 { 62 delay(0); 63 } 64 Serial.println("nothing"); 65 66 67 } 68 69 if (recieved == '#') 70 { 71 72 inData.remove(inData.length() -1, 1); 73 lcd.setCursor(0,1); 74 75 lcd.print(inData); 76 inData = ""; 77 } 78 } 79}
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.
1import psutil 2import serial 3import time 4import os 5ser = serial.Serial('/dev/ttyUSB0') #define serial port. 6line = "nothing"; #create the variable where incoming communication is gonna be saved. 7while True: 8 sep = ',' 9 sep1 = '.' 10 t = os.popen('uptime -p').read()[:-1] #open the file /proc/uptime and read it 11 cpu = psutil.cpu_percent(interval=1) #reads the cpu åercetange every second 12 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 13 cpu = '0',str(cpu) 14 cpu = str(cpu) 15 cpu = str(cpu).replace('"', "")# Remove unecessary characters from string 16 cpu = str(cpu).replace("'", "")# --||-- 17 cpu = str(cpu).replace(' ', "")# --||-- 18 cpu = 'CPU', str(cpu).split(sep1, 1)[0] #split the string at the first dot and remove anything behind it 19 amount_of_ram = str(psutil.virtual_memory()[2]).split(sep1, 1)[0] # --||-- 20 ram = 'RAM', str(amount_of_ram) # Convert the variable to a string. (Why??) 21 uptime = t #more spaghetti for the sake of it 22 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. 23 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. 24 downdisk = str(downdisk) #convert the variable to a string?? 25 upstr = str(updisk) #--||-- 26 upstr = upstr.replace("(", "") #replace all unecessary characters from the strings 27 upstr = upstr.replace(",", "") 28 upstr = upstr.replace("'", "") 29 upstr = upstr.replace(")", "") 30 upstr = upstr.replace("minutes", "m") 31 upstr = upstr.replace("minute", "m") 32 upstr = upstr.replace("hours", "h") 33 upstr = upstr.replace("hour", "h") 34 upstr = upstr.replace("days", "d") 35 upstr = upstr.replace("day", "d") 36 downdisk = downdisk.replace("(", "") 37 downdisk = downdisk.replace(",", "") 38 downdisk = downdisk.replace("'", "") 39 downdisk = downdisk.replace(")", "") 40 ser.write(b"*") # Let the arduino know we're about to write onto the second row 41 ser.write(upstr.encode()) # Write the data 42 ser.write(b"#") # Let the arduino know we're about to write onto the first row 43 ser.write(downdisk.encode()) 44 line = ser.readline() # Read the serial output 45 if (not line == b'nothing\ \ 46'): # if false any buttons were'nt pressed, do nothing. 47 if (line == b'reboot\ \ 48'): # A reboot was ordered by pressing a button 49 os.system("reboot") #reboot the host system, replace the command if you wish to do something else when a button is pressed. 50 if (line == b'shutdown\ \ 51'): # A shutdown was ordered 52 os.system("shutdown now") 53 time.sleep(2) # Wait two seconds 54 55 56# I'm sorry for the spaghetti and the excessive use of the commenting feature :)
Arduino code
c_cpp
This is the code that runs on the Arduino
1#include <Wire.h> 2#include <LiquidCrystal.h> 3 4LiquidCrystal lcd(12, 11, 5, 4, 3, 2); 5String inData; 6const int buttonPin = 6; 7const int buttonPin2 = 7; 8const int ledPin = 8; 9int buttonState = 0; 10int buttonState2 = 0; 11 12void setup() { 13 pinMode(buttonPin2, INPUT); 14 pinMode(buttonPin, INPUT); 15 pinMode(ledPin, OUTPUT); 16 Serial.begin(9600); 17 lcd.begin(16, 2); 18} 19 20void loop() { 21 ledPin == HIGH; 22 buttonState = digitalRead(buttonPin); 23 buttonState2 = digitalRead(buttonPin2); 24 if (buttonState == HIGH) { 25 Serial.println("reboot"); 26 delay(250); 27 28 } 29 if (buttonState2 == HIGH) { 30 Serial.println("shutdown"); 31 delay(250); 32 33 } 34 35 while (Serial.available() > 0) 36 { 37 char recieved = Serial.read(); 38 inData += recieved; 39 40 if (recieved == '*') 41 { 42 inData.remove(inData.length() -1, 1); 43 lcd.setCursor(0,0); 44 lcd.print(inData); 45 inData = ""; 46 47 if(inData == "DIS") 48 { 49 delay(0); 50 } 51 Serial.println("nothing"); 52 53 } 54 55 if (recieved == '#') 56 { 57 inData.remove(inData.length() -1, 1); 58 lcd.setCursor(0,1); 59 lcd.print(inData); 60 inData = ""; 61 } 62 } 63}
Downloadable files
Schematics
Schematics
Comments
Only logged in users can leave comments