Skip to content

Instantly share code, notes, and snippets.

@rajasiman
Last active December 4, 2019 21:38
Show Gist options
  • Select an option

  • Save rajasiman/06e0b640127aa12e0284db73d9c33a4f to your computer and use it in GitHub Desktop.

Select an option

Save rajasiman/06e0b640127aa12e0284db73d9c33a4f to your computer and use it in GitHub Desktop.
DHT22 Test
import Adafruit_DHT
import time
import sys
import datetime
import socket
from influxdb import InfluxDBClient
DHT_SENSOR = Adafruit_DHT.DHT22
DHT_PIN = 4
# Configure InfluxDB connection variables
host = "18.185.135.181" # AWS EC2 Instance
port = 8086 # default port
user = "grafana" # the user
password = "graf2020"
dbname = "garden" # the database
interval = 5 # Sample period in seconds
# Create the InfluxDB client object
client = InfluxDBClient(host, port, user, password, dbname)
measurement = "DHT22"
# location will be used as a grouping tag
location = socket.gethostname()
try:
while True:
humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)
if humidity is not None and temperature is not None:
data = [
{
"measurement": measurement,
"tags": {
"location": location,
},
"time": int(time.time()),
"fields": {
"temperature" : temperature,
"humidity": humidity
}
}
]
# Send the JSON data to InfluxDB
client.write_points(data, time_precision='s')
print("Temp={0:0.1f}*C Humidity={1:0.1f}%".format(temperature, humidity))
# Wait until it's time to query again...
time.sleep(interval)
else:
print("Failed to retrieve data from humidity sensor")
time.sleep(5)
except KeyboardInterrupt:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment