-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathdht11_app.c
executable file
·83 lines (75 loc) · 2.12 KB
/
dht11_app.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <stdio.h>
//#include <curses.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#define SET_GPIO39 0
#define SET_GPIO40 1
#define SET_GPIO41 2
#define SET_GPIO42 3
int main(int argc, char **argv)
{
int fd;
unsigned int dht11_data = 0;
unsigned int int_tmp = 0;
float humi,temp;
unsigned int pin_num;
//------- check input syntax DEFAUL GPIO40
if((argc>1 && (atoi(argv[1])<39 || atoi(argv[1])>42)) || (argv[1]=="-h"))
{
printf("Syntax:%s pin_number defaul:GPIO40\n",argv[0]);
printf("Example: %s 40 \n",argv[0]);
printf("please select one of GPIO pin_number from 39-42 for device connection! \n");
return 1;
}
//------- open driver--------
fd = open("/dev/DHT11", O_RDWR | O_NONBLOCK);
if (fd < 0)
{
printf("can't open /dev/DHT11\n");
return -1;
}
//------------- set GPIO pin number for device connection ---------
if(argc==1)
pin_num=40; //----default pin number 40
else
pin_num=atoi(argv[1]);
switch(pin_num)
{
case 39:
ioctl(fd,SET_GPIO39);
break;
case 40:
ioctl(fd,SET_GPIO40);
break;
case 41:
ioctl(fd,SET_GPIO41);
break;
case 42:
ioctl(fd,SET_GPIO42);
break;
default:
printf("Please enter valid GPIO number: 39-42\n");
return 1;
break;
}
//------------------------------------- read Temperature and humidity -------
while(1)
{
read(fd, &dht11_data, sizeof(dht11_data));
printf("dht11_data = 0x%08x\n",dht11_data);
if(dht11_data==0)
continue;
int_tmp = (dht11_data>>16);
temp = (int_tmp>>8) + (int_tmp&0x000000ff)/256.0;
int_tmp = dht11_data&0x0000ffff;
humi = (int_tmp>>8) + (int_tmp&0x000000ff)/256.0;
printf("Current temperature: %4.1f deg\n",temp);
printf("Current humidity: %4.1f %%\n",humi);
sleep(1); //--wait at least 1s as per DHT11 datasheet
}
close(fd);
return 0;
}