1. ติดตั้ง OS
2. ติดตั้ง Python
- sudo apt-get install python{,3}-pifacedigitalio
3. ติดตั้ง library สำหรับ Run python เช็คอุณหภูมิ https://learn.adafruit.com/dht-humidity ... ll-updated
- cd Adafruit_Python_DHT
- sudo apt-get update
- sudo apt-get install build-essential python-dev python-openssl
- sudo python setup.py instal
4. สร้างไฟล์ checkair.py ด้วยคำสั่ง
- nano checkair.py
ใส่ code
- #!/usr/bin/python
- #### Check temperature Adafruit_DHT 22 23 #######
- import sys
- import Adafruit_DHT
- # Parse command line parameters.
- # Use This.
- #sensor_args = { '11': Adafruit_DHT.DHT11,
- # '22': Adafruit_DHT.DHT22,
- # '2302': Adafruit_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 ./Adafruit_DHT.py [11|22|2302] GPIOpin#'
- # print 'example: sudo ./Adafruit_DHT.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 = Adafruit_DHT.read_retry(sensor, pin)
- humidity, temperature = Adafruit_DHT.read_retry(22, 23)
- # 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)
- #### Opne and Close Relay By Piface #####
- import pifacedigitalio
- pf = pifacedigitalio.PiFaceDigital()
- #pf.leds[1].turn_on()
- #pf.leds[2].turn_on()
- #pf.leds[3].turn_on()
- #pf.leds[4].turn_on()
- #pf.leds[5].turn_on()
- #pf.leds[6].turn_on()
- #pf.leds[7].turn_on()
- #### ON #####
- import smtplib
- if temperature > 27:
- pf.relays[1].turn_on()
- pf.relays[0].turn_on()
- from smtplib import SMTP
- recipients = ['suwit.jph@gmail.com', '"komkid@gmail.com', 'nontiwaza@gmail.com']
- def send_email (message, status):
- fromaddr = 'mail'
- server = SMTP('smtp.gmail.com:587')
- server.ehlo()
- server.starttls()
- server.ehlo()
- server.login('mail', 'password')
- server.sendmail(fromaddr, recipients, 'Subject: %s\r\n%s' % (status, message))
- server.quit()
- send_email("High temp alert! in Server Room A. " '{0:0.1f}*C'.format(temperature), "High temp alert! Turn ON relays.")
- else:
- pf.relays[0].turn_off()
- pf.relays[1].turn_off()
- #from smtplib import SMTP
- #recipients = ['suwit.jph@gmail.com', '"komkid@gmail.com', 'nontiwaza@gmail.com']
- #def send_email (message, status):
- # fromaddr = 'mail'
- # server = SMTP('smtp.gmail.com:587')
- # server.ehlo()
- # server.starttls()
- # server.ehlo()
- # server.login('mail', 'password')
- # server.sendmail(fromaddr, recipients, 'Subject: %s\r\n%s' % (status, message))
- # server.quit()
- #send_email("Temp alert! in Server Room A. " '{0:0.1f}*C'.format(temperature), "Temp alert! Turn OFF Relays.")
- pf.deinit_board()
5. Run ไฟล์ py ด้วยคำสั่ง
- ./checkair.py 22 23
6. ตั้งให้ Run ตามเวลาใน crontab
- crontab -e
เพิ่มเวลาที่ต้องการให้ run เข้าไป
*/5 * * * * ./checkair.py 22 23
การต่อ วงจร ตามรูป
ไม่มีความคิดเห็น:
แสดงความคิดเห็น