forked from lewisxhe/esp32-camera-series
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iot-cam_serv.sh
executable file
·55 lines (53 loc) · 3.21 KB
/
iot-cam_serv.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/bin/bash
################################################################################
# Server Example 04: 防犯システム
# Copyright (c) 2016-2019 Wataru KUNINO
################################################################################
PORT=1024 # 受信UDPポート番号を1024に
REED=1 # ドアスイッチON検出=0 OFF=1
IP_CAM="192.168.0.5" # カメラのIPアドレス(仮)
FILE="cam_a_5" # カメラ名・保存時ファイル名
echo "Server Example 04 Cam (usage: $0 port)" # タイトル表示
if [ $# -ge 1 ]; then # 入力パラメータ数の確認
if [ $1 -ge 1024 ] && [ $1 -le 65535 ]; then # ポート番号の範囲確認
PORT=$1 # ポート番号を設定
fi # ifの終了
fi # ifの終了
echo "Listening UDP port "$PORT"..." # ポート番号表示
mkdir photo >& /dev/null # 写真保存用フォルダ作成
while true; do # 永遠に繰り返し
UDP=`nc -luw0 $PORT|tr -d [:cntrl:]|\
tr -d "\!\"\$\%\&\'\(\)\*\+\;\<\=\>\?\[\\\]\^\{\|\}\~"`
# UDPパケットを取得
DATE=`date "+%Y/%m/%d %R"` # 日時を取得
DEV=${UDP#,*} # デバイス名を取得(前方)
DEV=${DEV%%,*} # デバイス名を取得(後方)
echo -E $DATE, $UDP # 取得日時とデータを表示
# echo -E $DATE, $UDP >> log_$DEV.csv # 取得日時とデータを保存
CAM=0 # 変数CAMの初期化
case "$DEV" in # DEVの内容に応じて
"rd_sw_"? ) DET=`echo -E $UDP|tr -d ' '|cut -d, -f2`
if [ $DET -eq $REED ]; then # 応答値とREED値が同じとき
CAM=1
fi ;;
"pir_s_"? ) DET=`echo -E $UDP|tr -d ' '|cut -d, -f2`
if [ $DET != 0 ]; then # 応答値が0以外の時
CAM=1
fi ;;
"Pong" ) CAM=1 ;;
"${FILE}" ) IP_CAM=`echo -E $UDP|tr -d ' '|cut -d'/' -f3`
echo "IP_CAM="${IP_CAM}
;;
esac
if [ $CAM != 0 ]; then # CAMが0で無いとき
echo -n "wget "${IP_CAM}"/cam.jpg"
wget -qT10 $IP_CAM/cam.jpg # 写真撮影と写真取得
if [ $? -eq 0 ]; then
echo " OK"
else
echo " ERROR"
fi
SFX=`date "+%Y%m%d-%H%M"` # 撮影日時を取得し変数SFXへ
mv cam.jpg photo/$FILE"_"$SFX.jpg >& /dev/null
fi # 写真の保存
done # 繰り返しここまで