def do_work(): D = [] # Try to grab a sensor reading. Use the read_retry method which will retry up # to 15 times to get a sensor reading (waiting 2 seconds between each retry). H0, T0 = bonediagd_DHT.read_retry(sensor_type, sensor_pin) # Note that sometimes you won't get a reading and # the results will be null (because Linux can't # guarantee the timing of calls to read the sensor). # If this happens try again! if H0 is not None and T0 is not None: H = H0 * DHT22H_gain + DHT22H_offset T = T0 * DHT22T_gain + DHT22T_offset D.append(T) D.append(H) if DEBUG:print ' T0 = {0:0.1f}*C H0 = {1:0.1f}%'.format(T0, H0) if DEBUG:print 'Temp = {0:0.1f}*C Humidity = {1:0.1f}%'.format(T, H) return D
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. import bonediagd_DHT # Sensor should be set to bonediagd_DHT.DHT11, # bonediagd_DHT.DHT22, or bonediagd_DHT.AM2302. sensor = bonediagd_DHT.DHT22 # Example using a Beaglebone Black with DHT sensor # connected to pin P8_11. pin = 'P8_15' # Try to grab a sensor reading. Use the read_retry method which will retry up # to 15 times to get a sensor reading (waiting 2 seconds between each retry). humidity, temperature = bonediagd_DHT.read_retry(sensor, pin) # Note that sometimes you won't get a reading and # the results will be null (because Linux can't # guarantee the timing of calls to read the sensor). # If this happens try again! if humidity is not None and temperature is not None: print 'Temp={0:0.1f}*C Humidity={1:0.1f}%'.format(temperature, humidity) else: print 'Failed to get reading. Try again!'
import bonediagd_DHT # Parse command line parameters. sensor_args = {"11": bonediagd_DHT.DHT11, "22": bonediagd_DHT.DHT22, "2302": bonediagd_DHT.AM2302} if len(sys.argv) == 3 and sys.argv[1] in sensor_args: sensor = sensor_args[sys.argv[1]] pin = sys.argv[2] else: print "usage: sudo ./readDHT.py [11|22|2302] GPIOpin#" print "example: sudo ./readDHT.py 2302 4 - Read from an AM2302 connected to GPIO #4" sys.exit(1) # Try to grab a sensor reading. Use the read_retry method which will retry up # to 15 times to get a sensor reading (waiting 2 seconds between each retry). humidity, temperature = bonediagd_DHT.read_retry(sensor, pin) # Un-comment the line below to convert the temperature to Fahrenheit. # temperature = temperature * 9/5.0 + 32 # Note that sometimes you won't get a reading and # the results will be null (because Linux can't # guarantee the timing of calls to read the sensor). # If this happens try again! if humidity is not None and temperature is not None: print "Temp={0:0.1f}* Humidity={1:0.1f}%".format(temperature, humidity) else: print "Failed to get reading. Try again!" sys.exit(1)