来自:https://www.hackster.io/heposheikki/lin ... tor-1e197e
一个方便的小项目,可以在 LCD 显示屏上查看系统统计信息并通过按键执行命令。
设计这个状态监视器是因为我想在没有任何单独的监控软件的情况下实时查看我的家庭服务器的状态。我在网上搜索了几分钟,找到了一些类似的项目,但他们都遗漏了一些东西,那个东西是我真正理解的脚本,并通过按下按钮来执行命令。放学后我花了几个晚上才把这些放在一起。它的工作方式是,在主机上运行一个单独的 Python 脚本,它使用名为PySerial的 Python 库将系统信息从 /proc/uptime 和 psutil (由 Giampaolo Rodola)通过 USB 提供给 arduino(由 Chris Liechti 撰写)。然后 arduino 将处理后的信息打印到 16x2 LCD 上。arduino 还有两个(或者更多)按钮,用于在主机系统上执行命令。这是我发布的第一个项目,我希望有人觉得它有用:)
python 代码
代码: 全选
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 :)
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 = "";
}
}
}