lib/dht11.c

changeset 36
8701b4dab77b
parent 32
3bac8fd4173d
equal deleted inserted replaced
35:f3c5ae78b746 36:8701b4dab77b
118 * If there is no sensor, j = 0 118 * If there is no sensor, j = 0
119 */ 119 */
120 if ((counter == 255) && (j == 0)) { 120 if ((counter == 255) && (j == 0)) {
121 if (dht11_temperature != -1) { 121 if (dht11_temperature != -1) {
122 syslog(LOG_NOTICE, "dht11 sensor disappeared"); 122 syslog(LOG_NOTICE, "dht11 sensor disappeared");
123 } else {
124 syslog(LOG_NOTICE, "dht11 sensor not present");
123 } 125 }
124 dht11_temperature = -1; 126 dht11_temperature = -1;
125 dht11_humidity = -1; 127 dht11_humidity = -1;
126 dht11_valid = false; 128 dht11_valid = false;
127 } else { 129 return;
130 }
128 131
129 /* 132 /*
130 * check we read 40 bits (8bit x 5 ) + verify checksum in the last byte 133 * check we read 40 bits (8bit x 5 ) + verify checksum in the last byte
131 * print it out if data is good 134 * print it out if data is good
132 */ 135 */
133 if ((j >= 40) && (dht11_dat[4] == ((dht11_dat[0] + dht11_dat[1] + dht11_dat[2] + dht11_dat[3]) & 0xFF))) { 136 if ((j >= 40) && (dht11_dat[4] == ((dht11_dat[0] + dht11_dat[1] + dht11_dat[2] + dht11_dat[3]) & 0xFF))) {
134 got_correct_data = 1; 137 got_correct_data = 1;
135 138
136 int h = dht11_dat[0] + dht11_dat[1]; 139 int h = dht11_dat[0] + dht11_dat[1];
137 int t = (dht11_dat[2] & 0x7F) + dht11_dat[3]; 140 int t = (dht11_dat[2] & 0x7F) + dht11_dat[3];
138 t += dht11_t_offset; 141 t += dht11_t_offset;
142 t *= -1; 145 t *= -1;
143 146
144 dht11_temperature = t; 147 dht11_temperature = t;
145 dht11_humidity = h; 148 dht11_humidity = h;
146 dht11_valid = true; 149 dht11_valid = true;
147 } else { 150 } else {
148 tries--; 151 tries--;
149 if (tries == 0) 152 if (tries == 0)
150 syslog(LOG_INFO, "dht11 data checksum was wrong 5 times"); 153 syslog(LOG_INFO, "dht11 data checksum was wrong 5 times");
151 usleep(100000); 154 usleep(100000);
152 }
153 } 155 }
154 } 156 }
155 } 157 }
156 158
157 159

mercurial