2025 年 6 月 7 日

燕子監視器 — ESP32 CAM + Motion

這篇寫了大半年,從2023年中寫到2024 XD

2022的燕子,巢做到一半,就沒再回來了,巢就被長期躲在家裡配電孔裡的麻雀佔走,
只是麻雀似乎也不會築巢,巢就一天一天的剝落,最就整個掉下來,
2023春天,發現燕子又來家裡築巢,所以趕緊啟動去年預計要做的燕子監視器,
記錄一下來家裡做客的燕子。

燕子監視器的重點有幾個:

  • 低成本
  • 無線傳輸
  • 具動態偵測
  • 可長時間記錄

ESP32 CAM可以達到低成本和無線傳輸的要求,但要動態偵測和長時間記錄,就需要一台電腦主機的配合。

利用先前架好的小伺服器,裝上Motion這個套件,這套件主要的功能有(以下翻自官網):

  • 在偵測到動作時,建立影片或是儲存照片
  • 直接錄製多台IP Cam的影像
  • 攝影機即時影像串流直播
  • 觸發動作時呼叫script程式
  • 活動記錄到各種資料庫
  • 可針對隱私或動態偵測需求,完全客制化遮罩
  • Webcam control和串流完全支援有認証的t ls (https)

可搭配各種類型的設備

  • 網路攝影機(RTSP, RTMP, HTTP)
  • PI camera(樹莓派攝影機)
  • V4L2 webcams
  • 影像擷取卡
  • 現有的影像檔

ESP32 CAM相關設定

ESP32 CAM介紹與教學,網路及youtube上已有相當多的教學,這邊只就設定部份及注意事項加以說明。

架好ESP32 CAM,啟動後,在電腦的瀏覽器上打入ESP32 CAM的ip,就可以進入到詘定頁面,

  • Resolution設定會影響到Motion,因此先選定你要的解析度,解析度愈高,影像檔也愈大
  • Quality部份,數值愈小,影像愈清晰

其他設定可自行依喜好設定,影像解析度愈大,愈清晰,影像檔愈大,後端處理的電腦要求也會愈高,這部份需要依自己的硬體去調整;ESP32 CAM同時只能接受一個影象串流,在設定畫面串流時,Motion即無法取得串流影像,反過來也是,此部份需注意。


Motion安裝

後端電腦是使用架在Proxmox上的Ubuntu Server VM,安裝Motion的方式可以用apt的方式安裝,使用以下指令

sudo apt-iget install motion

這種安裝方式較簡單,Motion版本可能較舊,但穩定、非常方便。因我還在熟悉及學習Linux,因此選擇自行編譯,一步一步完成,參考官網教學,找到Abbreviate Building GUide,Debian / Ubuntu /Raspbian packages(依自已使用的作業系統選擇安裝的說明),先安裝必要套件,

udo apt-get install autoconf automake autopoint build-essential pkgconf libtool libzip-dev libjpeg-dev git libavformat-dev libavcodec-dev libavutil-dev libswscale-dev libavdevice-dev libwebp-dev gettext libmicrohttpd-dev

另外可依自已需求,再安裝其他套件,建議FFMpeg套件(影像檔製作、使用網路攝影機)一併安裝

sudo apt-get install libavformat-dev libavcodec-dev libavutil-dev libswscale-dev libavdevice-dev

cd到根目錄,自github上下載Motion套件,預設是當時最新版板本

cd ~
git clone https://github.com/Motion-Project/motion.git

cd到Motion目錄,製作.configure檔,之後進行make編譯,make時間依cpu效能不同,可能會花一點時間,

cd motion
autoreconf -fiv
./configure
make

安裝編譯好的套件

make install

編輯motion.conf檔

conig檔位置在

/usr/local/etc/motion

首先編輯motion.conf,可以用vi或nano編輯

幾個要注意的地方如下:

設定motion背景啟動
#Start in daemon (background) mode and release terminal.
daemon on

設定motion截取的圖片、影片的存放目錄,必須手動建立,為開啟存取權限,這邊假設是在”/motion_pic”
#Target directory for pictures, snapshots and movies
target_dir /motion_pic

無偵測到動作仍持續截取圖片、影片,設定為off
Always save pictures and movies even if there was no motion.
emulate_motion off

偵測到動作之判定及截取影片相關設定,以上提供參考,可自行測試調整
#Threshold for number of changed pixels that triggers motion.
threshold 1500

#Noise threshold for the motion detection.
noise_level 32

#Despeckle the image using (E/e)rode or (D/d)ilate or (l)abel.
despeckle_filter EedDl

#Number of images that must contain motion to trigger an event.
minimum_motion_frames 1

#Gap in seconds of no motion detected that triggers the end of an event.
event_gap 30

#The number of pre-captured (buffered) pictures from before motion.
pre_capture 30

#Number of frames to capture after motion is no longer detected.
post_capture 245

#Output pictures when motion is detected
picture_output off

#Create movies of motion events.
movie_output on

#The encoding quality of the movie. (0=use bitrate. 1=worst quality, 100=best)
movie_quality 45

#Container/Codec to used for the movie. See motion_guide.html
movie_codec hevc
movie_duplicate_frames on

Webcontrol及Stream設定
#Restrict webcontrol connections to the localhost.
webcontrol_localhost on

#The port number for the live stream.
stream_port 8081

#Restrict stream connections to the localhost.
stream_localhost off

Camera config檔路徑設定,由於之後可能會用多台ESP CAM,因此將Camera config獨立設定
#Directory to read ‘.conf’ files for cameras.
camera_dir /usr/local/etc/motion/conf.d

編輯Camera config檔設定


copy範列檔camera1.dist.conf至上列camera config設定檔存放目錄,將檔名改為camera1,並編輯
#User defined name for the camera. (攝影機名稱)
camera_name ESP32-CAM

#Numeric identifier for the camera. (攝影機id)
camera_id 101

#The full URL of the network camera stream. (設定ESP32串流路網址,預設是”http://ip:81/stream
netcam_url http://192.168.1.10:81/stream

#Image width in pixels (截取影像寬)
width 1024

#Image height in pixels. (截取影像長)
height 768

截取影像在畫面上的浮水印文字設定,依自己需要設定
#Text to be overlayed in the lower left corner of images
text_left ESP32

#Text to be overlayed in the lower right corner of images.
text_right ESP32-CAM\n%Y-%m-%d\n%T-%q

#File name(without extension) for movies relative to target directory
movie_filename ESP_CAM_%t-%v-%Y%m%d%H%M%S

串流瀏覽權限設定
motion直播串流網址”http://ip:8081/101/”
ip是motion主機ip,port依motion.conf設定的stream port, 101是camera id

開啟驗證設定,避免他人可不經許可即可瀏覽,
需將下列stream_authentication的id與password改為您的設定
stream_auth_method 1
stream_authentication id:password

啟動motion,設定motion為服務

如以apt install方式安裝motion,預設即可以用sudo systemctl start motion的方式啟動motion,
自行安裝的話,在/etc/init.d新增”motion”檔案,加入以下內容

#!/bin/sh -e
#
# /etc/init.d/motion: Start the motion detection
#
### BEGIN INIT INFO
# Provides:	  motion
# Required-Start: $local_fs $syslog $remote_fs
# Required-Stop: $remote_fs
# Default-Start:  2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start Motion detection
# Description: loads motion and assigns privileges
### END INIT INFO

# Ported to new debian way using sh and /lib/lsb/init-functions
# by Angel Carpintero <ack@telefonica.net>
# Modified by : Juan Angulo Moreno <juan@apuntale.com>
#               Eddy Petrisor <eddy.petrisor@gmail.com>
#               ArAge <ArAge@gmx.co.uk>
NAME=motion
PATH_BIN=/bin:/usr/bin:/sbin:/usr/sbin
DAEMON=/usr/local/bin/motion
DEFAULTS=/etc/default/$NAME
DESC="motion detection daemon"

ENV="env -i LANG=C PATH=$PATH_BIN"

. /lib/lsb/init-functions

test -x $DAEMON || exit 0

RET=0

[ -r "$DEFAULTS" ] && . "$DEFAULTS" || start_motion_daemon=yes


check_daemon_enabled () {
    if [ "$start_motion_daemon" = "yes" ] ; then
        return 0
    else
        log_warning_msg "Not starting $NAME daemon, disabled via /etc/default/$NAME"
        return 1
NAME=motion
PATH_BIN=/bin:/usr/bin:/sbin:/usr/sbin
DAEMON=/usr/local/bin/motion
DEFAULTS=/etc/default/$NAME
DESC="motion detection daemon"

ENV="env -i LANG=C PATH=$PATH_BIN"

. /lib/lsb/init-functions

test -x $DAEMON || exit 0

RET=0

[ -r "$DEFAULTS" ] && . "$DEFAULTS" || start_motion_daemon=yes


check_daemon_enabled () {
    if [ "$start_motion_daemon" = "yes" ] ; then
        return 0
    else
        log_warning_msg "Not starting $NAME daemon, disabled via /etc/default/$NAME"
        return 1
    fi

}


case "$1" in
  start)
    if check_daemon_enabled ; then
        if ! [ -d /var/run/motion ]; then
            mkdir -m 02750 /var/run/motion
            chown motion:motion /var/run/motion
        fi
        if ! [ -d /var/lib/motion ]; then
            mkdir -m 02750 /var/lib/motion
            chown motion:adm /var/lib/motion
        fi
        if ! [ -d /var/log/motion ]; then
            mkdir -m 02750 /var/log/motion
            chown motion:adm /var/log/motion
        fi
        log_daemon_msg "Starting $DESC" "$NAME"
        if start-stop-daemon --start --oknodo --exec $DAEMON -b --chuid motion ; then
            log_end_msg 0
        else
            log_end_msg 1
            RET=1
        fi
    fi
    ;;

  stop)
    log_daemon_msg "Stopping $DESC" "$NAME"
    if start-stop-daemon --stop --oknodo --exec $DAEMON --retry 30 ; then
        log_end_msg 0
    else
        log_end_msg 1
        RET=1
    fi
    ;;

  reload|force-reload)
    log_daemon_msg "Reloading $NAME configuration"
    if start-stop-daemon --stop --signal HUP --exec $DAEMON ; then
        log_end_msg 0
    else
        log_end_msg 1
        RET=1
    fi
    ;;

  restart-motion)
    if check_daemon_enabled ; then
        log_action_begin_msg "Restarting $NAME"
        if $0 stop && $0 start ; then
            log_action_end_msg 0
        else
            log_action_cont_msg "(failed)"
            RET=1
        fi
    fi
    ;;

  restart)
    $0 restart-motion
    ;;

  status)
    status_of_proc /usr/local/bin/motion motion
    ;;

  *)
    echo "Usage: /etc/init.d/$NAME {start|stop|restart|reload|status}"
    RET=1
    ;;
esac


exit $RET

執行

sudo systemctl daemon-reload

之後可以啟動motion

sudo systemctl start motion

其他

在監視器完成沒多久,燕子也加緊腳步把巢完成,接下來就下蛋,其中一隻燕子幾乎都留在巢裡孵蛋,只是三週後,小燕子出來了, 有天傍晚,燕子巢邊繞飛,一邊叫,就像平常麻雀(或是我 )靠近巢的時候,只是查看時,沒看到什塺,隔天燕子就沒再回來,後來仔細看巢邊有黑色小點,密密麻麻的,是螞蟻,才驚覺巢內鶵已經死了,可惜無法記錄到完整育鶵的過程。

2024年,再重新把監視器架起來,為了改善過熱問題,重印ESP32 CAM的外殼,希望今年燕子能再來築巢。

下面是2023年錄到的一些畫面,燕子離開後,巢就被麻雀佔走了XD

ESP_CAM_101-38-20230531011545

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *