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
 99You can download my [STL file for the enclosure
100here](/assets/posts/world-clock/enclosure.stl), but make sure that dimensions make
101sense and also opening for USB port should be added or just use a drill and some
102hot glue to make it stick in the enclosure.