User Tools

Site Tools


dh22

This is an old revision of the document!


DH22

#!/usr/bin/python
 
import os, sys
import Adafruit_DHT
import time
import requests
import html
 
def sendData(name, sensor, value, unit):
    headers = {
        'Authorization': 'Bearer !!!TOKEN!!!',
        'Content-type': 'application/json',
    }
    data = '{"state": "' + str(value) + '", "attributes": {"unit_of_measurement": "' + str(unit) + '", "friendly_name": "' + str(name) + '"}}'
    response = requests.post('http://192.168.178.114:8123/api/states/sensor.' + str(sensor), headers=headers, data=data)
 
 
# The sensor type
sensor = Adafruit_DHT.DHT22
 
# The GPIO17 - Board number 11
pin = '17'
 
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
 
if humidity is not None and temperature is not None:
    epoch = int(time.time())
    now = time.strftime('%d-%m-%Y %H:%M:%S', time.localtime(epoch))
 
    sendData('Pi Humidity', 'radiopi_humidity', humidity, '%')
    sendData('Pi Temperature', 'radiopi_temperature', temperature, 'C')
 
    print('"{0:s}","{1:0.1f}","{2:0.1f}"'.format(now, temperature, humidity))
else:
    print('Failed to get reading. Try again!')
dh22.1639517551.txt.gz · Last modified: 2021/12/14 22:32 by admin