.. 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_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 .. code-block:: cd ~/raphael-kit/python/ **Step 3:** Run. .. raw:: html .. code-block:: python3 4.1.5_DoorBell.py After the code runs: - The program waits for the doorbell button to be pressed. - When pressed, a doorbell sound plays and a 5-second video is recorded. - The video is saved as ``visitor.mp4`` in the user’s home directory. - The system then returns to standby until the next button press. - Press ``Ctrl+C`` to exit and clean up resources. **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 .. code-block:: python #!/usr/bin/env python3 import time import os import RPi.GPIO as GPIO from pygame import mixer from picamera2 import Picamera2, Preview from picamera2.encoders import H264Encoder from picamera2.outputs import FfmpegOutput # -------------------------------------------------- # USER DIRECTORY # -------------------------------------------------- user = os.getlogin() user_home = os.path.expanduser(f"~{user}") # -------------------------------------------------- # CAMERA SETUP (Picamera2) # -------------------------------------------------- camera = Picamera2() # Create a video configuration WITHOUT the deprecated "video=" argument video_config = camera.create_video_configuration( main={"size": (1280, 720), "format": "XBGR8888"} ) camera.configure(video_config) # Create H264 encoder (10 Mbps is good quality for doorbell) encoder = H264Encoder(bitrate=10_000_000) # -------------------------------------------------- # GPIO SETUP # -------------------------------------------------- BtnPin = 18 status = False def setup(): GPIO.setmode(GPIO.BCM) GPIO.setup(BtnPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) mixer.init() def button_pressed(pin): """Button callback""" global status status = True # -------------------------------------------------- # MAIN LOOP # -------------------------------------------------- def main(): global status GPIO.add_event_detect(BtnPin, GPIO.FALLING, callback=button_pressed, bouncetime=250) print("Doorbell system running... Press the button to record.") while True: if status: print("Visitor detected!") # Play doorbell sound mixer.music.load(f"{user_home}/raphael-kit/music/doorbell.wav") mixer.music.set_volume(0.7) mixer.music.play() # Use QTGL preview camera.start_preview(Preview.QTGL) # Output file output_path = f"{user_home}/visitor.mp4" output = FfmpegOutput(output_path) # Start recording camera.start_recording(encoder, output) print(f"Recording video to {output_path}") time.sleep(5) # Record for 5 seconds # Stop everything mixer.music.stop() camera.stop_recording() camera.stop_preview() print("Recording finished.\n") status = False time.sleep(0.05) # -------------------------------------------------- # CLEAN EXIT # -------------------------------------------------- def destroy(): print("\nExiting...") mixer.quit() GPIO.cleanup() camera.close() print("Program exited cleanly.") if __name__ == "__main__": setup() try: main() except KeyboardInterrupt: destroy() **Code Explanation** #. Retrieves the current user's home directory to store the recorded video. .. code-block:: python user = os.getlogin() user_home = os.path.expanduser(f"~{user}") #. Creates a Picamera2 instance and configures it for video recording. .. code-block:: python camera = Picamera2() video_config = camera.create_video_configuration( main={"size": (1280, 720), "format": "XBGR8888"} ) camera.configure(video_config) #. Sets up the GPIO button on ``GPIO18`` as an input with a pull-up resistor. .. code-block:: python GPIO.setmode(GPIO.BCM) GPIO.setup(BtnPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) #. Loads and initializes the audio mixer used to play the doorbell sound. .. code-block:: python mixer.init() #. Registers a callback that sets ``status`` to ``True`` when the button is pressed. .. code-block:: python GPIO.add_event_detect(BtnPin, GPIO.FALLING, callback=button_pressed, bouncetime=250) #. Plays the doorbell sound, starts the preview window, and begins video recording. .. code-block:: python mixer.music.load(f"{user_home}/raphael-kit/music/doorbell.wav") mixer.music.play() camera.start_preview(Preview.QTGL) camera.start_recording(encoder, output) #. Records a 5-second video and saves it as ``visitor.mp4``. .. code-block:: python time.sleep(5) camera.stop_recording() camera.stop_preview() #. Cleans up all resources when the program is stopped with ``Ctrl+C``. .. code-block:: python mixer.quit() GPIO.cleanup() camera.close() Phenomenon Picture ------------------------ .. image:: ../img/4.1.5door_bell.JPG :align: center