aboutsummaryrefslogtreecommitdiff
path: root/content/posts/2021-06-26-simple-world-clock.md
diff options
context:
space:
mode:
authorMitja Felicijan <mitja.felicijan@gmail.com>2024-03-10 14:59:14 +0100
committerMitja Felicijan <mitja.felicijan@gmail.com>2024-03-10 14:59:14 +0100
commit1100562e29f6476448b656dbddd4cf22505523f6 (patch)
tree442eec492199104bd49dfd74474ce89ade8fcac9 /content/posts/2021-06-26-simple-world-clock.md
parenta40d80be378e46a6c490e1b99b0d8f4acd968503 (diff)
downloadmitjafelicijan.com-1100562e29f6476448b656dbddd4cf22505523f6.tar.gz
Move back to JBMAFP
Diffstat (limited to 'content/posts/2021-06-26-simple-world-clock.md')
-rw-r--r--content/posts/2021-06-26-simple-world-clock.md107
1 files changed, 107 insertions, 0 deletions
diff --git a/content/posts/2021-06-26-simple-world-clock.md b/content/posts/2021-06-26-simple-world-clock.md
new file mode 100644
index 0000000..e72744c
--- /dev/null
+++ b/content/posts/2021-06-26-simple-world-clock.md
@@ -0,0 +1,107 @@
1---
2title: Simple world clock with eInk display and Raspberry Pi Zero
3url: /simple-world-clock-with-eiink-display-and-raspberry-pi-zero.html
4date: 2021-06-26T12:00:00+02:00
5type: post
6draft: false
7---
8
9Our team is spread across the world, from the USA all the way to Australia, so
10having some sort of world clock makes sense.
11
12Currently, I am using an extension for Gnome called [Timezone
13extension](https://extensions.gnome.org/extension/2657/timezones-extension/),
14and it serves the purpose quite well.
15
16But I also have a bunch of electronics that I bought through the time, and I am
17not using any of them, and it's time to stop hording this stuff and use it in a
18project.
19
20A while ago I bought a small eInk display [Inky
21pHAT](https://shop.pimoroni.com/products/inky-phat?variant=12549254217811) and I
22have a bunch of [Raspberry Pi's
23Zero](https://www.raspberrypi.org/products/raspberry-pi-zero/) lying around that
24I really need to use.
25
26![Inky pHAT, Raspberry Pi Zero](/assets/posts/world-clock/hardware.jpg)
27
28Since the Inky [Inky
29pHAT](https://shop.pimoroni.com/products/inky-phat?variant=12549254217811) is
30essentially a HAT, it can easily be added on top of the [Raspberry Pi
31Zero](https://www.raspberrypi.org/products/raspberry-pi-zero/).
32
33First, I installed the necessary software on Raspberry Pi with `pip3 install
34inky`.
35
36And then I created a file `clock.py` in home directory `/home/pi`.
37
38```python
39#!/usr/bin/env python
40# -*- coding: utf-8 -*-
41
42import sys
43import os
44from inky.auto import auto
45from PIL import Image, ImageFont, ImageDraw
46from font_fredoka_one import FredokaOne
47
48clocks = [
49 'America/New_York',
50 'Europe/Ljubljana',
51 'Australia/Brisbane',
52]
53
54board = auto()
55board.set_border(board.WHITE)
56board.rotation = 90
57
58img = Image.new('P', (board.WIDTH, board.HEIGHT))
59draw = ImageDraw.Draw(img)
60
61big_font = ImageFont.truetype(FredokaOne, 18)
62small_font = ImageFont.truetype(FredokaOne, 13)
63
64x = board.WIDTH / 3
65y = board.HEIGHT / 3
66
67idx = 1
68for clock in clocks:
69 ctime = os.popen('TZ="{}" date +"%a,%H:%M"'.format(clock))
70 ctime = ctime.read().strip().split(',')
71 city = clock.split('/')[1].replace('_', ' ')
72
73 draw.text((15, (idx*y)-y+10), city, fill=board.BLACK, font=small_font)
74 draw.text((110, (idx*y)-y+7), str(ctime[0]), fill=board.BLACK, font=big_font)
75 draw.text((155, (idx*y)-y+7), str(ctime[1]), fill=board.BLACK, font=big_font)
76
77 idx += 1
78
79board.set_image(img)
80board.show()
81```
82
83And because eInk displays are rather slow to refresh and the clock requires
84refreshing only once a minute, this can be done through cronjob.
85
86Before we add this job to cron we need to make `clock.py` executable with `chmod
87+x clock.py`.
88
89Then we add a cronjob with `crontab -e`.
90
91```txt
92* * * * * /home/pi/clock.py
93```
94
95So, we end up with a result like this.
96
97![World Clock](/assets/posts/world-clock/world-clock.jpg)
98
99And for the enclosure that can be 3D printed, but I haven't yet something like
100this can be used.
101
102<iframe id="vs_iframe" src="https://www.viewstl.com/?embedded&url=https%3A%2F%2Fmitjafelicijan.com%2Fposts%2Fworld-clock%2Fenclosure.stl&color=gray&bgcolor=white&edges=no&orientation=front&noborder=no" style="border:0;margin:0;width:100%;height:400px;"></iframe>
103
104You can download my [STL file for the enclosure
105here](/assets/posts/world-clock/enclosure.stl), but make sure that dimensions make
106sense and also opening for USB port should be added or just use a drill and some
107hot glue to make it stick in the enclosure.