Skip to content

Instantly share code, notes, and snippets.

@pingud98
Created May 19, 2019 09:58
Show Gist options
  • Save pingud98/817b7f54a541fa8c7d1f8c8707d99934 to your computer and use it in GitHub Desktop.
Save pingud98/817b7f54a541fa8c7d1f8c8707d99934 to your computer and use it in GitHub Desktop.
Python script to display status info on a cheap I2C lcd screen with my NanoPi Neo2
import socket
import fcntl
import struct
import psutil
import os
import sys
import time
home_dir = os.path.expanduser("~")
sys.path.append(home_dir)
import I2C_LCD_Driver
import SDL_DS1307
mylcd = I2C_LCD_Driver.lcd()
ds1307 = SDL_DS1307.SDL_DS1307(0, 0x68)
def get_ip_address(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915,
struct.pack('256s', ifname[:15])
)[20:24])
#mylcd.lcd_display_string("hello",1)
#mylcd.lcd_display_string("IP Address:", 1)
#mylcd.lcd_display_string(get_ip_address('eth0'), 2)
# Return RAM information (unit=kb) in a list
# Index 0: total RAM
# Index 1: used RAM
# Index 2: free RAM
def getRAMinfo():
p = os.popen('free')
i = 0
while 1:
i = i + 1
line = p.readline()
if i==2:
return(line.split()[1:4])
mylcd.lcd_clear()
while True:
#CPU usage and time
CPU_usage = psutil.cpu_percent(interval = .5)
mylcd.lcd_display_string("CPU Load %s%% " % (CPU_usage), 1)
mylcd.lcd_display_string("%s" % ds1307.read_datetime(), 2)
time.sleep(3)
mylcd.lcd_clear()
#IP ADDRESS
mylcd.lcd_display_string("IP Address : ", 1)
mylcd.lcd_display_string(get_ip_address('eth0'), 2)
time.sleep(3)
mylcd.lcd_clear()
#RAM USAGE
RAM_stats = getRAMinfo()
RAM_used = round(int(RAM_stats[1]) / 1000,1)
RAM_free = round(int(RAM_stats[2]) / 1000,1)
mylcd.lcd_display_string("RAM Used %sMB" % (RAM_used),1)
mylcd.lcd_display_string("RAM Free %sMB" % (RAM_free),2)
time.sleep(3)
mylcd.lcd_clear()
mylcd.lcd_display_string("All your base",1)
mylcd.lcd_display_string("are belong to us",2)
time.sleep(3)
mylcd.lcd_clear()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment