-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAM2302.py
43 lines (35 loc) · 1.18 KB
/
AM2302.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
import os
import sys
from requests.auth import HTTPBasicAuth
import requests
import adafruit_dht
from board import D4
domoticz_ip='127.0.0.1'
domoticz_port='8080'
user=''
password=''
domoticz_idx=1
dht_device = adafruit_dht.DHT22(D4)
temperature = dht_device.temperature
humidity = dht_device.humidity
if humidity is not None and temperature is not None:
print("Temp={0:0.1f}*C Humidity={1:0.1f}%".format(temperature, humidity))
else:
print("Failed to retrieve data from humidity sensor")
def maj_widget(val_url):
requete='http://'+domoticz_ip+':'+domoticz_port+val_url
#print requete
r=requests.get(requete,auth=HTTPBasicAuth(user,password))
if r.status_code != 200:
print( "Erreur API Domoticz")
if humidity is not None and temperature is not None:
print('Temp={0:0.1f}* Humidity={1:0.1f}%'.format(temperature, humidity))
# l URL Domoticz pour le widget virtuel
url='/json.htm?type=command¶m=udevice&idx='+str(domoticz_idx)
url+='&nvalue=0&svalue='
url+=str('{0:0.1f};{1:0.1f};2').format(temperature, humidity)
#print url
maj_widget(url)
else:
print('Probleme avec la lecture du DHT. Try again!')
sys.exit(1)