添加传感器支持

This commit is contained in:
aixiao 2020-09-10 13:23:23 +08:00
parent ad19bf1770
commit d1a28489aa
27 changed files with 915 additions and 501 deletions

View File

@ -9,9 +9,8 @@ raspberrypi zero w GPIO 实现的智能家居
- 谢谢@威威君帮写的app - 谢谢@威威君帮写的app
app配合socket服务器设置 app配合socket服务器设置
![Image text](https://github.com/niuyuling/raspberrypi/blob/master/app/Screenshot_LightControl_20180906-120133.png) <img src="https://github.com/niuyuling/raspberrypi/blob/master/app/Screenshot_LightControl_20180906-120133.png" height="1000px" width="500px" >
app主界面
app主界面 <img src="https://github.com/niuyuling/raspberrypi/blob/master/app/Screenshot_LightControl_20180906-120216.png" height="1000px" width="500px" >
![Image text](https://github.com/niuyuling/raspberrypi/blob/master/app/Screenshot_LightControl_20180906-120216.png)

Binary file not shown.

After

Width:  |  Height:  |  Size: 64 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 74 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 105 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 84 KiB

Binary file not shown.

View File

@ -1,60 +1,37 @@
CC = gcc CROSS_COMPILE ?=
RM = rm CC := $(CROSS_COMPILE)gcc
libs = info_infrared.o info_light.o info_pin.o info_sound.o info_ultrasound.o wind.o dth11.o STRIP := $(CROSS_COMPILE)strip
CFLAGS = -g -Wall -Werror CFLAGS += -g -O2 -Wall
LIB = -lwiringPi 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 infrared: infrared.o
$(CC) $(CFLAGS) info_infrared.o -o info_infrared $(LIB) $(CC) $(CFLAGS) -o infrared $^ $(LIBS)
$(RM) info_infrared.o
infrared.o:
$(CC) $(CFLAGS) -c info_infrared.c $(LIB)
light: light.o light: light.o
$(CC) $(CFLAGS) info_light.o -o info_light $(LIB) $(CC) $(CFLAGS) -o light $^ $(LIBS)
$(RM) info_light.o
light.o:
$(CC) $(CFLAGS) -c info_light.c $(LIB)
pin: pin.o pin: pin.o
$(CC) $(CFLAGS) info_pin.o -o info_pin $(LIB) $(CC) $(CFLAGS) -o pin $^ $(LIBS)
$(RM) info_pin.o
pin.o:
$(CC) $(CFLAGS) -c info_pin.c $(LIB)
sound: sound.o sound: sound.o
$(CC) $(CFLAGS) info_sound.o -o info_sound $(LIB) $(CC) $(CFLAGS) -o sound $^ $(LIBS)
$(RM) info_sound.o
sound.o:
$(CC) $(CFLAGS) -c info_sound.c $(LIB)
ultrasound: ultrasound.o ultrasound: ultrasound.o
$(CC) $(CFLAGS) info_ultrasound.o -o info_ultrasound $(LIB) $(CC) $(CFLAGS) -o ultrasound $^ $(LIBS)
$(RM) info_ultrasound.o
ultrasound.o:
$(CC) $(CFLAGS) -c info_ultrasound.c $(LIB)
wind: wind.o wind: wind.o
$(CC) $(CFLAGS) wind.o -o wind $(CC) $(CFLAGS) -o wind $^ $(LIBS)
$(RM) wind.o dht11: dht11.o
wind.o: $(CC) $(CFLAGS) -o dht11 $^ $(LIBS)
$(CC) $(CFLAGS) -c wind.c 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 clean:
$(CC) $(CFLAGS) dth11.o -o dth11 $(LIB) rm -rf *.o
$(RM) dth11.o rm $(BIN)
dth11.o:
$(CC) $(CFLAGS) -c dth11.c $(LIB)
.PHONY : clean
clean :
rm -f $(libs) info_infrared info_light info_sound info_ultrasound wind dth11

View File

@ -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"; l="60";
s="150"; s="150";
wiringpi_tring="8"; #脚位 wiringpi_tring="8"; #脚位
wiringpi_echo="9"; #脚位 wiringpi_echo="9"; #脚位
#灯 #灯
high="1700"; #起始时间 high="1700"; #起始时间
low="2300"; #关闭时间 low="2300"; #关闭时间
s_low="1900" #周日关闭时间 s_low="1900" #周日关闭时间
wiringpi_lightpin="4"; #脚位 wiringpi_lightpin="4"; #脚位
#风扇、温度传感器 #风扇、温度传感器
wiringpi_wind="1"; #脚位 wiringpi_wind="1"; #脚位
device="28-031682c7baff" #设备 device="28-031682c7baff" #设备
l_temperature="27"; #不高于这个摄氏温度 l_temperature="27"; #不高于这个摄氏温度
step_wait="300" #开启时间 step_wait="300" #开启时间
step_stop="180" #关闭时间 step_stop="180" #关闭时间
#霍尔元件
helement_pin="1";

128
gpio/dht11.c Normal file
View File

@ -0,0 +1,128 @@
/*
* DTH11 湿
*
*/
#include <wiringPi.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#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;
}

123
gpio/dht12.c Normal file
View File

@ -0,0 +1,123 @@
/*
* read temperature and humidity from DHT11 or DHT22 sensor
*
*/
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#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);
}

View File

@ -1,131 +0,0 @@
/* DTH11 温度湿度传感器
*
*
*/
#include <wiringPi.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#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 _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<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){
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;
}

59
gpio/helement.c Normal file
View File

@ -0,0 +1,59 @@
/*
*
*
*/
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
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;
}

View File

@ -47,20 +47,20 @@ function INIT()
null="/dev/null"; null="/dev/null";
bindir="/root/gpio"; bindir="/root/gpio";
! test -d ${bindir} && exit 1; ! test -d ${bindir} && exit 1;
. ${bindir}/conf/info.conf . ${bindir}/conf/info.conf
#时间 #时间
y=$(date "+%y"); y=$(date "+%y");
m=$(date "+%m"); m=$(date "+%m");
d=$(date "+%d"); d=$(date "+%d");
H=$(date "+%H"); H=$(date "+%H");
M=$(date "+%M"); M=$(date "+%M");
S=$(date "+%S"); S=$(date "+%S");
W=$(date "+%w"); : 周几? W=$(date "+%w"); : 周几?
start=1; start=1;
stop=0; stop=0;
#当前时间 #当前时间
now=$(date +%H%M); now=$(date +%H%M);
@ -70,7 +70,7 @@ function INIT()
phonelogic="$(ping ${phoneip} -c 1 -S 1 &> /dev/null; echo $?)"; phonelogic="$(ping ${phoneip} -c 1 -S 1 &> /dev/null; echo $?)";
#判断人体红外线传感器 #判断人体红外线传感器
wiringpi_infrared="6"; wiringpi_infrared="6";
#infrared=$(${bindir}/info_infrared ${wiringpi_infrared}); #infrared=$(${bindir}/info_infrared ${wiringpi_infrared});
infrared=$(cat ${bindir}/data/infrared.data 2> /dev/null); infrared=$(cat ${bindir}/data/infrared.data 2> /dev/null);
@ -90,23 +90,26 @@ function INIT()
#lightpinvalue="$(gpio -g read ${bcm_lightpin})"; #lightpinvalue="$(gpio -g read ${bcm_lightpin})";
#开灯&关灯 #开灯&关灯
#high="1700"; #时间范围 #high="1700"; #时间范围
#low="2300"; #low="2300";
#wiringpi_lightpin="4"; #wiringpi_lightpin="4";
start_light="${bindir}/info_light ${wiringpi_lightpin} 1"; start_light="${bindir}/info_light ${wiringpi_lightpin} 1";
stop_light="${bindir}/info_light ${wiringpi_lightpin} 0"; stop_light="${bindir}/info_light ${wiringpi_lightpin} 0";
} }
#风扇 #风扇
#wiringpi_wind="1"; #脚位 #wiringpi_wind="1"; #脚位
#l_temperature="28"; #不高于这个摄氏温度 #l_temperature="28"; #不高于这个摄氏温度
temperature=$(cat ${bindir}/data/wind.data 2> ${null}); #温度,摄氏度 temperature=$(cat ${bindir}/data/wind.data 2> ${null}); #温度,摄氏度
start_wind="${bindir}/info_light ${wiringpi_wind} 1"; #${wiringpi_wind} 高电压 start_wind="${bindir}/info_light ${wiringpi_wind} 1"; #${wiringpi_wind} 高电压
stop_wind="${bindir}/info_light ${wiringpi_wind} 0"; #${wiringpi_wind} 低电压 stop_wind="${bindir}/info_light ${wiringpi_wind} 0"; #${wiringpi_wind} 低电压
status_wind="$(${bindir}/info_pin ${wiringpi_wind})"; : PIN状态. status_wind="$(${bindir}/info_pin ${wiringpi_wind})"; : PIN状态.
#霍尔元件
helement_pin="1";
} }
#红外线守护进程. # 红外线守护进程.
function infrared() function infrared()
{ {
while true; do while true; do
@ -115,15 +118,15 @@ function infrared()
done done
} }
#灯主进程. # 灯主进程.
function light() function light()
{ {
INIT; INIT;
while true; do while true; do
if test ${W} != "0"; then : 不是周日. if test ${W} != "0"; then : 不是周日.
if [ $now -ge $high -a $now -le $low ]; then : 19:30 - 23:00自动检测,其他时间段不管,手动. if [ $now -ge $high -a $now -le $low ]; then : 19:30 - 23:00自动检测,其他时间段不管,手动.
if [ "${phonelogic}" == "0" ]; then : phone 如果在线. if [ "${phonelogic}" == "0" ]; then : phone 如果在线.
if test "${lightpinvalue}" = "0"; then : 检测灯pin值. if test "${lightpinvalue}" = "0"; then : 检测灯pin值.
${start_light}; ${start_light};
fi fi
else else
@ -138,13 +141,13 @@ while true; do
fi fi
fi fi
fi fi
else else
: 还一个周日 : 还一个周日
if test "$W" = "0"; then : 是周日. if test "$W" = "0"; then : 是周日.
if [ ${now} -le ${low} -a ${now} -ge ${s_low} ]; then : 8:00 - 23:00,自动化. if [ ${now} -le ${low} -a ${now} -ge ${s_low} ]; then : 8:00 - 23:00,自动化.
if [ "${phonelogic}" == "0" ]; then : phone 如果在线. if [ "${phonelogic}" == "0" ]; then : phone 如果在线.
if test "${lightpinvalue}" = "0"; then : 检测灯pin值. if test "${lightpinvalue}" = "0"; then : 检测灯pin值.
${start_light}; ${start_light};
fi fi
else else
if test "${lightpinvalue}" = "1"; then if test "${lightpinvalue}" = "1"; then
@ -153,19 +156,19 @@ while true; do
fi fi
else else
: 不在时间段就关闭,手动. : 不在时间段就关闭,手动.
if test "${lightpinvalue}" = "1"; then if test "${lightpinvalue}" = "1"; then
${stop_light}; ${stop_light};
fi fi
fi fi
fi fi
fi fi
INIT; INIT;
SLEEP 60; SLEEP 60;
done done
} }
#声音传感器守护进程. # 声音传感器守护进程.
function sound_daemon() function sound_daemon()
{ {
while :; do while :; do
@ -174,46 +177,68 @@ function sound_daemon()
done done
} }
#声音传感器主进程. # 声音传感器主进程.
function sound() function sound()
{ {
: :
} }
#风扇守护进程. # 风扇守护进程.
function wind_daemon() function wind_daemon()
{ {
while true; do while true; do
${bindir}/wind -d ${device} > ${bindir}/data/wind.data; : 温度传感器获取室内温度信息. ${bindir}/wind -d ${device} > ${bindir}/data/wind.data; : 温度传感器获取室内温度信息.
SLEEP 9; SLEEP 9;
done done
} }
#风扇主进程. # 风扇主进程.
function wind() function wind()
{ {
INIT INIT
while true ; do while true ; do
if [[ "${phonelogic}" == "0" ]]; then : 手机在不在? if [[ "${phonelogic}" == "0" ]]; then : 手机在不在?
if [[ "${temperature}" -ge "${l_temperature}" ]]; then : 温度大于等于$l_temperature摄氏度. if [[ "${temperature}" -ge "${l_temperature}" ]]; then : 温度大于等于$l_temperature摄氏度.
if test "${status_wind}" = "0"; then if test "${status_wind}" = "0"; then
${start_wind}; ${start_wind};
SLEEP $step_wait; : 开启后风扇运行5分钟. SLEEP $step_wait; : 开启后风扇运行5分钟.
${stop_wind}; ${stop_wind};
SLEEP $step_stop; : 开启后风扇停止运行1分钟,这样是怕吹感冒了. SLEEP $step_stop; : 开启后风扇停止运行1分钟,这样是怕吹感冒了.
fi fi
else else
test "${status_wind}" = "1" && ${stop_wind}; : 小于$l_temperature温度就检测或关闭. test "${status_wind}" = "1" && ${stop_wind}; : 小于$l_temperature温度就检测或关闭.
fi fi
else else
test "${status_wind}" = "1" && ${stop_wind}; : 手机不再就检测或关闭. test "${status_wind}" = "1" && ${stop_wind}; : 手机不再就检测或关闭.
fi fi
SLEEP 7; SLEEP 7;
INIT; INIT;
done 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_() function main_()
{ {
INIT; INIT;
@ -224,21 +249,21 @@ case ${ai} in
daemon='&'; daemon='&';
;; ;;
s) s)
#echo $@ #echo $@
#exit #exit
case $OPTARG in case $OPTARG in
"stop") "stop")
case $3 in case $3 in
"wind") "wind")
kill $(cat ${bindir}/log/wind_daemon.pid) 2> ${null} kill $(cat ${bindir}/log/wind_daemon.pid) 2> ${null}
kill $(cat ${bindir}/log/wind.pid) 2> ${null} kill $(cat ${bindir}/log/wind.pid) 2> ${null}
exit 1; exit 1;
;; ;;
"light") "light")
kill `cat ${bindir}/log/light.pid` 2> ${null} kill `cat ${bindir}/log/light.pid` 2> ${null}
exit 1; exit 1;
;; ;;
esac esac
array=$(ls ${bindir}/log/) array=$(ls ${bindir}/log/)
for arr in ${array[@]}; do for arr in ${array[@]}; do
kill $(cat ${bindir}/log/${arr}) &> ${null} kill $(cat ${bindir}/log/${arr}) &> ${null}
@ -246,21 +271,21 @@ case ${ai} in
done done
exit 0 exit 0
;; ;;
"reload") "reload")
case $3 in case $3 in
"wind") "wind")
kill $(cat ${bindir}/log/wind_daemon.pid) 2> ${null} kill $(cat ${bindir}/log/wind_daemon.pid) 2> ${null}
kill $(cat ${bindir}/log/wind.pid) 2> ${null} kill $(cat ${bindir}/log/wind.pid) 2> ${null}
: 重启wind : 重启wind
bash $0 -d wind bash $0 -d wind
;; ;;
"light") "light")
kill `cat ${bindir}/log/light.pid` 2> ${null} kill `cat ${bindir}/log/light.pid` 2> ${null}
: 重启light : 重启light
bash $0 -d light bash $0 -d light
;; ;;
esac esac
;; ;;
*) *)
: :
;; ;;
@ -282,8 +307,10 @@ $0 Usage: $0 [-?|h] [-d] [-s] [-x] [light|temperature|wind]
light : light Modular. light : light Modular.
temperature : temperature Modular. temperature : temperature Modular.
wind : Wind Modular. wind : Wind Modular.
helement : Hall element.
by aixiao@aixiao.me. by aixiao@aixiao.me.
" "
exit 0 exit 0
;; ;;
@ -315,6 +342,12 @@ case $i in
eval wind ${daemon} eval wind ${daemon}
echo $! > ${bindir}/log/wind.pid 2> ${null} 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; exit 1;
;; ;;

View File

@ -1,24 +0,0 @@
#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);
pullUpDnControl(pin, PUD_DOWN);
status = digitalRead(pin);
printf("%d\n", status);
return status;
}

View File

@ -1,64 +0,0 @@
/* 设置PIN脚为1或0电位(电压)
*
*
*/
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
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<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) {
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]);
}

View File

@ -1,23 +0,0 @@
/* 测试PIN脚是否1或0
*
*
*/
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
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;
}

View File

@ -1,24 +0,0 @@
#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);
pullUpDnControl(pin, PUD_DOWN);
status = digitalRead(pin);
printf("%d\n", status);
return status;
}

View File

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

57
gpio/infrared.c Normal file
View File

@ -0,0 +1,57 @@
/*
* 线
*
*/
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
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;
}

View File

@ -0,0 +1,54 @@
/*
* raspberrypi GPIO key light
* key等于1时
*/
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#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 ;
}
}
}

48
gpio/key_light.c Normal file
View File

@ -0,0 +1,48 @@
/*
* raspberrypi key light
* key等于1时
*/
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#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);
}
}

58
gpio/light.c Normal file
View File

@ -0,0 +1,58 @@
/*
* PIN脚为1或0电位()
*
*/
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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]);
}

49
gpio/pin.c Normal file
View File

@ -0,0 +1,49 @@
/*
* PIN脚是否1或0
*
*/
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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;
}

57
gpio/sound.c Normal file
View File

@ -0,0 +1,57 @@
/*
*
*
*/
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
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;
}

4
gpio/temperature.sh Normal file
View File

@ -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

77
gpio/ultrasound.c Normal file
View File

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

View File

@ -1,9 +1,8 @@
/* DS18B20 温度传感器 /*
* * DS18B20 (Debian jessie)
* *
*/ */
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
@ -12,51 +11,27 @@
#include <time.h> #include <time.h>
#include <stdlib.h> #include <stdlib.h>
#define N 99 #define BUF 270
int main(int argc, char *argv[]) int _read_ds18b20(char *device)
{
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)
{ {
char path[50] = "/sys/bus/w1/devices/"; char path[50] = "/sys/bus/w1/devices/";
char rom[20]; char rom[20];
char buffer[N+1]; char buffer[BUF + 1];
DIR *dirp; DIR *dirp;
struct dirent *direntp; struct dirent *direntp;
FILE *fp; FILE *fp;
char *temp; char *temp;
float value; float value;
//char *device; // 设备
//if(optarg == '\0') {
// strcmp(device, optarg);
//}
system("sudo modprobe w1-gpio"); system("sudo modprobe w1-gpio");
system("sudo modprobe w1-therm"); system("sudo modprobe w1-therm");
if((dirp = opendir(path)) == NULL) { if ((dirp = opendir(path)) == NULL) {
exit(0); exit(0);
} }
while((direntp = readdir(dirp)) != NULL) { while ((direntp = readdir(dirp)) != NULL) {
if(strstr(direntp->d_name, optarg)) { if (strstr(direntp->d_name, device)) {
strcpy(rom, direntp->d_name); strcpy(rom, direntp->d_name);
} }
} }
@ -67,9 +42,9 @@ int _main(char *optarg)
//printf("%s\n", path); //printf("%s\n", path);
if ((fp = fopen(path, "r")) < 0) { 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); fclose(fp);
return value; 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;
}