MCPcopy Create free account
hub / github.com/WiringPi/WiringPi / readRHT03

Function readRHT03

devLib/maxdetect.c:177–234  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

175 */
176
177int readRHT03 (const int pin, int *temp, int *rh)
178{
179 static struct timeval then ; // will initialise to zero
180 static int lastTemp = 0 ;
181 static int lastRh = 0 ;
182
183 int result ;
184 struct timeval now, timeOut ;
185 unsigned char buffer [4] ;
186
187// The data sheets say to not read more than once every 2 seconds, so you
188// get the last good reading
189
190 gettimeofday (&now, NULL) ;
191 if (timercmp (&now, &then, <))
192 {
193 *rh = lastRh ;
194 *temp = lastTemp ;
195 return true ;
196 }
197
198// Set timeout for next read
199
200 gettimeofday (&now, NULL) ;
201 timerclear (&timeOut) ;
202 timeOut.tv_sec = 2 ;
203 timeradd (&now, &timeOut, &then) ;
204
205// Read ...
206
207 result = maxDetectRead (pin, buffer) ;
208
209 if (!result) // Try again, but just once
210 result = maxDetectRead (pin, buffer) ;
211
212 if (!result)
213 return false ;
214
215 *rh = (buffer [0] * 256 + buffer [1]) ;
216 *temp = (buffer [2] * 256 + buffer [3]) ;
217
218 if ((*temp & 0x8000) != 0) // Negative
219 {
220 *temp &= 0x7FFF ;
221 *temp = -*temp ;
222 }
223
224// Discard obviously bogus readings - the checksum can't detect a 2-bit error
225// (which does seem to happen - no realtime here)
226
227 if ((*rh > 999) || (*temp > 800) || (*temp < -400))
228 return false ;
229
230 lastRh = *rh ;
231 lastTemp = *temp ;
232
233 return true ;
234}

Callers 1

mainFunction · 0.85

Calls 1

maxDetectReadFunction · 0.70

Tested by

no test coverage detected