18 * You should have received a copy of the GNU General Public License |
18 * You should have received a copy of the GNU General Public License |
19 * along with EC-65K; see the file COPYING. If not, write to the Free |
19 * along with EC-65K; see the file COPYING. If not, write to the Free |
20 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
20 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
21 *****************************************************************************/ |
21 *****************************************************************************/ |
22 |
22 |
23 #include "../lib/mbselib.h" |
|
24 #include "dht11.h" |
23 #include "dht11.h" |
25 |
24 |
26 #ifdef HAVE_WIRINGPI_H |
25 #ifdef HAVE_WIRINGPI_H |
27 |
26 |
28 extern int dht11_valid; |
27 #define MAXTIMINGS 100 |
29 extern int dht11_temperature; |
28 |
30 extern int dht11_humidity; |
29 int dht11_pin = -1; |
|
30 int dht11_temperature = -1; |
|
31 int dht11_humidity = -1; |
|
32 int dht11_valid = false; |
|
33 int dht11_t_offset = 0; |
|
34 int dht11_h_offset = 0; |
|
35 |
|
36 |
|
37 |
|
38 static uint8_t sizecvt(const int read_value) { |
|
39 /* |
|
40 * digitalRead() and friends from wiringpi are defined as returning a value |
|
41 * < 256. However, they are returned as int() types. This is a safety function |
|
42 */ |
|
43 if (read_value > 255 || read_value < 0) { |
|
44 syslog(LOG_NOTICE, "invalid data from wiringPi library"); |
|
45 } |
|
46 |
|
47 return (uint8_t)read_value; |
|
48 } |
|
49 |
|
50 |
|
51 |
|
52 /* |
|
53 * DHT11 sensor read. This should be used in a thread loop. |
|
54 */ |
|
55 void dht11Read(void) { |
|
56 int tries = 5; |
|
57 unsigned short got_correct_data = 0; |
|
58 |
|
59 if (dht11_pin == -1) |
|
60 return; |
|
61 |
|
62 while (tries && !got_correct_data) { |
|
63 uint8_t laststate = HIGH; |
|
64 uint8_t counter = 0; |
|
65 uint8_t j = 0, i = 0; |
|
66 int dht11_dat[5] = {0,0,0,0,0}; |
|
67 |
|
68 /* |
|
69 * Select output mode to send the start signal. |
|
70 */ |
|
71 pinMode(dht11_pin, OUTPUT); |
|
72 digitalWrite(dht11_pin, HIGH); |
|
73 usleep(1000); |
|
74 |
|
75 /* |
|
76 * Low for at least 18 milliseconds |
|
77 */ |
|
78 digitalWrite(dht11_pin, LOW); |
|
79 usleep(20000); |
|
80 digitalWrite(dht11_pin, HIGH); |
|
81 pinMode(dht11_pin, INPUT); |
|
82 |
|
83 /* |
|
84 * Detect change and read data |
|
85 */ |
|
86 for (i=0; i<MAXTIMINGS; i++) { |
|
87 counter = 0; |
|
88 delayMicroseconds(10); |
|
89 while (sizecvt(digitalRead(dht11_pin)) == laststate) { |
|
90 counter++; |
|
91 delayMicroseconds(1); |
|
92 if (counter == 255) { |
|
93 break; |
|
94 } |
|
95 } |
|
96 laststate = sizecvt(digitalRead(dht11_pin)); |
|
97 |
|
98 if (counter == 255) |
|
99 break; |
|
100 |
|
101 /* |
|
102 * ignore first 3 transitions |
|
103 */ |
|
104 if ((i >= 4) && (i%2 == 0)) { |
|
105 |
|
106 // shove each bit into the storage bytes |
|
107 dht11_dat[(int)((double)j/8)] <<= 1; |
|
108 if (counter > 16) |
|
109 dht11_dat[(int)((double)j/8)] |= 1; |
|
110 j++; |
|
111 } |
|
112 } |
|
113 |
|
114 /* |
|
115 * If there is no sensor, j = 0 |
|
116 */ |
|
117 if ((counter == 255) && (j == 0)) { |
|
118 if (dht11_temperature != -1) { |
|
119 syslog(LOG_NOTICE, "dht11 sensor disappeared"); |
|
120 } else { |
|
121 syslog(LOG_NOTICE, "dht11 sensor not present"); |
|
122 } |
|
123 dht11_temperature = -1; |
|
124 dht11_humidity = -1; |
|
125 dht11_valid = false; |
|
126 return; |
|
127 } |
|
128 |
|
129 /* |
|
130 * check we read 40 bits (8bit x 5 ) + verify checksum in the last byte |
|
131 * print it out if data is good |
|
132 */ |
|
133 if ((j >= 40) && (dht11_dat[4] == ((dht11_dat[0] + dht11_dat[1] + dht11_dat[2] + dht11_dat[3]) & 0xFF))) { |
|
134 got_correct_data = 1; |
|
135 |
|
136 int h = dht11_dat[0] + dht11_dat[1]; |
|
137 int t = (dht11_dat[2] & 0x7F) + dht11_dat[3]; |
|
138 t += dht11_t_offset; |
|
139 h += dht11_h_offset; |
|
140 |
|
141 if ((dht11_dat[2] & 0x80) != 0) |
|
142 t *= -1; |
|
143 |
|
144 dht11_temperature = t; |
|
145 dht11_humidity = h; |
|
146 dht11_valid = true; |
|
147 } else { |
|
148 tries--; |
|
149 if (tries == 0) |
|
150 syslog(LOG_INFO, "dht11 data checksum was wrong 5 times"); |
|
151 usleep(100000); |
|
152 } |
|
153 } |
|
154 } |
|
155 |
|
156 |
|
157 |
|
158 void dht11Init(int pin, int t_offset, int h_offset) { |
|
159 dht11_pin = pin; |
|
160 dht11_t_offset = t_offset; |
|
161 dht11_h_offset = h_offset; |
|
162 } |
|
163 |
|
164 |
31 |
165 |
32 int main(int argc, char *argv[]) { |
166 int main(int argc, char *argv[]) { |
33 |
167 |
34 int PIN = 3; |
168 int PIN = 3; |
35 |
169 |