Advertisement
Guest User

Untitled

a guest
Apr 10th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 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. import time
  24. import Adafruit_DHT
  25. import smtplib
  26.  
  27.  
  28.  
  29. # Parse command line parameters.
  30. sensor_args = { '11': Adafruit_DHT.DHT11,
  31. '22': Adafruit_DHT.DHT22,
  32. '2302': Adafruit_DHT.AM2302 }
  33. if len(sys.argv) == 4 and sys.argv[1] in sensor_args:
  34. sensor = sensor_args[sys.argv[1]]
  35. pin = sys.argv[2]
  36. caution = int(sys.argv[3])
  37. else:
  38. print 'usage: sudo ./Adafruit_DHT.py [11|22|2302] GPIOpin#'
  39. print 'example: sudo ./Adafruit_DHT.py 2302 4 - Read from an AM2302 connected to GPIO #4'
  40. sys.exit(1)
  41.  
  42.  
  43. # Try to grab a sensor reading. Use the read_retry method which will retry up
  44. # to 15 times to get a sensor reading (waiting 2 seconds between each retry).
  45. while True:
  46. humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
  47. if humidity is not None and temperature is not None:
  48. msg = 'Temp={0:0.1f}* Humidity={1:0.1f}%'.format(temperature, humidity)
  49. if (int(temperature) >= caution):
  50. smtpUser = 'zzz'
  51. smtpPass = 'zzz'
  52. toAdd = 'zzz'
  53. fromAdd = smtpUser
  54. subject = 'Warning Temp'
  55. header = 'To: ' + toAdd + '\n' + 'From: ' + fromAdd + '\n' + 'Subject: ' + subject
  56. body = msg
  57. print header + '\n' + body
  58. s = smtplib.SMTP('smtp.gmail.com',587)
  59. s.ehlo()
  60. s.starttls()
  61. s.ehlo()
  62. s.login(smtpUser,smtpPass)
  63. s.sendmail(fromAdd, toAdd, header + '\n' + body)
  64. s.quit()
  65. time.sleep(60)
  66. else:
  67. print 'Failed to get reading. Try again!'
  68. sys.exit(1)
  69. # Un-comment the line below to convert the temperature to Fahrenheit.
  70. # temperature = temperature * 9/5.0 + 32
  71.  
  72. # Note that sometimes you won't get a reading and
  73. # the results will be null (because Linux can't
  74. # guarantee the timing of calls to read the sensor).
  75. # If this happens try again!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement