-
Notifications
You must be signed in to change notification settings - Fork 0
/
dht11.py
executable file
·63 lines (59 loc) · 2 KB
/
dht11.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import time
import dht11lib
import RPi.GPIO as GPIO
import socket
GPIO.setmode(GPIO.BOARD)
red = 37
green = 33
yellow = 35
Humidity =0
temperature = 0
#GPIO.cleanup()
GPIO.setup(red,GPIO.OUT)
GPIO.setup(yellow,GPIO.OUT)
GPIO.setup(green,GPIO.OUT)
sensor=dht11lib.DHT11(pin = 31)
def clean_LED():
GPIO.output(red,False)
GPIO.output(green,False)
GPIO.output(yellow,False)
def get_temp_humidity():
while(1):
try:
print("getting temp")
result=sensor.read()
print("geted")
if result.is_valid():
clean_LED()
#Humidity, temperature = Adafruit_DHT.read_retry(sensor, DHT_connected_gpio)
#result =sensor.read()
temperature=result.temperature
Humidity=result.humidity
print('Temp={0:0.1f}*C Humidity={1:0.1f}%'.format(temperature, Humidity))
if temperature >= 40 :
clean_LED()
GPIO.output(red,True)
elif temperature <= 30 :
clean_LED()
GPIO.output(green,True)
else:
clean_LED()
GPIO.output(yellow,True)
#return Humidity , temperature
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('192.168.88.213', 16662))
s.send("temp:"+str(temperature)+",humidity:"+str(Humidity))
s.close()
except :
print("socket not co")
print("end")
else:
print('Failed to get !',result.error_code)
try:
time.sleep(1)
except:
print("err")
except:
print("error in try")
get_temp_humidity()