This repository was archived by the owner on Dec 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdht.c
More file actions
180 lines (152 loc) · 4.21 KB
/
dht.c
File metadata and controls
180 lines (152 loc) · 4.21 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
* dht.c:
* Simple test program to test the wiringPi functions
* Based on the existing dht22.c that was based in dht11.c
* Amended by technion@lolware.net
*/
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
/* WiringPi pin */
#define DHTPIN 5
/* waiting time */
#define INTERVAL 8 /* with rpimonitor default, better be 8 */
/* the temperature sensor seems consistently higher. Try to calibrate this with another sensor or a real termomether */
#define TEMPERATURECORRECTION -20 /* 2 degress */
#define DATADIR "/var/run/dht"
#define MAXTIMINGS 85
static int dht22_dat[5] = {0,0,0,0,0};
static uint8_t sizecvt(const int read)
{
/* digitalRead() and friends from wiringpi are defined as returning a value
< 256. However, they are returned as int() types. This is a safety function */
if (read > 255 || read < 0)
{
printf("Invalid data from wiringPi library\n");
exit(EXIT_FAILURE);
}
return (uint8_t)read;
}
static int readDHT(int pin, int *temp, int *rh)
{
uint8_t laststate = HIGH;
uint8_t counter = 0;
uint8_t j = 0, i;
dht22_dat[0] = dht22_dat[1] = dht22_dat[2] = dht22_dat[3] = dht22_dat[4] = 0;
// pull pin down for 18 milliseconds
pinMode(pin, OUTPUT);
// digitalWrite(pin, HIGH);
// delay(10);
digitalWrite(pin, LOW);
delay(18);
// then pull it up for 40 microseconds
digitalWrite(pin, HIGH);
delayMicroseconds(40);
// prepare to read the pin
pinMode(pin, INPUT);
// detect change and read data
for ( i=0; i< MAXTIMINGS; i++) {
counter = 0;
while (sizecvt(digitalRead(pin)) == laststate) {
counter++;
delayMicroseconds(1);
if (counter == 255) {
break;
}
}
laststate = sizecvt(digitalRead(pin));
if (counter == 255) break;
// ignore first 3 transitions
if ((i >= 4) && (i%2 == 0)) {
// shove each bit into the storage bytes
dht22_dat[j/8] <<= 1;
if (counter > 16)
dht22_dat[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) &&
(dht22_dat[4] == ((dht22_dat[0] + dht22_dat[1] + dht22_dat[2] + dht22_dat[3]) & 0xFF)) ) {
*rh = dht22_dat[0] * 256 + dht22_dat[1];
*temp = (dht22_dat[2] & 0x7F)* 256 + dht22_dat[3];
/* negative temp */
if ((dht22_dat[2] & 0x80) != 0) *temp = -*temp;
return 1;
}
else
{
//printf("Data not good, skip\n");
return 0;
}
}
int main (int argc, char *argv[])
{
// int tries = 10;
FILE * f;
int newTemp, newRh;
if (wiringPiSetup () == -1)
exit(EXIT_FAILURE) ;
mkdir(DATADIR, 0755 );
if ( chown(DATADIR, getuid(), -1) == -1 )
{
fprintf(stderr, "Was not able to create /var/run/dht\n");
exit(1);
}
if (setuid(getuid()) < 0)
{
perror("Dropping privileges failed\n");
exit(EXIT_FAILURE);
}
//chdir(DATADIR);
f = fopen("/var/run/dht/pid", "w" );
fprintf(f, "%d", getpid() );
fclose(f);
while(1)
{
// while (read_dht22_dat() == 0 && tries--)
// {
// delay(1000); // wait 1sec to refresh
// }
// if (readRHT03 (DHTPIN, &newTemp, &newRh))
if ( readDHT(DHTPIN, &newTemp, &newRh) )
{
static int t2, h2, ft = 1, terror=0, herror=0;
if (ft || (terror>4) || (herror>4) )
{
ft=0;
h2=newRh; t2=newTemp;
terror=herror=0;
}
if ( (newRh > 0) && (newRh < 950) && (abs(h2-newRh)<10) )
{
f=fopen("/var/run/dht/humidity.x", "w");
fprintf(f, "%d", newRh );
fclose(f);
rename("/var/run/dht/humidity.x", "/var/run/dht/humidity");
h2 = newRh;
herror=0;
}
else
herror++;
if ( ( newTemp < 600 ) && (abs(abs(t2)-abs(newTemp))<10) )
{
f=fopen("/var/run/dht/temp.x", "w");
fprintf(f, "%d", newTemp + (TEMPERATURECORRECTION));
fclose(f);
rename("/var/run/dht/temp.x", "/var/run/dht/temp");
t2 = newTemp;
terror=0;
}
else
terror++;
}
sleep(INTERVAL);
}
return 0 ;
}