Digital Desk Clock
How do IoT clocks work? In this lesson you will learn how to build an digital clock that gets its time from the Internet. This can run on BrainPad Pulse.
Prerequisite
You must have basic understanding of coding and have completed the Drawing lesson.
The API
This program use the timeapi.io API. It is a free service that returns a JSON file containing time related data based on the URL you send it.
import urllib3
import json
import time
from DUE.DUEController import DUEController
availablePort = DUEController.GetConnectionPort()
BrainPad = DUEController(availablePort)
while True:
http = urllib3.PoolManager()
response = http.request('GET',"https://www.timeapi.io/api/Time/current/zone?timeZone=America/Detroit")
clock=json.loads(response.data)
hour = (clock['hour'])
min = (clock['minute'])
sec = (clock['seconds'])
if hour>12:hour=hour-12
BrainPad.Display.Clear(0)
if hour > 0 and hour < 10:
BrainPad.Display.DrawTextScale(hour,1,18,20,3,3)
else:
BrainPad.Display.DrawTextScale(hour,1,0,20,3,3)
BrainPad.Display.DrawTextScale(":",1,36,20,3,3)
if min < 10:
BrainPad.Display.DrawTextScale("0",1,48,20,3,3)
BrainPad.Display.DrawTextScale(min,1,66,20,3,3)
else:
BrainPad.Display.DrawTextScale(min,1,48,20,3,3)
BrainPad.Display.DrawTextScale(":",1,83,20,3,3)
if sec < 10:
BrainPad.Display.DrawTextScale("0",1,95,20,3,3)
BrainPad.Display.DrawTextScale(sec,1,113,20,3,3)
else:
BrainPad.Display.DrawTextScale(sec,1,95,20,3,3)
BrainPad.Display.Show()
time.sleep(0.200)