12. IoT Case 09: Smart Digital Morning Clock

Level: level auto_fit

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. pic

12.3. Part List


pic_90

12.4. Assembly step


Step 1

In this case, build the “Big Style Model” as a home base.

pic_90

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.

pic_90

Step 3

Attach the OLED display as a digital clock to the model E3 using M2 * 10mm screws and nuts.

pic_90

Step 4

Put the model E4 onto cardboard A, align with the holes at model A and B4.

auto_fit

Step 5

To build a bed. Put model J2 onto the model J1, then put together all the cardboard parts (J1-J3).

pic_90

Step 6

The bed completed!

pic_90

Step 7

Place the bed next to the digital clock at the room.

pic_90

Step 8

Assembly Completed!

pic_90

12.5. Hardware connect


  1. Connect OLED to I2C port

  2. Pull down the buzzer switch to connect the buzzer

pic_80

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 to on start

  • Snap Initialize IoT:bit TX P16 RX P8 to on start

  • Snap Set Wi-Fi to ssid pwd to on start and enter the SSID and password

  • Create the variable alarmTrig

  • Set the alarmTrig variable to false auto_fit

Step 2. Check Wi-Fi connection state

  • Snap the On WiFi connected block to editor

  • Put a show icon tick inside the block to show tick icon after connected

auto_fit

Step 3. Get the NTP time

  • Put a if statement in Forever

  • Use WiFi connected? as condition

  • In if segment, put Get NTP Current Time at city HongKong(UTC+8) to get NTP time

  • Pause 500ms to get the NTP time with each 0.5 second

pic_90

Step 4. Show the NTP time on display

  • Put On NTP received to editor

  • When 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 auto_fit

Step 5. Set the alarm

  • Put a if statement with multiple conditions Hour = 6 and Minute = 0 and Second = 6

  • Put set alarmTrig to true in if segment auto_fit

Step 6. Set up the sound

  • Put another Forever to editor

  • Put a if-else statement inside the Forever

  • Use alarmTrig = true as condition

  • Play the alarm by melody at tempo 500 bpm

  • Otherwise, stop the alarm auto_fit

Step 7. Turn off alarm

  • Put on Button A pressed to editor

  • Put set alarmTrig to false into the block auto_fit

Full Solution

MakeCode: https://makecode.microbit.org/_ExgH3FKJWT2b

You could also download the program from the following website:

12.7. Result

After you connected to the Internet, the tick will open an start getting NTP time.

auto_fit

12.8. Think


Q1. Apart from showing the time and alarm, any other tasks can be done when we know the time?