lib/dht11.c

changeset 31
89dd2b691701
child 32
3bac8fd4173d
equal deleted inserted replaced
30:9c7119ac0455 31:89dd2b691701
1 /*****************************************************************************
2 * Copyright (C) 2008-2014
3 *
4 * Michiel Broek <mbroek at mbse dot eu>
5 *
6 * This file is part of the mbsePi-apps
7 *
8 * Parts original by CurlyMo from the pilight project.
9 *
10 * This is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2, or (at your option) any
13 * later version.
14 *
15 * mbsePi-apps is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with EC-65K; see the file COPYING. If not, write to the Free
22 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23 *****************************************************************************/
24
25 #include "../config.h"
26 #include "mbselib.h"
27
28 #ifdef HAVE_WIRINGPI_H
29
30 #define MAXTIMINGS 100
31
32 int dht11_pin = -1;
33 int dht11_temperature = -1;
34 int dht11_humidity = -1;
35 int dht11_valid = false;
36 int dht11_t_offset = 0;
37 int dht11_h_offset = 0;
38
39
40
41 static uint8_t sizecvt(const int read_value) {
42 /*
43 * digitalRead() and friends from wiringpi are defined as returning a value
44 * < 256. However, they are returned as int() types. This is a safety function
45 */
46 if (read_value > 255 || read_value < 0) {
47 syslog(LOG_NOTICE, "invalid data from wiringPi library");
48 }
49
50 return (uint8_t)read_value;
51 }
52
53
54
55 /*
56 * DHT11 sensor read. This should be used in a thread loop.
57 */
58 void dht11Read(void) {
59 int tries = 5;
60 unsigned short got_correct_data = 0;
61
62 if (dht11_pin == -1)
63 return;
64
65 while (tries && !got_correct_data) {
66 uint8_t laststate = HIGH;
67 uint8_t counter = 0;
68 uint8_t j = 0, i = 0;
69 int dht11_dat[5] = {0,0,0,0,0};
70
71 /*
72 * Select output mode to send the start signal.
73 */
74 pinMode(dht11_pin, OUTPUT);
75 digitalWrite(dht11_pin, HIGH);
76 usleep(1000);
77
78 /*
79 * Low for at least 18 milliseconds
80 */
81 digitalWrite(dht11_pin, LOW);
82 usleep(20000);
83 digitalWrite(dht11_pin, HIGH);
84 pinMode(dht11_pin, INPUT);
85
86 /*
87 * Detect change and read data
88 */
89 for (i=0; i<MAXTIMINGS; i++) {
90 counter = 0;
91 delayMicroseconds(10);
92 while (sizecvt(digitalRead(dht11_pin)) == laststate) {
93 counter++;
94 delayMicroseconds(1);
95 if (counter == 255) {
96 break;
97 }
98 }
99 laststate = sizecvt(digitalRead(dht11_pin));
100
101 if (counter == 255)
102 break;
103
104 /*
105 * ignore first 3 transitions
106 */
107 if ((i >= 4) && (i%2 == 0)) {
108
109 // shove each bit into the storage bytes
110 dht11_dat[(int)((double)j/8)] <<= 1;
111 if (counter > 16)
112 dht11_dat[(int)((double)j/8)] |= 1;
113 j++;
114 }
115 }
116
117 /*
118 * If there is no sensor, j = 0
119 */
120 if ((counter == 255) && (j == 0)) {
121 if (dht11_temperature != -1) {
122 syslog(LOG_NOTICE, "dht11 sensor disappeared");
123 }
124 dht11_temperature = -1;
125 dht11_humidity = -1;
126 dht11_valid = false;
127 } else {
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
159 void dht11Init(int pin, int t_offset, int h_offset) {
160 dht11_pin = pin;
161 dht11_t_offset = t_offset;
162 dht11_h_offset = h_offset;
163 }
164
165
166 #endif
167

mercurial