thermferm/devices.c

changeset 644
07cc86900473
parent 643
586d376ab629
child 645
49eb753a958b
equal deleted inserted replaced
643:586d376ab629 644:07cc86900473
75 * DHT11 sensor read. 75 * DHT11 sensor read.
76 */ 76 */
77 void dht11Read(void) { 77 void dht11Read(void) {
78 int tries = 5; 78 int tries = 5;
79 unsigned short got_correct_data = 0; 79 unsigned short got_correct_data = 0;
80 struct timespec ts;
80 81
81 if (dht11_pin == -1) 82 if (dht11_pin == -1)
82 return; 83 return;
83 84
84 while (tries && !got_correct_data) { 85 while (tries && !got_correct_data) {
90 /* 91 /*
91 * Select output mode to send the start signal. 92 * Select output mode to send the start signal.
92 */ 93 */
93 pinMode(dht11_pin, OUTPUT); 94 pinMode(dht11_pin, OUTPUT);
94 digitalWrite(dht11_pin, HIGH); 95 digitalWrite(dht11_pin, HIGH);
95 usleep(1000); 96 /* 1 mSec */
97 ts.tv_sec = 0;
98 ts.tv_nsec = 1000000;
99 nanosleep(&ts, &ts);
96 100
97 /* 101 /*
98 * Low for at least 18 milliseconds 102 * Low for at least 18 milliseconds
99 */ 103 */
100 digitalWrite(dht11_pin, LOW); 104 digitalWrite(dht11_pin, LOW);
101 usleep(20000); 105 /* 20 mSec */
106 ts.tv_sec = 0;
107 ts.tv_nsec = 20 * 1000000;
108 nanosleep(&ts, &ts);
102 digitalWrite(dht11_pin, HIGH); 109 digitalWrite(dht11_pin, HIGH);
103 pinMode(dht11_pin, INPUT); 110 pinMode(dht11_pin, INPUT);
104 111
105 /* 112 /*
106 * Detect change and read data 113 * Detect change and read data
107 */ 114 */
108 for (i=0; i<MAXTIMINGS; i++) { 115 for (i=0; i<MAXTIMINGS; i++) {
109 counter = 0; 116 counter = 0;
117 /* 10 uS */
118 ts.tv_sec = 0;
119 ts.tv_nsec = 10 * 1000;
120 // nanosleep(&ts, NULL);
110 delayMicroseconds(10); 121 delayMicroseconds(10);
111 while (sizecvt(digitalRead(dht11_pin)) == laststate) { 122 while (sizecvt(digitalRead(dht11_pin)) == laststate) {
112 counter++; 123 counter++;
124 /* 1 uS */
125 ts.tv_sec = 0;
126 ts.tv_nsec = 1000;
127 // nanosleep(&ts, NULL);
113 delayMicroseconds(1); 128 delayMicroseconds(1);
114 if (counter == 255) { 129 if (counter == 255) {
115 break; 130 break;
116 } 131 }
117 } 132 }
162 t *= -1; 177 t *= -1;
163 178
164 dht11_temperature = t; 179 dht11_temperature = t;
165 dht11_humidity = h; 180 dht11_humidity = h;
166 dht11_valid = TRUE; 181 dht11_valid = TRUE;
182 syslog(LOG_NOTICE, "dht11 t:%d h:%d tries:%d", t, h, 6-tries);
167 } else { 183 } else {
168 tries--; 184 tries--;
169 if (tries == 0) 185 if (tries == 0)
170 syslog(LOG_NOTICE, "dht11 data checksum was wrong 5 times"); 186 syslog(LOG_NOTICE, "dht11 data checksum was wrong 5 times");
171 usleep(100000); 187 usleep(100000);

mercurial