dht11/dht11.c

changeset 538
6d139c21e22c
parent 537
4eebab50993e
child 539
300b5c4cd977
equal deleted inserted replaced
537:4eebab50993e 538:6d139c21e22c
1 /*****************************************************************************
2 * Copyright (C) 2014
3 *
4 * Michiel Broek <mbroek at mbse dot eu>
5 *
6 * This file is part of the mbsePi-apps
7 *
8 * This is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
11 * later version.
12 *
13 * mbsePi-apps is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
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
20 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 *****************************************************************************/
22
23 #include "../config.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <stdint.h>
27 #include <stdarg.h>
28 #include <syslog.h>
29 #include <unistd.h>
30
31 #include "dht11.h"
32
33 #ifdef HAVE_WIRINGPI_H
34 #include <wiringPi.h>
35
36 #define MAXTIMINGS 100
37
38 int dht11_pin = -1;
39 int dht11_temperature = -1;
40 int dht11_humidity = -1;
41 int dht11_valid = FALSE;
42 int dht11_t_offset = 0;
43 int dht11_h_offset = 0;
44
45
46
47 static uint8_t sizecvt(const int read_value) {
48 /*
49 * digitalRead() and friends from wiringpi are defined as returning a value
50 * < 256. However, they are returned as int() types. This is a safety function
51 */
52 if (read_value > 255 || read_value < 0) {
53 syslog(LOG_NOTICE, "invalid data from wiringPi library");
54 }
55
56 return (uint8_t)read_value;
57 }
58
59
60
61 /*
62 * DHT11 sensor read. This should be used in a thread loop.
63 */
64 void dht11Read(void) {
65 int tries = 5;
66 unsigned short got_correct_data = 0;
67
68 if (dht11_pin == -1)
69 return;
70
71 while (tries && !got_correct_data) {
72 uint8_t laststate = HIGH;
73 uint8_t counter = 0;
74 uint8_t j = 0, i = 0;
75 int dht11_dat[5] = {0,0,0,0,0};
76
77 /*
78 * Select output mode to send the start signal.
79 */
80 pinMode(dht11_pin, OUTPUT);
81 digitalWrite(dht11_pin, HIGH);
82 usleep(1000);
83
84 /*
85 * Low for at least 18 milliseconds
86 */
87 digitalWrite(dht11_pin, LOW);
88 usleep(20000);
89 digitalWrite(dht11_pin, HIGH);
90 pinMode(dht11_pin, INPUT);
91
92 /*
93 * Detect change and read data
94 */
95 for (i=0; i<MAXTIMINGS; i++) {
96 counter = 0;
97 delayMicroseconds(10);
98 while (sizecvt(digitalRead(dht11_pin)) == laststate) {
99 counter++;
100 delayMicroseconds(1);
101 if (counter == 255) {
102 break;
103 }
104 }
105 laststate = sizecvt(digitalRead(dht11_pin));
106
107 if (counter == 255)
108 break;
109
110 /*
111 * ignore first 3 transitions
112 */
113 if ((i >= 4) && (i%2 == 0)) {
114
115 // shove each bit into the storage bytes
116 dht11_dat[(int)((double)j/8)] <<= 1;
117 if (counter > 16)
118 dht11_dat[(int)((double)j/8)] |= 1;
119 j++;
120 }
121 }
122
123 /*
124 * If there is no sensor, j = 0
125 */
126 if ((counter == 255) && (j == 0)) {
127 if (dht11_temperature != -1) {
128 syslog(LOG_NOTICE, "dht11 sensor disappeared");
129 } else {
130 syslog(LOG_NOTICE, "dht11 sensor not present");
131 }
132 dht11_temperature = -1;
133 dht11_humidity = -1;
134 dht11_valid = FALSE;
135 return;
136 }
137
138 /*
139 * check we read 40 bits (8bit x 5 ) + verify checksum in the last byte
140 * print it out if data is good
141 */
142 if ((j >= 40) && (dht11_dat[4] == ((dht11_dat[0] + dht11_dat[1] + dht11_dat[2] + dht11_dat[3]) & 0xFF))) {
143 got_correct_data = 1;
144
145 int h = dht11_dat[0] + dht11_dat[1];
146 int t = (dht11_dat[2] & 0x7F) + dht11_dat[3];
147 t += dht11_t_offset;
148 h += dht11_h_offset;
149
150 if ((dht11_dat[2] & 0x80) != 0)
151 t *= -1;
152
153 dht11_temperature = t;
154 dht11_humidity = h;
155 dht11_valid = TRUE;
156 } else {
157 tries--;
158 if (tries == 0)
159 syslog(LOG_INFO, "dht11 data checksum was wrong 5 times");
160 usleep(100000);
161 }
162 }
163 }
164
165
166
167 void dht11Init(int pin, int t_offset, int h_offset) {
168 dht11_pin = pin;
169 dht11_t_offset = t_offset;
170 dht11_h_offset = h_offset;
171 }
172
173
174
175 int main(int argc, char *argv[]) {
176
177 int PIN = 3;
178
179 if (wiringPiSetup() == -1)
180 return 0;
181
182 dht11Init(PIN, 1, 6);
183 dht11Read();
184 if (dht11_valid) {
185 fprintf(stdout, "DHT11: temperature %d degrees, humidity %d%%\n", dht11_temperature, dht11_humidity);
186 } else {
187 fprintf(stdout, "DHT11: no valid data or sensor not connected\n");
188 }
189
190 exit(0);
191 }
192
193 #else
194
195 int main(int argc, char *argv[]) {
196 fprintf(stderr, "This program does nothing without the wiringPi library\n");
197 return 0;
198 }
199
200 #endif
201

mercurial