Raspberry Pi 4 Data Logger | DHT11/DHT22 Sensor Data Logger

Publié le: 12 avril 2020
sur la chaîne: Robotica DIY
30,484
320

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)

https://amzn.to/2noWmWm (Raspberry pi 4 model B)
http://amzn.to/2vqIKJP (DHT22)
http://amzn.to/2wxPmWz (Breadboard)
http://amzn.to/2vJ3lvo (Jumper wire)
http://amzn.to/2vmSK8l (Resistor)

Music: Elektronomia - Ibiza [NomiaTunes Release]
Watch:    • Elektronomia - Ibiza [COPYRIGHT FREE]  
Stream: https://spoti.fi/3aDDfuV

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
file = open('sensor_readings.txt', 'w')
file.write('time and date, temperature (C),temperature (F), humidity\n')

#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
file.write(time.strftime('%H:%M:%S %d/%m/%Y') + ', ' + str(temperature) + ', '+ str(temperature_f)+',' + str(humidity) + '\n')
time.sleep(1)

else:
print('Failed to get reading. Try again!')
time.sleep(1)

except KeyboardInterrupt:
print ('Program stopped')
running = False
file.close()


Sur cette page du site, vous pouvez voir la vidéo en ligne Raspberry Pi 4 Data Logger | DHT11/DHT22 Sensor Data Logger durée heure minute seconde en bonne qualité , qui a été Téléchargé par l'utilisateur Robotica DIY 12 avril 2020, Partagez le lien avec vos amis et connaissances, sur youtube cette vidéo a déjà été regardée 30,484 fois et il a aimé 320 téléspectateurs. Bon visionnage!