Last active
August 29, 2015 14:12
-
-
Save famanson/f7de3d03623c1c9d3ce9 to your computer and use it in GitHub Desktop.
This file contains 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
// include the library code: | |
#include <Wire.h> | |
#include <Adafruit_MCP23017.h> | |
#include <Adafruit_RGBLCDShield.h> | |
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield(); | |
// These #defines make it easy to set the backlight color | |
#define RED 0x1 | |
#define YELLOW 0x3 | |
#define GREEN 0x2 | |
#define TEAL 0x6 | |
#define BLUE 0x4 | |
#define VIOLET 0x5 | |
#define WHITE 0x7 | |
void setup() { | |
Serial.begin(9600); | |
lcd.begin(16, 2); | |
lcd.setBacklight(TEAL); | |
lcd.clear(); | |
} | |
uint8_t i=0; | |
void loop() { | |
// Backlight control | |
uint8_t buttons = lcd.readButtons(); | |
if (buttons) { | |
if (buttons & BUTTON_UP) { | |
lcd.setBacklight(RED); | |
} | |
if (buttons & BUTTON_DOWN) { | |
lcd.setBacklight(YELLOW); | |
} | |
if (buttons & BUTTON_LEFT) { | |
lcd.setBacklight(GREEN); | |
} | |
if (buttons & BUTTON_RIGHT) { | |
lcd.setBacklight(BLUE); | |
} | |
if (buttons & BUTTON_SELECT) { | |
lcd.setBacklight(VIOLET); | |
} | |
} | |
// Show stuff: | |
if(Serial.available() > 0) { | |
String input = Serial.readStringUntil('\n'); | |
int firstCommaIndex = input.indexOf(','); | |
int secondCommaIndex = input.indexOf(',', firstCommaIndex+1); | |
int thirdCommaIndex = input.indexOf(',', secondCommaIndex+1); | |
String cpu = input.substring(0, firstCommaIndex); | |
String ram = input.substring(firstCommaIndex+1, secondCommaIndex); | |
String down = input.substring(secondCommaIndex+1, thirdCommaIndex); | |
String temp = input.substring(thirdCommaIndex+1, thirdCommaIndex+5); | |
Serial.flush() | |
/* | |
Serial.println(cpu); | |
Serial.println(ram); | |
Serial.println(down); | |
Serial.println(up); | |
Serial.println(); | |
*/ | |
lcd.clear(); | |
// CPU: | |
lcd.setCursor(0,0); | |
lcd.print(cpu); | |
lcd.print("%"); | |
// RAM | |
lcd.setCursor(9,0); | |
lcd.print(ram); | |
lcd.print("MB"); | |
// Download | |
lcd.setCursor(0,1); | |
lcd.print(down); | |
lcd.print("k"); | |
// Temp | |
lcd.setCursor(9,1); | |
lcd.print(temp); | |
lcd.print("C"); | |
} | |
} |
This file contains 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 psutil | |
import serial | |
import time | |
import datetime | |
import sensors | |
from serial import SerialException | |
init = False | |
ser = None | |
def write(): | |
global init, ser | |
try: | |
if not init: | |
# connect to the Arduino if necessary | |
try: | |
ser = serial.Serial('/dev/ttyACM0', 9600, timeout=1) | |
except SerialException: | |
ser = serial.Serial('/dev/ttyACM1', 9600, timeout=1) | |
init = True | |
# write data to the Arduino | |
_data = [str(DATA['cpu']), str(DATA['ram']), str(DATA['down']), str(DATA['temp'])] | |
# print _data | |
if ser: | |
ser.write(",".join(_data)) | |
except SerialException: | |
if ser: | |
ser.close() | |
init = False # reinitialise in the next loop | |
pass | |
def get_temp(): | |
sensors.init() | |
try: | |
for chip in sensors.iter_detected_chips(): | |
for feature in chip: | |
if feature.label.startswith("Physical"): | |
return feature.get_value() | |
finally: | |
sensors.cleanup() | |
DATA = None | |
poll_time = 0 # time of last poll | |
last_recv = 0 | |
last_sent = 0 | |
def poll_resources(): | |
global DATA, poll_time, last_recv, last_sent | |
now = datetime.datetime.now() | |
cpu_percent = psutil.cpu_percent() | |
ram_avail = psutil.phymem_usage().available/(1024*1024) | |
net_io_counters = psutil.network_io_counters() | |
# calculate time | |
new_poll_time = (now.day * 24 * 60 * 60 + now.second) * 1000 + now.microsecond / 1000.0 | |
diff_time = (new_poll_time - poll_time)/1000.0 # seconds diff | |
poll_time = new_poll_time | |
# calculate download rate | |
down = (net_io_counters.bytes_recv-last_recv)/(1024.0*diff_time) | |
down = 0.0 if down <= 0.0 else round(down,1) | |
# Temperature | |
temp = get_temp() | |
# remember state | |
last_recv = net_io_counters.bytes_recv | |
last_sent = net_io_counters.bytes_sent | |
DATA = dict(cpu=cpu_percent, ram=ram_avail, down=down, temp=temp, poll_time=poll_time) | |
if __name__ == '__main__': | |
# init data | |
while True: | |
# gather data (resource usage) | |
poll_resources() | |
write() | |
time.sleep(2) |
This file contains 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 psutil | |
import serial | |
import time | |
import datetime | |
from serial import SerialException | |
init = False | |
ser = None | |
def write(): | |
global init, ser | |
try: | |
if not init: | |
# connect to the Arduino if necessary | |
ser = serial.Serial('COM4', 9600, timeout=1) | |
init = True | |
# write data to the Arduino | |
_data = [str(DATA['cpu']), str(DATA['ram']), str(DATA['down']), str(DATA['temp'])] | |
# print _data | |
if ser: | |
ser.write(",".join(_data).encode()) | |
except SerialException: | |
if ser: | |
ser.close() | |
init = False # reinitialise in the next loop | |
pass | |
def get_temp(): | |
return "--" | |
DATA = None | |
poll_time = 0 # time of last poll | |
last_recv = 0 | |
last_sent = 0 | |
def poll_resources(): | |
global DATA, poll_time, last_recv, last_sent | |
now = datetime.datetime.now() | |
cpu_percent = psutil.cpu_percent() | |
ram_avail = psutil.phymem_usage().available/(1024*1024) | |
net_io_counters = psutil.network_io_counters() | |
# calculate time | |
new_poll_time = (now.day * 24 * 60 * 60 + now.second) * 1000 + now.microsecond / 1000.0 | |
diff_time = (new_poll_time - poll_time)/1000.0 # seconds diff | |
poll_time = new_poll_time | |
# calculate download rate | |
down = (net_io_counters.bytes_recv-last_recv)/(1024.0*diff_time) | |
down = 0.0 if down <= 0.0 else round(down,1) | |
# Temperature | |
temp = get_temp() | |
# remember state | |
last_recv = net_io_counters.bytes_recv | |
last_sent = net_io_counters.bytes_sent | |
DATA = dict(cpu=cpu_percent, ram=ram_avail, down=down, temp=temp, poll_time=poll_time) | |
if __name__ == '__main__': | |
# init data | |
while True: | |
# gather data (resource usage) | |
poll_resources() | |
write() | |
time.sleep(2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment