12. IoT Case 09: Smart Digital Morning Clock¶
Level:
12.1. Goal¶
Make a digital clock that performs a normal clock function with auto-synchronizing Internet time, and alert at specific time.
12.2. Background¶
What is Smart Digital Clock?
Smart digital clock is a clock that shows time as accurate as to atomic clock by connecting to the Internet.
Smart Digital Clock Principle
NTP protocol ensures devices synchronize their time with an accurate time internet provider. With connecting to the Internet, the digital clock can show the time close to atomic clock in millisecond .
In the program, it will get the internet time every 0.5 second to show the time on the OLED. When the time is arrived to specific time (e.g. 06:00:00), it will turn on the alert. Otherwise, it will turn of the buzzer. When the user pressed the button A, the alarm can be closed.
12.3. Part List¶
12.4. Assembly step¶
Step 1
In this case, build the “Big Style Model” as a home base.
Step 2
Let’s build a bed room to install a digital clock. Put the model E3 onto cardboard A, align with the holes at model A and B3.
Step 3
Attach the OLED display as a digital clock to the model E3 using M2 * 10mm screws and nuts.
Step 4
Put the model E4 onto cardboard A, align with the holes at model A and B4.
Step 5
To build a bed. Put model J2 onto the model J1, then put together all the cardboard parts (J1-J3).
Step 6
The bed completed!
Step 7
Place the bed next to the digital clock at the room.
Step 8
Assembly Completed!
12.6. Programming (MakeCode)¶
Step 1. Initialize OLED, IoT:bit and connect to WiFi, create the variable
Snap
Initialize OLED with width 128, height 64
toon start
Snap
Initialize IoT:bit TX P16 RX P8
toon start
Snap
Set Wi-Fi to ssid pwd
toon start
and enter the SSID and passwordCreate the variable
alarmTrig
Set the
alarmTrig
variable tofalse
Step 2. Check Wi-Fi connection state
Snap the
On WiFi connected
block to editorPut a
show icon tick
inside the block to show tick icon after connected
Step 3. Get the NTP time
Put a
if
statement inForever
Use
WiFi connected?
as conditionIn
if
segment, putGet NTP Current Time at city HongKong(UTC+8)
to get NTP timePause 500ms
to get the NTP time with each 0.5 second
Step 4. Show the NTP time on display
Put
On NTP received
to editorWhen get the NTP time, the program in this block will running
Clear the display before each update by
Clear OLED display
Show the formatted time string with
show string join Date: day.Month.Year
Show the formatted time string with
show string join Time: Hour:Minute:Second
Step 5. Set the alarm
Put a
if
statement with multiple conditionsHour = 6
andMinute = 0
andSecond = 6
Put
set alarmTrig to true
inif
segment
Step 6. Set up the sound
Put another
Forever
to editorPut a
if-else
statement inside theForever
Use
alarmTrig = true
as conditionPlay the alarm by melody at tempo 500 bpm
Otherwise, stop the alarm
Step 7. Turn off alarm
Put
on Button A pressed
to editorPut
set alarmTrig to false
into the block
Full Solution
MakeCode: https://makecode.microbit.org/_ExgH3FKJWT2b
You could also download the program from the following website:
12.8. Think¶
Q1. Apart from showing the time and alarm, any other tasks can be done when we know the time?