From b02d4d81ce6060cb86fd7f1deee63b3a72a2b8bf Mon Sep 17 00:00:00 2001 From: Kshitij Date: Mon, 7 Oct 2024 00:03:57 +0530 Subject: [PATCH] Added sample code for alternative temperature measure and added method in README file. --- README.md | 28 ++++++++++++++++++++++++++++ alt-temp.py | 21 +++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 alt-temp.py diff --git a/README.md b/README.md index eed5cb0..d1c3fe1 100644 --- a/README.md +++ b/README.md @@ -3,3 +3,31 @@ This project is a uses the AdaFruit libraries for sensing temperature using DHT11 sensor and a Raspberry Pi. --- + + +## Alternative method + +> [!NOTE] +> This alternative method uses the `DHT11` python library for reading the sensor data. +> This is a pure Python library for reading DHT11 sensor on Raspberry Pi. + +1. Install prerequisites: +```shell +sudo apt update &&\ +sudo apt upgrade -y &&\ +sudo apt install python3 python3-pip -y + +``` + +2. Setup a virtual environment & install the library: +```shell +mkdir /home/$(whoami)/temp-sensor +cd /home/$(whoami)/temp-sensor +python3 -m venv . +source bin/activate +pip install dht11 setuptools RPi.GPIO +``` + +3. Run the code [alt-temp.py](https://git.kska.io/notkshitij/DHT11/src/branch/main/alt-temp.py) + +--- diff --git a/alt-temp.py b/alt-temp.py new file mode 100644 index 0000000..7af256d --- /dev/null +++ b/alt-temp.py @@ -0,0 +1,21 @@ +# NOTE: GPIO PIN 16 used + +import RPi.GPIO as GPIO +import dht11 + +# initialize GPIO +GPIO.setwarnings(False) +GPIO.setmode(GPIO.BCM) +GPIO.cleanup() + +# 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) + +GPIO.cleanup()