forked from technion/lol_dht22
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdht22.c
180 lines (149 loc) · 3.78 KB
/
dht22.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
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
/*
* dht22.c:
* Simple test program to test the wiringPi functions
* Based on the existing dht11.c
* Amended by [email protected]
*/
#include <wiringPi.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/types.h>
#include <unistd.h>
#include "locking.h"
#define MAXTIMINGS 85
static int DHTPIN = 7;
int printtemp = 1;
int printhumid = 1;
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)
{
fprintf(stderr, "Invalid data from wiringPi library\n");
exit(EXIT_FAILURE);
}
return (uint8_t)read;
}
static int read_dht22_dat()
{
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(DHTPIN, OUTPUT);
digitalWrite(DHTPIN, HIGH);
delay(10);
digitalWrite(DHTPIN, LOW);
delay(18);
// then pull it up for 40 microseconds
digitalWrite(DHTPIN, HIGH);
delayMicroseconds(40);
// prepare to read the pin
pinMode(DHTPIN, INPUT);
// detect change and read data
for ( i=0; i< MAXTIMINGS; i++) {
counter = 0;
while (sizecvt(digitalRead(DHTPIN)) == laststate) {
counter++;
delayMicroseconds(1);
if (counter == 255) {
break;
}
}
laststate = sizecvt(digitalRead(DHTPIN));
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)) ) {
float t, h;
h = (float)dht22_dat[0] * 256 + (float)dht22_dat[1];
h /= 10;
t = (float)(dht22_dat[2] & 0x7F)* 256 + (float)dht22_dat[3];
t /= 10.0;
if ((dht22_dat[2] & 0x80) != 0) t *= -1;
if(printhumid != 0) {
printf("%.2f", h);
if (printtemp != 0) {
putchar(',');
}
}
if(printtemp != 0) {
printf("%.2f", t);
}
//printf("Humidity = %.2f %% Temperature = %.2f *C \n", h, t );
return 1;
}
else
{
fprintf(stderr, "Data not good, skip\n");
return 0;
}
}
int main (int argc, char *argv[])
{
//int lockfd;
int c;
int retries = 10;
while ((c = getopt (argc, argv, "tmr:p:")) != -1)
switch (c)
{
case 't':
printhumid = 0;
break;
case 'm':
printtemp = 0;
break;
case 'r':
retries = atoi(optarg);
break;
case 'p':
DHTPIN = atoi(optarg);
break;
case '?':
if (optopt == 'p')
fprintf (stderr, "Option -%c requires port number an argument.\n", optopt);
else if (isprint (optopt))
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf (stderr,
"Unknown option character `\\x%x'.\n",
optopt);
return 1;
default:
fprintf(stderr, "This should never happen.\n");
abort();
}
//printf ("Raspberry Pi wiringPi DHT22 reader\nwww.lolware.net\n") ;
//lockfd = open_lockfile(LOCKFILE);
if (wiringPiSetup () == -1)
exit(EXIT_FAILURE) ;
//if (setuid(getuid()) < 0)
//{
// perror("Dropping privileges failed\n");
// exit(EXIT_FAILURE);
//}
int fails = 0;
while ((read_dht22_dat() == 0) && (fails < retries))
{
delay(500); // wait 1sec to refresh
fails ++;
}
delay(1500);
//close_lockfile(lockfd);
return 0 ;
}