Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.33 KB | None | 0 0
  1. #!/usr/bin/python
  2. # Copyright (c) 2014 Adafruit Industries
  3. # Author: Tony DiCola
  4.  
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11.  
  12. # The above copyright notice and this permission notice shall be included in all
  13. # copies or substantial portions of the Software.
  14.  
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. import sys
  23.  
  24. import Adafruit_DHT
  25.  
  26.  
  27. # Parse command line parameters.
  28. sensor_args = { '11': Adafruit_DHT.DHT11,
  29.                 '22': Adafruit_DHT.DHT22,
  30.                 '2302': Adafruit_DHT.AM2302 }
  31. if len(sys.argv) == 3 and sys.argv[1] in sensor_args:
  32.     sensor = sensor_args[sys.argv[1]]
  33.     pin = sys.argv[2]
  34. else:
  35.     print('usage: sudo ./Adafruit_DHT.py [11|22|2302] GPIOpin#')
  36.     print('example: sudo ./Adafruit_DHT.py 2302 4 - Read from an AM2302 connected to GPIO #4')
  37.     sys.exit(1)
  38.  
  39. # Try to grab a sensor reading.  Use the read_retry method which will retry up
  40. # to 15 times to get a sensor reading (waiting 2 seconds between each retry).
  41. humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
  42.  
  43. # Un-comment the line below to convert the temperature to Fahrenheit.
  44. # temperature = temperature * 9/5.0 + 32
  45.  
  46. # Note that sometimes you won't get a reading and
  47. # the results will be null (because Linux can't
  48. # guarantee the timing of calls to read the sensor).
  49. # If this happens try again!
  50. if humidity is not None and temperature is not None:
  51.     print('Temp={0:0.1f}*  Humidity={1:0.1f}%'.format(temperature, humidity))
  52. else:
  53.     print('Failed to get reading. Try again!')
  54.     sys.exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement