diff --git a/gpio/Makefile b/gpio/Makefile new file mode 100644 index 0000000..07b7597 --- /dev/null +++ b/gpio/Makefile @@ -0,0 +1,24 @@ +CC=gcc +libs=info_infrared.o info_light.o info_pin.o info_sound.o info_ultrasound.o + +all: infrared light pin sound ultrasound + +infrared: info_infrared.o + $(CC) -c info_infrared.c + $(CC) info_infrared.o -o info_infrared -lwiringPi +light: info_light.o + $(CC) -c info_light.c + $(CC) info_light.o -o info_light -lwiringPi +pin: info_pin.o + $(CC) -c info_pin.c + $(CC) info_pin.o -o info_pin -lwiringPi +sound: info_sound.o + $(CC) -c info_sound.c + $(CC) info_sound.o -o info_sound -lwiringPi +ultrasound: info_ultrasound.o + $(CC) -c info_ultrasound.c + $(CC) info_ultrasound.o -o info_ultrasound -lwiringPi + +.PHONY : clean +clean : + rm -f $(libs) info_infrared info_light info_sound info_ultrasound diff --git a/gpio/data/infrared.data b/gpio/data/infrared.data new file mode 100644 index 0000000..573541a --- /dev/null +++ b/gpio/data/infrared.data @@ -0,0 +1 @@ +0 diff --git a/gpio/data/sound.data b/gpio/data/sound.data new file mode 100644 index 0000000..573541a --- /dev/null +++ b/gpio/data/sound.data @@ -0,0 +1 @@ +0 diff --git a/gpio/data/wind.data b/gpio/data/wind.data new file mode 100644 index 0000000..4099407 --- /dev/null +++ b/gpio/data/wind.data @@ -0,0 +1 @@ +23 diff --git a/gpio/info.sh b/gpio/info.sh new file mode 100644 index 0000000..5bf232d --- /dev/null +++ b/gpio/info.sh @@ -0,0 +1,286 @@ +#!/bin/bash +# +# reapberry zero w & pi 3. +# GPIO module +# Date: 20180118 +# Time: 17:17 +# +# 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; + + #时间 + y=$(date "+%y"); + m=$(date "+%m"); + d=$(date "+%d"); + H=$(date "+%H"); + M=$(date "+%M"); + S=$(date "+%S"); + W=$(date "+%w"); : 周几? + + start=1; + stop=0; + + #时间范围 + high="1730"; + low="2300"; + + #当前时间 + now=$(date +%H%M); + + #判断手机是否在线 + phoneip="192.168.99.60"; + phonelogic="$(ping ${phoneip} -c 1 -S 1 &> /dev/null; echo $?)"; + + #判断人体红外线传感器 + wiringpi_infrared="6"; + #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})"; + + #开灯&关灯 + wiringpi_lightpin="4"; + start_light="${bindir}/info_light ${wiringpi_lightpin} 1"; + stop_light="${bindir}/info_light ${wiringpi_lightpin} 0"; +} + #风扇 + wiringpi_wind="1"; #脚位 + l_temperature="20"; #不高于这个摄氏温度 + 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状态. + +} + +#红外线守护进程 +function infrared() +{ + while true; do + ${bindir}/info_infrared ${wiringpi_infrared} > ${bindir}/data/infrared.data; + SLEEP 1; + done +} + +#灯守护进程 +function light_daemon() +{ +while true; do + if test ${W} != "0"; then : 不是周日. + if [ $now -ge $high -a $now -le $low ]; then : 7:30 - 19:00自动检测,其他时间段不管,手动. + if [ "${phonelogic}" == "0" ]; then : phone 如果在线. + if test "${lightpinvalue}" = "0"; then : 检测灯pin值. + ${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 + else + : 还一个周日 + if test "$W" = "0"; then : 是周日. + if [ ${now} -le ${low} -a ${now} -ge 0800 ]; then : 8:00 - 23:00,自动化. + if [ "${phonelogic}" == "0" ]; then : phone 如果在线. + if test "${lightpinvalue}" = "0"; then : 检测灯pin值. + ${start_light}; + fi + fi + else + if [ "${phonelogic}" == "1" ]; then : 不在时间段就关闭,手动. + ${stop_light}; + fi + fi + fi + fi + +INIT; +SLEEP 60; +done +} + +function sound_daemon() +{ + while :; do + ${bindir}/info_sound ${wiringpi_sound} > ${bindir}/data/sound.data + SLEEP 0.3; + done +} + +function sound() +{ + : +} + +#灯主进程 +function wind() +{ +INIT + while true ; do + if [[ "${phonelogic}" == "0" ]]; then : 手机在不在. + if [[ "${temperature}" -ge "${l_temperature}" ]]; then : 温度大于37摄氏度. + echo "温度够" + if test "${status_wind}" = "0"; then + ${start_wind}; + fi + else + test "${status_wind}" = "1" && ${stop_wind}; + fi + else + test "${status_wind}" = "1" && ${stop_wind}; + fi + SLEEP 9; + INIT; + done +} + +#风扇守护进程 +function wind_daemon() +{ + while true; do + ${bindir}/wind > ${bindir}/data/wind.data; + SLEEP 60; + done +} + +#主进程 +function main_() +{ +INIT; +while getopts :ds:x ai +do +case ${ai} in + d) + daemon='&'; + ;; + s) + case $OPTARG in + "stop") + array=$(ls ${bindir}/log/) + for arr in ${array[@]}; do + kill $(cat ${bindir}/log/${arr}) &> ${null} + rm ${bindir}/log/${arr} 2> ${null} + done + exit 0 + ;; + *) + : + ;; + esac + exit 0 + ;; + x) + set -x + ;; + h|?) + echo -ne " +\e[1;31mraspberrypi zero w GPIO Modular\e[0m +$0 Usage: $0 [-?|h] [-d] [-s] [-x] [light|temperature|wind] + -d : Daemon. + -s : kill Daemon, signal: stop. + -x : Print commands and their arguments as they are executed. + + light : light Modular. + temperature : temperature Modular. + wind : Wind Modular. + +by aixiao. +" +exit 0 + ;; +esac +done + +shift $((OPTIND-1)) +#array=($(echo ${@})) +eval array=(${@}) +for i in ${array[@]}; do +case $i in + light) + eval light_daemon ${daemon} + 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 & + echo $! > ${bindir}/log/wind.pid 2> ${null} + eval wind ${daemon} + echo $! > ${bindir}/log/main.pid 2> ${null} + ;; + *) + exit 1; + ;; +esac +done +} + +main_ $@ + diff --git a/gpio/info_infrared b/gpio/info_infrared new file mode 100644 index 0000000..8c0c55e Binary files /dev/null and b/gpio/info_infrared differ diff --git a/gpio/info_infrared.c b/gpio/info_infrared.c new file mode 100644 index 0000000..410e5c8 --- /dev/null +++ b/gpio/info_infrared.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); + +} diff --git a/gpio/info_infrared.o b/gpio/info_infrared.o new file mode 100644 index 0000000..6e016e5 Binary files /dev/null and b/gpio/info_infrared.o differ diff --git a/gpio/info_light b/gpio/info_light new file mode 100644 index 0000000..b29f0fa Binary files /dev/null and b/gpio/info_light differ diff --git a/gpio/info_light.c b/gpio/info_light.c new file mode 100644 index 0000000..a0af61e --- /dev/null +++ b/gpio/info_light.c @@ -0,0 +1,31 @@ +#include +#include +#include + +/* +void pinMode (int pin, int mode) ; +void pullUpDnControl (int pin, int pud) ; +void digitalWrite (int pin, int value) ; +void pwmWrite (int pin, int value) ; +int digitalRead (int pin) ; +analogRead (int pin) ; +analogWrite (int pin, int value) ; +*/ +int main(int argc, char *argv[]) +{ + if( argc != 3) { + exit(0); + } + + int pin = atol(argv[1]); + wiringPiSetup(); + + pinMode(pin, OUTPUT); + if(atol(argv[2]) == 1) { + digitalWrite(pin, HIGH); + } + if(atol(argv[2]) == 0) { + digitalWrite(pin, LOW); + } + +} diff --git a/gpio/info_light.o b/gpio/info_light.o new file mode 100644 index 0000000..eba21b3 Binary files /dev/null and b/gpio/info_light.o differ diff --git a/gpio/info_pin b/gpio/info_pin new file mode 100644 index 0000000..bb8e05f Binary files /dev/null and b/gpio/info_pin differ diff --git a/gpio/info_pin.c b/gpio/info_pin.c new file mode 100644 index 0000000..2cd857b --- /dev/null +++ b/gpio/info_pin.c @@ -0,0 +1,24 @@ +#include +#include +#include + +/* +void pinMode (int pin, int mode) ; +void pullUpDnControl (int pin, int pud) ; +void digitalWrite (int pin, int value) ; +void pwmWrite (int pin, int value) ; +int digitalRead (int pin) ; +analogRead (int pin) ; +analogWrite (int pin, int value) ; +*/ +int main(int argc, char *argv[]) +{ + if( argc != 2) { + exit(0); + } + + int pin = atol(argv[1]); + wiringPiSetup(); + pinMode(pin, OUTPUT); + printf("%d\n", digitalRead(pin)); +} diff --git a/gpio/info_pin.o b/gpio/info_pin.o new file mode 100644 index 0000000..94dcacb Binary files /dev/null and b/gpio/info_pin.o differ diff --git a/gpio/info_sound b/gpio/info_sound new file mode 100644 index 0000000..0c0936b Binary files /dev/null and b/gpio/info_sound differ diff --git a/gpio/info_sound.c b/gpio/info_sound.c new file mode 100644 index 0000000..410e5c8 --- /dev/null +++ b/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); + +} diff --git a/gpio/info_sound.o b/gpio/info_sound.o new file mode 100644 index 0000000..b4f2be6 Binary files /dev/null and b/gpio/info_sound.o differ diff --git a/gpio/info_ultrasound b/gpio/info_ultrasound new file mode 100644 index 0000000..740f89d Binary files /dev/null and b/gpio/info_ultrasound differ diff --git a/gpio/info_ultrasound.c b/gpio/info_ultrasound.c new file mode 100644 index 0000000..e17200e --- /dev/null +++ b/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; +} diff --git a/gpio/info_ultrasound.o b/gpio/info_ultrasound.o new file mode 100644 index 0000000..56d099e Binary files /dev/null and b/gpio/info_ultrasound.o differ diff --git a/gpio/infrared.log b/gpio/infrared.log new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/gpio/infrared.log @@ -0,0 +1 @@ +1 diff --git a/gpio/log/infrared.pid b/gpio/log/infrared.pid new file mode 100644 index 0000000..e83f6f1 --- /dev/null +++ b/gpio/log/infrared.pid @@ -0,0 +1 @@ +14389 diff --git a/gpio/log/light.pid b/gpio/log/light.pid new file mode 100644 index 0000000..88b5477 --- /dev/null +++ b/gpio/log/light.pid @@ -0,0 +1 @@ +19529 diff --git a/gpio/log/main.pid b/gpio/log/main.pid new file mode 100644 index 0000000..35c7a33 --- /dev/null +++ b/gpio/log/main.pid @@ -0,0 +1 @@ +14392 diff --git a/gpio/wind b/gpio/wind new file mode 100644 index 0000000..c28e8d6 Binary files /dev/null and b/gpio/wind differ diff --git a/gpio/wind.c b/gpio/wind.c new file mode 100644 index 0000000..3461708 --- /dev/null +++ b/gpio/wind.c @@ -0,0 +1,55 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#define N 80 + +int main(int argc, char *argv[]) +{ + char path[50] = "/sys/bus/w1/devices/"; + char rom[20]; + char buffer[N+1]; + DIR *dirp; + struct dirent *direntp; + FILE *fp; + char *temp; + float value; + + system("sudo modprobe w1-gpio"); + system("sudo modprobe w1-therm"); + if((dirp = opendir(path)) == NULL) { + exit(0); + } + + while((direntp = readdir(dirp)) != NULL) { + if(strstr(direntp->d_name, "28-0")) { + strcpy(rom, direntp->d_name); + } + } + closedir(dirp); + + strcat(path, rom); + strcat(path, "/w1_slave"); + //printf("%s\n", path); + + if ((fp = fopen(path, "r")) < 0) { + exit(0); /* 文件不存在,则退出. */ + } + while(fgets(buffer, N, fp) != NULL){ + ; + } + + temp = strchr(buffer, 't'); + sscanf(temp, "t=%s", temp); + value = atof(temp) / 1000; + printf("%.0f\n", value); + + fclose(fp); + return 0; +} + diff --git a/原理图片.jpg b/原理图片.jpg new file mode 100644 index 0000000..4aa4e04 Binary files /dev/null and b/原理图片.jpg differ diff --git a/现实图片.jpg b/现实图片.jpg new file mode 100644 index 0000000..36059d9 Binary files /dev/null and b/现实图片.jpg differ