Created
March 1, 2018 14:29
-
-
Save hasenbalg/08c1b6bebdcb7bfb0a4de2cb2cf5694e to your computer and use it in GitHub Desktop.
system monitor for a 4line lcd
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import time | |
from subprocess import * | |
import RPi.GPIO as GPIO | |
GPIO.setwarnings(False) | |
# Zuordnung der GPIO Pins (ggf. anpassen) | |
DISPLAY_RS = 7 | |
DISPLAY_E = 8 | |
DISPLAY_DATA4 = 25 | |
DISPLAY_DATA5 = 24 | |
DISPLAY_DATA6 = 23 | |
DISPLAY_DATA7 = 18 | |
DISPLAY_WIDTH = 20 # Zeichen je Zeile | |
DISPLAY_LINE_1 = 0x80 # Adresse der ersten Display Zeile | |
DISPLAY_LINE_2 = 0xC0 # Adresse der zweiten Display Zeile | |
DISPLAY_LINE_3 = 0x94 # Adresse der ersten Display Zeile | |
DISPLAY_LINE_4 = 0xD4 # Adresse der zweiten Display Zeile | |
# (bei 4x20-Displays lauten die Adressen der dritten und vierten Zeile: 0x94 und 0xD4) | |
DISPLAY_CHR = True | |
DISPLAY_CMD = False | |
E_PULSE = 0.0005 | |
E_DELAY = 0.0005 | |
def main(): | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setup(DISPLAY_E, GPIO.OUT) | |
GPIO.setup(DISPLAY_RS, GPIO.OUT) | |
GPIO.setup(DISPLAY_DATA4, GPIO.OUT) | |
GPIO.setup(DISPLAY_DATA5, GPIO.OUT) | |
GPIO.setup(DISPLAY_DATA6, GPIO.OUT) | |
GPIO.setup(DISPLAY_DATA7, GPIO.OUT) | |
display_init() | |
i = 0 | |
thrd_line = frth_line = '' | |
ip = get_ip() | |
while True: | |
lcd_byte(DISPLAY_LINE_1, DISPLAY_CMD) | |
lcd_string('disk:' + get_disk_use() + ' cpu:' + get_cpu_use()) | |
lcd_byte(DISPLAY_LINE_2, DISPLAY_CMD) | |
lcd_string('mem:' + get_mem_use()) | |
lcd_byte(DISPLAY_LINE_3, DISPLAY_CMD) | |
lcd_string(thrd_line) | |
lcd_byte(DISPLAY_LINE_4, DISPLAY_CMD) | |
lcd_string(frth_line) | |
time.sleep(5) | |
i += 1 | |
thrd_line = ip if i%2 == 0 else get_uptime() | |
frth_line = get_net_use() if i%2 == 0 else time.strftime("%d.%m.%Y %H:%M") | |
def display_init(): | |
lcd_byte(0x33,DISPLAY_CMD) | |
lcd_byte(0x32,DISPLAY_CMD) | |
lcd_byte(0x28,DISPLAY_CMD) | |
lcd_byte(0x0C,DISPLAY_CMD) | |
lcd_byte(0x06,DISPLAY_CMD) | |
lcd_byte(0x01,DISPLAY_CMD) | |
def lcd_string(message): | |
message = message.ljust(DISPLAY_WIDTH," ") | |
for i in range(DISPLAY_WIDTH): | |
lcd_byte(ord(message[i]),DISPLAY_CHR) | |
def lcd_byte(bits, mode): | |
GPIO.output(DISPLAY_RS, mode) | |
GPIO.output(DISPLAY_DATA4, False) | |
GPIO.output(DISPLAY_DATA5, False) | |
GPIO.output(DISPLAY_DATA6, False) | |
GPIO.output(DISPLAY_DATA7, False) | |
if bits&0x10==0x10: | |
GPIO.output(DISPLAY_DATA4, True) | |
if bits&0x20==0x20: | |
GPIO.output(DISPLAY_DATA5, True) | |
if bits&0x40==0x40: | |
GPIO.output(DISPLAY_DATA6, True) | |
if bits&0x80==0x80: | |
GPIO.output(DISPLAY_DATA7, True) | |
time.sleep(E_DELAY) | |
GPIO.output(DISPLAY_E, True) | |
time.sleep(E_PULSE) | |
GPIO.output(DISPLAY_E, False) | |
time.sleep(E_DELAY) | |
GPIO.output(DISPLAY_DATA4, False) | |
GPIO.output(DISPLAY_DATA5, False) | |
GPIO.output(DISPLAY_DATA6, False) | |
GPIO.output(DISPLAY_DATA7, False) | |
if bits&0x01==0x01: | |
GPIO.output(DISPLAY_DATA4, True) | |
if bits&0x02==0x02: | |
GPIO.output(DISPLAY_DATA5, True) | |
if bits&0x04==0x04: | |
GPIO.output(DISPLAY_DATA6, True) | |
if bits&0x08==0x08: | |
GPIO.output(DISPLAY_DATA7, True) | |
time.sleep(E_DELAY) | |
GPIO.output(DISPLAY_E, True) | |
time.sleep(E_PULSE) | |
GPIO.output(DISPLAY_E, False) | |
time.sleep(E_DELAY) | |
def get_disk_use(): | |
output = check_output(['df', '-h', '/var/www/html']).split()[11] | |
#print output | |
return output | |
def get_net_use(): | |
output = check_output(['cat', '/proc/net/dev']).split() | |
output = 'in:' + str("{:.2f}".format(float(output[21])*.0000000001)) + 'GB out:' + str("{:.2f}".format(float(output[29])*.0000000001)) + 'GB' | |
#print output | |
return output | |
def get_mem_use(): | |
output = check_output(['cat', '/proc/meminfo']).split() | |
output = str("{:.0f}".format(float(output[4])*.001)) + 'MB of ' + str("{:.0f}".format(float(output[1])*.001)) + 'MB' | |
#print output | |
return output | |
def get_cpu_use(): | |
output = check_output(['ps', '-eo' , 'pcpu']).split() | |
output.pop(0) | |
nr = 0 | |
for x in output: | |
nr += float(x) | |
output = str("{:.2f}".format(nr)) + '%' | |
#print output | |
return output | |
def get_ip(): | |
output = check_output(['ifconfig', 'wlan0']).split()[6] | |
output = 'ip:' + output[5:len(output)] | |
#print output | |
return output | |
def get_uptime(): | |
output = check_output(['uptime', '-p']) | |
#print output | |
return output | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment