raspberrypi/gpio/info.sh

360 lines
9.6 KiB
Bash
Raw Normal View History

2018-03-29 20:06:23 +08:00
#!/bin/bash
#
# reapberry zero w & pi 3.
# GPIO module
# Date: 20180118
# Time: 17:17
2018-05-16 20:53:50 +08:00
#
2018-03-29 20:06:23 +08:00
#
# AUthor: aixiao@aixiao.me.
#
: 等待.
function SLEEP()
{
sleep $@
}
: 18b20温度传感器.
function get18b20data()
{
MODEL="28-04174019f0ff";
while true; do
if test -d "/sys/devices/w1_bus_master1/${MODEL}"; then
temperature=$(cat /sys/devices/w1_bus_master1/28-031682c7baff/ | grep 't=' | sed 's/^.*t=//g' | awk '{print $1/1000}');
#echo $y年$m月$d日$H时$M分$S秒
#echo -ne "当前室内温度: ";
#echo -e "\e[1;31m${temperature}℃ \e[0m";
echo "${temperature}" >> ${bindir}/data/temperature.data;
fi
SLEEP 60;
done
}
: CPU温度.
function getcpudata()
{
MHZ=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq | awk '{print $1/1000}');
temperature=$(cat /sys/class/thermal/thermal_zone0/temp | awk '{print $1/1000}');
percentage=$(top -b -n 2 | grep ^%Cpu | sed -n "2p" | sed -e 's/^.*://g;s/..,//g;s/st//g' | awk '{print "scale=1; 100-" $4 }' | bc);
echo "Cpu Frequencyi: ${MHZ}MHZ";
echo -e "\e[1;31mCpu temperature: ${temperature}℃ \e[0m";
echo "Cpu Percentage: ${percentage}%";
}
function INIT()
{
null="/dev/null";
bindir="/root/gpio";
! test -d ${bindir} && exit 1;
2020-09-10 13:23:23 +08:00
. ${bindir}/conf/info.conf
#时间
y=$(date "+%y");
2018-03-29 20:06:23 +08:00
m=$(date "+%m");
d=$(date "+%d");
H=$(date "+%H");
M=$(date "+%M");
S=$(date "+%S");
2020-09-10 13:23:23 +08:00
W=$(date "+%w"); : 周几?
start=1;
stop=0;
2018-03-29 20:06:23 +08:00
#当前时间
now=$(date +%H%M);
#判断手机是否在线
2018-06-05 10:42:40 +08:00
#phoneip="192.168.99.60";
2018-03-29 20:06:23 +08:00
phonelogic="$(ping ${phoneip} -c 1 -S 1 &> /dev/null; echo $?)";
#判断人体红外线传感器
2020-09-10 13:23:23 +08:00
wiringpi_infrared="6";
2018-03-29 20:06:23 +08:00
#infrared=$(${bindir}/info_infrared ${wiringpi_infrared});
infrared=$(cat ${bindir}/data/infrared.data 2> /dev/null);
#声音传感器
wiringpi_sound=25;
sound=$(cat ${bindir}/data/sound.data 2> /dev/null);
#距离传感器
l="60";
s="150";
wiringpi_tring="8";
wiringpi_echo="9";
#distance="$(${bindir}/info_ultrasound ${wiringpi_tring} ${wiringpi_echo})";
{
#读取灯pin值
lightpinvalue="$(${bindir}/info_pin ${wiringpi_lightpin})";
#lightpinvalue="$(gpio -g read ${bcm_lightpin})";
#开灯&关灯
2020-09-10 13:23:23 +08:00
#high="1700"; #时间范围
2018-06-05 10:42:40 +08:00
#low="2300";
2020-09-10 13:23:23 +08:00
#wiringpi_lightpin="4";
2018-03-29 20:06:23 +08:00
start_light="${bindir}/info_light ${wiringpi_lightpin} 1";
stop_light="${bindir}/info_light ${wiringpi_lightpin} 0";
}
2020-09-10 13:23:23 +08:00
#风扇
#wiringpi_wind="1"; #脚位
#l_temperature="28"; #不高于这个摄氏温度
temperature=$(cat ${bindir}/data/wind.data 2> ${null}); #温度,摄氏度
start_wind="${bindir}/info_light ${wiringpi_wind} 1"; #${wiringpi_wind} 高电压
stop_wind="${bindir}/info_light ${wiringpi_wind} 0"; #${wiringpi_wind} 低电压
status_wind="$(${bindir}/info_pin ${wiringpi_wind})"; : PIN状态.
#霍尔元件
helement_pin="1";
2018-03-29 20:06:23 +08:00
}
2020-09-10 13:23:23 +08:00
# 红外线守护进程.
2018-03-29 20:06:23 +08:00
function infrared()
{
while true; do
${bindir}/info_infrared ${wiringpi_infrared} > ${bindir}/data/infrared.data;
SLEEP 1;
done
}
2020-09-10 13:23:23 +08:00
# 灯主进程.
2018-05-16 20:53:50 +08:00
function light()
2018-03-29 20:06:23 +08:00
{
2018-06-05 10:42:40 +08:00
INIT;
2018-03-29 20:06:23 +08:00
while true; do
if test ${W} != "0"; then : 不是周日.
2020-09-10 13:23:23 +08:00
if [ $now -ge $high -a $now -le $low ]; then : 19:30 - 23:00自动检测,其他时间段不管,手动.
if [ "${phonelogic}" == "0" ]; then : phone 如果在线.
if test "${lightpinvalue}" = "0"; then : 检测灯pin值.
2018-03-29 20:06:23 +08:00
${start_light};
fi
else
if test "${lightpinvalue}" = "1"; then
${stop_light};
fi
fi
else
if [ ${now} -gt ${high} ]; then : 如果大于23点就关灯,不管,手动.
if test "${lightpinvalue}" = "1"; then
${stop_light};
fi
fi
fi
2020-09-10 13:23:23 +08:00
else
2018-03-29 20:06:23 +08:00
: 还一个周日
2020-09-10 13:23:23 +08:00
if test "$W" = "0"; then : 是周日.
if [ ${now} -le ${low} -a ${now} -ge ${s_low} ]; then : 8:00 - 23:00,自动化.
if [ "${phonelogic}" == "0" ]; then : phone 如果在线.
if test "${lightpinvalue}" = "0"; then : 检测灯pin值.
${start_light};
2018-03-29 20:06:23 +08:00
fi
2018-05-16 20:53:50 +08:00
else
if test "${lightpinvalue}" = "1"; then
${stop_light};
fi
2018-03-29 20:06:23 +08:00
fi
else
2018-06-05 10:42:40 +08:00
: 不在时间段就关闭,手动.
2020-09-10 13:23:23 +08:00
if test "${lightpinvalue}" = "1"; then
${stop_light};
fi
2018-03-29 20:06:23 +08:00
fi
2020-09-10 13:23:23 +08:00
fi
fi
2018-03-29 20:06:23 +08:00
INIT;
SLEEP 60;
done
}
2020-09-10 13:23:23 +08:00
# 声音传感器守护进程.
2018-03-29 20:06:23 +08:00
function sound_daemon()
{
while :; do
${bindir}/info_sound ${wiringpi_sound} > ${bindir}/data/sound.data
SLEEP 0.3;
done
}
2020-09-10 13:23:23 +08:00
# 声音传感器主进程.
2018-03-29 20:06:23 +08:00
function sound()
{
:
}
2020-09-10 13:23:23 +08:00
# 风扇守护进程.
2018-05-16 20:53:50 +08:00
function wind_daemon()
{
2020-09-10 13:23:23 +08:00
while true; do
${bindir}/wind -d ${device} > ${bindir}/data/wind.data; : 温度传感器获取室内温度信息.
SLEEP 9;
done
2018-05-16 20:53:50 +08:00
}
2020-09-10 13:23:23 +08:00
# 风扇主进程.
2018-03-29 20:06:23 +08:00
function wind()
{
INIT
while true ; do
2020-09-10 13:23:23 +08:00
if [[ "${phonelogic}" == "0" ]]; then : 手机在不在?
if [[ "${temperature}" -ge "${l_temperature}" ]]; then : 温度大于等于$l_temperature摄氏度.
if test "${status_wind}" = "0"; then
${start_wind};
SLEEP $step_wait; : 开启后风扇运行5分钟.
${stop_wind};
SLEEP $step_stop; : 开启后风扇停止运行1分钟,这样是怕吹感冒了.
fi
else
test "${status_wind}" = "1" && ${stop_wind}; : 小于$l_temperature温度就检测或关闭.
fi
else
test "${status_wind}" = "1" && ${stop_wind}; : 手机不再就检测或关闭.
fi
SLEEP 7;
INIT;
2018-03-29 20:06:23 +08:00
done
}
2020-09-10 13:23:23 +08:00
# 霍尔元件守护进程
function helement_daemon()
{
while true; do
${bindir}/helement ${helement_pin} > ${bindir}/data/helement.data; : 霍尔传感器获取状态信息.
SLEEP 0.1;
done
}
# 霍尔元件主进程
function helement()
{
while true; do
DATE=$(date "+%y年%m月%d日%H时%M分%S秒");
status="$(cat ${bindir}/data/helement.data)";
if [ "$status" = "0" ]; then
/root/c/c 192.168.1.89 "echo ${DATE} 请注意,抽屉已经打开"
fi
done
}
# 主进程
2018-03-29 20:06:23 +08:00
function main_()
{
INIT;
while getopts :ds:x ai
do
case ${ai} in
d)
daemon='&';
;;
s)
2020-09-10 13:23:23 +08:00
#echo $@
#exit
2018-03-29 20:06:23 +08:00
case $OPTARG in
"stop")
2020-09-10 13:23:23 +08:00
case $3 in
"wind")
kill $(cat ${bindir}/log/wind_daemon.pid) 2> ${null}
kill $(cat ${bindir}/log/wind.pid) 2> ${null}
exit 1;
;;
"light")
kill `cat ${bindir}/log/light.pid` 2> ${null}
exit 1;
;;
esac
2018-03-29 20:06:23 +08:00
array=$(ls ${bindir}/log/)
for arr in ${array[@]}; do
kill $(cat ${bindir}/log/${arr}) &> ${null}
rm ${bindir}/log/${arr} 2> ${null}
done
exit 0
;;
2020-09-10 13:23:23 +08:00
"reload")
case $3 in
"wind")
kill $(cat ${bindir}/log/wind_daemon.pid) 2> ${null}
kill $(cat ${bindir}/log/wind.pid) 2> ${null}
: 重启wind
bash $0 -d wind
;;
"light")
kill `cat ${bindir}/log/light.pid` 2> ${null}
: 重启light
bash $0 -d light
;;
esac
;;
2018-03-29 20:06:23 +08:00
*)
:
;;
esac
exit 0
;;
x)
set -x
;;
h|?)
echo -ne "
2018-05-16 20:53:50 +08:00
\e[1;31mraspberrypi zero w & 3, GPIO Modular\e[0m
2018-03-29 20:06:23 +08:00
$0 Usage: $0 [-?|h] [-d] [-s] [-x] [light|temperature|wind]
2018-05-16 20:53:50 +08:00
-?|h : Printf Help
-d : Daemon.
2018-06-05 10:42:40 +08:00
-s : kill Daemon, signal: stop, reload.
2018-05-16 20:53:50 +08:00
-x : Print commands and their arguments as they are executed.
2018-03-29 20:06:23 +08:00
light : light Modular.
temperature : temperature Modular.
wind : Wind Modular.
2020-09-10 13:23:23 +08:00
helement : Hall element.
2018-03-29 20:06:23 +08:00
2018-05-16 20:53:50 +08:00
by aixiao@aixiao.me.
2020-09-10 13:23:23 +08:00
2018-03-29 20:06:23 +08:00
"
exit 0
;;
esac
done
shift $((OPTIND-1))
#array=($(echo ${@}))
eval array=(${@})
for i in ${array[@]}; do
case $i in
light)
2018-05-16 20:53:50 +08:00
eval light ${daemon}
2018-03-29 20:06:23 +08:00
echo $! > ${bindir}/log/light.pid 2> ${null}
;;
temperature)
#get18b20data &
#echo $! > log/temperature.pid 2> ${null}
:
;;
sound)
sound_daemon &
echo $! > ${bindir}/log/sound.pid 2> ${null};
eval sound ${daemon};
;;
wind)
wind_daemon &
2018-06-05 10:42:40 +08:00
echo $! > ${bindir}/log/wind_daemon.pid 2> ${null}
2018-03-29 20:06:23 +08:00
eval wind ${daemon}
2018-06-05 10:42:40 +08:00
echo $! > ${bindir}/log/wind.pid 2> ${null}
2018-03-29 20:06:23 +08:00
;;
2020-09-10 13:23:23 +08:00
helement)
helement_daemon &
echo $! > ${bindir}/log/helement_daemon.pid 2> ${null};
eval helement ${daemon}
echo $! > ${bindir}/log/helement.pid 2> ${null}
;;
2018-03-29 20:06:23 +08:00
*)
exit 1;
;;
esac
done
}
main_ $@