This commit is contained in:
aixiao 2018-01-28 17:48:27 +08:00
parent 0e94cc6935
commit 28c5a00d19
5 changed files with 118 additions and 11 deletions

View File

@ -8,7 +8,6 @@
# AUthor: aixiao@aixiao.me. # AUthor: aixiao@aixiao.me.
# #
function DATE() function DATE()
{ {
y=$(date "+%y"); y=$(date "+%y");
@ -47,20 +46,31 @@ function lightinfo()
bindir="/root/gpio" bindir="/root/gpio"
! test -d ${bindir} && exit 1; ! test -d ${bindir} && exit 1;
#时间范围 #时间范围
high="1400"; high="1900";
low="1430"; low="2400";
#当前时间 #当前时间
now=$(date +%H%M); now=$(date +%H%M);
bcm_lightpin="20"; bcm_lightpin="21";
wiringpi_lightpin="28"; 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 $?)"; 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值 #读取灯pin值
lightpinvalue="$(${bindir}/info_pin ${wiringpi_lightpin})"; 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"; start_light="${bindir}/info_light ${wiringpi_lightpin} 1";
@ -73,10 +83,14 @@ DATE;
#get18b20data >> ~/temperature.txt; #get18b20data >> ~/temperature.txt;
lightinfo; lightinfo;
function main_()
{
while true; do while true; do
if [ "${now}" -ge "${high}" -a "${now}" -le "${low}" ]; then #检测时间段 if [ "${now}" -ge "${high}" -a "${now}" -le "${low}" ]; then #检测时间段
if [ "${phonelogic}" == "0" ]; then #检测手机IP if [ "${phonelogic}" == "0" ]; then #检测手机IP
if test "${lightpinvalue}" = "0"; then #检测灯pin值 #if [ "${infrared}" = "1" ]; then #检测人体红外线
#if [ "${distance}" -ge ${l} -a "${distance}" -le ${s} ]; then #检测距离范围
if test "${lightpinvalue}" = "0"; then #检测灯pin值
${start_light}; ${start_light};
fi fi
else else
@ -90,6 +104,20 @@ while true; do
fi fi
fi fi
lightinfo; #再次获取信息 lightinfo; #再次获取信息
sleep 9; #睡眠 sleep 7; #睡眠
done
}
while getopts :d pi
do
case ${pi} in
d)
daemon='&';
;;
h|?)
:
;;
esac
done done
eval main_ ${daemon}

Binary file not shown.

View File

@ -0,0 +1,37 @@
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
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);
}

Binary file not shown.

View File

@ -0,0 +1,42 @@
#include <wiringPi.h>
#include <sys/time.h>
#include <stdlib.h>
#include <stdio.h>
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;
}