dht11/dht11.c

Sun, 25 May 2014 22:06:56 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Sun, 25 May 2014 22:06:56 +0200
changeset 51
a03b6dac5398
parent 35
f3c5ae78b746
child 146
11f431ac253d
permissions
-rw-r--r--

Removed library, bumped to version 0.0.7

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

mercurial