# IoT Case 07: Smart defense system Level: ![level](images/level3.png) ![auto_fit](images/Case7/case-07.png)

## Goal


Make a smart defense system which emits sound and sends an email to the house owner if there are any suspicious movement near the door.

## Background


What is IFTTT?

IFTTT can help connect all your different apps and devices which enables them to work together to do specific tasks.

Smart defense system operation

The motion sensor can deliver a motion signal to the micro:bit. When the micro:bit detects the signal, the buzzer will emit sound and send an email to specific email account through IFTTT. Also, a monstor icon will be shown on the micro:bit if there are suspicious people passes by.

![auto_fit](images/Case7/Concept-diagram-Case7.png)

## Part List


![auto_fit](images/Case7/Case7_parts.png)

## Assembly step


Step 1

Attach the motion sensor to F1 model. ![auto_fit](images/Case7/Case7_ass1.png)

![auto_fit](images/Case7/Case7_ass2.png)

Step 2

Put together all the cardboard parts (F1-F2) ![auto_fit](images/Case7/Case7_ass3.png)

Step 3

Assembly completed! ![pic_60](images/Case7/Case7_ass4.png)

## Hardware connect


Connect the Motion Sensor to P1 port of IoT:bit

Turn on the Buzzer Switch on P0 port of IoT:bit

![auto_fit](images/Case7/Case7_hardware.png)

*Pull the buzzer switch 'down' to connect the buzzer in this execrise* ## Programming (MakeCode)


Step 1. Initialize OLED, IoT:bit and connect to WiFi

* Snap `Initialize OLED with width:128, height: 64` to `on start` * Snap `Initialize IoT:bit TX P16 RX P8` from `IoT:bit` to `on start` * Snap `Set Wi-Fi to ssid pwd` from `IoT:bit` * Enter your Wi-Fi name and password. Here we set `smarthon` as `SSID` and `12345678` as `password` ![auto_fit](images/Case7/Case7_p1.png)

Step 2: Show icon "tick" after WiFi connection

* Snap `show icon` from `basic` to `On WiFi connected` and select icon `tick` *Snap `show string` to `On WiFi connected` and show `Device ID` ![auto_fit](images/Case7-Fix/Case7-Fix_p5.png)

Step 3. Check motion sensor value

* Snap `if statement` to block `forever` * If `WiFi is connected`, * If `get motion (triggered or not) at Pin P1` = `true` then, that’s say someone is near the door. ![auto_fit](images/Case7/Case7_p3.png)

Step 4. Send notification when someone pass by

* Snap `if statement` to block `forever` * If `WiFi is connected`, then snap `play tone Middle C for 1 beat` from `music` * Snap `show icon` from `basic` and select a `monster icon` * Snap `Send IFTTT event_name*…` from `IoT:bit` > `IoT Services`, input your event name `SendEmail` ![auto_fit](images/Case7-Fix/Case7-Fix_p1.png)

Step 5. Show smile icon when no one pass by

* Snap `show icon` into `Else`, and select icon `smile` * Snap `Pause` after `if statement` to the loop for 1 second delay for checking ![auto_fit](images/Case7-Fix/Case7-Fix_p2.png)

Full Solution

MakeCode: https://makecode.microbit.org/S25125-53311-95019-94178 ![auto_fit](images/Case7-Fix/Case7-Fix_p3.png)

Download the hex file:
## IoT (IFTTT)


* For more details, please refer to Chapter 2: Send Email by IFTTT

Step 1

Go to [http://www.ifttt.com](http://www.ifttt.com), register an account and login to the platform

![auto_fit](images/Case7-Fix/Ch2_ifttt1.png)

Step 2

On the top right menu, click “Create” > “Applets”

![auto_fit](images/Case7-Fix/Ch2_ifttt2.png)

Step 3

* Select this * Select Smarthon IoT * Input Device ID and Event Name. (eg. Device ID: 0x55a842e3477a, Event Name: SendEmail) * Click “Create trigger”

![auto_fit](images/Case7-Fix/Case7-Fix_p6.png)

Step 4

Select “That” > Email > Email

![auto_fit](images/Case7-Fix/Case7-Fix_p7.png)

Step 5

Select “Send me an email” , Input email title and body, then click “Create action”

![auto_fit](images/Case7-Fix/Case7-Fix_p8.png)

## Result


When WiFi is connected, if there are any suspicious movement near the door, the buzzer will emit sound and an email will be sent to the house owner. Also, a monster icon will be shown on the micro:bit.

![auto_fit](images/Case7/Case7_result1.png) ![auto_fit](images/Case7/Case7_result2.gif) ## Think


Q1. How to avoid sending duplicate emails? (tips: using variable)

Q2. How to distinguish suspicious people (e.g. password authentication)?