3 * Driver for the MaxDetect series sensors
5 * Copyright (c) 2013 Gordon Henderson.
6 ***********************************************************************
7 * This file is part of wiringPi:
8 * https://projects.drogon.net/raspberry-pi/wiringpi/
10 * wiringPi is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
15 * wiringPi is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
22 ***********************************************************************
31 #include "maxdetect.h"
40 * maxDetectLowHighWait:
41 * Wait for a transition from high to low on the bus
42 *********************************************************************************
45 static void maxDetectLowHighWait (const int pin)
47 unsigned int timeOut = millis () + 2000 ;
49 while (digitalRead (pin) == HIGH)
50 if (millis () > timeOut)
53 while (digitalRead (pin) == LOW)
54 if (millis () > timeOut)
61 * Read in a single byte from the MaxDetect bus
62 *********************************************************************************
65 static unsigned int maxDetectClockByte (const int pin)
67 unsigned int byte = 0 ;
70 for (bit = 0 ; bit < 8 ; ++bit)
72 maxDetectLowHighWait (pin) ;
74 // bit starting now - we need to time it.
76 delayMicroseconds (30) ;
78 if (digitalRead (pin) == HIGH) // It's a 1
88 * Read in and return the 4 data bytes from the MaxDetect sensor.
89 * Return TRUE/FALSE depending on the checksum validity
90 *********************************************************************************
93 int maxDetectRead (const int pin, unsigned char buffer [4])
96 unsigned int checksum ;
97 unsigned char localBuf [5] ;
99 // Wake up the RHT03 by pulling the data line low, then high
100 // Low for 10mS, high for 40uS.
102 pinMode (pin, OUTPUT) ;
103 digitalWrite (pin, 0) ; delay (10) ;
104 digitalWrite (pin, 1) ; delayMicroseconds (40) ;
105 pinMode (pin, INPUT) ;
107 // Now wait for sensor to pull pin low
109 maxDetectLowHighWait (pin) ;
111 // and read in 5 bytes (40 bits)
113 for (i = 0 ; i < 5 ; ++i)
114 localBuf [i] = maxDetectClockByte (pin) ;
117 for (i = 0 ; i < 4 ; ++i)
119 buffer [i] = localBuf [i] ;
120 checksum += localBuf [i] ;
124 return checksum == localBuf [4] ;
130 * Read the Temperature & Humidity from an RHT03 sensor
131 *********************************************************************************
134 int readRHT03 (const int pin, int *temp, int *rh)
136 static unsigned int nextTime = 0 ;
137 static int lastTemp = 0 ;
138 static int lastRh = 0 ;
139 static int lastResult = TRUE ;
141 unsigned char buffer [4] ;
143 // Don't read more than once a second
145 if (millis () < nextTime)
152 lastResult = maxDetectRead (pin, buffer) ;
156 *temp = lastTemp = (buffer [2] * 256 + buffer [3]) ;
157 *rh = lastRh = (buffer [0] * 256 + buffer [1]) ;
158 nextTime = millis () + 2000 ;