增加 dth11 温度湿度传感器
目前工作的有DS18B20温度传感、DTH11温度湿度传感器
未测试的有info_sound、info_ultrasound、info_infrared二进制程序

	修改:     gpio/Makefile
	新文件:   gpio/conf/info.conf
	新文件:   gpio/dth11.c
	删除:     gpio/info_infrared
	修改:     gpio/info_infrared.c
	删除:     gpio/info_light
	修改:     gpio/info_light.c
	删除:     gpio/info_pin
	修改:     gpio/info_pin.c
	删除:     gpio/info_sound
	修改:     gpio/info_sound.c
	删除:     gpio/info_ultrasound
	修改:     gpio/info_ultrasound.c
	修改:     gpio/log/light.pid
	修改:     gpio/log/wind.pid
	新文件:   gpio/log/wind_daemon.pid
	删除:     gpio/wind
	修改:     gpio/wind.c
This commit is contained in:
aixiao 2018-09-11 14:35:16 +00:00
parent 3ee8e95084
commit 084f378a18
18 changed files with 278 additions and 39 deletions

View File

@ -1,21 +1,60 @@
CC=gcc CC = gcc
libs=info_infrared.o info_light.o info_pin.o info_sound.o info_ultrasound.o 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
all: infrared light pin sound ultrasound wind_ all: infrared light pin sound ultrasound wind dth11
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)
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)
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)
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)
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)
wind: wind.o
$(CC) $(CFLAGS) wind.o -o wind
$(RM) wind.o
wind.o:
$(CC) $(CFLAGS) -c wind.c
dth11: dth11.o
$(CC) $(CFLAGS) dth11.o -o dth11 $(LIB)
$(RM) dth11.o
dth11.o:
$(CC) $(CFLAGS) -c dth11.c $(LIB)
infrared: info_infrared
$(CC) info_infrared.c -o info_infrared -lwiringPi
light: info_light
$(CC) info_light.c -o info_light -lwiringPi
pin: info_pin
$(CC) info_pin.c -o info_pin -lwiringPi
sound: info_sound
$(CC) info_sound.c -o info_sound -lwiringPi
ultrasound: info_ultrasound
$(CC) info_ultrasound.c -o info_ultrasound -lwiringPi
wind_: wind
${CC} wind.c -o wind
.PHONY : clean .PHONY : clean
clean : clean :
rm -f $(libs) info_infrared info_light info_sound info_ultrasound rm -f $(libs) info_infrared info_light info_sound info_ultrasound wind dth11

28
gpio/conf/info.conf Normal file
View File

@ -0,0 +1,28 @@
#判断手机是否在线
phoneip="192.168.99.60"; #IP
#人体红外线传感器
wiringpi_infrared="6"; #脚位
#声音传感器
wiringpi_sound="25"; #脚位
#距离传感器
l="60";
s="150";
wiringpi_tring="8"; #脚位
wiringpi_echo="9"; #脚位
#灯
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" #关闭时间

131
gpio/dth11.c Normal file
View File

@ -0,0 +1,131 @@
/* 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;
}

Binary file not shown.

View File

@ -19,5 +19,6 @@ int main(int argc, char *argv[])
status = digitalRead(pin); status = digitalRead(pin);
printf("%d\n", status); printf("%d\n", status);
return status;
} }

Binary file not shown.

View File

@ -1,6 +1,13 @@
/* 设置PIN脚为1或0电位(电压)
*
*
*/
#include <wiringPi.h> #include <wiringPi.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
/* /*
void pinMode (int pin, int mode) ; void pinMode (int pin, int mode) ;
@ -11,11 +18,36 @@ int digitalRead (int pin) ;
analogRead (int pin) ; analogRead (int pin) ;
analogWrite (int pin, int value) ; 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[]) int main(int argc, char *argv[])
{ {
if( argc != 3) { if( argc != 3) {
exit(0); exit(0);
} }
if(_main(argv, 1) == 0) {
printf("参数错误, 参数1为PIN引脚值\n");
exit(1);
}
int pin = atol(argv[1]); int pin = atol(argv[1]);
wiringPiSetup(); wiringPiSetup();
@ -27,5 +59,6 @@ int main(int argc, char *argv[])
if(atol(argv[2]) == 0) { if(atol(argv[2]) == 0) {
digitalWrite(pin, LOW); digitalWrite(pin, LOW);
} }
return atol(argv[2]);
} }

Binary file not shown.

View File

@ -1,16 +1,13 @@
/* 测试PIN脚是否1或0
*
*
*/
#include <wiringPi.h> #include <wiringPi.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.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(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
if( argc != 2) { if( argc != 2) {
@ -21,4 +18,6 @@ int main(int argc, char *argv[])
wiringPiSetup(); wiringPiSetup();
pinMode(pin, OUTPUT); pinMode(pin, OUTPUT);
printf("%d\n", digitalRead(pin)); printf("%d\n", digitalRead(pin));
return pin;
} }

Binary file not shown.

View File

@ -19,5 +19,6 @@ int main(int argc, char *argv[])
status = digitalRead(pin); status = digitalRead(pin);
printf("%d\n", status); printf("%d\n", status);
return status;
} }

Binary file not shown.

View File

@ -5,7 +5,6 @@
int main(int argc, char * argv[]) int main(int argc, char * argv[])
{ {
int i;
if( argc != 3) { if( argc != 3) {
exit(0); exit(0);
} }

View File

@ -1 +1 @@
8982 30955

View File

@ -1 +1 @@
7721 1299

1
gpio/log/wind_daemon.pid Normal file
View File

@ -0,0 +1 @@
1297

BIN
gpio/wind

Binary file not shown.

View File

@ -1,4 +1,9 @@
#include <stdio.h> /* DS18B20 温度传感器
*
*
*/
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
@ -11,6 +16,8 @@
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int _main(char *optarg);
int ch; int ch;
opterr = 0; opterr = 0;
while((ch = getopt(argc, argv, "d:h?")) != -1) while((ch = getopt(argc, argv, "d:h?")) != -1)
@ -25,6 +32,7 @@ int main(int argc, char *argv[])
default: default:
; ;
} }
return 1;
} }
int _main(char *optarg) int _main(char *optarg)
@ -37,10 +45,10 @@ int _main(char *optarg)
FILE *fp; FILE *fp;
char *temp; char *temp;
float value; float value;
char *device; // 设备 //char *device; // 设备
if(optarg == '\0') //if(optarg == '\0') {
strcmp(device, optarg); // 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) {
@ -48,7 +56,7 @@ int _main(char *optarg)
} }
while((direntp = readdir(dirp)) != NULL) { while((direntp = readdir(dirp)) != NULL) {
if(strstr(direntp->d_name, device)) { if(strstr(direntp->d_name, optarg)) {
strcpy(rom, direntp->d_name); strcpy(rom, direntp->d_name);
} }
} }
@ -71,6 +79,5 @@ int _main(char *optarg)
printf("%.0f\n", value); printf("%.0f\n", value);
fclose(fp); fclose(fp);
return 0; return value;
} }