Après avoir un peu galéré pour trouver de la ressource sur internet concernant l'utilisation de la sonde DHT avec les nouveaux Rpi 4 (l'ancienne librairie Adafruit n'est plus compatible) je vous partage ce script utilisant la nouvelle librairie https://github.com/adafruit/Adafruit_CircuitPython_DHT.
Code : Tout sélectionner
#!/usr/bin/python
# -*- coding: latin-1 -*-
# basé sur le script Adafruit et adapté pour Domoticz
# Copyright (c) 2014 Adafruit Industries
# Author: Tony DiCola
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import os
import sys
import adafruit_dht
import time
from requests.auth import HTTPBasicAuth
import requests
import board
############# Parametres #################################
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# les parametres de Domoticz
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
domoticz_ip='192.168.1.94'
domoticz_port='8080'
user=''
password=''
domoticz_idx=2
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# les parametres du DHT
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#sensor est 11,22,ou 2302
# pin est le numero d la pin que vous avez cablée
sensor=22
PIN=4
#Declarer la sonde DHT22 (à remplacer par DHT11 si besoin) sur le GPIO4
dhtDevice = adafruit_dht.DHT22(board.D4)
temperature = dhtDevice.temperature
humidity = dhtDevice.humidity
############# Fin des parametres #################################
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# le fomat pour la temp hum est celui ci
#/json.htm?type=command¶m=udevice&idx=IDX&nvalue=0&svalue=TEMP;HUM;HUM_STAT
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)Bonne soirée