diff options
Diffstat (limited to 'content/posts/2021-06-26-simple-world-clock.md')
| -rw-r--r-- | content/posts/2021-06-26-simple-world-clock.md | 107 |
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 | --- | ||
| 2 | title: Simple world clock with eInk display and Raspberry Pi Zero | ||
| 3 | url: /simple-world-clock-with-eiink-display-and-raspberry-pi-zero.html | ||
| 4 | date: 2021-06-26T12:00:00+02:00 | ||
| 5 | type: post | ||
| 6 | draft: false | ||
| 7 | --- | ||
| 8 | |||
| 9 | Our team is spread across the world, from the USA all the way to Australia, so | ||
| 10 | having some sort of world clock makes sense. | ||
| 11 | |||
| 12 | Currently, I am using an extension for Gnome called [Timezone | ||
| 13 | extension](https://extensions.gnome.org/extension/2657/timezones-extension/), | ||
| 14 | and it serves the purpose quite well. | ||
| 15 | |||
| 16 | But I also have a bunch of electronics that I bought through the time, and I am | ||
| 17 | not using any of them, and it's time to stop hording this stuff and use it in a | ||
| 18 | project. | ||
| 19 | |||
| 20 | A while ago I bought a small eInk display [Inky | ||
| 21 | pHAT](https://shop.pimoroni.com/products/inky-phat?variant=12549254217811) and I | ||
| 22 | have a bunch of [Raspberry Pi's | ||
| 23 | Zero](https://www.raspberrypi.org/products/raspberry-pi-zero/) lying around that | ||
| 24 | I really need to use. | ||
| 25 | |||
| 26 |  | ||
| 27 | |||
| 28 | Since the Inky [Inky | ||
| 29 | pHAT](https://shop.pimoroni.com/products/inky-phat?variant=12549254217811) is | ||
| 30 | essentially a HAT, it can easily be added on top of the [Raspberry Pi | ||
| 31 | Zero](https://www.raspberrypi.org/products/raspberry-pi-zero/). | ||
| 32 | |||
| 33 | First, I installed the necessary software on Raspberry Pi with `pip3 install | ||
| 34 | inky`. | ||
| 35 | |||
| 36 | And 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 | |||
| 42 | import sys | ||
| 43 | import os | ||
| 44 | from inky.auto import auto | ||
| 45 | from PIL import Image, ImageFont, ImageDraw | ||
| 46 | from font_fredoka_one import FredokaOne | ||
| 47 | |||
| 48 | clocks = [ | ||
| 49 | 'America/New_York', | ||
| 50 | 'Europe/Ljubljana', | ||
| 51 | 'Australia/Brisbane', | ||
| 52 | ] | ||
| 53 | |||
| 54 | board = auto() | ||
| 55 | board.set_border(board.WHITE) | ||
| 56 | board.rotation = 90 | ||
| 57 | |||
| 58 | img = Image.new('P', (board.WIDTH, board.HEIGHT)) | ||
| 59 | draw = ImageDraw.Draw(img) | ||
| 60 | |||
| 61 | big_font = ImageFont.truetype(FredokaOne, 18) | ||
| 62 | small_font = ImageFont.truetype(FredokaOne, 13) | ||
| 63 | |||
| 64 | x = board.WIDTH / 3 | ||
| 65 | y = board.HEIGHT / 3 | ||
| 66 | |||
| 67 | idx = 1 | ||
| 68 | for 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 | |||
| 79 | board.set_image(img) | ||
| 80 | board.show() | ||
| 81 | ``` | ||
| 82 | |||
| 83 | And because eInk displays are rather slow to refresh and the clock requires | ||
| 84 | refreshing only once a minute, this can be done through cronjob. | ||
| 85 | |||
| 86 | Before we add this job to cron we need to make `clock.py` executable with `chmod | ||
| 87 | +x clock.py`. | ||
| 88 | |||
| 89 | Then we add a cronjob with `crontab -e`. | ||
| 90 | |||
| 91 | ```txt | ||
| 92 | * * * * * /home/pi/clock.py | ||
| 93 | ``` | ||
| 94 | |||
| 95 | So, we end up with a result like this. | ||
| 96 | |||
| 97 |  | ||
| 98 | |||
| 99 | And for the enclosure that can be 3D printed, but I haven't yet something like | ||
| 100 | this 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 | |||
| 104 | You can download my [STL file for the enclosure | ||
| 105 | here](/assets/posts/world-clock/enclosure.stl), but make sure that dimensions make | ||
| 106 | sense and also opening for USB port should be added or just use a drill and some | ||
| 107 | hot glue to make it stick in the enclosure. | ||
