RaspberryPi with DHT22 Sensor for Humidity and Temperature Data with InfluxDB
I have been playing with RaspberryPI for a while. There are several project I tried to do with RaspberryPI. These boards play a significant role in my home lab for different experiments like Docker Private Repository, Media Server, Ad Blocker with pi-hole, Secure Proxy for anonymity, secure network access with VPN, Home Security Camera/Surveillance, Data Collector or scraper etc. But This time, I did buy a “DHT-22” sensor for monitoring room temperature and humidity. Humidity is my main concern and wanted to see how it fluctuates.
So, my main plan was to get a DHT-22 to connect properly
with my spare RapberryPI 3B+. I did order the sensor and 3 male-to-male jumper for connectivity. Though I should have had a BreadBoard too. However, raspberryPI had already Ubuntu Server 20 installed.
For DHT-22, I needed to connect the female-to-female into the pins
14: Ground 17: 3v3 Power 18: GPIO 24
This GPIO-24 was important to note down as it was needed in Python Code to access the sensor through this pin.
Yes, I have used python to access the sensor. For testing, I have used this code:
#!/usr/bin/python3
import Adafruit_DHT
import datetime
def th_reader():
# for DHT11
# sensor = 11
# for DHT22
sensor = 22
gpio = 24
h_t = Adafruit_DHT.read_retry(sensor, gpio)
humidity = h_t[0]
temperature = h_t[1]
if humidity is not None and temperature is not None:
temp = '{0:0.1f}°C'.format(temperature)
humid = '{0:0.1f}%'.format(humidity)
return {
"temperature": temp,
"humidity": humid
}
else:
temp = None
humid = None
return {
"temperature": temp,
"humidity": humid
}
if __name__ == '__main__':
print(th_reader())
This was enough for me. I wanted to run the script every 5 minutes through cron and store the data to already running influxDB instance in another RaspberryPI at which I can create dashboards and others. You can check how I am using InfluxDB through here: https://fahadahammed.com/explore-influxdb-part-1/
So, I did create a bucket in the influxDB and modified the code to this-
#!/usr/bin/python3
import Adafruit_DHT
import datetime
import threading
from influxdb_client import InfluxDBClient, Point, WritePrecision
from influxdb_client.client.write_api import SYNCHRONOUS
def th_reader():
# for DHT11
# sensor = 11
# for DHT22
sensor = 22
gpio = 24
h_t = Adafruit_DHT.read_retry(sensor, gpio)
humidity = h_t[0]
temperature = h_t[1]
if humidity is not None and temperature is not None:
temp = '{0:0.1f}°C'.format(temperature)
humid = '{0:0.1f}%'.format(humidity)
return {
"temperature": temperature,
"humidity": humidity
}
else:
temp = None
humid = None
return {
"temperature": temp,
"humidity": humid
}
def write_to_influxdb(data_to_write):
# You can generate an API token from the "API Tokens Tab" in the UI
token = "<INFLUX_TOKEN>"
org = "myorg"
bucket = "dht"
influx_url = "<INFLUX_URL>"
with InfluxDBClient(url=influx_url, token=token, org=org) as client:
write_api = client.write_api(write_options=SYNCHRONOUS)
pt = Point("temperature").tag("host", "pi").field("temperature", int(data_to_write.get("temperature"))).time(datetime.datetime.utcnow(), WritePrecision.NS)
write_api.write(bucket, org, pt)
client.close()
with InfluxDBClient(url=influx_url, token=token, org=org) as client:
write_api = client.write_api(write_options=SYNCHRONOUS)
ph = Point("humidity").tag("host", "pi").field("humidity", data_to_write.get("humidity")).time(datetime.datetime.utcnow(), WritePrecision.NS)
write_api.write(bucket, org, ph)
client.close()
return True
if __name__ == '__main__':
th_data = th_reader()
print(th_data)
# write_to_influxdb(data_to_write=th_data)
t1 = threading.Thread(target=write_to_influxdb, kwargs={"data_to_write": th_data})
t1.start()
If you want to use the code block, please do replace the <INFLUX_URL> and <INFLUX_TOKEN> with valid ones.
Now the output is the same json but without the ‘%’ and ‘°C’. Because I needed to get a numerical value and set the required prefixes in graph.
I have used InfluxDB Data Point to write data.
Exported JSON of this minimal dashboard is-
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I have other plans to use the DHT-22 like controlling exhaust fan or similar. Maybe later. For now, I will just collect data and observe the fluctuation of my rooms temperature and humidity.
It was fun working with InfluxDB and DHT-22 with RaspberryPI.
Thanks for reading. Please give me feedback If you have idea for me or point me some improvements.