# IoT Case 09: Smart Digital Morning Clock Level: ![level](images/level4.png) ![auto_fit](images/Case9/intro.png)

## Goal


Make a digital clock that performs a normal clock function with auto-synchronizing Internet time, and alert at specific time.

## 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](images/Case9/Case9_flowchart.png)

## Part List


![pic_90](images/Case9/Case9_parts1.png)

## Assembly step


Step 1

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

![pic_90](images/Case9/Case9_ass1.png)

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](images/Case9/Case9_ass2.png)

Step 3

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

![pic_90](images/Case9/Case9_ass3.png)

Step 4

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

![auto_fit](images/Case9/Case9_ass4.png)

Step 5

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

![pic_90](images/Case9/Case9_ass5.png)

Step 6

The bed completed!

![pic_90](images/Case9/Case9_ass6.png)

Step 7

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

![pic_90](images/Case9/Case9_ass7.png)

Step 8

Assembly Completed!

![pic_90](images/Case9/Case9_ass8.png)

## Hardware connect


1. Connect OLED to I2C port 2. Pull down the buzzer switch to connect the buzzer ![pic_80](images/Case9/Case9_hardware.png)

## 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](images/Case9/Case9_p1.png)

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](images/Case9/Case9_p2.png)

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](images/Case9/Case9_p3.png)

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](images/Case9/Case9_p4.png)

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](images/Case9/Case9_p5.png)

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](images/Case9/Case9_p6.png)

Step 7. Turn off alarm

* Put `on Button A pressed` to editor * Put `set alarmTrig to false` into the block ![auto_fit](images/Case9/Case9_p7.png)

Full Solution

MakeCode: [https://makecode.microbit.org/S77738-06496-37751-47481](https://makecode.microbit.org/S77738-06496-37751-47481)

You could also download the program from the following website:
## Result After you connected to the Internet, the tick will open an start getting NTP time.

![auto_fit](images/Case9/Case9_result.gif)

## Think


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