diff --git a/raspberrypi_zero/gpio/info.sh b/raspberrypi_zero/gpio/info.sh index 78c3feb..ac2f90d 100644 --- a/raspberrypi_zero/gpio/info.sh +++ b/raspberrypi_zero/gpio/info.sh @@ -8,7 +8,6 @@ # AUthor: aixiao@aixiao.me. # - function DATE() { y=$(date "+%y"); @@ -47,20 +46,31 @@ function lightinfo() bindir="/root/gpio" ! test -d ${bindir} && exit 1; #时间范围 - high="1400"; - low="1430"; + high="1900"; + low="2400"; #当前时间 now=$(date +%H%M); - bcm_lightpin="20"; - wiringpi_lightpin="28"; + bcm_lightpin="21"; + wiringpi_lightpin="29"; + #wiringpi_infrared="20"; #判断手机是否在线 - phoneip="192.168.137.175"; + phoneip="192.168.137.27"; phonelogic="$(ping ${phoneip} -c 1 -S 1 &> /dev/null; echo $?)"; + #判断人体红外线传感器 + infrared=$(${bindir}/info_infrared ${wiringpi_infrared}); + + #距离传感器 + 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})"; + #lightpinvalue="$(gpio -g read ${bcm_lightpin})"; #开灯&关灯 start_light="${bindir}/info_light ${wiringpi_lightpin} 1"; @@ -73,10 +83,14 @@ DATE; #get18b20data >> ~/temperature.txt; lightinfo; +function main_() +{ while true; do - if [ "${now}" -ge "${high}" -a "${now}" -le "${low}" ]; then #检测时间段 - if [ "${phonelogic}" == "0" ]; then #检测手机IP - if test "${lightpinvalue}" = "0"; then #检测灯pin值 + if [ "${now}" -ge "${high}" -a "${now}" -le "${low}" ]; then #检测时间段 + if [ "${phonelogic}" == "0" ]; then #检测手机IP + #if [ "${infrared}" = "1" ]; then #检测人体红外线 + #if [ "${distance}" -ge ${l} -a "${distance}" -le ${s} ]; then #检测距离范围 + if test "${lightpinvalue}" = "0"; then #检测灯pin值 ${start_light}; fi else @@ -90,6 +104,20 @@ while true; do fi fi lightinfo; #再次获取信息 - sleep 9; #睡眠 + sleep 7; #睡眠 +done +} + +while getopts :d pi +do +case ${pi} in + d) + daemon='&'; + ;; + h|?) + : + ;; +esac done +eval main_ ${daemon} diff --git a/raspberrypi_zero/gpio/info_infrared b/raspberrypi_zero/gpio/info_infrared new file mode 100644 index 0000000..8c0c55e Binary files /dev/null and b/raspberrypi_zero/gpio/info_infrared differ diff --git a/raspberrypi_zero/gpio/info_infrared.c b/raspberrypi_zero/gpio/info_infrared.c new file mode 100644 index 0000000..1226d91 --- /dev/null +++ b/raspberrypi_zero/gpio/info_infrared.c @@ -0,0 +1,37 @@ +#include +#include +#include +#include + +int main(int argc, char *argv[]) +{ + if(argc != 2) { + exit(0); + } + int pin = atol(argv[1]); + int status = 0; + wiringPiSetup(); + + //设置为输入模式 + pinMode(pin, INPUT); + + //很重要。控制默认情况下此PIN处于下拉低电平状态 + pullUpDnControl(pin, PUD_DOWN); + +/* + while(true) + { + delay(1000); + //读取当前pin的输入状态 + if(digitalRead(pin) == 1) { + printf("There is somebody here\n"); + } + else { + printf("There is no one here\n"); + } + } +*/ + status = digitalRead(pin); + printf("%d\n", status); + +} diff --git a/raspberrypi_zero/gpio/info_ultrasound b/raspberrypi_zero/gpio/info_ultrasound new file mode 100644 index 0000000..740f89d Binary files /dev/null and b/raspberrypi_zero/gpio/info_ultrasound differ diff --git a/raspberrypi_zero/gpio/info_ultrasound.c b/raspberrypi_zero/gpio/info_ultrasound.c new file mode 100644 index 0000000..e17200e --- /dev/null +++ b/raspberrypi_zero/gpio/info_ultrasound.c @@ -0,0 +1,42 @@ +#include +#include +#include +#include + +int main(int argc, char * argv[]) +{ + int i; + if( argc != 3) { + exit(0); + } + + int pin_tring = atol(argv[1]); + int pin_echo = atol(argv[2]); + struct timeval tv1; + struct timeval tv2; + long start_time, stop_time; + float distance; + + if(wiringPiSetup() == -1) { + exit(0); + return 0; + } + digitalWrite(pin_tring, LOW); + digitalWrite(pin_tring, HIGH); + delayMicroseconds(10); + digitalWrite(pin_tring, LOW); + + while(!(digitalRead(pin_echo) == 1)); + gettimeofday(&tv1, NULL); + + while(!(digitalRead(pin_echo) == 0)); + gettimeofday(&tv2, NULL); + + start_time = tv1.tv_sec * 1000000 + tv1.tv_usec; + stop_time = tv2.tv_sec * 1000000 + tv2.tv_usec; + + distance = (float)(stop_time - start_time) / 1000000 * 34000 / 2; + + printf("%0.0f\n", distance); + return distance; +}