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