-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathminifi.py
135 lines (107 loc) · 3.55 KB
/
minifi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python
import time
import colorsys
import os
import json
import sys, socket
import subprocess
import time
import datetime
from time import sleep
from time import gmtime, strftime
import psutil
import rainbowhat
#### Constants
QNH=1020
RAINBOW_BRIGHTNESS = 255
HUE_COLD = 225
HUE_WARM = 0
T_COLD = 15
T_WARM = 30
running = 1
#### Initialization
# yyyy-mm-dd hh:mm:ss
currenttime= strftime("%Y-%m-%d %H:%M:%S",gmtime())
external_IP_and_port = ('198.41.0.4', 53) # a.root-servers.net
socket_family = socket.AF_INET
host = os.uname()[1]
#### Methods
def getAltitude(qnh, pressure):
"""Return the current approximate altitude.
:param qnh: Your local value for atmospheric pressure adjusted to sea level.
"""
return 44330.0 * (1.0 - pow(pressure / (qnh*100), (1.0/5.255))) # Calculate altitute from pressure & qnh
def display_message(message):
rainbowhat.display.print_str(message)
rainbowhat.display.show()
def display_float(fval):
rainbowhat.display.print_float(fval)
rainbowhat.display.show()
@rainbowhat.touch.A.press()
def touch_a(channel):
global ftemp
display_float(ftemp)
@rainbowhat.touch.B.press()
def touch_b(channel):
global temperature
display_float(temperature)
@rainbowhat.touch.C.press()
def touch_c(channel):
global altitude
display_float(altitude)
def getCPUtemperature():
res = os.popen('vcgencmd measure_temp').readline()
return(res.replace("temp=","").replace("'C\n",""))
def set_rainbow(temp):
temp = max(temp,T_COLD)
temp = min(temp,T_WARM)
temp -= T_COLD
temp /= float(T_WARM - T_COLD)
hue = (1.0-temp) * abs(HUE_WARM - HUE_COLD) / 360.0
r, g, b = [int(c * 255) for c in colorsys.hsv_to_rgb(hue, 1.0, 1.0)]
rainbowhat.rainbow.set_all(r, g, b, brightness=0.1)
rainbowhat.rainbow.show()
def IP_address():
try:
s = socket.socket(socket_family, socket.SOCK_DGRAM)
s.connect(external_IP_and_port)
answer = s.getsockname()
s.close()
return answer[0] if answer else None
except socket.error:
return None
def output_row():
global temperature
global pressure
global altitude
global cpuTemp
global calctemp
global ftemp
temperature = rainbowhat.weather.temperature()
pressure = rainbowhat.weather.pressure()
altitude = getAltitude(QNH, pressure)
cpuTemp=int(float(getCPUtemperature()))
calctemp = temperature - ((cpuTemp - temperature)/ 1.5)
ftemp = 9.0/5.0 * calctemp + 32
ipaddress = IP_address()
uniqueid = 'rainbow_uuid_{0}'.format(strftime("%Y%m%d%H%M%S",gmtime()))
usage = psutil.disk_usage("/")
mem = psutil.virtual_memory()
diskrootfree = "{:.1f} MB".format(float(usage.free) / 1024 / 1024)
mempercent = mem.percent
row = { 'uniqueid': uniqueid, 'memory': mempercent,
'diskfree': diskrootfree,'ts': currenttime, 'host': host, 'cputemp': round(cpuTemp,2), 'ipaddress': ipaddress, 'temp': round(temperature,2), 'tempf': round(ftemp,2), 'pressure': round(pressure,2), 'altitude': round(altitude,2), 'tempf2': round(calctemp,2) }
json_string = json.dumps(row)
print(json_string)
# print 'Temperature: {0:0.1f} C'.format(temperature)
# print 'CPU: {0:0.1f} C'.format(cpuTemp)
# print 'Corrected Temp: {0:0.1f} C'.format(calctemp)
# print 'Room Temp: {0:0.1f} F'.format(ftemp)
# print 'Pressure: {0:0.1f} Pa'.format(pressure)
# print 'Altitude: {0:0.1f}'.format(altitude)
output_row()
sys.exit(0)
#display_float(ftemp)
#sleep(5)
#rainbowhat.display.clear()
#rainbowhat.display.show()