Raspberry Pi 安裝motioneye

更新 2026.09.21

本文說明如何在 Raspberry Pi 上安裝 motionEye,並在偵測到動作時,透過 MQTT 通知自動以 VLC 全螢幕播放即時影像;動作結束 30 秒後關閉 VLC 並啟動螢幕保護程式。

前提與注意事項

  • 以下以使用者 pi 為例,路徑中的 /home/pi 請換成實際的家目錄。新版 Raspberry Pi OS 安裝時需自訂使用者名稱,不一定有 pi 帳號。
  • 本流程使用 xscreensaver、lxterminal 與 ~/.config/autostart,適用於 X11(LXDE)桌面。若系統預設使用 Wayland,這幾項可能無法運作,可先用 sudo raspi-config 在進階選項中切換回 X11(選項名稱依系統版本而異)。
  • 腳本與 .desktop 檔案直接用一般使用者建立,只需加上執行權限,不需要 sudo,也不要設成 777。

1. 安裝並更新 Raspberry Pi OS

到官方頁面下載 Raspberry Pi OS,選擇 Raspberry Pi OS with desktop and recommended software:

https://www.raspberrypi.com/software/operating-systems/

安裝完成後更新系統:

sudo apt update
sudo apt full-upgrade -y

2. 安裝 motionEye

依專案官方說明安裝:https://github.com/motioneye-project/motioneye

專案 README 的安裝流程如下(Python 3.7 以上):

sudo apt update
sudo apt --no-install-recommends install ca-certificates curl python3
sudo python3 -m pip install motioneye
sudo motioneye_init
  • 在 ARMv6/ARMv7(32 位元)等架構上,可能還需要額外的編譯相依套件,請參考 README。
  • 新版 Raspberry Pi OS 的 pip 可能會回報 externally-managed-environment,請依 README 目前的說明處理(例如改用虛擬環境)。

安裝完成後,motionEye 預設監聽 8765 port:

  • 網址:http://你的IP位址:8765/
  • 預設登入:使用者名稱 admin,密碼留空
  • 第一次登入後,請立即為 admin 設定密碼

3. 安裝 Mosquitto

參考文章:https://blog.gtwang.org/iot/raspberry-pi/raspberry-pi-mosquitto-mqtt-broker-iot-integration/

sudo apt install mosquitto mosquitto-clients

檢查服務狀態:

sudo systemctl status mosquitto

4. 安裝虛擬鍵盤

sudo apt install matchbox-keyboard

5. 安裝螢幕保護程式

sudo apt install xscreensaver

開啟 xscreensaver 設定,調整以下項目:

  • Mode:Blank Screen Only
  • Blank After:1 minute
  • Cycle After:1 minute

6. 在 motionEye 後台設定 Motion Notifications

在 motionEye 後台的 Motion Notifications 區塊設定:

Run A Command:

mosquitto_pub -t motioneye/notifications -m "start detection"

Run An End Command:

mosquitto_pub -t motioneye/notifications -m "end detection"

7. 建立 Linux shell 腳本

mkdir -p /home/pi/Motion-view
cd /home/pi/Motion-view

motionplay.sh

nano motionplay.sh

檔案內容:

#!/bin/bash
vlc --fullscreen http://127.0.0.1:8081

若串流需要帳號密碼,改用下列寫法:

#!/bin/bash
vlc --fullscreen http://user:[email protected]:8081

密碼會以明文存在腳本內,請將這個檔案的權限限制為只有自己可讀寫(chmod 700 motionplay.sh)。

motionstop.sh

nano motionstop.sh

檔案內容:

#!/bin/bash
sleep 30
killall vlc
xscreensaver-command -activate

motiondetect.sh

nano motiondetect.sh

檔案內容:

#!/bin/bash
mosquitto_sub -t motioneye/notifications -v | while read -r topic payload
do
    echo "$(date '+%Y-%m-%d %H:%M:%S') ${topic}: ${payload}"

    if [ "${payload}" = "start detection" ]
    then
        # 30 秒內再次偵測到動作時,取消尚未執行的關閉程序
        pkill -f motionstop.sh

        # 已有 VLC 在播放時,不再開新視窗
        if ! pgrep -x vlc > /dev/null
        then
            echo "...run motionplay.sh"
            lxterminal -e 'bash /home/pi/Motion-view/motionplay.sh' &
        fi
    elif [ "${payload}" = "end detection" ]
    then
        echo "...run motionstop.sh"
        lxterminal -e 'bash /home/pi/Motion-view/motionstop.sh' &
    else
        echo "...nothing"
    fi
done

與原本的差異:

  • 加上 #!/bin/bash,讓腳本可以直接執行。
  • 收到 start detection 時先取消尚未執行的關閉程序,避免 30 秒內再次偵測到動作時,剛開啟的 VLC 被前一次的計時關掉。
  • 已經有 VLC 在播放時,不再重複開啟視窗。
  • lxterminal 加上 & 放到背景,避免迴圈被卡住而無法及時處理下一則通知。

設定執行權限

chmod +x /home/pi/Motion-view/*.sh

8. 建立桌面捷徑

cd /home/pi/Desktop

play.desktop

nano play.desktop

檔案內容:

[Desktop Entry]
Name=Motion Play
Icon=/usr/share/pixmaps/openbox.xpm
Exec=lxterminal -e 'bash /home/pi/Motion-view/motionplay.sh'
Type=Application
Encoding=UTF-8
Terminal=false

detection.desktop

nano detection.desktop

檔案內容:

[Desktop Entry]
Name=Motion Detection
Icon=/usr/share/pixmaps/openbox.xpm
Exec=lxterminal -e 'bash /home/pi/Motion-view/motiondetect.sh'
Type=Application
Encoding=UTF-8
Terminal=false

設定執行權限

chmod +x play.desktop detection.desktop

9. 開機自動啟動

開機時只需要自動啟動偵測腳本,把 detection.desktop 複製到 autostart 資料夾:

mkdir -p /home/pi/.config/autostart
cp /home/pi/Desktop/detection.desktop /home/pi/.config/autostart/
chmod +x /home/pi/.config/autostart/detection.desktop

10. 測試

先確認 detection.desktop 已啟動(重新開機或雙擊桌面捷徑),再手動送出通知,模擬動作偵測:

mosquitto_pub -t motioneye/notifications -m "start detection"
mosquitto_pub -t motioneye/notifications -m "end detection"

預期結果:送出 start detection 後 VLC 全螢幕播放;送出 end detection 約 30 秒後 VLC 關閉,並啟動螢幕保護程式。