-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdht11.py
executable file
·37 lines (29 loc) · 983 Bytes
/
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import urllib2
import Adafruit_DHT
import paho.mqtt.publish as mospub
from time import sleep
sensor = Adafruit_DHT.DHT11
pin = 4
mqttserv = '127.0.0.1'
mqtttopt = 'sensors/temperatur/innen'
mqtttoph = 'sensors/luftfeuchtigkeit/innen'
host = 'https://hackerspace-bielefeld.de/spacestatus/spacestatus.php'
loops = 5
humidity = 0.0
temperature = 0.0
for i in range(loops):
h, t = Adafruit_DHT.read_retry(sensor, pin)
humidity = humidity + h
temperature = temperature + t
sleep(1.2)
temperature = round(temperature / loops,1)
humidity = round(humidity / loops,1)
if humidity is not None and temperature is not None:
mospub.single(mqtttopt, payload=temperature, hostname=mqttserv)
sleep(1)
mospub.single(mqtttoph, payload=humidity, hostname=mqttserv)
print('Temp={0:0.1f}*C Humidity={1:0.1f}%'.format(temperature, humidity))
urllib2.urlopen(host +"?temp_in="+ str(temperature) +"&humi_in="+ str(humidity)).read()