.. note::

    Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.

    **Why Join?**

    - **Expert Support**: Solve post-sale issues and technical challenges with help from our community and team.
    - **Learn & Share**: Exchange tips and tutorials to enhance your skills.
    - **Exclusive Previews**: Get early access to new product announcements and sneak peeks.
    - **Special Discounts**: Enjoy exclusive discounts on our newest products.
    - **Festive Promotions and Giveaways**: Take part in giveaways and holiday promotions.

    👉 Ready to explore and create with us? Click [|link_sf_facebook|] and join today!

.. _4.1.5_py:

4.1.5 Intelligent Visual Doorbell
==========================================

Introduction
-----------------

In this project, let's make a DIY intelligent visual doorbell.

Required Components
------------------------------

In this project, we need the following components.

.. image:: ../img/3.1.19components.png
  :width: 800
  :align: center

It's definitely convenient to buy a whole kit, here's the link: 

.. list-table::
    :widths: 20 20 20
    :header-rows: 1

    *   - Name	
        - ITEMS IN THIS KIT
        - LINK
    *   - Raphael Kit
        - 337
        - |link_Raphael_kit|

You can also buy them separately from the links below.

.. list-table::
    :widths: 30 20
    :header-rows: 1

    *   - COMPONENT INTRODUCTION
        - PURCHASE LINK

    *   - :ref:`cpn_gpio_extension_board`
        - |link_gpio_board_buy|
    *   - :ref:`cpn_breadboard`
        - |link_breadboard_buy|
    *   - :ref:`cpn_wires`
        - |link_wires_buy|
    *   - :ref:`cpn_resistor`
        - |link_resistor_buy|
    *   - :ref:`cpn_button`
        - |link_button_buy|
    *   - :ref:`cpn_audio_speaker`
        - \-
    *   - :ref:`cpn_camera_module`
        - |link_camera_buy|


Schematic Diagram
-----------------------

============ ======== ======== ===
T-Board Name physical wiringPi BCM
GPIO27       Pin 13   2        27
============ ======== ======== ===

.. image:: ../img/3.1.19_schematic.png
   :width: 500
   :align: center


Experimental Procedures
------------------------------

**Step 1:** Build the circuit.

.. image:: ../img/3.1.19fritzing.png
  :width: 800
  :align: center

Before this project, you need to make sure you complete :ref:`3.1.3_py` & :ref:`3.1.2_py`.

**Step 2:** Get into the folder of the code.

.. raw:: html

    <run></run>

.. code-block::

    cd ~/raphael-kit/python/

**Step 3:** Run.

.. raw:: html

    <run></run>

.. code-block::

    python3 4.1.5_DoorBell.py

After the code runs, when the button is pressed, a bell will sound, and the camera will record a 5s video, which is stored as the ``visitor.h264`` file in the ``~`` directory. If you have a screen, you can also view visitors by previewing the video in real time.

**Code**

.. note::
    You can **Modify/Reset/Copy/Run/Stop** the code below. But before that, you need to go to  source code path like ``raphael-kit/python``. After modifying the code, you can run it directly to see the effect.

.. raw:: html

    <run></run>

.. code-block:: python

    #!/usr/bin/env python3
    from picamera import PiCamera
    from pygame import mixer
    import RPi.GPIO as GPIO
    import time
    import os
    user = os.getlogin()
    user_home = os.path.expanduser(f'~{user}')


    camera = PiCamera()

    BtnPin = 18
    status = False

    def setup():
        GPIO.setmode(GPIO.BCM)
        GPIO.setup(BtnPin, GPIO.IN, GPIO.PUD_UP)
        mixer.init()

    def takePhotos(pin):
        global status
        status = True

    def main():
        global status
        GPIO.add_event_detect(BtnPin, GPIO.FALLING, callback=takePhotos)
        while True:
            if status:
                mixer.music.load(f'{user_home}/raphael-kit/music/doorbell.wav')
                mixer.music.set_volume(0.7)
                mixer.music.play()
                camera.start_preview(alpha=200)
                camera.start_recording(f'{user_home}/visitor.h264')
                print ('Have a visitor')
                time.sleep(5)
                mixer.music.stop()
                camera.stop_preview()
                camera.stop_recording()
                status = False 

    def destroy():
        GPIO.cleanup()
        mixer.music.stop()
        camera.stop_preview()
        camera.stop_recording()

    if __name__ == '__main__':
        setup()
        try:
            main()
        except KeyboardInterrupt:
            destroy()

**Code Explanation**

.. code-block:: python

    status = False

This is a flag used to record whether the doorbell is used.

.. code-block:: python

    GPIO.add_event_detect(BtnPin, GPIO.FALLING, callback=takePhotos)

Set the event of ``BtnPin``, when the button is pressed (the level signal changes from high to low) , call the function ``takePhotos()``.

.. code-block:: python

    if status:
        mixer.music.load(f'{user_home}/raphael-kit/music/doorbell.wav')
        mixer.music.set_volume(0.7)
        mixer.music.play()
        camera.start_preview(alpha=200)
        camera.start_recording(f'{user_home}/visitor.h264')
        print ('Have a visitor')
        time.sleep(5)
        mixer.music.stop()
        camera.stop_preview()
        camera.stop_recording()
        status = False 

Five seconds are used here to play music and record videos, thus functioning as a doorbell.


Phenomenon Picture
------------------------

.. image:: ../img/4.1.5door_bell.JPG
   :align: center


