46 温湿度传感器 dh11

 

 

#!/usr/bin/python
#https://blog.csdn.net/xujgcn/article/details/90670888
import Adafruit_DHT
 
sensor = Adafruit_DHT.DHT11
 
pin = 4  #GPIO4

#调用一次返回温度和适度数据
# 输入警报数值 其中湿度为百分比小数
def dh11_data(alram_tem,alram_hum):
    humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
     
    if humidity is not None and temperature is not None:
     
        print('Temp={0:0.1f}*C  Humidity={1:0.1f}%'.format(temperature, humidity))
        if temperature>alram_tem:
            print("waring alram_tem")
        if humidity>alram_hum:
            print("waring alram_hum ")
        
    else:
     
        print('Failed to get reading. Try again!')

#主函数
def main():
    dh11_data(23,5) 

    
    
main()   

   加入时间

 

#!/usr/bin/python
#https://blog.csdn.net/xujgcn/article/details/90670888
import Adafruit_DHT
# 导入time模块
import time
#https://www.cnblogs.com/komean/p/10603518.html
sensor = Adafruit_DHT.DHT11
 
pin = 4  #GPIO4

#调用一次返回温度和适度数据
# 输入警报数值 其中湿度为百分比小数
def dh11_data(alram_tem,alram_hum):
    humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
     
    if humidity is not None and temperature is not None:
     
        print('Temp={0:0.1f}*C  Humidity={1:0.1f}%'.format(temperature, humidity))
        if temperature>alram_tem:
            print("waring alram_tem")
        if humidity>alram_hum:
            print("waring alram_hum ")
        
    else:
     
        print('Failed to get reading. Try again!')

def time_test():
       # 打印时间戳
    print(time.time())
    # 格式化时间戳为本地的时间
    print(time.localtime(time.time()))
    print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))
    
    a = '2017-7-27'
    a2time = time.strptime(a, '%Y-%m-%d')
    localtime = time.localtime(time.time())
    if a2time < localtime:
        print("1")
    
#主函数
def main():
    dh11_data(23,5) 
    time_test()

    
    
main()   

  

你可能感兴趣的:(46 温湿度传感器 dh11)