iLMS知識社群(Sites)師生部落格(Blogs)朝陽首頁(Homepage)TronClass教學平台登入
位置: 郭軒志 > 未分類
20180416
by 郭軒志 2018-04-16 15:18:56, 回應(0), 人氣(89)
https://www.raspberrypi.org/forums/viewtopic.php?t=151078

import time

import sys

import Adafruit_DHT

import RPi.GPIO as GPIO


def readAdafruitDHT(moduleType,pin):


    sensor_args = { '22': Adafruit_DHT.DHT22}

    sensor = sensor_args[moduleType]

    humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
    print 'Temp={0:0.1f}*C  Humidity={1:0.1f}%'.format(temperature, humidity)
    return (int(humidity), int(temperature))

    



if __name__ == '__main__':



    GPIO.setmode(GPIO.BCM)

    GPIO.setwarnings(False)

    GPIO.setup(17,GPIO.OUT)

    

    try:

        while True:

         

            humidity, temperature = readAdafruitDHT('22',4)

          

            if (temperature >20 and humidity >30) :

                GPIO.output(17,GPIO.HIGH)

            else:

                GPIO.output(17,GPIO.LOW)

            time.sleep(1)

    except KeyboardInterrupt:

        GPIO.cleanup()

      

附件:
2. test.docx (14 KB)
回應