-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclock.py
105 lines (77 loc) · 3.61 KB
/
clock.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
from waveshare_epd import epd2in13_V3
import time
import datetime
import os
import PIL
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
import psutil
import requests
RES_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'resources/') # directory with fonts, pictures, etc...
DATEFORMAT = "%d/%m/%y"
TIMEFORMAT = "%H:%M"
FONT = os.path.join(RES_DIR, 'FreeMono.ttf')
FONTBOLD = os.path.join(RES_DIR, 'FreeMonoBold.ttf') # fonts
HA_IP = "your homeassistant ip:8123"
HA_TOKEN = "your long live homeassistant token"
timefont_size = 35
timefont = ImageFont.truetype(FONTBOLD, timefont_size) # font for display time
datefont_size = 15
datefont = ImageFont.truetype(FONTBOLD, datefont_size) # font for display date
resfont_size = 13
resfont = ImageFont.truetype(FONT, resfont_size) # font for display system resources
NumToClear = 0
url = f"http://{HA_IP}/api/states/sensor.datchik_za_oknom_temperature"
headers = {
"Authorization": f"Bearer {HA_TOKEN}",
"content-type": "application/json",
}
try:
epd = epd2in13_V3.EPD()
epd.init() # initial display
epd.Clear(0xFF) # clear display
while True:
epd.init() # initial display again after sleeping
if (NumToClear == 30): # numbers of display partial updates to do full update
epd.Clear(0xFF)
NumToClear = 0
response = requests.get(url, headers=headers)
if response.status_code == 200:
data = response.json()
out_temp_value = data['state']
#print("State value:", state_value)
else:
print("Failed to fetch data:", response.status_code)
dt_now = datetime.datetime.now()
seconds_until_next_minute = 60 - dt_now.time().second # calculate secons until next minute
time_rn = dt_now.strftime(TIMEFORMAT)
date_rn = dt_now.strftime(DATEFORMAT) # convert date and time to readable format
cpu_ = psutil.cpu_percent()
mem_ = psutil.virtual_memory()
cpu = "CPU:" + str(cpu_) + "%"
mem = "RAM:" + str(mem_.percent)+ "%"
out_temp_value_str = "Ул. температура: "+out_temp_value+" °C" # create string to send to display
image = Image.new('1', (epd.height, epd.width), 255) # 255: clear the frame
draw = ImageDraw.Draw(image)
draw.line([(0,20),(250,20)], fill = 0,width = 3) # draw upper line
draw.line([(0,105),(250,105)], fill = 0,width = 3) # draw lower line
draw.text((60, 25), time_rn, font = timefont, fill = 0) # display time
draw.text((75, 65), date_rn, font = datefont, fill = 0) # display date
draw.text((10, 80), out_temp_value_str, font = resfont, fill = 0) # display outdoor temp
draw.text((55, 3), "192.168.2.167", font = resfont, fill = 0) # display text
draw.text((5, 111), cpu, font = resfont, fill = 0) # display cpu usage
draw.text((170, 111), mem, font = resfont, fill = 0) # display ram usage
epd.displayPartial(epd.getbuffer(image))
epd.Clear(0xFF)
epd.sleep() # sleep, eco mode
NumToClear = NumToClear + 1
print(f"NumToClear:", NumToClear)
time.sleep(seconds_until_next_minute) # wait for the next minute
except IOError as e:
print("Oh, this fucking error happen again!")
print(e)
except KeyboardInterrupt:
print("ctrl + c:")
epd2in13_V3.epdconfig.module_exit()
exit()