filmov
tv
Raspberry Pi 4 Data Logger | DHT11/DHT22 Sensor Data Logger
Показать описание
Raspberry Pi 4 Data Logger | DHT11/DHT22 Sensor Data Logger
Scroll down for code.....
In this tutorial, we will learn how to use DHT11/ DHT22/AM2302 with Raspberry Pi 4. Also see how to log sensor data in raspberry pi 4. Once you learn how to log data you can log any sensor data in same the fashion. For this tutorial we are logging DHT22 sensor data to .txt file but you can also use .csv extension file to export data directly in .csv file or change .txt file extension to .csv extension.
If you want to support my video please buy any product through my amazon affiliate link. I will receive a commission, at no extra cost to you.
LIST OF COMPONENT (affiliate links)
Music: Elektronomia - Ibiza [NomiaTunes Release]
import Adafruit_DHT
import time
#comment and uncomment the lines below depending on your sensor
#sensor = Adafruit_DHT.DHT11
sensor = Adafruit_DHT.DHT22
#sensor = Adafruit_DHT.AM2302
#DHT pin connects to GPIO 4
sensor_pin = 4
#create a variable to control the while loop
running = True
#new .txt file created with header
#loop forever
while running:
try:
#read the humidity and temperature
humidity, temperature = Adafruit_DHT.read_retry(sensor, sensor_pin)
#uncomment the line below to convert to Fahrenheit
temperature_f = temperature * 9/5.0 + 32
#sometimes you won't get a reading and
#the results will be null
#the next statement guarantees that
#it only saves valid readings
if humidity is not None and temperature is not None:
#print temperature and humidity
print('Temperature = ' + str(temperature) +','+ 'Temperature Fahrenheit = ' + str(temperature_f) +',' + 'Humidity = ' + str(humidity))
#save time, date, temperature in Celsius, temperature in Fahrenheit and humidity in .txt file
else:
print('Failed to get reading. Try again!')
except KeyboardInterrupt:
print ('Program stopped')
running = False
Scroll down for code.....
In this tutorial, we will learn how to use DHT11/ DHT22/AM2302 with Raspberry Pi 4. Also see how to log sensor data in raspberry pi 4. Once you learn how to log data you can log any sensor data in same the fashion. For this tutorial we are logging DHT22 sensor data to .txt file but you can also use .csv extension file to export data directly in .csv file or change .txt file extension to .csv extension.
If you want to support my video please buy any product through my amazon affiliate link. I will receive a commission, at no extra cost to you.
LIST OF COMPONENT (affiliate links)
Music: Elektronomia - Ibiza [NomiaTunes Release]
import Adafruit_DHT
import time
#comment and uncomment the lines below depending on your sensor
#sensor = Adafruit_DHT.DHT11
sensor = Adafruit_DHT.DHT22
#sensor = Adafruit_DHT.AM2302
#DHT pin connects to GPIO 4
sensor_pin = 4
#create a variable to control the while loop
running = True
#new .txt file created with header
#loop forever
while running:
try:
#read the humidity and temperature
humidity, temperature = Adafruit_DHT.read_retry(sensor, sensor_pin)
#uncomment the line below to convert to Fahrenheit
temperature_f = temperature * 9/5.0 + 32
#sometimes you won't get a reading and
#the results will be null
#the next statement guarantees that
#it only saves valid readings
if humidity is not None and temperature is not None:
#print temperature and humidity
print('Temperature = ' + str(temperature) +','+ 'Temperature Fahrenheit = ' + str(temperature_f) +',' + 'Humidity = ' + str(humidity))
#save time, date, temperature in Celsius, temperature in Fahrenheit and humidity in .txt file
else:
print('Failed to get reading. Try again!')
except KeyboardInterrupt:
print ('Program stopped')
running = False
Комментарии