dht11/dht11.c

Sat, 14 Jul 2018 17:21:25 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Sat, 14 Jul 2018 17:21:25 +0200
changeset 533
49580ca85ab7
parent 146
11f431ac253d
permissions
-rw-r--r--

Versie 0.6.3. MQTT device berichten alleen als een fermenter ingeschakeld is. MQTT fermenter birth en death berichhten als een fementer in of uitgeschakeld wordt. MQTT node death bericht bij normaal afsluiten van de daemon. Alle MQTT persistent berichten worden nu goed opgeruikmd.

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
146
11f431ac253d Code fixes and cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 51
diff changeset
23 #include "../config.h"
11f431ac253d Code fixes and cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 51
diff changeset
24 #include <stdio.h>
11f431ac253d Code fixes and cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 51
diff changeset
25 #include <stdlib.h>
11f431ac253d Code fixes and cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 51
diff changeset
26 #include <stdint.h>
11f431ac253d Code fixes and cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 51
diff changeset
27 #include <stdarg.h>
11f431ac253d Code fixes and cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 51
diff changeset
28 #include <syslog.h>
11f431ac253d Code fixes and cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 51
diff changeset
29 #include <unistd.h>
11f431ac253d Code fixes and cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 51
diff changeset
30
31
89dd2b691701 Added library code to read DHT11 temperature/humidity sensor
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
31 #include "dht11.h"
89dd2b691701 Added library code to read DHT11 temperature/humidity sensor
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
32
89dd2b691701 Added library code to read DHT11 temperature/humidity sensor
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
33 #ifdef HAVE_WIRINGPI_H
146
11f431ac253d Code fixes and cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 51
diff changeset
34 #include <wiringPi.h>
31
89dd2b691701 Added library code to read DHT11 temperature/humidity sensor
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
35
51
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
36 #define MAXTIMINGS 100
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 int dht11_pin = -1;
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
39 int dht11_temperature = -1;
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
40 int dht11_humidity = -1;
146
11f431ac253d Code fixes and cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 51
diff changeset
41 int dht11_valid = FALSE;
51
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
42 int dht11_t_offset = 0;
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
43 int dht11_h_offset = 0;
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
44
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 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
48 /*
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
49 * 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
50 * < 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
51 */
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
52 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
53 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
54 }
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
55
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
56 return (uint8_t)read_value;
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
57 }
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
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
60
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 * 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
63 */
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
64 void dht11Read(void) {
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
65 int tries = 5;
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
66 unsigned short got_correct_data = 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 if (dht11_pin == -1)
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
69 return;
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 while (tries && !got_correct_data) {
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
72 uint8_t laststate = HIGH;
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
73 uint8_t counter = 0;
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
74 uint8_t j = 0, i = 0;
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
75 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
76
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 * 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
79 */
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
80 pinMode(dht11_pin, OUTPUT);
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
81 digitalWrite(dht11_pin, HIGH);
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
82 usleep(1000);
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 /*
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
85 * Low for at least 18 milliseconds
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
86 */
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
87 digitalWrite(dht11_pin, LOW);
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
88 usleep(20000);
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
89 digitalWrite(dht11_pin, HIGH);
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
90 pinMode(dht11_pin, INPUT);
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
91
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
92 /*
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
93 * Detect change and read data
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 for (i=0; i<MAXTIMINGS; i++) {
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
96 counter = 0;
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
97 delayMicroseconds(10);
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
98 while (sizecvt(digitalRead(dht11_pin)) == laststate) {
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
99 counter++;
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
100 delayMicroseconds(1);
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
101 if (counter == 255) {
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
102 break;
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 }
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
105 laststate = sizecvt(digitalRead(dht11_pin));
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
106
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
107 if (counter == 255)
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
108 break;
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
109
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
110 /*
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
111 * ignore first 3 transitions
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 if ((i >= 4) && (i%2 == 0)) {
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 // 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
116 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
117 if (counter > 16)
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
118 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
119 j++;
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
120 }
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
121 }
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 /*
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
124 * 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
125 */
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
126 if ((counter == 255) && (j == 0)) {
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
127 if (dht11_temperature != -1) {
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
128 syslog(LOG_NOTICE, "dht11 sensor disappeared");
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
129 } else {
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
130 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
131 }
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
132 dht11_temperature = -1;
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
133 dht11_humidity = -1;
146
11f431ac253d Code fixes and cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 51
diff changeset
134 dht11_valid = FALSE;
51
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
135 return;
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
136 }
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
137
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
138 /*
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
139 * 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
140 * 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
141 */
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
142 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
143 got_correct_data = 1;
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
144
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
145 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
146 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
147 t += dht11_t_offset;
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
148 h += dht11_h_offset;
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
149
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
150 if ((dht11_dat[2] & 0x80) != 0)
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
151 t *= -1;
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 dht11_temperature = t;
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
154 dht11_humidity = h;
146
11f431ac253d Code fixes and cleanup
Michiel Broek <mbroek@mbse.eu>
parents: 51
diff changeset
155 dht11_valid = TRUE;
51
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
156 } else {
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
157 tries--;
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
158 if (tries == 0)
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
159 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
160 usleep(100000);
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
161 }
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
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
165
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
166
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
167 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
168 dht11_pin = pin;
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
169 dht11_t_offset = t_offset;
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
170 dht11_h_offset = h_offset;
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
171 }
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
172
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents: 35
diff changeset
173
31
89dd2b691701 Added library code to read DHT11 temperature/humidity sensor
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
174
89dd2b691701 Added library code to read DHT11 temperature/humidity sensor
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
175 int main(int argc, char *argv[]) {
89dd2b691701 Added library code to read DHT11 temperature/humidity sensor
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
176
89dd2b691701 Added library code to read DHT11 temperature/humidity sensor
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
177 int PIN = 3;
89dd2b691701 Added library code to read DHT11 temperature/humidity sensor
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
178
89dd2b691701 Added library code to read DHT11 temperature/humidity sensor
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
179 if (wiringPiSetup() == -1)
89dd2b691701 Added library code to read DHT11 temperature/humidity sensor
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
180 return 0;
89dd2b691701 Added library code to read DHT11 temperature/humidity sensor
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
181
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
182 dht11Init(PIN, 1, 6);
31
89dd2b691701 Added library code to read DHT11 temperature/humidity sensor
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
183 dht11Read();
89dd2b691701 Added library code to read DHT11 temperature/humidity sensor
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
184 if (dht11_valid) {
89dd2b691701 Added library code to read DHT11 temperature/humidity sensor
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
185 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
186 } else {
89dd2b691701 Added library code to read DHT11 temperature/humidity sensor
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
187 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
188 }
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 exit(0);
89dd2b691701 Added library code to read DHT11 temperature/humidity sensor
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
191 }
89dd2b691701 Added library code to read DHT11 temperature/humidity sensor
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
192
89dd2b691701 Added library code to read DHT11 temperature/humidity sensor
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
193 #else
89dd2b691701 Added library code to read DHT11 temperature/humidity sensor
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
194
89dd2b691701 Added library code to read DHT11 temperature/humidity sensor
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
195 int main(int argc, char *argv[]) {
89dd2b691701 Added library code to read DHT11 temperature/humidity sensor
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
196 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
197 return 0;
89dd2b691701 Added library code to read DHT11 temperature/humidity sensor
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
198 }
89dd2b691701 Added library code to read DHT11 temperature/humidity sensor
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
199
89dd2b691701 Added library code to read DHT11 temperature/humidity sensor
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
200 #endif
89dd2b691701 Added library code to read DHT11 temperature/humidity sensor
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
201

mercurial