9/09/2016

Python :: Python + Piface เช็คอุหภูมิปิดเปิดแอร์ Auto

Python :: Python + Piface เช็คอุณหภูมิปิดเปิดแอร์ Auto
1. ติดตั้ง OS
2. ติดตั้ง Python
  1. sudo apt-get install python{,3}-pifacedigitalio

3. ติดตั้ง library สำหรับ Run python เช็คอุณหภูมิ https://learn.adafruit.com/dht-humidity ... ll-updated
  1. cd Adafruit_Python_DHT
  1. sudo apt-get update
  1. sudo apt-get install build-essential python-dev python-openssl
  1. sudo python setup.py instal
l
4. สร้างไฟล์ checkair.py ด้วยคำสั่ง
  1. nano checkair.py

ใส่ code
  1.                                                                                               
  2. #!/usr/bin/python
  3. #### Check temperature Adafruit_DHT 22 23 #######
  4. import sys
  5.  
  6. import Adafruit_DHT
  7.  
  8. # Parse command line parameters.
  9. # Use This.
  10. #sensor_args = { '11': Adafruit_DHT.DHT11,
  11. #                                '22': Adafruit_DHT.DHT22,
  12. #                                '2302': Adafruit_DHT.AM2302 }
  13. #if len(sys.argv) == 3 and sys.argv[1] in sensor_args:
  14. #        sensor = sensor_args[sys.argv[1]]
  15. #        pin = sys.argv[2]
  16. #else:
  17. #        print 'usage: sudo ./Adafruit_DHT.py [11|22|2302] GPIOpin#'
  18. #        print 'example: sudo ./Adafruit_DHT.py 2302 4 - Read from an AM2302 connected to GPIO #4'
  19. #        sys.exit(1)
  20.  
  21. # Try to grab a sensor reading.  Use the read_retry method which will retry up
  22. # to 15 times to get a sensor reading (waiting 2 seconds between each retry).
  23. #humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
  24. humidity, temperature = Adafruit_DHT.read_retry(22, 23)
  25.  
  26.  
  27. # Un-comment the line below to convert the temperature to Fahrenheit.
  28. # temperature = temperature * 9/5.0 + 32
  29.  
  30. # Note that sometimes you won't get a reading and
  31. # the results will be null (because Linux can't
  32. # guarantee the timing of calls to read the sensor).  
  33. # If this happens try again!
  34. if humidity is not None and temperature is not None:
  35.         print 'Temp={0:0.1f}*  Humidity={1:0.1f}%'.format(temperature, humidity)
  36. else:
  37.         print 'Failed to get reading. Try again!'
  38.         sys.exit(1)
  39.  
  40. #### Opne and Close Relay By Piface  #####
  41.  
  42. import pifacedigitalio
  43. pf = pifacedigitalio.PiFaceDigital()
  44.  
  45. #pf.leds[1].turn_on()
  46. #pf.leds[2].turn_on()
  47. #pf.leds[3].turn_on()
  48. #pf.leds[4].turn_on()
  49. #pf.leds[5].turn_on()
  50. #pf.leds[6].turn_on()
  51. #pf.leds[7].turn_on()
  52. #### ON #####
  53. import smtplib
  54.  
  55. if temperature > 27:
  56.         pf.relays[1].turn_on()
  57.         pf.relays[0].turn_on()
  58.  
  59.         from smtplib import SMTP
  60.  
  61.         recipients = ['suwit.jph@gmail.com', '"komkid@gmail.com', 'nontiwaza@gmail.com']
  62.  
  63.         def send_email (message, status):
  64.                 fromaddr = 'mail'
  65.                 server = SMTP('smtp.gmail.com:587')
  66.                 server.ehlo()
  67.                 server.starttls()
  68.                 server.ehlo()
  69.                 server.login('mail', 'password')
  70.                 server.sendmail(fromaddr, recipients, 'Subject: %s\r\n%s' % (status, message))
  71.                 server.quit()
  72.  
  73.         send_email("High temp alert! in Server Room A. " '{0:0.1f}*C'.format(temperature), "High temp alert! Turn ON relays.")
  74. else:
  75.         pf.relays[0].turn_off()
  76.         pf.relays[1].turn_off()
  77.  
  78.         #from smtplib import SMTP
  79.  
  80.         #recipients = ['suwit.jph@gmail.com', '"komkid@gmail.com', 'nontiwaza@gmail.com']
  81.  
  82.         #def send_email (message, status):
  83.         #        fromaddr = 'mail'
  84.         #        server = SMTP('smtp.gmail.com:587')
  85.         #        server.ehlo()
  86.         #        server.starttls()
  87.         #        server.ehlo()
  88.         #        server.login('mail', 'password')
  89.         #        server.sendmail(fromaddr, recipients, 'Subject: %s\r\n%s' % (status, message))
  90.         #        server.quit()
  91.         #send_email("Temp alert! in Server Room A. " '{0:0.1f}*C'.format(temperature), "Temp alert! Turn OFF Relays.")
  92.  
  93. pf.deinit_board()
  94.  
  95.  

5. Run ไฟล์ py ด้วยคำสั่ง
  1. ./checkair.py 22 23

6. ตั้งให้ Run ตามเวลาใน crontab
  1. crontab -e

เพิ่มเวลาที่ต้องการให้ run เข้าไป
*/5 * * * * ./checkair.py 22 23

การต่อ วงจร ตามรูป

No comments:

Post a Comment