diff --git a/README.md b/README.md index 73c8dc9..b4e1556 100644 --- a/README.md +++ b/README.md @@ -9,9 +9,8 @@ raspberrypi zero w GPIO 实现的智能家居 - 谢谢@威威君帮写的app -app配合socket服务器设置 -![Image text](https://github.com/niuyuling/raspberrypi/blob/master/app/Screenshot_LightControl_20180906-120133.png) +app配合socket服务器设置 + - -app主界面 -![Image text](https://github.com/niuyuling/raspberrypi/blob/master/app/Screenshot_LightControl_20180906-120216.png) \ No newline at end of file +app主界面 + \ No newline at end of file diff --git a/app/Screenshot_20200910-131104_LightControl.png b/app/Screenshot_20200910-131104_LightControl.png new file mode 100644 index 0000000..ea6c4fe Binary files /dev/null and b/app/Screenshot_20200910-131104_LightControl.png differ diff --git a/app/Screenshot_20200910-131834_LightControl.png b/app/Screenshot_20200910-131834_LightControl.png new file mode 100644 index 0000000..116f877 Binary files /dev/null and b/app/Screenshot_20200910-131834_LightControl.png differ diff --git a/app/Screenshot_LightControl_20180906-120133.png b/app/Screenshot_LightControl_20180906-120133.png deleted file mode 100644 index 9853922..0000000 Binary files a/app/Screenshot_LightControl_20180906-120133.png and /dev/null differ diff --git a/app/Screenshot_LightControl_20180906-120216.png b/app/Screenshot_LightControl_20180906-120216.png deleted file mode 100644 index 2352f58..0000000 Binary files a/app/Screenshot_LightControl_20180906-120216.png and /dev/null differ diff --git a/app/lightcontrol.apk b/app/lightcontrol.apk index e25c224..970ae63 100644 Binary files a/app/lightcontrol.apk and b/app/lightcontrol.apk differ diff --git a/gpio/Makefile b/gpio/Makefile index bd5313a..41c1cfe 100644 --- a/gpio/Makefile +++ b/gpio/Makefile @@ -1,60 +1,37 @@ -CC = gcc -RM = rm -libs = info_infrared.o info_light.o info_pin.o info_sound.o info_ultrasound.o wind.o dth11.o -CFLAGS = -g -Wall -Werror -LIB = -lwiringPi +CROSS_COMPILE ?= +CC := $(CROSS_COMPILE)gcc +STRIP := $(CROSS_COMPILE)strip +CFLAGS += -g -O2 -Wall +LIBS = -lwiringPi +BIN = infrared light pin sound ultrasound wind dht11 dht12 helement key_light -all: infrared light pin sound ultrasound wind dth11 +all: $(BIN) infrared: infrared.o - $(CC) $(CFLAGS) info_infrared.o -o info_infrared $(LIB) - $(RM) info_infrared.o -infrared.o: - $(CC) $(CFLAGS) -c info_infrared.c $(LIB) - - + $(CC) $(CFLAGS) -o infrared $^ $(LIBS) light: light.o - $(CC) $(CFLAGS) info_light.o -o info_light $(LIB) - $(RM) info_light.o -light.o: - $(CC) $(CFLAGS) -c info_light.c $(LIB) - - + $(CC) $(CFLAGS) -o light $^ $(LIBS) pin: pin.o - $(CC) $(CFLAGS) info_pin.o -o info_pin $(LIB) - $(RM) info_pin.o -pin.o: - $(CC) $(CFLAGS) -c info_pin.c $(LIB) - - + $(CC) $(CFLAGS) -o pin $^ $(LIBS) sound: sound.o - $(CC) $(CFLAGS) info_sound.o -o info_sound $(LIB) - $(RM) info_sound.o -sound.o: - $(CC) $(CFLAGS) -c info_sound.c $(LIB) - - + $(CC) $(CFLAGS) -o sound $^ $(LIBS) ultrasound: ultrasound.o - $(CC) $(CFLAGS) info_ultrasound.o -o info_ultrasound $(LIB) - $(RM) info_ultrasound.o -ultrasound.o: - $(CC) $(CFLAGS) -c info_ultrasound.c $(LIB) - - + $(CC) $(CFLAGS) -o ultrasound $^ $(LIBS) wind: wind.o - $(CC) $(CFLAGS) wind.o -o wind - $(RM) wind.o -wind.o: - $(CC) $(CFLAGS) -c wind.c + $(CC) $(CFLAGS) -o wind $^ $(LIBS) +dht11: dht11.o + $(CC) $(CFLAGS) -o dht11 $^ $(LIBS) +dht12: dht12.o + $(CC) $(CFLAGS) -o dht12 $^ $(LIBS) +helement: helement.o + $(CC) $(CFLAGS) -o helement $^ $(LIBS) +key_light: key_light.o + $(CC) $(CFLAGS) -o key_light $^ $(LIBS) +.c.o: + $(CC) $(CFLAGS) -c $< $(LIBS) -dth11: dth11.o - $(CC) $(CFLAGS) dth11.o -o dth11 $(LIB) - $(RM) dth11.o -dth11.o: - $(CC) $(CFLAGS) -c dth11.c $(LIB) +clean: + rm -rf *.o + rm $(BIN) - -.PHONY : clean -clean : - rm -f $(libs) info_infrared info_light info_sound info_ultrasound wind dth11 diff --git a/gpio/conf/info.conf b/gpio/conf/info.conf index e5b192e..636c91c 100644 --- a/gpio/conf/info.conf +++ b/gpio/conf/info.conf @@ -1,28 +1,31 @@ #判断手机是否在线 -phoneip="192.168.99.60"; #IP +phoneip="192.168.99.60"; #IP #人体红外线传感器 -wiringpi_infrared="6"; #脚位 +wiringpi_infrared="6"; #脚位 #声音传感器 -wiringpi_sound="25"; #脚位 +wiringpi_sound="25"; #脚位 #距离传感器 l="60"; -s="150"; -wiringpi_tring="8"; #脚位 -wiringpi_echo="9"; #脚位 +s="150"; +wiringpi_tring="8"; #脚位 +wiringpi_echo="9"; #脚位 #灯 -high="1700"; #起始时间 -low="2300"; #关闭时间 -s_low="1900" #周日关闭时间 -wiringpi_lightpin="4"; #脚位 +high="1700"; #起始时间 +low="2300"; #关闭时间 +s_low="1900" #周日关闭时间 +wiringpi_lightpin="4"; #脚位 #风扇、温度传感器 -wiringpi_wind="1"; #脚位 -device="28-031682c7baff" #设备 -l_temperature="27"; #不高于这个摄氏温度 -step_wait="300" #开启时间 -step_stop="180" #关闭时间 +wiringpi_wind="1"; #脚位 +device="28-031682c7baff" #设备 +l_temperature="27"; #不高于这个摄氏温度 +step_wait="300" #开启时间 +step_stop="180" #关闭时间 + +#霍尔元件 +helement_pin="1"; diff --git a/gpio/dht11.c b/gpio/dht11.c new file mode 100644 index 0000000..e7c8d38 --- /dev/null +++ b/gpio/dht11.c @@ -0,0 +1,128 @@ +/* + * DTH11 温度湿度传感器 + * + */ + +#include +#include +#include +#include +#include +#include +#define MAX_TIME 85 +#define MAX_TRIES 100 + +int dht11_val[5] = { 0, 0, 0, 0, 0 }; + +int dht11_read_val(int *h, int *t, int pin) +{ + uint8_t lststate = HIGH; + uint8_t counter = 0; + uint8_t j = 0, i; + + for (i = 0; i < 5; i++) { + dht11_val[i] = 0; + } + + pinMode(pin, OUTPUT); + digitalWrite(pin, LOW); + delay(18); + digitalWrite(pin, HIGH); + delayMicroseconds(40); + pinMode(pin, INPUT); + + for (i = 0; i < MAX_TIME; i++) { + counter = 0; + while (digitalRead(pin) == lststate) { + counter++; + delayMicroseconds(1); + if (counter == 255) + break; + } + lststate = digitalRead(pin); + if (counter == 255) + break; + // top 3 transitions are ignored + if ((i >= 4) && (i % 2 == 0)) { + dht11_val[j / 8] <<= 1; + if (counter > 16) + dht11_val[j / 8] |= 1; + j++; + } + } + + // verify cheksum and print the verified data + if ((j >= 40) + && (dht11_val[4] == + ((dht11_val[0] + dht11_val[1] + dht11_val[2] + + dht11_val[3]) & 0xFF))) { + // Only return the integer part of humidity and temperature. The sensor + // is not accurate enough for decimals anyway + *h = dht11_val[0]; + *t = dht11_val[2]; + return 0; + } else { + // invalid data + return 1; + } +} + +int is_num(int n, char *argv[]) +{ + int j = 0; + int i; + char parameter[10]; + strcpy(parameter, argv[n]); + int len = strlen(parameter); + + for (i = 0; i < len; i++) { + if (parameter[i] <= 57 && parameter[i] >= 48) //0~9的ASCII码是48~57 + { + j++; + } + } + if (j == len) { + return 1; + } else { + return 0; + } +} + +int main(int argc, char *argv[]) +{ + if (argc != 2) { + printf("Parameter error.\n"); + exit(1); + } + if (is_num(1, argv) == 0) { + printf("Parameter error, parameter 1 is PIN pin value\n"); + exit(1); + } + + int pin = atol(argv[1]); + + int h; //humidity + int t; //temperature in degrees Celsius + + wiringPiSetup(); + + // read the sensor until we get a pair of valid measurements + // but bail out if we tried too many times + int retval = 1; + int tries = 0; + while (retval != 0 && tries < MAX_TRIES) { + retval = dht11_read_val(&h, &t, pin); + if (retval == 0) { + //printf("%d %d\n", h, t); + printf("Humidity = %d Temperature = %d ℃ ( %f ℉ )\n", h, t, ((t*1.8)+32)); + } else { + delay(3000); + } + tries += 1; + } + if (tries < MAX_TRIES) + return 0; + else + return 1; + +} diff --git a/gpio/dht12.c b/gpio/dht12.c new file mode 100644 index 0000000..6b39fb6 --- /dev/null +++ b/gpio/dht12.c @@ -0,0 +1,123 @@ +/* + * read temperature and humidity from DHT11 or DHT22 sensor + * + */ + +#include +#include +#include +#include +#include + +#define MAX_TIMINGS 85 + +int data[5] = { 0, 0, 0, 0, 0 }; + +int read_dht_data(float *h, float *c, float *f, int PIN) +{ + uint8_t laststate = HIGH; + uint8_t counter = 0; + uint8_t j = 0, i; + + data[0] = data[1] = data[2] = data[3] = data[4] = 0; + + /* pull pin down for 18 milliseconds */ + pinMode(PIN, OUTPUT); + digitalWrite(PIN, LOW); + delay(18); + + /* prepare to read the pin */ + pinMode(PIN, INPUT); + + /* detect change and read data */ + for (i = 0; i < MAX_TIMINGS; i++) { + counter = 0; + while (digitalRead(PIN) == laststate) { + counter++; + delayMicroseconds(1); + if (counter == 255) { + break; + } + } + laststate = digitalRead(PIN); + + if (counter == 255) + break; + + /* ignore first 3 transitions */ + if ((i >= 4) && (i % 2 == 0)) { + /* shove each bit into the storage bytes */ + data[j / 8] <<= 1; + if (counter > 16) + data[j / 8] |= 1; + j++; + } + } + + /* + * check we read 40 bits (8bit x 5 ) + verify checksum in the last byte + * print it out if data is good + */ + if ((j >= 40) && + (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF))) { + *h = (float)((data[0] << 8) + data[1]) / 10; + if (*h > 100) { + *h = data[0]; // for DHT11 + } + *c = (float)(((data[2] & 0x7F) << 8) + data[3]) / 10; + if (*c > 125) { + *c = data[2]; // for DHT11 + } + if (data[2] & 0x80) { + *c = -(*c); + } + *f = *c * 1.8f + 32; + return 0; + } else { + return 1; + } +} + +int is_num(int n, char *argv[]) +{ + int j = 0; + int i; + char parameter[10]; + strcpy(parameter, argv[n]); + int len = strlen(parameter); + + for (i = 0; i < len; i++) { + if (parameter[i] <= 57 && parameter[i] >= 48) //0~9的ASCII码是48~57 + { + j++; + } + } + if (j == len) { + return 1; + } else { + return 0; + } +} + +int main(int argc, char *argv[]) +{ + if (argc != 2) { + printf("Parameter error.\n"); + exit(1); + } + if (is_num(1, argv) == 0) { + printf("Parameter error, parameter 1 is PIN pin value\n"); + exit(1); + } + + if (wiringPiSetup() == -1) + exit(1); + int PIN = atol(argv[1]); + float h; + float c; + float f; + read_dht_data(&h, &c, &f, PIN); + printf("Humidity = %.1f Temperature = %.1f ℃ ( %.1f ℉ )\n", h, c, f); + + return (0); +} diff --git a/gpio/dth11.c b/gpio/dth11.c deleted file mode 100644 index 2ac0eb9..0000000 --- a/gpio/dth11.c +++ /dev/null @@ -1,131 +0,0 @@ -/* DTH11 温度湿度传感器 - * - * - */ - - -#include -#include -#include -#include -#include -#include -#define MAX_TIME 85 -#define MAX_TRIES 100 - -int dht11_val[5]={0,0,0,0,0}; - -int dht11_read_val(int *h, int *t, int pin) { - uint8_t lststate=HIGH; - uint8_t counter=0; - uint8_t j=0,i; - - for (i=0;i<5;i++) { - dht11_val[i]=0; - } - - pinMode(pin, OUTPUT); - digitalWrite(pin, LOW); - delay(18); - digitalWrite(pin, HIGH); - delayMicroseconds(40); - pinMode(pin, INPUT); - - for (i=0;i=4) && (i%2==0)) { - dht11_val[j/8]<<=1; - if(counter>16) - dht11_val[j/8]|=1; - j++; - } - } - - // verify cheksum and print the verified data - if ((j>=40) && (dht11_val[4]==((dht11_val[0]+dht11_val[1]+dht11_val[2]+dht11_val[3])& 0xFF))) { - // Only return the integer part of humidity and temperature. The sensor - // is not accurate enough for decimals anyway - *h = dht11_val[0]; - *t = dht11_val[2]; - return 0; - } - else { - // invalid data - return 1; - } -} - -int _main(int n, char *argv[]) -{ - int j = 0; - int i; - char parameter[10]; - strcpy(parameter, argv[n]); - int len = strlen(parameter); - - for(i=0; i=48) //0~9的ASCII码是48~57 - {j++;} - } - if(j==len){ - return 1; - } else { - return 0; - } -} - -int main(int argc, char *argv[]) { - if(argc != 2){ - exit(1); - } - if(_main(1, argv) == 0) { - printf("参数错误, 参数1为PIN引脚值\n"); - exit(1); - } - - int pin = atol(argv[1]); - - int i; - int h; //humidity - int t; //temperature in degrees Celsius - - - wiringPiSetup(); - - // throw away the first 3 measurements - for (i=0; i<3; i++) { - dht11_read_val(&h, &t, pin); - delay(3000); - } - - // read the sensor until we get a pair of valid measurements - // but bail out if we tried too many times - int retval=1; - int tries=0; - while (retval != 0 && tries < MAX_TRIES) - { - retval = dht11_read_val(&h, &t, pin); - if (retval == 0) { - printf("%d %d\n", h, t); - } else { - delay(3000); - } - tries += 1; - } - if (tries < MAX_TRIES) - return 0; - else - return 1; - -} \ No newline at end of file diff --git a/gpio/helement.c b/gpio/helement.c new file mode 100644 index 0000000..e3f4e08 --- /dev/null +++ b/gpio/helement.c @@ -0,0 +1,59 @@ +/* + * 霍尔元件 + * + */ + +#include +#include +#include +#include +#include + +int is_num(int n, char *argv[]) +{ + int j = 0; + int i; + char parameter[10]; + strcpy(parameter, argv[n]); + int len = strlen(parameter); + + for (i = 0; i < len; i++) { + if (parameter[i] <= 57 && parameter[i] >= 48) //0~9的ASCII码是48~57 + { + j++; + } + } + if (j == len) { + return 1; + } else { + return 0; + } +} + +int main(int argc, char *argv[]) +{ + + if (argc != 2) { + printf("Parameter error.\n"); + exit(1); + } + if (is_num(1, argv) == 0) { + printf("Parameter error, parameter 1 is PIN pin value\n"); + exit(1); + } + + int pin = atol(argv[1]); + int status = 0; + + wiringPiSetup(); + + pinMode(pin, INPUT); + + //pullUpDnControl(pin, PUD_DOWN); + + status = digitalRead(pin); + + printf("%d\n", status); + + return status; +} diff --git a/gpio/info.sh b/gpio/info.sh index 8832101..2fd0d54 100644 --- a/gpio/info.sh +++ b/gpio/info.sh @@ -47,20 +47,20 @@ function INIT() null="/dev/null"; bindir="/root/gpio"; ! test -d ${bindir} && exit 1; - - . ${bindir}/conf/info.conf - - #时间 - y=$(date "+%y"); + + . ${bindir}/conf/info.conf + + #时间 + 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; + W=$(date "+%w"); : 周几? + + start=1; + stop=0; #当前时间 now=$(date +%H%M); @@ -70,7 +70,7 @@ function INIT() phonelogic="$(ping ${phoneip} -c 1 -S 1 &> /dev/null; echo $?)"; #判断人体红外线传感器 - wiringpi_infrared="6"; + wiringpi_infrared="6"; #infrared=$(${bindir}/info_infrared ${wiringpi_infrared}); infrared=$(cat ${bindir}/data/infrared.data 2> /dev/null); @@ -90,23 +90,26 @@ function INIT() #lightpinvalue="$(gpio -g read ${bcm_lightpin})"; #开灯&关灯 - #high="1700"; #时间范围 + #high="1700"; #时间范围 #low="2300"; - #wiringpi_lightpin="4"; + #wiringpi_lightpin="4"; start_light="${bindir}/info_light ${wiringpi_lightpin} 1"; stop_light="${bindir}/info_light ${wiringpi_lightpin} 0"; } - #风扇 - #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状态. - + #风扇 + #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"; + } -#红外线守护进程. +# 红外线守护进程. function infrared() { while true; do @@ -115,15 +118,15 @@ function infrared() done } -#灯主进程. +# 灯主进程. function light() { INIT; while true; do if test ${W} != "0"; then : 不是周日. - if [ $now -ge $high -a $now -le $low ]; then : 19:30 - 23:00自动检测,其他时间段不管,手动. - if [ "${phonelogic}" == "0" ]; then : phone 如果在线. - if test "${lightpinvalue}" = "0"; then : 检测灯pin值. + if [ $now -ge $high -a $now -le $low ]; then : 19:30 - 23:00自动检测,其他时间段不管,手动. + if [ "${phonelogic}" == "0" ]; then : phone 如果在线. + if test "${lightpinvalue}" = "0"; then : 检测灯pin值. ${start_light}; fi else @@ -138,13 +141,13 @@ while true; do fi fi fi - else + else : 还一个周日 - 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}; + 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}; fi else if test "${lightpinvalue}" = "1"; then @@ -153,19 +156,19 @@ while true; do fi else : 不在时间段就关闭,手动. - if test "${lightpinvalue}" = "1"; then - ${stop_light}; - fi + if test "${lightpinvalue}" = "1"; then + ${stop_light}; + fi fi - fi - fi + fi + fi INIT; SLEEP 60; done } -#声音传感器守护进程. +# 声音传感器守护进程. function sound_daemon() { while :; do @@ -174,46 +177,68 @@ function sound_daemon() done } -#声音传感器主进程. +# 声音传感器主进程. function sound() { : } -#风扇守护进程. +# 风扇守护进程. function wind_daemon() { - while true; do - ${bindir}/wind -d ${device} > ${bindir}/data/wind.data; : 温度传感器获取室内温度信息. - SLEEP 9; - done + while true; do + ${bindir}/wind -d ${device} > ${bindir}/data/wind.data; : 温度传感器获取室内温度信息. + SLEEP 9; + done } -#风扇主进程. +# 风扇主进程. function wind() { INIT while true ; do - 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; + 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; done } -#主进程 +# 霍尔元件守护进程 +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 +} + + +# 主进程 function main_() { INIT; @@ -224,21 +249,21 @@ case ${ai} in daemon='&'; ;; s) - #echo $@ - #exit + #echo $@ + #exit case $OPTARG in "stop") - 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 + 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 array=$(ls ${bindir}/log/) for arr in ${array[@]}; do kill $(cat ${bindir}/log/${arr}) &> ${null} @@ -246,21 +271,21 @@ case ${ai} in done exit 0 ;; - "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 - ;; + "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 + ;; *) : ;; @@ -282,8 +307,10 @@ $0 Usage: $0 [-?|h] [-d] [-s] [-x] [light|temperature|wind] light : light Modular. temperature : temperature Modular. wind : Wind Modular. + helement : Hall element. by aixiao@aixiao.me. + " exit 0 ;; @@ -315,6 +342,12 @@ case $i in eval wind ${daemon} echo $! > ${bindir}/log/wind.pid 2> ${null} ;; + helement) + helement_daemon & + echo $! > ${bindir}/log/helement_daemon.pid 2> ${null}; + eval helement ${daemon} + echo $! > ${bindir}/log/helement.pid 2> ${null} + ;; *) exit 1; ;; diff --git a/gpio/info_infrared.c b/gpio/info_infrared.c deleted file mode 100644 index 0dd8754..0000000 --- a/gpio/info_infrared.c +++ /dev/null @@ -1,24 +0,0 @@ -#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); - - return status; -} diff --git a/gpio/info_light.c b/gpio/info_light.c deleted file mode 100644 index 1186622..0000000 --- a/gpio/info_light.c +++ /dev/null @@ -1,64 +0,0 @@ -/* 设置PIN脚为1或0电位(电压) - * - * - */ - - -#include -#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(char *argv[], int n) -{ - int j = 0; - int i; - char parameter[10]; - strcpy(parameter, argv[n]); - int len = strlen(parameter); - - for(i=0; i=48) //0~9的ASCII码是48~57 - {j++;} - } - if(j==len){ - return 1; - } else { - return 0; - } -} - -int main(int argc, char *argv[]) -{ - if( argc != 3) { - exit(0); - } - if(_main(argv, 1) == 0) { - printf("参数错误, 参数1为PIN引脚值\n"); - exit(1); - } - - 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); - } - return atol(argv[2]); -} - diff --git a/gpio/info_pin.c b/gpio/info_pin.c deleted file mode 100644 index 840123e..0000000 --- a/gpio/info_pin.c +++ /dev/null @@ -1,23 +0,0 @@ -/* 测试PIN脚是否1或0 - * - * - */ - - -#include -#include -#include - -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)); - - return pin; -} diff --git a/gpio/info_sound.c b/gpio/info_sound.c deleted file mode 100644 index 7b2c8e8..0000000 --- a/gpio/info_sound.c +++ /dev/null @@ -1,24 +0,0 @@ -#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); - - return status; -} diff --git a/gpio/info_ultrasound.c b/gpio/info_ultrasound.c deleted file mode 100644 index 60ce7b2..0000000 --- a/gpio/info_ultrasound.c +++ /dev/null @@ -1,41 +0,0 @@ -#include -#include -#include -#include - -int main(int argc, char * argv[]) -{ - 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/infrared.c b/gpio/infrared.c new file mode 100644 index 0000000..7bc5f79 --- /dev/null +++ b/gpio/infrared.c @@ -0,0 +1,57 @@ +/* + * 人体红外线传感器 + * + */ + +#include +#include +#include +#include +#include + +int is_num(int n, char *argv[]) +{ + int j = 0; + int i; + char parameter[10]; + strcpy(parameter, argv[n]); + int len = strlen(parameter); + + for (i = 0; i < len; i++) { + if (parameter[i] <= 57 && parameter[i] >= 48) //0~9的ASCII码是48~57 + { + j++; + } + } + if (j == len) { + return 1; + } else { + return 0; + } +} + +int main(int argc, char *argv[]) +{ + if (argc != 2) { + printf("Parameter error.\n"); + exit(1); + } + if (is_num(1, argv) == 0) { + printf("Parameter error, parameter 1 is PIN pin value\n"); + exit(1); + } + + int pin = atol(argv[1]); + int status = 0; + + wiringPiSetup(); + + pinMode(pin, INPUT); + + pullUpDnControl(pin, PUD_DOWN); + + status = digitalRead(pin); + printf("%d\n", status); + + return status; +} diff --git a/gpio/key_light-interrupt.c b/gpio/key_light-interrupt.c new file mode 100644 index 0000000..ecbbe32 --- /dev/null +++ b/gpio/key_light-interrupt.c @@ -0,0 +1,54 @@ +/* + * raspberrypi GPIO key light + * 功能:key等于1时,亮的灯灭、灭的灯亮 + */ + +#include +#include +#include +#include +#include + +#define LIGHT 3 // 灯PIN引脚 +#define KEY 4 // 按键PIN引脚 + +void buttonpressed(void); + +int main() +{ + if (-1 == wiringPiSetup()) { + exit(-1); + } + daemon(1, 1); + pinMode(LIGHT, OUTPUT); // 灯PIN输出模式 + pinMode(KEY, INPUT); // 按键PIN输入模式 + pullUpDnControl(KEY, PUD_DOWN); // 按键启用下拉电阻,引脚电平拉到GND + + //注册中断处理函数, INT_EDGE_RISING是高电平触发 + if (0 > wiringPiISR(KEY, INT_EDGE_RISING, buttonpressed)) { + exit(-1); + } + + while (1) ; + + return 0; +} + +//中断处理函数:反转LED的电平 +void buttonpressed(void) +{ + //printf("%d\n", digitalRead(KEY)); + delay(250); // 模拟人手按下开关的时间 + int light_status = digitalRead(LIGHT); // 灯状态 + int key_status = digitalRead(KEY); // 按键状态 + if (key_status == 1) { + if (light_status == 0) { + digitalWrite(LIGHT, HIGH); // 开灯 + return ; + } + if (light_status == 1) { + digitalWrite(LIGHT, LOW); // 关灯 + return ; + } + } +} diff --git a/gpio/key_light.c b/gpio/key_light.c new file mode 100644 index 0000000..c8cb435 --- /dev/null +++ b/gpio/key_light.c @@ -0,0 +1,48 @@ +/* + * raspberrypi key light + * 功能:key等于1时,亮的灯灭、灭的灯亮 + */ + +#include +#include +#include +#include +#include + +#define LIGHT 3 // 灯PIN引脚 +#define KEY 4 // 按键PIN引脚 + +int main(int argc, char **argv) { + + daemon(1, 1); + + if(-1 == wiringPiSetup()) + { + printf("wiringPi setup error\n"); + exit(-1); + } + + pinMode(KEY, INPUT); // 按键输入模式 + pullUpDnControl(KEY, PUD_DOWN); // 按键启用下拉电阻,引脚电平拉到GND + pinMode(LIGHT, OUTPUT); // 灯PIN引脚输出模式 + + int light_status; + int key_status; + + + while(1){ + light_status = digitalRead(LIGHT); // 灯状态 + key_status = digitalRead(KEY); // 按键状态 + if (key_status == 1) { + if (light_status == 0) { + digitalWrite(LIGHT, HIGH); // 开灯 + } + if (light_status == 1) { + digitalWrite(LIGHT, LOW); // 关灯 + } + } + printf("%d\n", digitalRead(KEY)); + delay(200); + + } +} \ No newline at end of file diff --git a/gpio/light.c b/gpio/light.c new file mode 100644 index 0000000..8edcedd --- /dev/null +++ b/gpio/light.c @@ -0,0 +1,58 @@ +/* + * 设置PIN脚为1或0电位(电压) + * + */ + +#include +#include +#include +#include + +int is_num(int n, char *argv[]) +{ + int j = 0; + int i; + char parameter[10]; + strcpy(parameter, argv[n]); + int len = strlen(parameter); + + for (i = 0; i < len; i++) { + if (parameter[i] <= 57 && parameter[i] >= 48) //0~9的ASCII码是48~57 + { + j++; + } + } + if (j == len) { + return 1; + } else { + return 0; + } +} + +int main(int argc, char *argv[]) +{ + if (argc != 3) { + printf("Parameter error.\n"); + exit(1); + } + if (is_num(1, argv) == 0) { + printf("Parameter error, parameter 1 is PIN pin value\n"); + exit(1); + } + if (is_num(2, argv) == 0) { + printf("Parameter error, parameter 2 is true or false\n"); + exit(1); + } + + 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); + } + return atol(argv[2]); +} diff --git a/gpio/pin.c b/gpio/pin.c new file mode 100644 index 0000000..d832274 --- /dev/null +++ b/gpio/pin.c @@ -0,0 +1,49 @@ +/* + * 测试PIN脚是否1或0 + * + */ + +#include +#include +#include +#include + +int is_num(int n, char *argv[]) +{ + int j = 0; + int i; + char parameter[10]; + strcpy(parameter, argv[n]); + int len = strlen(parameter); + + for (i = 0; i < len; i++) { + if (parameter[i] <= 57 && parameter[i] >= 48) //0~9的ASCII码是48~57 + { + j++; + } + } + if (j == len) { + return 1; + } else { + return 0; + } +} + +int main(int argc, char *argv[]) +{ + if (argc != 2) { + printf("Parameter error.\n"); + exit(1); + } + if (is_num(1, argv) == 0) { + printf("Parameter error, parameter 1 is PIN pin value\n"); + exit(1); + } + + int pin = atol(argv[1]); + wiringPiSetup(); + pinMode(pin, OUTPUT); + printf("%d\n", digitalRead(pin)); + + return pin; +} diff --git a/gpio/sound.c b/gpio/sound.c new file mode 100644 index 0000000..81fda33 --- /dev/null +++ b/gpio/sound.c @@ -0,0 +1,57 @@ +/* + * 声音传感器 + * + */ + +#include +#include +#include +#include +#include + +int is_num(int n, char *argv[]) +{ + int j = 0; + int i; + char parameter[10]; + strcpy(parameter, argv[n]); + int len = strlen(parameter); + + for (i = 0; i < len; i++) { + if (parameter[i] <= 57 && parameter[i] >= 48) //0~9的ASCII码是48~57 + { + j++; + } + } + if (j == len) { + return 1; + } else { + return 0; + } +} + +int main(int argc, char *argv[]) +{ + if (argc != 2) { + printf("Parameter error.\n"); + exit(1); + } + if (is_num(1, argv) == 0) { + printf("Parameter error, parameter 1 is PIN pin value\n"); + exit(1); + } + + int pin = atol(argv[1]); + int status = 0; + + wiringPiSetup(); + + pinMode(pin, INPUT); + + //pullUpDnControl(pin, PUD_DOWN); + + status = digitalRead(pin); + printf("%d\n", status); + + return status; +} diff --git a/gpio/temperature.sh b/gpio/temperature.sh new file mode 100644 index 0000000..1c4ba2a --- /dev/null +++ b/gpio/temperature.sh @@ -0,0 +1,4 @@ +#!/bin/bash +DATE=$(date "+%y年%m月%d日%H时%M分%S秒"); +T="$(/root/gpio/wind -d 28-031682c7baff)" +echo "${DATE} ${T}" >> /root/temperature.data diff --git a/gpio/ultrasound.c b/gpio/ultrasound.c new file mode 100644 index 0000000..2ee8014 --- /dev/null +++ b/gpio/ultrasound.c @@ -0,0 +1,77 @@ +/* + * 超声波距离传感器 + * + */ + +#include +#include +#include +#include +#include + +int is_num(int n, char *argv[]) +{ + int j = 0; + int i; + char parameter[10]; + strcpy(parameter, argv[n]); + int len = strlen(parameter); + + for (i = 0; i < len; i++) { + if (parameter[i] <= 57 && parameter[i] >= 48) //0~9的ASCII码是48~57 + { + j++; + } + } + if (j == len) { + return 1; + } else { + return 0; + } +} + +int main(int argc, char *argv[]) +{ + if (argc != 3) { + printf("Parameter error.\n"); + exit(1); + } + if (is_num(1, argv) == 0) { + printf("Parameter error, Parameter 1 is the tring pin\n"); + exit(1); + } + if (is_num(2, argv) == 0) { + printf("Parameter error, Parameter 2 is the echo pin\n"); + exit(1); + } + + 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/wind.c b/gpio/wind.c index 6974705..0004c8a 100644 --- a/gpio/wind.c +++ b/gpio/wind.c @@ -1,9 +1,8 @@ -/* DS18B20 温度传感器 - * +/* + * DS18B20 温度传感器(Debian jessie) * */ - #include #include #include @@ -12,51 +11,27 @@ #include #include -#define N 99 +#define BUF 270 -int main(int argc, char *argv[]) -{ - int _main(char *optarg); - - int ch; - opterr = 0; - while((ch = getopt(argc, argv, "d:h?")) != -1) - switch (ch) { - case 'd': - _main(optarg); - break; - case 'h': case '?': - printf("wind -d device\n"); - exit(0); - break; - default: - ; - } - return 1; -} - -int _main(char *optarg) +int _read_ds18b20(char *device) { char path[50] = "/sys/bus/w1/devices/"; char rom[20]; - char buffer[N+1]; + char buffer[BUF + 1]; DIR *dirp; struct dirent *direntp; FILE *fp; char *temp; float value; - //char *device; // 设备 - //if(optarg == '\0') { - // strcmp(device, optarg); - //} + system("sudo modprobe w1-gpio"); system("sudo modprobe w1-therm"); - if((dirp = opendir(path)) == NULL) { + if ((dirp = opendir(path)) == NULL) { exit(0); } - while((direntp = readdir(dirp)) != NULL) { - if(strstr(direntp->d_name, optarg)) { + while ((direntp = readdir(dirp)) != NULL) { + if (strstr(direntp->d_name, device)) { strcpy(rom, direntp->d_name); } } @@ -67,9 +42,9 @@ int _main(char *optarg) //printf("%s\n", path); if ((fp = fopen(path, "r")) < 0) { - exit(0); /* 文件不存在,则退出. */ + exit(0); /* 文件不存在,则退出. */ } - while(fgets(buffer, N, fp) != NULL){ + while (fgets(buffer, BUF, fp) != NULL) { ; } @@ -81,3 +56,23 @@ int _main(char *optarg) fclose(fp); return value; } + +int main(int argc, char *argv[]) +{ + int ch; + opterr = 0; + while ((ch = getopt(argc, argv, "d:h?")) != -1) + switch (ch) { + case 'd': + _read_ds18b20(optarg); + break; + case 'h': case '?': + printf("wind -d device\n"); + exit(0); + break; + default: + ; + } + return 1; +} +