2024-10-16 22:22:45 +05:30
|
|
|
"""
|
|
|
|
THIS CODE HAS BEEN TESTED ON RASPBERRY PI 3B, 4B AND IS FULLY OPERATIONAL.
|
|
|
|
|
|
|
|
Problem Statement: Temperature and humidity sensing using DHT11.
|
|
|
|
|
|
|
|
Code from InternetOfThingsAndEmbeddedSystems (SPPU - Third Year - Computer Engineering - Content) repository on KSKA Git: https://git.kska.io/sppu-te-comp-content/InternetOfThingsAndEmbeddedSystems
|
|
|
|
"""
|
|
|
|
|
2024-10-23 13:40:19 +05:30
|
|
|
# NOTE: PLEASE REFER DHT11 repo by notkshitij for initialization: https://git.kska.io/notkshitij/DHT11/
|
|
|
|
|
|
|
|
# This code will only work in the virtual environment
|
|
|
|
# To enable the virtual environment, change current working directory using 'cd <DIR>'
|
|
|
|
# Activate the virtual environment using 'source bin/activate'
|
|
|
|
# Run this code 'python3 IoT\ -\ Code-4.py'
|
|
|
|
|
2024-10-16 22:22:45 +05:30
|
|
|
# BEGINNING OF CODE
|
|
|
|
# NOTE: GPIO PIN 16 used
|
|
|
|
|
|
|
|
import RPi.GPIO as GPIO
|
|
|
|
import dht11
|
|
|
|
import time
|
|
|
|
|
|
|
|
# initialize GPIO
|
|
|
|
GPIO.setmode(GPIO.BCM)
|
2024-11-06 11:52:16 +05:30
|
|
|
GPIO.setwarnings(False) # Set True if you are having trouble connecting the DHT11 sensor to Raspberry Pi. Doing so will show warnings on screen.
|
2024-10-16 22:22:45 +05:30
|
|
|
GPIO.cleanup()
|
|
|
|
|
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
# read data using pin 16
|
|
|
|
instance = dht11.DHT11(pin = 16)
|
|
|
|
result = instance.read()
|
|
|
|
|
|
|
|
if result.is_valid():
|
|
|
|
print("Temperature: %-3.1f C" % result.temperature)
|
|
|
|
print("Humidity: %-3.1f %%" % result.humidity)
|
|
|
|
else:
|
|
|
|
print("Error: %d" % result.error_code)
|
2024-10-23 13:40:19 +05:30
|
|
|
# Error 1 implies sensor not detected, thus no data
|
|
|
|
# Error 2 implies checksum error, data corrupted, i.e. GPIO connection might be lose
|
2024-10-16 22:22:45 +05:30
|
|
|
time.sleep(3)
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
print("Program stopped by user.")
|
|
|
|
GPIO.cleanup()
|
|
|
|
# END OF CODE
|