ScAlAn

Raspberry Pi as a clock

Task: Display clock on the Raspberry Pi display

Implementation with ‘epd’ library:

1. Create python application (main.py) to display date and time:

import epd2in7
import datetime
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw

def main():
   epd = epd2in7.EPD()
   epd.init()

   font = ImageFont.truetype('/usr/share/fonts/truetype/freefont/FreeMonoBold.ttf', 42)
   font42 = ImageFont.truetype('/usr/share/fonts/truetype/freefont/FreeMonoBold.ttf', 80)

   image = Image.new('1', (epd2in7.EPD_WIDTH, epd2in7.EPD_HEIGHT), 255)

   # Time HH:MI
   text = datetime.datetime.today().strftime("%H:%M")
   img_txt = Image.new('L', font42.getsize(text), 255)
   draw_txt = ImageDraw.Draw(img_txt)
   draw_txt.text((0, 0), text, font = font42, fill = 0)
   t = img_txt.rotate(90, expand=1)
   image.paste(t, (10, 10))

   # Date YYYY/MM/DD
   text = datetime.datetime.today().strftime("%Y/%m/%d")
   img_txt = Image.new('L', font.getsize(text), 255)
   draw_txt = ImageDraw.Draw(img_txt)
   draw_txt.text((0, 0), text, font = font, fill = 0)
   t = img_txt.rotate(90, expand=1)
   image.paste(t, (110, 5))

   # Draw
   epd.display_frame(epd.get_frame_buffer(image))

if __name__ == '__main__':
   main()

2. Create run.sh script to run python application:

cd /home/ubuntu/bin/clock
sudo python3 main.py

3. Add library files:

epd2in7.py
epdif.py
main.py
run.sh

4. Add run it every minute by crontab job:

* * * * *       root    /home/ubuntu/bin/clock/run.sh

Result:

Have a fun.

Leave a Reply

Your email address will not be published. Required fields are marked *