深度优先

这个家伙好懒,除了文章什么都没留下

0%

【树莓派】延时摄影项目分享

http://www.hardyun.com/355.html

视频链接

Youtube: https://www.youtube.com/watch?v=1qs97MuROMs
B站: https://www.bilibili.com/video/BV1ZE411F7pB

相关配置和脚本

1. crontab 定时任务列表

1
2
3
4
5
6
7
pi@raspberrypi:~ $ crontab -l
*/2 * * * * /usr/local/bin /camera.sh
00 04 * * * /usr/local/bin/deleteold.sh
00 03 * * * /usr/local/bin/sun.sh
05 06 * * * /usr/local/bin/daynight.sh day
19 18 * * * /usr/local/bin/daynight.sh night
@reboot /usr/local/bin/initgpio.sh

2. 拍照脚本 camera.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
pi@raspberrypi:~ $ cat /usr/local/bin/camera.sh
#!/bin/bash

photo_root_dir="/home/pi/Pictures"
ircutnode="/sys/class/gpio/gpio17/value"
suffix=""
filename=""

cmdname="veye_raspistill"

date_day=$(date +%Y-%m-%d)
date_time=$(date  +%Y-%m-%d-%H-%M )
#echo $date_time

errorlog="/home/pi/error.txt"
ret=$(ps -A -ww | grep $cmdname | awk '{print $4}')
if [ "$ret" = "$cmdname" ];then
  echo "$date_time: Photo command process $ret blocked, reboot!" >> $errorlog
  /usr/bin/sudo /sbin/shutdown -r now
  exit 1
fi

if [ -f $ircutnode ];then
  ircutvalue=$( cat $ircutnode)
  if [ "$ircutvalue" = "0" ];then
    suffix="day"
  else
    suffix="night"
  fi
fi
#echo $suffix

filename=${date_time}_${suffix}

current_dir=$photo_root_dir/$date_day
#echo $current_dir

if [ ! -d $current_dir ]; then
    mkdir -p $current_dir
fi

/usr/local/bin/$cmdname -o $current_dir/ $filename.jpg

temperature_18b20=$(cat /sys/bus/w1/devices/ 28-00000b48b969/w1_slave | grep 't=' | sed 's/^.*t=//g' | awk '{print $1/1000}')
#echo $temperature_18b20

temperature_cpu=$(cat /sys/class/thermal/thermal_zone0 /temp | awk '{print $1/1000}')
#echo $temperature_cpu

#echo $date_time $temperature_18b20 $temperature_cpu > $current_dir/$filename.txt
printf "%s %-7s %-6sn" $date_time $temperature_18b20 $temperature_cpu > $current_dir/$filename.txt

/bin/sync

**3. 删除前第6天照片的脚本 deleteold.sh**

pi@raspberrypi:~ $ cat /usr/local/bin/deleteold.sh
#!/bin/bash

photo_root_dir="/home/pi/Pictures"

#date_day=$(date +%Y-%m-%d)
date_day=$(date -I -d "$(date +%Y-%m-%d) -6 days")

to_clear_dir=$photo_root_dir/$date_day

if [ -d $to_clear_dir ]; then
  rm -rf "$to_clear_dir"
  echo "$to_clear_dir has been deleted!"
  /bin/ sync
fi

4. 绐定经纬度和日期,批量生成每天日出和日落时间的脚本 create-date-list.sh
派上需要安装的软件

1
sudo apt install lynx jq
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
pi@raspberrypi:~ $ cat create-date-list.sh
#!/bin/bash

lat=42.33
lng=118.88

date_start="2020-02-17"
days=30

for i in $(eval echo "{0..${days}"}); do
  dateid=$(date -I -d "$date_start + $i days")
  #echo ===$dateid
  sunrise_utc=$(lynx --dump   "https://api.sunrise-sunset.org/json?lat=$lat&lng=$lng&date=$dateid" | jq '.results.sunrise' )
  sunrise_utc="${sunrise_utc//"}"
  sunrise_utc=$(date -d "$dateid $sunrise_utc UTC" "+%Y-%m-%d %H:%M")
  #echo $sunrise_utc

  #date_day=$(echo $sunrise_utc | awk '{print $1}')
  date_day=$(date -I -d "$date_start +$(expr $i + 1) days")
  date_sunrise=$(echo $sunrise_utc | awk '{print $2}')

  sleep 1

  #echo ===$date_day
  #echo $date_sunrise
  sunset_utc=$(lynx --dump  "https://api.sunrise-sunset.org/json?lat=$lat&lng=$lng&date=$date_day" | jq '.results.sunset' )
  sunset_utc="${sunset_utc//"}"
  sunset_utc=$(date -d "$date_day $sunset_utc UTC" "+%Y-%m-%d %H:%M")
  #echo $sunset_utc

  date_sunset=$(echo $sunset_utc | awk '{print $2}' )

  echo $date_day $date_sunrise $date_sunset

  sleep 1
done

将生成的日出日落时间数据保存到sun.txt文件中

1
./create-date-list.sh 2>&1 | tee sun.txt

5. 生成每天日出日落时切换摄像头日夜模式的脚本 sun.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
#!/bin/bash

sunfile="/home/pi/sun.txt"
suncron="/home/pi/crontabfile"

date_day=$(date +%Y-%m-%d)

sun=$(cat $sunfile | grep $date_day)
sunrise=$(echo $sun | awk '{print $2}')
sunset=$(echo $sun | awk '{print $3}')

#echo $sunrise $sunset

sunrise_h=$(echo $sunrise | awk -F ':' '{print $1}')
sunrise_m=$(echo $sunrise | awk -F ':' '{print $2}')

if [ $(($sunrise_m % 2)) -eq 0 ];then
  sunrise_m=$(printf "%02d" $((sunrise_m+1)))
fi

sunset_h=$(echo $sunset | awk -F ':' '{print $1}')
sunset_m=$(echo $sunset | awk -F ':' '{print $2}')

if [ $(($sunset_m % 2)) -eq 0 ];then
  sunset_m=$(printf "%02d" $((sunset_m+1)))
fi

echo "*/2 * * * * /usr/local/bin/camera.sh" > $suncron
echo "00 04 * * * /usr/local/bin/deleteold.sh" >> $suncron
echo "00 03 * * * /usr/local/bin/sun.sh" >> $suncron

echo $sunrise_m $sunrise_h "* * * /usr/local/bin/daynight.sh day"   >> $suncron
echo $sunset_m  $sunset_h  "* * * /usr/local/bin/daynight.sh night" >> $suncron

echo "@reboot /usr/local/bin/initgpio.sh" >> $suncron

/bin/sync
/usr/bin/crontab $suncron

6. 通过GPIO切换摄像头日夜模式的脚本 daynight.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
#!/bin/bash

gpiodir="/sys/class/gpio"
gpio_irled=5    # 0 - on,  1 - off
gpio_led=6  # 0 - on,  1 - off
gpio_mode=17    # 0 - day, 1 - night

gpio_init ()
{
  echo $1 $2
  echo $1 > $gpiodir /export
  sleep 1
  echo out > $gpiodir/gpio $1/direction
  echo $2 > $gpiodir/gpio$1 /value
}

gpio_ctrl ()
{
  echo gpio$1 $2
  if [ ! -f $gpiodir/gpio$1/value ];then
    gpio_init $1 $2
  else
    echo $2 > $gpiodir/gpio $1/value
  fi
}

#init_gpio 17 0

ir_control ()
{
  if [ "$1" = "night" ]; then
    echo "=== night mode ==="
    gpio_ctrl $gpio_irled 0
    gpio_ctrl $gpio_mode 1
  else
    echo "=== day mode ==="
    gpio_ctrl $gpio_irled 1
    gpio_ctrl $gpio_mode 0
  fi
}

# turn off led light
gpio_ctrl $gpio_led 1

ir_control $1

7. 派重启后恢复摄像头日夜模式的脚本 initgpio.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
#!/bin/bash

sunfile="/home/pi/sun.txt"

date_day=$(date +%Y-%m-%d)

sun=$(cat $sunfile | grep $date_day)
sunrise=$(echo $sun | awk '{print $2}')
sunset=$(echo $sun | awk '{print $3}')

#echo $sunrise $sunset

sunrise_h=$(echo $sunrise | awk -F ':' '{print $1}')
sunrise_m=$(echo $sunrise | awk -F ':' '{print $2}')

if [ $(($sunrise_m % 2)) -eq 0 ];then
  sunrise_m=$(printf "%02d" $((sunrise_m+1)))
fi

sunset_h=$(echo $sunset | awk -F ':' '{print $1}')
sunset_m=$(echo $sunset | awk -F ':' '{print $2}')

if [ $(($sunset_m % 2)) -eq 0 ];then
  sunset_m=$(printf "%02d" $((sunset_m+1)))
fi

sunrise="$sunrise_h:$sunrise_m"
sunset="$sunset_h:$sunset_m"

currenttime=$(date +%H:%M)

if [[ "$currenttime" > "$sunrise" ]] && [[ "$currenttime" < "$sunset" ]];then
  /usr/local/bin /daynight.sh day
else
  /usr/local/bin/daynight.sh night
fi

在异地另一个树莓派上远程获取照片

1. 定时任务

1
2
pi@raspberrypi:~ $ crontab -l
0 1 * * * /usr/local/bin/getphoto.sh

2. 每天自动获取远程派摄像头内照片的脚本 getphoto.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
#!/bin/bash

target_dir="/opt/picamera"
mailto="xxx@163.com"

date_day=$(date -I -d "$(date +%Y-%m-%d) -1 days")

if [ ! -d $target_dir ];then
  mkdir -p $target_dir
fi

cmd="rsync -av -e 'ssh -p[PORT]' pi@[IP]:/home/pi/Pictures/$date_day $target_dir"

n=0
timeout=50
until [ $n -ge $timeout ]
do
  eval $cmd && break
  n=$[$n+ 1]
  sleep 5
done

file_num=$(ls $target_dir/$date_day | wc -l)
#echo $file_num

if [ "$file_num" = "1440" ];then
  title= "Get photos successfully [$file_num]"
else
  title="Failed to get photos [$file_num]"
fi

photo_num=$(ls $target_dir/$date_day/*.jpg | wc -l)
txt_num=$(ls $target_dir/$date_day/*.txt | wc -l)

msg="photo:$photo_num, txt:$txt_num, retry:$n"

#echo $title : $msg
echo "$msg" | mutt -s "$title" -- $mailto

邮件功能需要安装

1
sudo apt install msmtp mutt

相关配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
pi@raspberrypi:~ $ cat ~/.msmtprc
account default
host smtp.163.com
from xxx@163.com
auth login
user xxx@163.com
password [PASSWORD]
logfile ~/.msmtp.log

pi@raspberrypi:~ $ cat ~/.muttrc
set sendmail="/usr/bin/msmtp"
set use_from=yes
set realname="rpi-camera"
set from="xxx@163.com"
set envelope_from=yes
set editor="vim -nw"

3. 在树莓派上用ffmpeg将照片合成视频的命令

1
ffmpeg -framerate 25 -pattern_type glob -i '*.jpg' -c:v libx264 -profile:v high -crf 20 -pix_fmt yuv420p output.mp4