diff --git a/raspberrypi_zero/gpio/.tar.bz2 b/raspberrypi_zero/gpio/.tar.bz2 new file mode 100644 index 0000000..3f6db39 Binary files /dev/null and b/raspberrypi_zero/gpio/.tar.bz2 differ diff --git a/raspberrypi_zero/gpio/data/infrared.data b/raspberrypi_zero/gpio/data/infrared.data new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/raspberrypi_zero/gpio/data/infrared.data @@ -0,0 +1 @@ +1 diff --git a/raspberrypi_zero/gpio/data/sound.data b/raspberrypi_zero/gpio/data/sound.data new file mode 100644 index 0000000..573541a --- /dev/null +++ b/raspberrypi_zero/gpio/data/sound.data @@ -0,0 +1 @@ +0 diff --git a/raspberrypi_zero/gpio/info.sh b/raspberrypi_zero/gpio/info.sh index 81ba268..4a05df8 100644 --- a/raspberrypi_zero/gpio/info.sh +++ b/raspberrypi_zero/gpio/info.sh @@ -26,14 +26,16 @@ function SLEEP() function get18b20data() { MODEL="28-04174019f0ff"; - if test -d "/sys/devices/w1_bus_master1/${MODEL}"; then - temperature=$(cat /sys/devices/w1_bus_master1/28-04174019f0ff/w1_slave | 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"; - else - return 1; - fi + while true; do + if test -d "/sys/devices/w1_bus_master1/${MODEL}"; then + temperature=$(cat /sys/devices/w1_bus_master1/28-04174019f0ff/w1_slave | 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}℃ " >> data/temperature.data; + fi + SLEEP 60; + done } function getcpudata() @@ -46,9 +48,10 @@ function getcpudata() echo "Cpu Percentage: ${percentage}%"; } -function lightinfo() +function INIT() { - bindir="/root/gpio" + null="/dev/null"; + bindir="/root/gpio"; ! test -d ${bindir} && exit 1; #时间范围 high="1500"; @@ -67,7 +70,11 @@ function lightinfo() #判断人体红外线传感器 #infrared=$(${bindir}/info_infrared ${wiringpi_infrared}); - infrared=$(cat ${bindir}/infrared.log 2> /dev/null); + infrared=$(cat ${bindir}/data/infrared.data 2> /dev/null); + + #声音传感器 + wiringpi_sound=25; + sound=$(cat ${bindir}/data/sound.data 2> /dev/null); #距离传感器 l="60"; @@ -86,21 +93,15 @@ function lightinfo() } -#DATE; -#getcpudata; -#get18b20data >> ~/temperature.txt; -lightinfo; - - function WHILE() { while true; do - ${bindir}/info_infrared ${wiringpi_infrared} > infrared.log - SLEEP 1 + ${bindir}/info_infrared ${wiringpi_infrared} > data/infrared.data; + SLEEP 1; done } -function main_() +function light() { while true; do if [ "${now}" -ge "${high}" -a "${now}" -le "${low}" ]; then #检测时间段 @@ -122,14 +123,37 @@ while true; do ${stop_light}; fi fi - lightinfo; #再次获取信息 + INIT; #再次获取信息 SLEEP 1; #睡眠 done } -while getopts :ds: pi +function light_daemon() +{ + while true; do + ${bindir}/info_infrared ${wiringpi_infrared} > data/infrared.data; + SLEEP 1; + done +} + +function sound_daemon() +{ + while :; do + ${bindir}/info_sound ${wiringpi_sound} > data/sound.data + SLEEP 0.3; + done +} + +function sound() +{ + : +} + + +INIT; +while getopts :ds:x ai do -case ${pi} in +case ${ai} in d) daemon='&'; ;; @@ -138,7 +162,8 @@ case ${pi} in "stop") array=$(ls ${bindir}/log/) for arr in ${array[@]}; do - kill $(cat ${bindir}/log/${arr}) + kill $(cat ${bindir}/log/${arr}) &> ${null} + rm ${bindir}/log/${arr} 2> ${null} done exit 0 ;; @@ -148,12 +173,19 @@ case ${pi} in esac exit 0 ;; + x) + set -x + ;; h|?) - echo " -raspberrypi zero w 智能灯控 -$0 Usage: $0 [-?|h] [-d] [-s] - -d Daemon. - -s kill Daemon, signal: stop. + echo -ne " +\e[1;31mraspberrypi zero w 智能家居\e[0m +$0 Usage: $0 [-?|h] [-d] [-s] [-x] [light|temperature] + -d : Daemon. + -s : kill Daemon, signal: stop. + -x : Print commands and their arguments as they are executed. + + light : light Modular. + temperature : temperature Modular. by aixiao. " @@ -162,8 +194,26 @@ exit 0 esac done -WHILE & -WHILEID="$(echo $!)" -echo ${WHILEID} > log/infrared.pid 2> /dev/null -eval main_ ${daemon} -echo $! > log/main.pid 2> /dev/null +shift $((OPTIND-1)) +case ${1} in + light) + light_daemon & + light_daemon_id="$(echo $!)" + echo ${light_daemon_id} > log/infrared.pid 2> ${null} + eval light ${daemon} + echo $! > log/light.pid 2> ${null} + ;; + temperature) + #get18b20data & + #echo $! > log/temperature.pid 2> ${null} + : + ;; + sound) + sound_daemon & + echo $! > log/sound.pid 2> ${null}; + eval sound ${daemon}; + ;; + *) + exit 1; + ;; +esac diff --git a/raspberrypi_zero/gpio/info_infrared.c b/raspberrypi_zero/gpio/info_infrared.c index 1226d91..410e5c8 100644 --- a/raspberrypi_zero/gpio/info_infrared.c +++ b/raspberrypi_zero/gpio/info_infrared.c @@ -10,27 +10,13 @@ int main(int argc, char *argv[]) } 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_pin.c b/raspberrypi_zero/gpio/info_pin.c index d1ae6b9..2cd857b 100644 --- a/raspberrypi_zero/gpio/info_pin.c +++ b/raspberrypi_zero/gpio/info_pin.c @@ -20,13 +20,5 @@ int main(int argc, char *argv[]) int pin = atol(argv[1]); wiringPiSetup(); pinMode(pin, OUTPUT); - /* - while(1) { - digitalWrite(28, HIGH); - digitalWrite(28, LOW); - } - - return digitalRead(28); - */ printf("%d\n", digitalRead(pin)); } diff --git a/raspberrypi_zero/gpio/info_sound b/raspberrypi_zero/gpio/info_sound new file mode 100644 index 0000000..0c0936b Binary files /dev/null and b/raspberrypi_zero/gpio/info_sound differ diff --git a/raspberrypi_zero/gpio/info_sound.c b/raspberrypi_zero/gpio/info_sound.c new file mode 100644 index 0000000..410e5c8 --- /dev/null +++ b/raspberrypi_zero/gpio/info_sound.c @@ -0,0 +1,23 @@ +#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); + + pullUpDnControl(pin, PUD_DOWN); + + status = digitalRead(pin); + printf("%d\n", status); + +}